WDPayLogic.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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\Jobs\Order;
  9. use App\Notification\TelegramBot;
  10. use App\Services\OrderServices;
  11. use App\Services\PayConfig;
  12. use App\Services\WDPay;
  13. use App\Services\CreateLog;
  14. use App\Util;
  15. use Illuminate\Support\Facades\DB;
  16. class WDPayLogic extends BaseApiLogic
  17. {
  18. public $result;
  19. public function pay_order($userId, $pay_amount, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  20. {
  21. $dao = new AccountPayInfo();
  22. [$userPhone, $userName, $userEmail] = $dao->payInfo($userId);
  23. // 礼包类型验证
  24. $PayVerify = new PayController();
  25. $pay_amount = $PayVerify->verify($userId, $GiftsID, $pay_amount);
  26. if ($PayVerify->verify($userId, $GiftsID, $pay_amount) === false) {
  27. $this->error = $PayVerify->getError();
  28. return false;
  29. }
  30. if ($pay_amount < 0) {
  31. $this->error = 'Payment error_4';
  32. return false;
  33. }
  34. $service = new WDPay();
  35. $config = $service->config;
  36. $order_sn = CreateOrder::order_sn($userId);
  37. // 生成订单信息(WDPay使用元,但数据库存储还是用分)
  38. $logic = new OrderLogic();
  39. $amount = (int) round($pay_amount * NumConfig::NUM_VALUE);
  40. $logic->orderCreate($order_sn, $amount, 'WDPay', $userId, '', $GiftsID, $AdId, $eventType);
  41. $payMethods = [
  42. 1 => 'cashapp',
  43. 2 => 'paypal',
  44. 4 => 'applepay',
  45. ];
  46. $wdMethod = @$payMethods[$pay_method];
  47. // 构建支付请求参数(按WDPay官方文档 /charging/create-pay-in)
  48. // 金额格式:元,必须保留2位小数,如 4.99
  49. $params = [
  50. "country" => "US", // 国家,默认US
  51. "currency" => "USD", // 货币,默认USD
  52. "amount" => number_format($pay_amount, 2, '.', ''), // 价格,必须保留2位小数
  53. "platform" =>Util::getDeviceType(), // 设备:android/ios
  54. "ip" => $buyIP, // 用户真实IP
  55. "username" => $userName ?: 'user' . $userId, // 用户名称
  56. "customer" => $config['customer'], // 商户简称
  57. "customerNo" => $config['customerNo'], // 商户编号
  58. "customerOrderNo" => $order_sn, // 商户自定义订单号
  59. "wayCode" => $wdMethod ? $wdMethod : ($config['wayCode'] ?? "cashapp"), // cashapp或paypal
  60. "timestamp" => (string)round(microtime(true) * 1000), // 13位时间戳(毫秒)
  61. "signType" => "MD5", // 传MD5
  62. "callback" => $config['notify'] ?? '', // 异步通知地址
  63. ];
  64. // redirectUrl 是可选参数
  65. if (!empty($config['return'])) {
  66. $params["redirectUrl"] = $config['return']; // 支付成功后跳转的页面
  67. }
  68. // 签名
  69. $signedParams = $service->sign($params);
  70. // 生成用户请求信息日志
  71. $request_extra = \GuzzleHttp\json_encode($signedParams);
  72. CreateLog::pay_request($userPhone, $request_extra, $order_sn, $userEmail, $userId, $userName);
  73. $url = $service->apiUrl;
  74. $result = $service->curlPost($url, $signedParams);
  75. $rresult = compact('result', 'signedParams', 'url');
  76. Util::WriteLog('WDPay', 'WDPay支付请求' . $url . " | " . $request_extra);
  77. Util::WriteLog('WDPay', 'WDPay支付结果' . json_encode($rresult));
  78. try {
  79. $res = \GuzzleHttp\json_decode($result, true);
  80. } catch (\Exception $e) {
  81. Util::WriteLog("WDPay_error", [$result, $e->getMessage(), $e->getTraceAsString()]);
  82. $this->error = 'Payment processing failed';
  83. return false;
  84. }
  85. // WDPay官方返回格式:
  86. // {"code": 200, "data": {...}, "message": "success"}
  87. if (isset($res['code']) && $res['code'] == 200) {
  88. // 转换为统一格式供控制器使用
  89. return [
  90. 'code' => 0, // 统一成功码
  91. 'data' => [
  92. 'cashierUrl' => $res['data']['paylink'] ?? '', // 支付链接
  93. 'mchOrderNo' => $order_sn, // 商户订单号
  94. 'orderNo' => $res['data']['id'] ?? '', // WDPay订单号
  95. 'amount' => $res['data']['amount'] ?? $pay_amount,
  96. 'currency' => $res['data']['currency'] ?? 'USD',
  97. 'status' => $res['data']['status'] ?? 'initiated',
  98. 'paychannel' => $res['data']['paychannel'] ?? '',
  99. 'time_created' => $res['data']['time_created'] ?? time(),
  100. ]
  101. ];
  102. } else {
  103. $this->error = $res['message'] ?? $res['msg'] ?? 'Payment failed';
  104. Util::WriteLog('WDPay_error', 'Payment failed: ' . json_encode($res));
  105. return false;
  106. }
  107. }
  108. public function notify($post)
  109. {
  110. try {
  111. if (!is_array($post)) $post = \GuzzleHttp\json_decode($post, true);
  112. Util::WriteLog('WDPay', "WDPay异步回调处理\n" . json_encode($post, JSON_UNESCAPED_UNICODE));
  113. // WDPay官方回调字段:
  114. // customerOrderNo: 商户订单号
  115. // orderNo: WDPay订单号
  116. // amount: 金额(元)
  117. // currency: 货币
  118. // status: 订单状态 (succeeded=成功, closed=失败)
  119. // signType: MD5
  120. // sign: 签名
  121. $order_sn = $post['customerOrderNo'] ?? ''; // 商户订单号
  122. $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->first();
  123. if (!$order) {
  124. Util::WriteLog('WDPay', '订单不存在: ' . $order_sn);
  125. return response()->json(['success' => false, 'message' => 'Order not found']);
  126. }
  127. // 订单已处理,直接返回成功
  128. if ((!empty($order->pay_at) || !empty($order->finished_at))) {
  129. Util::WriteLog('WDPay', '订单已处理: ' . $order_sn);
  130. // return 'success';
  131. return '{"msg":"success","code":200}';
  132. }
  133. $body = [
  134. 'payment_sn' => $post['orderNo'] ?? '', // WDPay订单号
  135. 'updated_at' => date('Y-m-d H:i:s'),
  136. ];
  137. $ordStatus = $post['status'] ?? ''; // WDPay状态字段
  138. $GiftsID = $order->GiftsID ?: '';
  139. $userID = $order->user_id ?: '';
  140. $AdId = $order->AdId ?: '';
  141. $eventType = $order->eventType ?: '';
  142. // WDPay金额是元,需要转换为分
  143. $payAmt = floatval($post['amount'] ?? 0);
  144. // WDPay状态判断:succeeded=成功, closed=失败
  145. switch (strtolower($ordStatus)) {
  146. case 'succeeded': // 支付成功(官方状态)
  147. $body['pay_status'] = 1;
  148. $body['pay_at'] = date('Y-m-d H:i:s');
  149. $body['finished_at'] = date('Y-m-d H:i:s');
  150. $body['amount'] = (int) round($payAmt * NumConfig::NUM_VALUE);
  151. $config = (new PayConfig())->getConfig('WDPay');
  152. $body['payment_fee'] = $body['amount'] * ($config['payin_fee'] ?? 0);
  153. try {
  154. // 获取金额
  155. $service = new OrderServices();
  156. if (intval($order->amount) != $body['amount']) {
  157. // 金额不匹配,按实际支付金额处理
  158. $body['GiftsID'] = 0;
  159. $body['amount'] = (int) round($payAmt * NumConfig::NUM_VALUE);
  160. $Recharge = $payAmt;
  161. $give = 0;
  162. $favorable_price = $Recharge + $give;
  163. $czReason = 1;
  164. $cjReason = 45;
  165. } else {
  166. [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $service->getPayInfo($GiftsID, $userID, $payAmt);
  167. }
  168. // 增加充值记录
  169. [$Score] = $service->addRecord($userID, $payAmt, $favorable_price, $order_sn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType);
  170. // 成功处理回调
  171. Order::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID, $order_sn]);
  172. Util::WriteLog("WDPay", "订单处理成功: {$order_sn}, 用户: {$userID}, 金额: {$payAmt}, 到账: {$Score}");
  173. } catch (\Exception $exception) {
  174. Util::WriteLog("WDPay_error", "订单处理异常: " . $exception->getMessage());
  175. throw $exception;
  176. }
  177. break;
  178. case 'closed': // 支付失败(官方状态)
  179. $body['pay_status'] = 2;
  180. Util::WriteLog('WDPay', "订单支付失败: {$order_sn}, 状态: closed");
  181. break;
  182. default: // 其他状态(不应该出现)
  183. Util::WriteLog('WDPay', "订单未知状态: {$order_sn}, 状态: {$ordStatus}");
  184. return '{"msg":"success","code":200}'; // 返回成功
  185. }
  186. $order_up = DB::connection('write')->table('agent.dbo.order')
  187. ->where('order_sn', $order_sn)
  188. ->update($body);
  189. if (!$order_up) {
  190. Util::WriteLog('WDPay', '订单更新失败: ' . $order_sn);
  191. return response()->json(['success' => false, 'message' => 'Update failed']);
  192. }
  193. Util::WriteLog("WDPay", "订单回调处理完成: {$order_sn}");
  194. return '{"msg":"success","code":200}';
  195. } catch (\Exception $exception) {
  196. Util::WriteLog("WDPay_error", "回调异常: " . $exception->getMessage() . "\n" . $exception->getTraceAsString());
  197. throw $exception;
  198. }
  199. }
  200. }