| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- namespace App\Http\logic\api;
- use App\Constant\Payment;
- use App\dao\Pay\PayController;
- use App\Http\helper\CreateOrder;
- use App\Http\helper\NumConfig;
- use App\Jobs\Order;
- use App\Services\OrderServices;
- use App\Services\PayConfig;
- use App\Services\StarPayService;
- use App\Services\SupefinaSpei;
- use App\Services\CreateLog;
- use App\Util;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * Supefina SPEI 墨西哥代收(充值)逻辑
- * 文档:https://docs.supefina.net/huan-ying-shi-yong-supefina-de-api-wen-dang/dai-shou/mo-xi-ge/spei
- * 代收回调以 realityAmount 为准入账(处理用户实转金额与订单金额不一致)
- */
- class StarPayLogic extends BaseApiLogic
- {
- public $result;
- public function pay_order($userId, $pay_amount, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
- {
- $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;
- }
- $payConfigService = new PayConfig();
- $config = $payConfigService->getConfig('StarPay') ?? [];
- if (empty($config)) {
- $this->error = 'Payment config error';
- return false;
- }
- $service = new StarPayService($config);
- $order_sn = CreateOrder::order_sn($userId);
- $logic = new OrderLogic();
- $amount = round($pay_amount, 2);
- $logic->orderCreate($order_sn, $amount * NumConfig::NUM_VALUE, 'StarPay', $userId, $pay_method, $GiftsID, $AdId, $eventType);
- try {
- $data = $service->create($order_sn, $amount, ['userId' => $userId]);
- } catch (\Throwable $e) {
- Util::WriteLog('StarPay_error', 'payin request exception: ' . $e->getMessage());
- $this->error = 'Payment processing error';
- return false;
- }
- if (!isset($data['code']) || $data['code'] !== 0) {
- $this->error = $data['msg'] ?? 'Payment request failed';
- return false;
- }
- $this->result = $data;
- return $data;
- }
- /**
- * 代收回调:仅代收存在“实际金额与订单金额不一致”,以 realityAmount 入账
- *
- * @param array<string,mixed> $post
- */
- public function notify($post)
- {
- $config = (new PayConfig())->getConfig('StarPay') ?? [];
- $starPayService = new StarPayService($config);
- $r = $starPayService->notify($post);
- if ($r->status === Payment::STATUS_UNKNOWN) {
- Util::WriteLog('StarPay', "unknown status {$r->orderSn}");
- return $starPayService->notifySuccess();
- }
- if ($r->status === Payment::STATUS_IN_PROGRESS) {
- return $starPayService->notifySuccess();
- }
- if ($r->status == Payment::STATUS_REFUND) {
- Log::error('订单退款' . $r->orderSn);
- return $starPayService->notifySuccess();
- }
- try {
- $order = DB::connection('write')->table('agent.dbo.order')
- ->where('order_sn', $r->orderSn)
- ->first();
- if (!$order) {
- Util::WriteLog('StarPay', 'payin order not found: ' . $r->orderSn);
- return 'success';
- }
- if (!empty($order->pay_at) || !empty($order->finished_at)) {
- if ($order->payment_sn != $r->orderNo) {
- $logic = new OrderLogic();
- $amount = 100;
- $order_sn = $r->orderSn.'#'.time();
- $logic->orderCreate($order_sn, $amount, 'StarPay', $order->user_id);
- $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)
- ->first();
- } else {
- return $starPayService->notifySuccess();
- }
- }
- if ($r->status == Payment::STATUS_FAIL) {
- $body = ['pay_status' => 2, 'updated_at' => date('Y-m-d H:i:s')];
- DB::connection('write')->table('agent.dbo.order')->where('order_sn', $r->orderSn)
- ->update($body);
- Util::WriteLog('StarPay', 'payin notify status not success: ' . $r->orderSn);
- return $starPayService->notifySuccess();
- }
- $GiftsID = $order->GiftsID ?: '';
- $userID = $order->user_id ?: '';
- $AdId = $order->AdId ?: '';
- $eventType = $order->eventType ?: '';
- $realityAmount = isset($post['paidAmount']) ? (float)$post['paidAmount'] : (float)($post['amount'] ?? 0);
- if ($realityAmount <= 0) {
- Util::WriteLog('StarPay_error', 'payin notify invalid realityAmount: ' . json_encode($post));
- return $starPayService->notifyFail();
- }
- $payAmt = round($realityAmount, 2);
- $amountInScore = (int) round($payAmt * NumConfig::NUM_VALUE);
- $paymentFee = $post['tradeCharge'] ?? 0;
- $body = [
- 'payment_sn' => $r->orderNo,
- 'pay_status' => 1,
- 'pay_at' => date('Y-m-d H:i:s'),
- 'finished_at' => date('Y-m-d H:i:s'),
- 'amount' => $amountInScore,
- 'updated_at' => date('Y-m-d H:i:s'),
- 'payment_fee' => $paymentFee * NumConfig::NUM_VALUE
- ];
- $service = new OrderServices();
- if ((int)$order->amount != $amountInScore) {
- $body['GiftsID'] = 0;
- $body['amount'] = $amountInScore;
- $Recharge = $payAmt;
- $give = 0;
- $favorable_price = $Recharge + $give;
- $czReason = 1;
- $cjReason = 45;
- } else {
- [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $service->getPayInfo($GiftsID, $userID, $payAmt);
- }
- [$Score] = $service->addRecord($userID, $payAmt, $favorable_price, $r->orderSn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType, $body['payment_fee'] ?? 0);
- Order::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID, $r->orderSn]);
- DB::connection('write')->table('agent.dbo.order')->where('order_sn', $r->orderSn)->update($body);
- Util::WriteLog('StarPay', 'payin success, order_sn=' . $r->orderSn . ', realityAmount=' . $realityAmount);
- return $starPayService->notifySuccess();
- } catch (\Throwable $exception) {
- Util::WriteLog('StarPay_error', $exception->getMessage() . "\n" . $exception->getTraceAsString());
- throw $exception;
- }
- }
- }
|