PagYeepPayLogic.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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\PagYeepPay;
  13. use App\Services\CreateLog;
  14. use App\Util;
  15. use Illuminate\Support\Facades\DB;
  16. class PagYeepPayLogic 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 PagYeepPay();
  35. $config = $service->config;
  36. $order_sn = CreateOrder::order_sn($userId);
  37. // 生成订单信息(PagYeepPay金额单位:分)
  38. $logic = new OrderLogic();
  39. $amount = (int) round($pay_amount * NumConfig::NUM_VALUE);
  40. $logic->orderCreate($order_sn, $amount, 'PagYeepPay', $userId, $pay_method, $GiftsID, $AdId, $eventType);
  41. // 支付方式映射(根据文档:CASH_APP、APPLE_PAY等)
  42. $payMethods = [
  43. 1 => '1204',
  44. // 2 => '1207',
  45. 4 => '1205',
  46. 8 => '1208'
  47. ];
  48. $payType = @$payMethods[$pay_method] ?: '1204';
  49. // 构建支付请求参数(根据文档)
  50. $params = [
  51. "version" => $config['version'] ?? "2.1", // 接口版本号
  52. "orgNo" => $config['orgNo'], // 机构编号
  53. "custId" => $config['custId'], // 商户编号
  54. "custOrderNo" => $order_sn, // 商户订单号
  55. // "tranType" => 1,
  56. "clearType" => "01",
  57. "tranType" => $payType, // 支付类型
  58. "payAmt" => (int) round($pay_amount * 100), // 支付金额(单位:分)
  59. "backUrl" => $config['notify'] ?? '', // 支付结果异步通知地址
  60. "frontUrl" => $config['return'] ?? '', // 支付成功返回地址
  61. "goodsName" => "Gift",
  62. "orderDesc" => "Gift Goos",
  63. "buyIp" => $buyIP,
  64. "userName" => $userName,
  65. "userEmail" => $userEmail,
  66. "userPhone" => $userPhone,
  67. "countryCode" => "US",
  68. "currency" => "USD", // 货币
  69. ];
  70. // 签名
  71. $signedParams = $service->sign($params);
  72. // 生成用户请求信息日志
  73. $request_extra = \GuzzleHttp\json_encode($signedParams);
  74. CreateLog::pay_request($userPhone, $request_extra, $order_sn, $userEmail, $userId, $userName);
  75. $url = $service->apiUrl.'/establish/defray.ac';
  76. $result = $service->curlPost($url, $signedParams);
  77. $rresult = compact('result', 'signedParams', 'url');
  78. Util::WriteLog('PagYeepPay', 'PagYeepPay支付请求' . $url . " | " . $request_extra);
  79. Util::WriteLog('PagYeepPay', 'PagYeepPay支付结果' . json_encode($rresult));
  80. try {
  81. // 解析返回结果(可能是form表单格式或JSON格式)
  82. // parse_str($result, $res);
  83. // if (empty($res)) {
  84. $res = \GuzzleHttp\json_decode($result, true);
  85. // }
  86. } catch (\Exception $e) {
  87. Util::WriteLog("PagYeepPay_error", [$result, $e->getMessage(), $e->getTraceAsString()]);
  88. $this->error = 'Payment processing failed';
  89. return false;
  90. }
  91. // 根据文档,返回码000000表示成功
  92. if (isset($res['code']) && $res['code'] == '000000') {
  93. // 转换为统一格式供控制器使用
  94. return [
  95. 'code' => 0, // 统一成功码
  96. 'data' => [
  97. 'cashierUrl' => @$res['busContent']['url'], // 支付链接(如果有)
  98. 'mchOrderNo' => $order_sn, // 商户订单号
  99. 'orderNo' => $res['prdOrdNo'] ?? '', // 平台订单号
  100. 'amount' => $pay_amount,
  101. 'currency' => 'USD',
  102. 'status' => 'initiated',
  103. ]
  104. ];
  105. } else {
  106. $this->error = $res['msg'] ?? 'Payment failed';
  107. Util::WriteLog('PagYeepPay_error', 'Payment failed: ' . json_encode($res));
  108. return false;
  109. }
  110. }
  111. public function notify($post)
  112. {
  113. try {
  114. if (!is_array($post)) {
  115. // 可能是form表单格式
  116. parse_str($post, $post);
  117. if (empty($post)) {
  118. $post = \GuzzleHttp\json_decode($post, true);
  119. }
  120. }
  121. Util::WriteLog('PagYeepPay', "PagYeepPay异步回调处理\n" . json_encode($post, JSON_UNESCAPED_UNICODE));
  122. // 根据文档,回调参数:
  123. // version, orgNo, custId, custOrderNo, prdOrdNo, payAmt, ordStatus, sign
  124. $order_sn = $post['custOrderNo'] ?? ''; // 商户订单号
  125. $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->first();
  126. if (!$order) {
  127. Util::WriteLog('PagYeepPay', '订单不存在: ' . $order_sn);
  128. return 'SC000000'; // 根据文档,返回SC000000表示成功
  129. }
  130. // 订单已处理,直接返回成功
  131. if ((!empty($order->pay_at) || !empty($order->finished_at))) {
  132. Util::WriteLog('PagYeepPay', '订单已处理: ' . $order_sn);
  133. return 'SC000000';
  134. }
  135. $body = [
  136. 'payment_sn' => $post['prdOrdNo'] ?? '', // 平台订单号
  137. 'updated_at' => date('Y-m-d H:i:s'),
  138. ];
  139. $ordStatus = $post['ordStatus'] ?? ''; // 订单状态
  140. $GiftsID = $order->GiftsID ?: '';
  141. $userID = $order->user_id ?: '';
  142. $AdId = $order->AdId ?: '';
  143. $eventType = $order->eventType ?: '';
  144. // 金额单位:分
  145. $payAmt = intval($post['payAmt'] ?? 0);
  146. $payAmtYuan = $payAmt / 100; // 转换为元
  147. // 根据文档,订单状态:01=成功, 02=失败, 其他=处理中
  148. switch ($ordStatus) {
  149. case '01': // 支付成功
  150. $body['pay_status'] = 1;
  151. $body['pay_at'] = date('Y-m-d H:i:s');
  152. $body['finished_at'] = date('Y-m-d H:i:s');
  153. $body['amount'] = $payAmt; // 已经是分
  154. $config = (new PayConfig())->getConfig('PagYeepPay');
  155. $body['payment_fee'] = $body['amount'] * ($config['payin_fee'] ?? 0);
  156. try {
  157. // 获取金额
  158. $service = new OrderServices();
  159. if (intval($order->amount) != $body['amount']) {
  160. // 金额不匹配,按实际支付金额处理
  161. $body['GiftsID'] = 0;
  162. $body['amount'] = $payAmt;
  163. $Recharge = $payAmtYuan;
  164. $give = 0;
  165. $favorable_price = $Recharge + $give;
  166. $czReason = 1;
  167. $cjReason = 45;
  168. } else {
  169. [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $service->getPayInfo($GiftsID, $userID, $payAmtYuan);
  170. }
  171. // 增加充值记录
  172. [$Score] = $service->addRecord($userID, $payAmtYuan, $favorable_price, $order_sn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType);
  173. // 成功处理回调
  174. Order::dispatch([$userID, $payAmtYuan, $Score, $favorable_price, $GiftsID, $order_sn]);
  175. Util::WriteLog("PagYeepPay", "订单处理成功: {$order_sn}, 用户: {$userID}, 金额: {$payAmtYuan}, 到账: {$Score}");
  176. } catch (\Exception $exception) {
  177. Util::WriteLog("PagYeepPay_error", "订单处理异常: " . $exception->getMessage());
  178. throw $exception;
  179. }
  180. break;
  181. case '02': // 支付失败
  182. $body['pay_status'] = 2;
  183. Util::WriteLog('PagYeepPay', "订单支付失败: {$order_sn}, 状态: 02");
  184. break;
  185. default: // 其他状态(处理中)
  186. Util::WriteLog('PagYeepPay', "订单处理中: {$order_sn}, 状态: {$ordStatus}");
  187. return 'SC000000'; // 返回成功
  188. }
  189. $order_up = DB::connection('write')->table('agent.dbo.order')
  190. ->where('order_sn', $order_sn)
  191. ->update($body);
  192. if (!$order_up) {
  193. Util::WriteLog('PagYeepPay', '订单更新失败: ' . $order_sn);
  194. return 'SC000000'; // 仍然返回成功
  195. }
  196. Util::WriteLog("PagYeepPay", "订单回调处理完成: {$order_sn}");
  197. return 'SC000000';
  198. } catch (\Exception $exception) {
  199. Util::WriteLog("PagYeepPay_error", "回调异常: " . $exception->getMessage() . "\n" . $exception->getTraceAsString());
  200. throw $exception;
  201. }
  202. }
  203. }