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 $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; } } }