WanCmsNotify.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\controller\Api;
  4. use app\common\logic\Pay as PayLogic;
  5. use app\common\logic\PayCallback;
  6. use app\common\model\Members;
  7. use app\common\model\ComplexPay;
  8. use app\common\model\PayCpinfo;
  9. use think\Controller;
  10. use think\Db;
  11. use think\Log;
  12. use app\common\model\ComplexMembers;
  13. class WanCmsNotify extends Controller
  14. {
  15. public function index()
  16. {
  17. $param = request()->param();
  18. if (!isset($param['type'])) {
  19. throw new \Exception("路由有误");
  20. }
  21. if (!is_dir(RUNTIME_PATH . 'wancms')) {
  22. mkdir(RUNTIME_PATH . 'wancms', 0777);
  23. }
  24. $logPath = RUNTIME_PATH . 'wancms/log.txt';
  25. if (!file_exists($logPath)) {
  26. touch($logPath);
  27. chmod($logPath, 0777);
  28. }
  29. file_put_contents($logPath, var_export($param, true), FILE_APPEND);
  30. file_put_contents($logPath, json_encode($param, JSON_UNESCAPED_UNICODE), FILE_APPEND);
  31. if (empty($param)) {
  32. file_put_contents($logPath, "参数为空" . "\n", FILE_APPEND);
  33. echo "error";
  34. die;
  35. }
  36. // $param = '{"orderid":"16370446509707","username":"gb713555299","gameid":"3237","roleid":"10086","serverid":"888","paytype":"ptb","amount":"1","paytime":"1637044650","attach":"CMWL16370446471004179310","sign":"04a0b6ad8e1b1ff3f3f52d4d5b871049","type":"2"}';
  37. // $param =json_decode($param,true);
  38. // if ($param['type'] == 1) { // 1. 安卓 2.ios
  39. // $appKey = "01c28224724dac701e06b90238ec7880";
  40. // } else {
  41. // $appKey = "558f7d1bfa36f9b3fb5fa81cd18262a8";
  42. // }
  43. $appKey = 'b61f7d6da1c424d74c0b9f385dc3216c';
  44. // 验签
  45. $signString = "orderid=" . $param['orderid'] . "&";
  46. $signString .= "username=" . $param['username'] . "&";
  47. $signString .= "gameid=" . $param['gameid'] . "&";
  48. $signString .= "roleid=" . $param['roleid'] . "&";
  49. $signString .= "serverid=" . $param['serverid'] . "&";
  50. $signString .= "paytype=" . $param['paytype'] . "&";
  51. $signString .= "amount=" . $param['amount'] . "&";
  52. $signString .= "paytime=" . $param['paytime'] . "&";
  53. $signString .= "attach=" . $param['attach'] . "&";
  54. $signString .= "appkey=" . $appKey;
  55. $signGen = strtolower(md5(urldecode($signString)));
  56. if ($signGen != $param['sign']) {
  57. // file_put_contents($logPath, "签名错误", FILE_APPEND);
  58. echo "errorSign";
  59. die;
  60. }
  61. $this->updateOrder($param['attach'], $param['amount']);
  62. echo "success";
  63. }
  64. /**
  65. * 更新订单信息
  66. *
  67. * @param string $orderid :订单编号
  68. * @param string $real_amount :第三方支付的金额(现金部分)
  69. *
  70. * @return boolean
  71. */
  72. public function updateOrder($orderid, $real_amount = '')
  73. {
  74. $complexPayModel = new ComplexPay();
  75. $payCpinfoModel = new PayCpinfo;
  76. $membersModel = new ComplexMembers();
  77. $result = false;
  78. //消费订单信息
  79. $payInfo = $complexPayModel->field('id,status,amount,userid,gameid,channel_id')->where(['orderid' => $orderid])->find();
  80. if ($real_amount != '' && $payInfo['amount'] != $real_amount) {
  81. $result = false;
  82. //写入日志
  83. log_message('订单编号:' . $orderid . '支付金额不一致,订单状态更新失败', 'error', LOG_PATH . '../paylog/');
  84. } //待支付状态时
  85. elseif ($payInfo['status'] == 0) {
  86. // 启动事务
  87. Db::startTrans();
  88. try {
  89. $complexPayModel->where(['id' => $payInfo['id']])->update(['status' => 1, 'pay_time' => NOW_TIMESTAMP]);
  90. //cy_paycpinfo表记录状态更新
  91. $payCpinfoModel->where(['orderid' => $orderid])->update(['payflag' => 1,'update_time'=>NOW_TIMESTAMP]);
  92. $result = true;
  93. // 提交事务
  94. Db::commit();
  95. } catch (\Exception $e) {
  96. // 回滚事务
  97. Db::rollback();
  98. $result = false;
  99. //写入日志
  100. log_message('订单编号:' . $orderid . '订单状态更新失败' . $e->getMessage(), 'error', LOG_PATH . '../paylog/');
  101. }
  102. }
  103. //订单状态更新成功
  104. if ($result) {
  105. //更新member表的total_pay_amount
  106. $membersModel->where(['id' => $payInfo['userid']])->update(['total_pay_amount' => Db::raw('total_pay_amount+' . $payInfo['amount'])]);
  107. // 通知CP,支付成功
  108. (new PayCallback())->callBackToCp($orderid);
  109. //预警邮件
  110. (new PayLogic())->payWarning($payInfo['userid'], $payInfo['channel_id'], $payInfo['gameid'], $payInfo['amount']);
  111. }
  112. }
  113. }