123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkAdmin
- // +----------------------------------------------------------------------
- // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
- // +----------------------------------------------------------------------
- // | 官方网站: http://demo.thinkadmin.top
- // +----------------------------------------------------------------------
- // | 开源协议 ( https://mit-license.org )
- // +----------------------------------------------------------------------
- // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
- // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
- // +----------------------------------------------------------------------
- namespace app\api\controller;
- use EasyWeChat\Factory;
- use think\Controller;
- use think\Db;
- use think\Exception;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
- use function AlibabaCloud\Client\value;
- /**
- * 支付管理类
- * Class Refund
- * @package app\api\controller\Refund
- */
- class Pay extends Controller
- {
- /**
- * 微信支付--商品支付成功回调订单
- */
- public function WxOrderNotify(){
- $payXml = file_get_contents("php://input");
- //将xml格式转化为json格式
- $jsonXml = json_encode(simplexml_load_string($payXml, 'SimpleXMLElement', LIBXML_NOCDATA));
- //将json格式转成数组格式 $result['out_trade_no']
- $result = json_decode($jsonXml, true);
- if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
- $return = $this->dealData($result);
- if ($return){
- $arr = array(
- 'return_code' => 'SUCCESS',
- 'return_msg' => 'OK',
- );
- return $this->arrayToXml($arr);
- }else{
- file_put_contents("order_pay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
- }
- }
- }
- /**
- * 支付宝支付--支付成功回调订单
- */
- public function alipayOrderNotify(){
- $result = input('post.');
- if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
- $return = $this->dealData($result);
- if ($return){
- echo 'success';
- }else{
- file_put_contents("order_alipay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
- }
- }
- }
- /**
- * 处理数据库信息
- * @param $result
- * @return bool
- */
- function dealData($result){
- Db::startTrans();
- try {
- $order = Db::name('store_order')->where('order_no',$result['out_trade_no'])->find();
- Db::name('store_order')
- ->where('order_no',$result['out_trade_no'])
- ->update(['status'=>1,'pay_at'=>date('Y-m-d H:i:s'),'return_success_info'=>json_encode($result,true)]);
- $array = [];
- for ($i=0;$i<$order['num'];$i++){
- //获取排名
- $rank = getRanking($order['c_id'])+1;
- $tag = getTag($order['c_id'],$rank,$order['inventory']);
- saveRanking($order['c_id']);
- $company = '象寻数字科技(上海)有限公司';
- $hash = getCompanyHash($order['c_id']);
- $company_hash = $hash['hash'];
- $ddcid = Db::name('hash')->where('hash',$hash['hash'])->value('ddcid');
- $company_hash_time = $hash['create_at'] ? $hash['create_at'] : date('Y-m-d H:i:s');
- Db::name('hash')->where('hash',$hash['hash'])->update(['status'=>1]);
- $collectors_hash = '';
- $date = [
- 'order_id'=>$order['id'],
- 'order_no'=>get_order_sn(),
- 'tag'=>$tag,
- 'mid'=>$order['mid'],
- 'c_id'=>$order['c_id'],
- 'pro_info'=>$order['pro_info'],
- 'company'=>$company,
- 'company_hash'=>$company_hash,
- 'company_hash_time'=>$company_hash_time,
- 'ddcid'=>$ddcid,
- 'collectors_hash'=>$collectors_hash,
- 'collectors_hash_time'=>''
- ];
- $array[] = $date;
- }
- Db::name('store_order_info')->insertAll($array);
- //送积分
- $by_collection_integral = getConfigValue('by_collection_integral');
- if ($by_collection_integral){
- $by_collection_integral = bcmul($by_collection_integral,$order['num']);
- memberMoneyChange($by_collection_integral,1,$order['mid'],'购买藏品',1,$order['id']);
- }
- Db::commit();
- return true;
- } catch (\Exception $e){
- Db::rollback();
- return false;
- }
- }
- /**
- * 微信支付--充值盲盒成功回调订单
- */
- public function BlindRechargeNotify(){
- $payXml = file_get_contents("php://input");
- //将xml格式转化为json格式
- $jsonXml = json_encode(simplexml_load_string($payXml, 'SimpleXMLElement', LIBXML_NOCDATA));
- //将json格式转成数组格式 $result['out_trade_no']
- $result = json_decode($jsonXml, true);
- if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
- $return = $this->dealBox($result);
- if ($return){
- $arr = array(
- 'return_code' => 'SUCCESS',
- 'return_msg' => 'OK',
- );
- return $this->arrayToXml($arr);
- }else{
- file_put_contents("order_pay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
- }
- }
- }
- /**
- * 支付宝支付--充值盲盒回调订单
- */
- public function alipayBlindRechargeNotify(){
- $result = input('post.');
- if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
- $return = $this->dealBox($result);
- if ($return){
- echo 'success';
- }else{
- file_put_contents("order_alipay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
- }
- }
- }
- /**
- * 盲盒充值次数处理数据库信息
- * @param $result
- * @return bool
- */
- function dealBox($result){
- Db::startTrans();
- try {
- $order = Db::name('store_blind_recharge')->where('order_no',$result['out_trade_no'])->find();
- if ($order['status']==1) return true;
- Db::name('store_blind_recharge')
- ->where('order_no',$result['out_trade_no'])
- ->update(['status'=>1,'pay_at'=>date('Y-m-d H:i:s'),'return_success_info'=>json_encode($result,true)]);
- //用户加次数
- Db::name('store_member')->where('id',$order['m_id'])->setInc('lottery_number',$order['num']);
- Db::commit();
- return true;
- } catch (\Exception $e){
- Db::rollback();
- return false;
- }
- }
- /**
- * 数组转xml
- * @ApiInternal
- */
- public function arrayToXml($arr)
- {
- $xml = "<xml>";
- foreach ($arr as $key => $val) {
- if (is_numeric($val)) {
- $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
- } else
- $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
- }
- $xml .= "</xml>";
- return $xml;
- }
- }
|