WiwiPayLogic.php 7.4 KB

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