WiwiPayLogic.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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\WiwiPay;
  13. use App\Services\CreateLog;
  14. use App\Util;
  15. use Illuminate\Support\Facades\DB;
  16. class WiwiPayLogic 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. // $pay_amount = (int)$pay_amount;
  24. // 礼包类型验证
  25. $PayVerify = new PayController();
  26. $pay_amount = $PayVerify->verify($userId, $GiftsID, $pay_amount);
  27. if ($PayVerify->verify($userId, $GiftsID, $pay_amount) === false) {
  28. $this->error = $PayVerify->getError();
  29. return false;
  30. }
  31. if ($pay_amount < 0) {
  32. $this->error = 'Payment error_4';
  33. return false;
  34. }
  35. $service = new WiwiPay();
  36. $config = $service->config;
  37. $order_sn = CreateOrder::order_sn($userId);
  38. // 生成订单信息
  39. $logic = new OrderLogic();
  40. $amount = $pay_amount * NumConfig::NUM_VALUE;
  41. $logic->orderCreate($order_sn, $amount, 'WiwiPay', $userId, '', $GiftsID, $AdId, $eventType);
  42. // 构建支付请求参数
  43. $params = [
  44. "mchNo" => $service->mchNo,
  45. "mchOrderNo" => $order_sn,
  46. "amount" => $amount,
  47. "currency" => $config['currency'] ?? "usd",
  48. "wayCode" => !empty($pay_method) ? $pay_method : ($config['wayCode'] ?? "cashapp"),
  49. "clientIp" => $buyIP,
  50. "notifyUrl" => $config['notify'] ?? '',
  51. "returnUrl" => $config['return'] ?? '',
  52. "expiredTime" => 1800,
  53. "wayParam" => [
  54. // "deviceId" => $userId,
  55. "clientId" => $userId
  56. ],
  57. "timestamp" => round(microtime(true) * 1000), // 13位时间戳
  58. "signType" => $config['signType'] ?? "MD5"
  59. ];
  60. // 签名
  61. $signedParams = $service->sign($params);
  62. // 生成用户请求信息日志
  63. $request_extra = \GuzzleHttp\json_encode($signedParams);
  64. CreateLog::pay_request($userPhone, $request_extra, $order_sn, $userEmail, $userId, $userName);
  65. $url = $service->apiUrl;
  66. $result = $service->curlPost($url, $signedParams);
  67. $this->result = compact('result', 'signedParams', 'url');
  68. Util::WriteLog('WiwiPay', 'WiwiPay支付请求' . $url . " | " . $request_extra);
  69. Util::WriteLog('WiwiPay', 'WiwiPay支付结果' . $result);
  70. try {
  71. $data = \GuzzleHttp\json_decode($result, true);
  72. } catch (\Exception $e) {
  73. Util::WriteLog("WiwiPay_error", [$result, $e->getMessage(), $e->getTraceAsString()]);
  74. $this->error = 'Payment processing error';
  75. return false;
  76. }
  77. return $data;
  78. }
  79. public function notify($post)
  80. {
  81. $order_sn = $post['mchOrderNo'];
  82. try {
  83. // 查询订单信息
  84. $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->first();
  85. if (!$order) {
  86. Util::WriteLog('WiwiPay', '订单不存在');
  87. return '{"success":false,"message":"订单不存在"}';
  88. }
  89. if ((!empty($order->pay_at) || !empty($order->finished_at))) {
  90. return 'success';
  91. }
  92. $body = [
  93. 'payment_sn' => $post['orderNo'] ?? $post['channelOrderNo'] ?? '',
  94. 'updated_at' => date('Y-m-d H:i:s'),
  95. ];
  96. // 支付订单状态
  97. //0-订单生成
  98. //1-支付中
  99. //2-支付成功
  100. //3-支付失败
  101. //4-已撤销
  102. //5-已退款
  103. //6-订单关闭
  104. $ordStatus = $post['state'] ??'';
  105. $GiftsID = $order->GiftsID ?: '';
  106. $userID = $order->user_id ?: '';
  107. $AdId = $order->AdId ?: '';
  108. $eventType = $order->eventType ?: '';
  109. $post['amount'] = $post['realAmount']??$post['amount'];
  110. $payAmt = round(intval($post['amount'])/100,2);
  111. switch ($ordStatus) {
  112. case 2: // 支付成功
  113. $body['pay_status'] = 1;
  114. $body['pay_at'] = date('Y-m-d H:i:s');
  115. $body['finished_at'] = date('Y-m-d H:i:s');
  116. $body['amount'] = $payAmt * NumConfig::NUM_VALUE;
  117. $config = (new PayConfig())->getConfig('WiwiPay');
  118. $body['payment_fee']=$body['amount']*$config['payin_fee'];
  119. try {
  120. // 获取金额
  121. $service = new OrderServices();
  122. if (intval($order->amount) != $body['amount']) {
  123. $body['GiftsID'] = 0;
  124. $body['amount'] = $payAmt * NumConfig::NUM_VALUE;
  125. $Recharge = $payAmt;
  126. $give = 0;
  127. $favorable_price = $Recharge + $give;
  128. $czReason = 1;
  129. $cjReason = 45;
  130. } else {
  131. [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $service->getPayInfo($GiftsID, $userID, $payAmt);
  132. }
  133. // 增加充值记录
  134. [$Score] = $service->addRecord($userID, $payAmt, $favorable_price, $order_sn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType);
  135. // 成功处理回调
  136. Order::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID, $order_sn]);
  137. }catch (\Exception $exception) {
  138. Util::WriteLog("WiwiPay_error", $exception->getMessage());
  139. }
  140. break;
  141. case 3: // 支付失败
  142. $body['pay_status'] = 2;
  143. $apply_data['is_error'] = 1;
  144. break;
  145. }
  146. $order_up = DB::connection('write')->table('agent.dbo.order')
  147. ->where('order_sn', $order_sn)
  148. ->update($body);
  149. // $apply = DB::connection('write')->table('agent.dbo.payment_apply')
  150. // ->insert($apply_data);
  151. if (($order_up) != true) {
  152. Util::WriteLog('WiwiPay','订单更新失败');
  153. return '{"success":false,"message":"商户自定义出错信息"}';
  154. }
  155. //Util::WriteLog('SitoBank','success');
  156. Util::WriteLog("WiwiPay", 'success');
  157. return 'success';
  158. } catch (\Exception $exception) {
  159. Util::WriteLog("WiwiPay_error", $exception->getMessage());
  160. throw $exception;
  161. }
  162. }
  163. }