CpsNotify.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * 支付的服务器异步通知控制器
  4. *
  5. */
  6. namespace app\api\controller\v1;
  7. use app\api\controller\Api;
  8. use app\api\service\PayNotifyService;
  9. use app\common\library\AirwallexPay;
  10. use app\common\model\Pay;
  11. use app\common\model\PayCpinfo;
  12. use app\common\model\Members;
  13. use app\common\logic\PayCallback;
  14. use app\common\logic\Pay as PayLogic;
  15. use app\common\library\WeixinPay;
  16. use app\guildapi\controller\ChannelRechargeCoin;
  17. use app\service\TiktokService;
  18. use think\Config;
  19. use think\Db;
  20. use think\Exception;
  21. class CpsNotify extends Api
  22. {
  23. /**
  24. * 不进行父类的登录验证,所以增加构造方法重写了父类的初始化方法
  25. */
  26. public function _initialize()
  27. {
  28. $log_postdata = '';
  29. try {
  30. $postData = input();
  31. if (!$postData) {
  32. $postData = request()->getInput();
  33. }
  34. foreach ($postData as $k => $v) {
  35. $log_postdata .= $k . ':' . $v . ' ';
  36. }
  37. } catch (\Exception $e) {
  38. $log_postdata = json_encode($postData);
  39. }
  40. log_message('支付订单回调:' . request()->url() . ' ' . $log_postdata, 'log', LOG_PATH . 'CpsNotify/');
  41. }
  42. /**
  43. * 支付宝Aop支付服务器异步通知页面方法
  44. */
  45. public function alipayAop()
  46. {
  47. //Alipay Aop Sdk支付回调
  48. vendor('alipayAop.AopSdk');
  49. $aop = new \AopClient();
  50. $public_key = Config::get('ali_pay_config')['configs'][Config::get('ali_pay_config')['default']['zfb']]['public_key'];
  51. $aop->alipayrsaPublicKey = $public_key;
  52. if ($result = $aop->rsaCheckV1($_POST, NULL, Config::get('ali_pay_config')['sign_type'])) {
  53. // 处理支付成功后的逻辑业务
  54. if (input('post.trade_status') == 'TRADE_SUCCESS') {
  55. //写入日志
  56. log_message($result, 'log', LOG_PATH . 'paylog/');
  57. $out_trade_no = input('out_trade_no'); //订单号
  58. if (!empty($out_trade_no)) {
  59. //更新订单状态处理
  60. $this->updateOrder($out_trade_no, input('total_fee'));
  61. }
  62. echo 'success';
  63. exit; //请不要修改或删除
  64. }
  65. echo 'fail';
  66. exit; //验证失败
  67. }
  68. echo 'fail';
  69. exit;
  70. }
  71. /**
  72. * 酷点h5支付
  73. */
  74. public function kdh5zhifu()
  75. {
  76. $request_data = file_get_contents('php://input');
  77. log_message('酷点h5支付回调参数:' . $request_data, 'log', LOG_PATH . '../paylog/');
  78. vendor('kdpaySdk.des');
  79. $sumpay = new \kdpay();
  80. $data = json_decode($request_data, true);
  81. $res_result = json_decode($data['result'], true);
  82. if ($res_result['status'] == '100') {
  83. $retjson = $sumpay->notifyVerify($data);
  84. if ($retjson == $data['sign']) {
  85. $orderid = trim($res_result['out_trade_no']);
  86. $amount = floatval($res_result['fee'] / 100);
  87. //订单编号不为空时
  88. if (!empty($orderid)) {
  89. //更新订单状态处理
  90. $this->updateOrder($orderid, $amount, $res_result['transaction_id']);
  91. }
  92. echo "success";
  93. die();
  94. } else {
  95. echo 'fail';
  96. exit();
  97. }
  98. } else {
  99. echo '支付失败';
  100. exit();
  101. }
  102. }
  103. private function updateOrder($orderid, $real_amount = 0, $callback_order = '')
  104. {
  105. $channelRechargeCoin = model('ChannelRechargeCoin')->field('id,orderid,channel_id,channel_name,send_channel_id,send_channel_name,amount,real_amount,status')
  106. ->where(['recharge_type'=>2,'orderid'=>$orderid])->find();
  107. if ($real_amount != '' && $channelRechargeCoin['real_amount'] != $real_amount) {
  108. $result = false;
  109. //写入日志
  110. log_message('订单编号:' . $orderid . '支付金额不一致,订单状态更新失败', 'error', LOG_PATH . 'cpsnotify/');
  111. } //待支付状态时
  112. elseif ($channelRechargeCoin['status'] == 0) {
  113. // 启动事务
  114. Db::startTrans();
  115. try {
  116. model('ChannelRechargeCoin')->where(['id'=>$channelRechargeCoin['id']])->update(['status'=>1,'callback_order'=>$callback_order]);
  117. $detData = array();
  118. $detData['channel_id'] = $channelRechargeCoin['channel_id'];
  119. $detData['channel_name'] = $channelRechargeCoin['channel_name'];
  120. $detData['change_amount'] = +$channelRechargeCoin['amount'];
  121. $detData['account_type'] = 3; //通用账户
  122. $detData['type'] = 4; //充值收入
  123. $detData['out_orderid'] = $channelRechargeCoin['orderid'];
  124. $detData['create_time'] = NOW_TIMESTAMP;
  125. $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
  126. if ( !$insertDetId) {
  127. throw new Exception("添加账户变动明细失败");
  128. }
  129. $updData = array();
  130. $updData['amount_coin'] = Db::raw("amount_coin+".$channelRechargeCoin['amount']);
  131. $updData['update_time'] = time();
  132. $updFromResult = model('Channel')->where(['id'=>$channelRechargeCoin['channel_id']])->update($updData);
  133. if (!$updFromResult) {
  134. throw new Exception("账户金额变动失败");
  135. }
  136. $data['finish_status'] = 1;
  137. $data['finish_time']= time();
  138. $result = model('ChannelRechargeCoin')->where(['id'=>$channelRechargeCoin['id'],'status'=>1])->update($data);
  139. if (!$result) {
  140. throw new Exception("充值失败");
  141. }
  142. // 提交事务
  143. Db::commit();
  144. } catch (\Exception $e) {
  145. // 回滚事务
  146. Db::rollback(); var_dump( $e->getMessage());
  147. //写入日志
  148. log_message('订单编号:' . $orderid . '订单状态更新失败' . $e->getMessage(), 'error', LOG_PATH . 'cpsnotify/');
  149. }
  150. }
  151. }
  152. }