MwNotify.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\PayCpinfo;
  8. use think\Controller;
  9. use think\Db;
  10. use think\Log;
  11. use think\Request;
  12. class MwNotify extends Controller
  13. {
  14. const APPSECCRET = "642f0193ec59dbb781e48ad94b32e735";
  15. public function index(Request $request)
  16. {
  17. $param = $request->post("data");
  18. $filename = ROOT_PATH."mw.txt";
  19. if(!file_exists($filename)){
  20. touch($filename);
  21. chmod($filename,0777);
  22. }
  23. file_put_contents($filename,$param,FILE_APPEND);
  24. if (empty($param)) {
  25. Log::info("参数错误");
  26. return "FAIL";
  27. }
  28. $data = json_decode($param, true);
  29. if (json_last_error() != JSON_ERROR_NONE) {
  30. Log::info("数据解析错误");
  31. return "FAIL";
  32. }
  33. // 验证签名
  34. if (!$this->checkSign($data)) {
  35. Log::info("签名错误");
  36. return "FAIL";
  37. }
  38. // 检查订单
  39. $orderInfo = db("nw_complex_pay")->where("orderid", $data['cp_order_id'])->find();
  40. if (!$orderInfo) {
  41. Log::info($data['cp_order_id'] . ":订单信息不存在");
  42. return "FAIL";
  43. }
  44. if ($orderInfo['status'] == 1) {
  45. Log::info($data['cp_order_id'] . ":订单信息已支付不能重复支付");
  46. return "FAIL";
  47. }
  48. if ($orderInfo['amount'] != intval($data['amount'])) {
  49. Log::info($data['cp_order_id'] . ":订单金额错误");
  50. return "FAIL";
  51. }
  52. $this->updateOrder($orderInfo['orderid'], $data['amount'],$data['order_id']);
  53. return "SUCCESS";
  54. }
  55. /**
  56. * 验证签名
  57. */
  58. public function checkSign($data)
  59. {
  60. $sign = $data['sign'];
  61. unset($data['sign']);
  62. ksort($data);
  63. $signString = "";
  64. foreach ($data as $key => $val) {
  65. $signString .= $key . "=" . $val . "&";
  66. }
  67. $signString = trim($signString, "&");
  68. $signString = self::APPSECCRET . $signString . self::APPSECCRET;
  69. $sign_result = strtoupper(md5($signString));
  70. if ($sign == $sign_result) {
  71. return true;
  72. }
  73. return false;
  74. }
  75. /**
  76. * 更新订单信息
  77. *
  78. * @param string $orderid :订单编号
  79. * @param string $real_amount :第三方支付的金额(现金部分)
  80. *
  81. * @return boolean
  82. */
  83. public function updateOrder($orderid, $real_amount = '',$sub_order_id=null)
  84. {
  85. $complexPayModel = new ComplexPay();
  86. $payCpinfoModel = new PayCpinfo;
  87. $membersModel = new ComplexMembers();
  88. $result = false;
  89. //消费订单信息
  90. $payInfo = $complexPayModel->field('id,status,amount,userid,gameid,channel_id')->where(['orderid' => $orderid])->find();
  91. if ($real_amount != '' && $payInfo['amount'] != $real_amount) {
  92. $result = false;
  93. //写入日志
  94. log_message('订单编号:' . $orderid . '支付金额不一致,订单状态更新失败', 'error', LOG_PATH . '../paylog/');
  95. } //待支付状态时
  96. elseif ($payInfo['status'] == 0) {
  97. // 启动事务
  98. Db::startTrans();
  99. try {
  100. $complexPayModel->where(['id' => $payInfo['id']])->update(
  101. [
  102. 'status' => 1,
  103. 'pay_time' => NOW_TIMESTAMP,
  104. 'sub_orderid'=>$sub_order_id
  105. ]);
  106. //cy_paycpinfo表记录状态更新
  107. $payCpinfoModel->where(['orderid' => $orderid])->update(['payflag' => 1, 'update_time' => NOW_TIMESTAMP]);
  108. $result = true;
  109. // 提交事务
  110. Db::commit();
  111. } catch (\Exception $e) {
  112. // 回滚事务
  113. Db::rollback();
  114. $result = false;
  115. //写入日志
  116. log_message('订单编号:' . $orderid . '订单状态更新失败' . $e->getMessage(), 'error', LOG_PATH . '../paylog/');
  117. }
  118. }
  119. //订单状态更新成功
  120. if ($result) {
  121. //更新member表的total_pay_amount
  122. $membersModel->where(['id' => $payInfo['userid']])->update(['total_pay_amount' => Db::raw('total_pay_amount+' . $payInfo['amount'])]);
  123. // 通知CP,支付成功
  124. (new PayCallback())->callBackToCp($orderid);
  125. //预警邮件
  126. (new PayLogic())->payWarning($payInfo['userid'], $payInfo['channel_id'], $payInfo['gameid'], $payInfo['amount']);
  127. }
  128. }
  129. }