| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace App\Http\logic\api;
- use App\dao\Pay\AccountPayInfo;
- use App\dao\Pay\PayController;
- use App\Facade\TableName;
- use App\Http\helper\CreateOrder;
- use App\Http\helper\NumConfig;
- use App\Services\WiwiPay;
- use App\Services\CreateLog;
- use App\Util;
- use Illuminate\Support\Facades\DB;
- class WiwiPayLogic extends BaseApiLogic
- {
- public $result;
- public function pay_order($userId, $pay_amount, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
- {
- $dao = new AccountPayInfo();
- [$userPhone, $userName, $userEmail] = $dao->payInfo($userId);
- $pay_amount = (int)$pay_amount;
-
- // 礼包类型验证
- $PayVerify = new PayController();
- $pay_amount = $PayVerify->verify($userId, $GiftsID, $pay_amount);
- if ($PayVerify->verify($userId, $GiftsID, $pay_amount) === false) {
- $this->error = $PayVerify->getError();
- return false;
- }
- if ($pay_amount < 0) {
- $this->error = 'Payment error_4';
- return false;
- }
- $service = new WiwiPay();
- $config = $service->config;
- $order_sn = CreateOrder::order_sn($userId);
- // 生成订单信息
- $logic = new OrderLogic();
- $amount = $pay_amount * NumConfig::NUM_VALUE;
- $logic->orderCreate($order_sn, $amount, 'WiwiPay', $userId, '', $GiftsID, $AdId, $eventType);
- // 构建支付请求参数
- $params = [
- "mchNo" => $service->mchNo,
- "mchOrderNo" => $order_sn,
- "amount" => 499,
- "currency" => $config['currency'] ?? "usd",
- "wayCode" => !empty($pay_method) ? $pay_method : ($config['wayCode'] ?? "cashapp"),
- "clientIp" => $buyIP,
- "notifyUrl" => $config['notify'] ?? '',
- "returnUrl" => $config['return'] ?? '',
- "expiredTime" => 1800,
- "wayParam" => [
- // "deviceId" => $userId,
- "clientId" => $userId
- ],
- "timestamp" => round(microtime(true) * 1000), // 13位时间戳
- "signType" => $config['signType'] ?? "MD5"
- ];
- // 签名
- $signedParams = $service->sign($params);
- // 生成用户请求信息日志
- $request_extra = \GuzzleHttp\json_encode($signedParams);
- CreateLog::pay_request($userPhone, $request_extra, $order_sn, $userEmail, $userId, $userName);
- $url = $service->apiUrl;
- $result = $service->curlPost($url, $signedParams);
- $this->result = compact('result', 'signedParams', 'url');
-
- Util::WriteLog('WiwiPay', 'WiwiPay支付请求' . $url . " | " . $request_extra);
- Util::WriteLog('WiwiPay', 'WiwiPay支付结果' . $result);
-
- try {
- $data = \GuzzleHttp\json_decode($result, true);
- } catch (\Exception $e) {
- Util::WriteLog("WiwiPay_error", [$result, $e->getMessage(), $e->getTraceAsString()]);
- $this->error = 'Payment processing error';
- return false;
- }
-
- return $data;
- }
- public function notify($post)
- {
- $order_sn = $post['mchOrderNo'];
- try {
- // 查询订单信息
- $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->first();
- if (!$order) {
- Util::WriteLog('WiwiPay', '订单不存在');
- return '{"success":false,"message":"订单不存在"}';
- }
- if ((!empty($order->pay_at) || !empty($order->finished_at))) {
- return 'SUCCESS';
- }
- $body = [
- 'payment_sn' => $post['orderNo'] ?? $post['platformOrderNo'] ?? '',
- 'updated_at' => date('Y-m-d H:i:s'),
- ];
- $apply_data = [
- 'order_id' => $order->id,
- 'payment_sn' => $post['orderNo'] ?? $post['platformOrderNo'] ?? '',
- 'payment_code' => 'WiwiPay',
- 'return' => \GuzzleHttp\json_encode($post),
- 'is_error' => 0,
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s'),
- ];
- $ordStatus = $post['status'] ?? $post['orderStatus'] ?? '';
- $GiftsID = $order->GiftsID ?: '';
- $userID = $order->user_id ?: '';
- $AdId = $order->AdId ?: '';
- $eventType = $order->eventType ?: '';
- $payAmt = (int)($post['amount'] ?? $post['orderAmount'] ?? 0);
- // 订单成功状态判断(根据实际API调整)
- if ($ordStatus == 'SUCCESS' || $ordStatus == 2 || $ordStatus == 'PAID') {
- $body['pay_at'] = date('Y-m-d H:i:s');
- $body['finished_at'] = date('Y-m-d H:i:s');
- $body['status'] = 2;
- DB::connection('write')->table('agent.dbo.order')->where('id', $order->id)->update($body);
- DB::connection('write')->table('agent.dbo.apply_record')->insert($apply_data);
- // 处理订单完成后的业务逻辑
- $logic = new OrderLogic();
- $logic->orderFinished($order->id, $order->order_sn, $GiftsID, $userID, $AdId, $eventType, $payAmt);
- return 'SUCCESS';
- } else {
- Util::WriteLog('WiwiPay', '订单状态异常:' . $ordStatus);
- return '{"success":false,"message":"订单状态异常"}';
- }
- } catch (\Exception $exception) {
- Util::WriteLog("WiwiPay_error", $exception->getMessage());
- throw $exception;
- }
- }
- }
|