WiwiPayLogic.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Http\logic\api;
  3. use App\dao\Pay\AccountPayInfo;
  4. use App\dao\Pay\PayController;
  5. use App\Facade\TableName;
  6. use App\Http\helper\CreateOrder;
  7. use App\Http\helper\NumConfig;
  8. use App\Services\WiwiPay;
  9. use App\Services\CreateLog;
  10. use App\Util;
  11. use Illuminate\Support\Facades\DB;
  12. class WiwiPayLogic extends BaseApiLogic
  13. {
  14. public $result;
  15. public function pay_order($userId, $pay_amount, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  16. {
  17. $dao = new AccountPayInfo();
  18. [$userPhone, $userName, $userEmail] = $dao->payInfo($userId);
  19. $pay_amount = (int)$pay_amount;
  20. // 礼包类型验证
  21. $PayVerify = new PayController();
  22. $pay_amount = $PayVerify->verify($userId, $GiftsID, $pay_amount);
  23. if ($PayVerify->verify($userId, $GiftsID, $pay_amount) === false) {
  24. $this->error = $PayVerify->getError();
  25. return false;
  26. }
  27. if ($pay_amount < 0) {
  28. $this->error = 'Payment error_4';
  29. return false;
  30. }
  31. $service = new WiwiPay();
  32. $config = $service->config;
  33. $order_sn = CreateOrder::order_sn($userId);
  34. // 生成订单信息
  35. $logic = new OrderLogic();
  36. $amount = $pay_amount * NumConfig::NUM_VALUE;
  37. $logic->orderCreate($order_sn, $amount, 'WiwiPay', $userId, '', $GiftsID, $AdId, $eventType);
  38. // 构建支付请求参数
  39. $params = [
  40. "mchNo" => $service->mchNo,
  41. "mchOrderNo" => $order_sn,
  42. "amount" => 499,
  43. "currency" => $config['currency'] ?? "usd",
  44. "wayCode" => !empty($pay_method) ? $pay_method : ($config['wayCode'] ?? "cashapp"),
  45. "clientIp" => $buyIP,
  46. "notifyUrl" => $config['notify'] ?? '',
  47. "returnUrl" => $config['return'] ?? '',
  48. "expiredTime" => 1800,
  49. "wayParam" => [
  50. // "deviceId" => $userId,
  51. "clientId" => $userId
  52. ],
  53. "timestamp" => round(microtime(true) * 1000), // 13位时间戳
  54. "signType" => $config['signType'] ?? "MD5"
  55. ];
  56. // 签名
  57. $signedParams = $service->sign($params);
  58. // 生成用户请求信息日志
  59. $request_extra = \GuzzleHttp\json_encode($signedParams);
  60. CreateLog::pay_request($userPhone, $request_extra, $order_sn, $userEmail, $userId, $userName);
  61. $url = $service->apiUrl;
  62. $result = $service->curlPost($url, $signedParams);
  63. $this->result = compact('result', 'signedParams', 'url');
  64. Util::WriteLog('WiwiPay', 'WiwiPay支付请求' . $url . " | " . $request_extra);
  65. Util::WriteLog('WiwiPay', 'WiwiPay支付结果' . $result);
  66. try {
  67. $data = \GuzzleHttp\json_decode($result, true);
  68. } catch (\Exception $e) {
  69. Util::WriteLog("WiwiPay_error", [$result, $e->getMessage(), $e->getTraceAsString()]);
  70. $this->error = 'Payment processing error';
  71. return false;
  72. }
  73. return $data;
  74. }
  75. public function notify($post)
  76. {
  77. $order_sn = $post['mchOrderNo'];
  78. try {
  79. // 查询订单信息
  80. $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->first();
  81. if (!$order) {
  82. Util::WriteLog('WiwiPay', '订单不存在');
  83. return '{"success":false,"message":"订单不存在"}';
  84. }
  85. if ((!empty($order->pay_at) || !empty($order->finished_at))) {
  86. return 'SUCCESS';
  87. }
  88. $body = [
  89. 'payment_sn' => $post['orderNo'] ?? $post['platformOrderNo'] ?? '',
  90. 'updated_at' => date('Y-m-d H:i:s'),
  91. ];
  92. $apply_data = [
  93. 'order_id' => $order->id,
  94. 'payment_sn' => $post['orderNo'] ?? $post['platformOrderNo'] ?? '',
  95. 'payment_code' => 'WiwiPay',
  96. 'return' => \GuzzleHttp\json_encode($post),
  97. 'is_error' => 0,
  98. 'created_at' => date('Y-m-d H:i:s'),
  99. 'updated_at' => date('Y-m-d H:i:s'),
  100. ];
  101. $ordStatus = $post['status'] ?? $post['orderStatus'] ?? '';
  102. $GiftsID = $order->GiftsID ?: '';
  103. $userID = $order->user_id ?: '';
  104. $AdId = $order->AdId ?: '';
  105. $eventType = $order->eventType ?: '';
  106. $payAmt = (int)($post['amount'] ?? $post['orderAmount'] ?? 0);
  107. // 订单成功状态判断(根据实际API调整)
  108. if ($ordStatus == 'SUCCESS' || $ordStatus == 2 || $ordStatus == 'PAID') {
  109. $body['pay_at'] = date('Y-m-d H:i:s');
  110. $body['finished_at'] = date('Y-m-d H:i:s');
  111. $body['status'] = 2;
  112. DB::connection('write')->table('agent.dbo.order')->where('id', $order->id)->update($body);
  113. DB::connection('write')->table('agent.dbo.apply_record')->insert($apply_data);
  114. // 处理订单完成后的业务逻辑
  115. $logic = new OrderLogic();
  116. $logic->orderFinished($order->id, $order->order_sn, $GiftsID, $userID, $AdId, $eventType, $payAmt);
  117. return 'SUCCESS';
  118. } else {
  119. Util::WriteLog('WiwiPay', '订单状态异常:' . $ordStatus);
  120. return '{"success":false,"message":"订单状态异常"}';
  121. }
  122. } catch (\Exception $exception) {
  123. Util::WriteLog("WiwiPay_error", $exception->getMessage());
  124. throw $exception;
  125. }
  126. }
  127. }