|
|
|
|
@ -21,13 +21,15 @@ class PaytaroPaymentService
|
|
|
|
|
$amount = $this->validateAmount($rawAmount, $settings);
|
|
|
|
|
$orderNo = $this->orderNo();
|
|
|
|
|
$now = time();
|
|
|
|
|
$quote = PaymentCheckoutQuotes::prepare('paytaro', $amount, $now,
|
|
|
|
|
$now + (int) $settings['paytaro_quote_minutes'] * 60);
|
|
|
|
|
if (PaytaroOrder::where('user_id', (int) $user->id)->where('created_at', '>=', $now - 60)->count() >= 5) {
|
|
|
|
|
throw new \RuntimeException('Paytaro 下单过于频繁,请稍后重试');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$connection = (new PaytaroOrder())->getConnection();
|
|
|
|
|
$connection->transaction(function () use (
|
|
|
|
|
$user, $amount, $orderNo, $now, $businessContext
|
|
|
|
|
$user, $amount, $orderNo, $now, $businessContext, $quote
|
|
|
|
|
) {
|
|
|
|
|
$lockedUser = User::where('id', (int) $user->id)->lockForUpdate()->first();
|
|
|
|
|
if ($lockedUser === null) {
|
|
|
|
|
@ -53,6 +55,7 @@ class PaytaroPaymentService
|
|
|
|
|
$order->created_at = $now;
|
|
|
|
|
$order->updated_at = $now;
|
|
|
|
|
$order->save();
|
|
|
|
|
PaymentCheckoutQuotes::persist($quote, $orderNo, (int) $user->id);
|
|
|
|
|
|
|
|
|
|
CloudInvoicePaymentService::recordAttempt(
|
|
|
|
|
$businessContext, $orderNo, $amount, (int) $user->id, $now
|
|
|
|
|
@ -67,7 +70,7 @@ class PaytaroPaymentService
|
|
|
|
|
'notify_url' => $baseUrl . '/paytaro/callback',
|
|
|
|
|
'return_url' => rtrim((string) $returnBaseUrl, '/') . '/paytaro/return',
|
|
|
|
|
'name' => 'Account balance recharge',
|
|
|
|
|
'money' => $amount,
|
|
|
|
|
'money' => PaymentCheckoutQuotes::formatUnits('CNY', $quote->expected_units),
|
|
|
|
|
];
|
|
|
|
|
try {
|
|
|
|
|
$gateway = $this->gatewayClient($settings)->createOrder($payload);
|
|
|
|
|
@ -113,17 +116,17 @@ class PaytaroPaymentService
|
|
|
|
|
$now = time();
|
|
|
|
|
$credited = false;
|
|
|
|
|
$duplicate = false;
|
|
|
|
|
$review = false;
|
|
|
|
|
$asset = isset($payload['pid']) ? strtoupper(trim((string) ($payload['currency'] ?? 'CNY'))) : 'MGATE';
|
|
|
|
|
|
|
|
|
|
$connection = (new PaytaroOrder())->getConnection();
|
|
|
|
|
$connection->transaction(function () use ($notification, $orderNo, $tradeNo, $eventKey, $now, &$credited, &$duplicate) {
|
|
|
|
|
$connection->transaction(function () use (
|
|
|
|
|
$notification, $orderNo, $tradeNo, $eventKey, $now, $asset, &$credited, &$duplicate, &$review
|
|
|
|
|
) {
|
|
|
|
|
$order = PaytaroOrder::where('order_no', $orderNo)->lockForUpdate()->first();
|
|
|
|
|
if ($order === null) {
|
|
|
|
|
throw new \RuntimeException('Paytaro 本地订单不存在');
|
|
|
|
|
}
|
|
|
|
|
if ($notification['amount_cents'] !== null
|
|
|
|
|
&& PaytaroProtocol::amountToCents($order->amount) !== (string) $notification['amount_cents']) {
|
|
|
|
|
throw new \RuntimeException('Paytaro 回调金额不匹配');
|
|
|
|
|
}
|
|
|
|
|
if ((string) $order->gateway_trade_no !== '' && (string) $order->gateway_trade_no !== $tradeNo) {
|
|
|
|
|
throw new \RuntimeException('Paytaro 平台订单号不匹配');
|
|
|
|
|
}
|
|
|
|
|
@ -133,13 +136,19 @@ class PaytaroPaymentService
|
|
|
|
|
|
|
|
|
|
$existing = PaytaroCallbackEvent::where('event_key', $eventKey)->first();
|
|
|
|
|
if ($existing !== null) {
|
|
|
|
|
if ((string) $order->status !== 'paid' || (int) $existing->processed_at <= 0) {
|
|
|
|
|
if ((int) $existing->processed_at <= 0) {
|
|
|
|
|
throw new \RuntimeException('Paytaro 重复回调事件状态异常');
|
|
|
|
|
}
|
|
|
|
|
$review = PaymentCheckoutQuotes::find('paytaro', $orderNo)?->status === 'review';
|
|
|
|
|
$duplicate = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ((string) $order->status === 'paid') {
|
|
|
|
|
$received = PaymentCheckoutQuotes::find('paytaro', $orderNo)?->received_units
|
|
|
|
|
?? (int) PaytaroProtocol::amountToCents($order->amount);
|
|
|
|
|
if ($received !== (int) $notification['amount_cents']) {
|
|
|
|
|
throw new \RuntimeException('Paytaro 已支付金额与回调不一致');
|
|
|
|
|
}
|
|
|
|
|
$duplicate = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
@ -162,11 +171,28 @@ class PaytaroPaymentService
|
|
|
|
|
$event->processed_at = 0;
|
|
|
|
|
$event->save();
|
|
|
|
|
|
|
|
|
|
$quote = PaymentCheckoutQuotes::receive('paytaro', $orderNo, (int) $order->user_id,
|
|
|
|
|
(string) $order->amount, (int) PaytaroProtocol::amountToCents($order->amount),
|
|
|
|
|
(int) $notification['amount_cents'], $tradeNo, $now, null, $asset);
|
|
|
|
|
if ($quote->status === 'review') {
|
|
|
|
|
$review = true;
|
|
|
|
|
$order->gateway_trade_no = $tradeNo;
|
|
|
|
|
$order->status = 'pending';
|
|
|
|
|
$order->payment_value = null;
|
|
|
|
|
$order->error_message = '收款凭证已保存,需人工核对';
|
|
|
|
|
$order->updated_at = $now;
|
|
|
|
|
$order->save();
|
|
|
|
|
$event->processed_at = $now;
|
|
|
|
|
$event->save();
|
|
|
|
|
CloudInvoicePaymentService::recordProviderReview('paytaro', $orderNo, $now);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$paylist = Paylist::where('id', (int) $order->paylist_id)->lockForUpdate()->first();
|
|
|
|
|
if ($paylist === null || (string) $paylist->tradeno !== $orderNo || (int) $paylist->userid !== (int) $order->user_id) {
|
|
|
|
|
throw new \RuntimeException('Paytaro 关联充值订单无效');
|
|
|
|
|
}
|
|
|
|
|
$credited = RechargeCreditService::creditLocked($paylist, self::LEDGER_TYPE, true);
|
|
|
|
|
$credited = RechargeCreditService::creditLocked($paylist, self::LEDGER_TYPE, true, $quote);
|
|
|
|
|
if (CloudInvoicePayment::where('provider', 'paytaro')
|
|
|
|
|
->where('provider_order_no', $orderNo)->exists()) {
|
|
|
|
|
CloudInvoicePaymentService::settleForProvider('paytaro', $orderNo);
|
|
|
|
|
@ -182,7 +208,7 @@ class PaytaroPaymentService
|
|
|
|
|
$event->save();
|
|
|
|
|
}, 3);
|
|
|
|
|
|
|
|
|
|
return ['credited' => $credited, 'duplicate' => $duplicate, 'order_no' => $orderNo];
|
|
|
|
|
return ['credited' => $credited, 'duplicate' => $duplicate, 'review' => $review, 'order_no' => $orderNo];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function returnOrderNo(array $payload)
|
|
|
|
|
@ -208,6 +234,10 @@ class PaytaroPaymentService
|
|
|
|
|
$connection = (new PaytaroOrder())->getConnection();
|
|
|
|
|
$connection->transaction(function () use ($orderNo, $tradeNo, $kind, $value, &$order) {
|
|
|
|
|
$order = PaytaroOrder::where('order_no', $orderNo)->lockForUpdate()->first();
|
|
|
|
|
if ($order !== null && (string) $order->gateway_trade_no === $tradeNo
|
|
|
|
|
&& PaymentCheckoutQuotes::find('paytaro', $orderNo)?->receipt_ref !== null) {
|
|
|
|
|
return; // The signed receipt arrived before the create response.
|
|
|
|
|
}
|
|
|
|
|
if ($order === null || (string) $order->status !== 'creating') {
|
|
|
|
|
throw new \RuntimeException('Paytaro 本地订单状态无效');
|
|
|
|
|
}
|
|
|
|
|
|