QuickCallback.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\common\logic\Pay as PayLogic;
  4. use app\common\logic\PayCallback;
  5. use app\common\model\ComplexMembers;
  6. use app\common\model\ComplexPay;
  7. use app\common\model\Members;
  8. use app\common\model\PayCpinfo;
  9. use think\Controller;
  10. use think\Db;
  11. use think\Exception;
  12. use think\Request;
  13. use app\service\QuickService;
  14. class QuickCallback extends Controller
  15. {
  16. protected $dir_name = "quick";
  17. const MD5KEY = "d0ekhdx5klc2zoelfa3jfaftkdzxgvdk"; // 自己的Quick md5key
  18. const CALLBACK_KEY = "95051247812538717836001325967096";//自己的callback_key
  19. public function index(Request $request)
  20. {
  21. $param = $request->param();
  22. // $param = [
  23. // "nt_data" => '@108@119@174@165@158@82@167@152@171@166@161@162@159@115@90@106@94@103@83@85@154@166@156@161@155@160@166@155@118@90@139@141@118@101@110@90@82@165@165@148@167@151@153@159@160@164@157@118@82@165@160@87@116@118@117@165@162@176@165@163@168@166@169@152@157@157@169@171@147@153@150@113@117@160@157@166@164@151@159@158@110@115@166@158@153@118@109@110@102@172@161@152@119@116@162@168@151@161@164@151@160@147@158@152@119@164@169@164@162@167@169@117@95@163@160@156@158@166@152@160@152@164@157@114@117@167@171@173@143@167@168@156@151@164@144@161@168@113@116@98@160@171@172@152@159@169@149@154@167@151@167@161@117@115@167@166@157@157@168@152@158@167@116@104@99@98@97@101@105@100@108@100@98@102@109@106@101@106@100@101@102@105@106@100@111@103@107@103@117@103@165@171@148@157@168@151@160@161@111@111@169@148@177@146@165@159@165@158@110@105@97@102@105@101@106@99@100@103@109@84@106@109@112@108@99@114@103@108@110@97@161@148@178@146@172@156@158@155@118@117@145@164@160@170@163@172@119@98@101@103@105@112@104@153@163@168@165@166@170@118@110@165@165@148@173@168@171@113@97@114@103@172@164@152@165@170@168@118@117@151@175@171@170@149@172@151@166@154@162@153@163@171@112@170@169@171@117@98@157@171@165@168@153@172@143@167@146@167@150@165@172@112@115@102@165@153@172@171@151@160@149@118@114@103@165@157@170@160@168@162@166@166@144@163@157@172@163@152@152@154@115',
  24. // "sign" => '@104@155@109@158@105@104@104@106@111@103@112@103@148@152@104@158@149@111@101@151@102@111@156@149@155@112@113@105@154@110@108@106',
  25. // "md5Sign" => '07dae8d71374651c1f0ee4cc16a37f6e',
  26. // ];
  27. $this->log_write_message("quick回调:", $param);
  28. if (empty($param)) {
  29. throw new Exception("参数错误");
  30. }
  31. if (!array_key_exists("nt_data", $param)) {
  32. $this->log_write_message("quick异常抛出:", "nt_data参数不存在");
  33. throw new Exception("nt_data参数不存在");
  34. }
  35. if (!array_key_exists("sign", $param)) {
  36. $this->log_write_message("quick异常抛出:", "sign参数不存在");
  37. throw new Exception("sign参数不存在");
  38. }
  39. if (!array_key_exists("md5Sign", $param)) {
  40. $this->log_write_message("quick异常抛出:", "md5Sign参数不存在");
  41. throw new Exception("md5Sign参数不存在");
  42. }
  43. $md5Sign = $param['md5Sign'];
  44. unset($param['md5Sign']);
  45. // // 验证签名
  46. if(QuickService::getSign($param,self::MD5KEY) != $md5Sign){
  47. $this->log_write_message("quick异常抛出:","签名错误" );
  48. throw new Exception("参数错误");
  49. }
  50. // 解密数据
  51. try {
  52. $nt_data_xml = QuickService::decode($param['nt_data'], self::CALLBACK_KEY);
  53. }catch (\Exception $e){
  54. $this->log_write_message("quick异常抛出:", "nt_data解密出错");
  55. throw new Exception("nt_data解密出错");
  56. }
  57. // 解析xml
  58. $result = QuickService::xmlToArray($nt_data_xml);
  59. $result = $result['message'];
  60. if (!$result) {
  61. $this->log_write_message("quick异常抛出:", "xml解密出错");
  62. throw new Exception("xml解密出错");
  63. }
  64. if ($result['status'] == 1) { //quick 支付状态判断
  65. $this->log_write_message("quick异常抛出:", $result['status'] . "在quick方支付失败");
  66. throw new Exception($result['status'] . "在quick方支付失败");
  67. }
  68. $this->updateOrder($result['game_order'], $result['amount'], $result['order_no']);
  69. echo "SUCCESS";exit;
  70. }
  71. public function log_write_message($tip, $param)
  72. {
  73. if (!is_dir(RUNTIME_PATH . 'quick')) {
  74. mkdir(RUNTIME_PATH . 'quick', 0777);
  75. }
  76. $logPath = RUNTIME_PATH . 'quick/log.txt';
  77. if (!file_exists($logPath)) {
  78. touch($logPath);
  79. chmod($logPath, 0777);
  80. }
  81. file_put_contents($logPath, $tip . ":\n" . var_export($param, true), FILE_APPEND);
  82. file_put_contents($logPath, $tip . ":\n" . json_encode($param, JSON_UNESCAPED_UNICODE), FILE_APPEND);
  83. }
  84. /**
  85. * 更新订单信息
  86. *
  87. * @param string $orderid :订单编号
  88. * @param string $real_amount :第三方支付的金额(现金部分)
  89. *
  90. * @return boolean
  91. */
  92. public function updateOrder($orderid, $real_amount = '', $sub_orderid = null)
  93. {
  94. $complexPayModel = new ComplexPay();
  95. $payCpinfoModel = new PayCpinfo;
  96. $complexMembersModel = new ComplexMembers();
  97. $result = false;
  98. //消费订单信息
  99. $payInfo = $complexPayModel->field('id,status,amount,userid,gameid,channel_id')->where(['orderid' => $orderid])->find();
  100. if ($real_amount != '' && $payInfo['amount'] != $real_amount) {
  101. $result = false;
  102. //写入日志
  103. $this->log_write_message("quick金额对比", '订单编号:' . $orderid . '支付金额不一致,订单状态更新失败');
  104. } //待支付状态时
  105. elseif ($payInfo['status'] == 0) {
  106. // 启动事务
  107. Db::startTrans();
  108. try {
  109. $complexPayModel->where(['id' => $payInfo['id']])->update(
  110. [
  111. 'status' => 1,
  112. 'sub_orderid' => $sub_orderid,
  113. 'pay_time' => NOW_TIMESTAMP
  114. ]);
  115. //cy_paycpinfo表记录状态更新
  116. $payCpinfoModel->where(['orderid' => $orderid])->update(['payflag' => 1, 'update_time' => NOW_TIMESTAMP]);
  117. $result = true;
  118. // 提交事务
  119. Db::commit();
  120. } catch (\Exception $e) {
  121. // 回滚事务
  122. Db::rollback();
  123. $result = false;
  124. //写入日志
  125. $this->log_write_message("quick异常抛出", '订单编号:' . $orderid . '订单状态更新失败' . $e->getMessage());
  126. }
  127. }
  128. //订单状态更新成功
  129. if ($result) {
  130. //更新member表的total_pay_amount
  131. $complexMembersModel->where(['id' => $payInfo['userid']])->update(['total_pay_amount' => Db::raw('total_pay_amount+' . $payInfo['amount'])]);
  132. // 通知CP,支付成功
  133. (new PayCallback())->callBackToCp($orderid);
  134. //预警邮件
  135. (new PayLogic())->payWarning($payInfo['userid'], $payInfo['channel_id'], $payInfo['gameid'], $payInfo['amount']);
  136. }
  137. }
  138. }