|
@@ -108,8 +108,8 @@ class GuestDeclarationOrderService
|
|
|
if ($draft->STATUS !== GuestDeclarationDraftStatus::PENDING_PAYMENT || $draft->isExpired()) {
|
|
if ($draft->STATUS !== GuestDeclarationDraftStatus::PENDING_PAYMENT || $draft->isExpired()) {
|
|
|
throw new BadRequestHttpException('Only an active pending-payment draft can be debug-completed.');
|
|
throw new BadRequestHttpException('Only an active pending-payment draft can be debug-completed.');
|
|
|
}
|
|
}
|
|
|
- if (!$draft->PAYMENT_REFERENCE || (float) $draft->PAY_AMOUNT <= 0 || !$draft->CURRENCY) {
|
|
|
|
|
- throw new BadRequestHttpException('The guest draft has no valid initialized payment.');
|
|
|
|
|
|
|
+ if ((float) $draft->PAY_AMOUNT <= 0 || !$draft->CURRENCY) {
|
|
|
|
|
+ throw new BadRequestHttpException('The guest draft has no valid payment amount.');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$now = gmdate('Y-m-d\\TH:i:s\\Z');
|
|
$now = gmdate('Y-m-d\\TH:i:s\\Z');
|
|
@@ -117,7 +117,7 @@ class GuestDeclarationOrderService
|
|
|
'id' => 'DEBUG-' . $draft->ID,
|
|
'id' => 'DEBUG-' . $draft->ID,
|
|
|
'domain' => 'debug',
|
|
'domain' => 'debug',
|
|
|
'status' => 'success',
|
|
'status' => 'success',
|
|
|
- 'reference' => $draft->PAYMENT_REFERENCE,
|
|
|
|
|
|
|
+ 'reference' => 'DEBUG-' . $draft->ORDER_SN,
|
|
|
'amount' => (int) round((float) $draft->PAY_AMOUNT * 100),
|
|
'amount' => (int) round((float) $draft->PAY_AMOUNT * 100),
|
|
|
'currency' => strtoupper((string) $draft->CURRENCY),
|
|
'currency' => strtoupper((string) $draft->CURRENCY),
|
|
|
'metadata' => ['custom_fields' => [
|
|
'metadata' => ['custom_fields' => [
|
|
@@ -141,9 +141,7 @@ class GuestDeclarationOrderService
|
|
|
throw new BadRequestHttpException('Guest order draft has expired.');
|
|
throw new BadRequestHttpException('Guest order draft has expired.');
|
|
|
}
|
|
}
|
|
|
if ($draft->STATUS === GuestDeclarationDraftStatus::PAYMENT_INITIALIZING) {
|
|
if ($draft->STATUS === GuestDeclarationDraftStatus::PAYMENT_INITIALIZING) {
|
|
|
- $recoveredStatus = $draft->PAYMENT_REFERENCE
|
|
|
|
|
- ? GuestDeclarationDraftStatus::PENDING_PAYMENT
|
|
|
|
|
- : GuestDeclarationDraftStatus::DRAFT;
|
|
|
|
|
|
|
+ $recoveredStatus = GuestDeclarationDraftStatus::DRAFT;
|
|
|
GuestDeclarationDraft::updateAll(
|
|
GuestDeclarationDraft::updateAll(
|
|
|
['STATUS' => $recoveredStatus, 'UPDATED_AT' => time()],
|
|
['STATUS' => $recoveredStatus, 'UPDATED_AT' => time()],
|
|
|
[
|
|
[
|
|
@@ -279,34 +277,12 @@ class GuestDeclarationOrderService
|
|
|
throw new BadRequestHttpException('Guest order draft is already being submitted.');
|
|
throw new BadRequestHttpException('Guest order draft is already being submitted.');
|
|
|
}
|
|
}
|
|
|
$draft->refresh();
|
|
$draft->refresh();
|
|
|
- $paymentReference = 'GD' . strtoupper(bin2hex(random_bytes(12)));
|
|
|
|
|
- $init = \common\helpers\PayStack::transactionInit(
|
|
|
|
|
- strtoupper($currency['CODE']),
|
|
|
|
|
- $amount,
|
|
|
|
|
- $input['email'],
|
|
|
|
|
- ['custom_fields' => [
|
|
|
|
|
- ['display_name' => 'Order SN', 'variable_name' => 'orderSn', 'value' => $orderSn],
|
|
|
|
|
- ['display_name' => 'Order Type', 'variable_name' => 'orderType', 'value' => 'guestUserDec'],
|
|
|
|
|
- ]],
|
|
|
|
|
- $paymentReference
|
|
|
|
|
- );
|
|
|
|
|
- if (empty($init['status']) || empty($init['data']['reference']) || empty($init['data']['authorization_url'])) {
|
|
|
|
|
- GuestDeclarationDraft::updateAll(
|
|
|
|
|
- ['STATUS' => $previousStatus, 'UPDATED_AT' => time()],
|
|
|
|
|
- ['AND', ['ID' => $draft->ID], ['STATUS' => GuestDeclarationDraftStatus::PAYMENT_INITIALIZING]]
|
|
|
|
|
- );
|
|
|
|
|
- throw new \RuntimeException('Unable to initialize payment. Please retry.');
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
$draft->PAYLOAD = $storedPayload;
|
|
$draft->PAYLOAD = $storedPayload;
|
|
|
$draft->ORDER_SNAPSHOT = json_encode(['amount' => $amount, 'pv' => $pv, 'goods' => $goods], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
$draft->ORDER_SNAPSHOT = json_encode(['amount' => $amount, 'pv' => $pv, 'goods' => $goods], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
|
$draft->PAY_AMOUNT = $amount;
|
|
$draft->PAY_AMOUNT = $amount;
|
|
|
$draft->CURRENCY = strtoupper($currency['CODE']);
|
|
$draft->CURRENCY = strtoupper($currency['CODE']);
|
|
|
- $draft->PAYMENT_REFERENCE = $init['data']['reference'];
|
|
|
|
|
- $draft->PAYMENT_NOTE = json_encode([
|
|
|
|
|
- 'authorization_url' => $init['data']['authorization_url'],
|
|
|
|
|
- 'access_code' => $init['data']['access_code'] ?? null,
|
|
|
|
|
- ]);
|
|
|
|
|
|
|
+ $draft->PAYMENT_REFERENCE = null;
|
|
|
|
|
+ $draft->PAYMENT_NOTE = null;
|
|
|
$draft->STATUS = GuestDeclarationDraftStatus::PENDING_PAYMENT;
|
|
$draft->STATUS = GuestDeclarationDraftStatus::PENDING_PAYMENT;
|
|
|
$draft->UPDATED_AT = time();
|
|
$draft->UPDATED_AT = time();
|
|
|
if (!$draft->save(false)) {
|
|
if (!$draft->save(false)) {
|
|
@@ -316,8 +292,6 @@ class GuestDeclarationOrderService
|
|
|
'ORDER_SNAPSHOT' => $draft->ORDER_SNAPSHOT,
|
|
'ORDER_SNAPSHOT' => $draft->ORDER_SNAPSHOT,
|
|
|
'PAY_AMOUNT' => $draft->PAY_AMOUNT,
|
|
'PAY_AMOUNT' => $draft->PAY_AMOUNT,
|
|
|
'CURRENCY' => $draft->CURRENCY,
|
|
'CURRENCY' => $draft->CURRENCY,
|
|
|
- 'PAYMENT_REFERENCE' => $draft->PAYMENT_REFERENCE,
|
|
|
|
|
- 'PAYMENT_NOTE' => $draft->PAYMENT_NOTE,
|
|
|
|
|
'UPDATED_AT' => time(),
|
|
'UPDATED_AT' => time(),
|
|
|
], ['ID' => $draft->ID]);
|
|
], ['ID' => $draft->ID]);
|
|
|
throw new \RuntimeException('Payment started but its draft could not be saved; manual review is required.');
|
|
throw new \RuntimeException('Payment started but its draft could not be saved; manual review is required.');
|
|
@@ -327,14 +301,10 @@ class GuestDeclarationOrderService
|
|
|
|
|
|
|
|
private function paymentResponse(GuestDeclarationDraft $draft): array
|
|
private function paymentResponse(GuestDeclarationDraft $draft): array
|
|
|
{
|
|
{
|
|
|
- $payment = json_decode((string) $draft->PAYMENT_NOTE, true) ?: [];
|
|
|
|
|
return [
|
|
return [
|
|
|
'orderSn' => $draft->ORDER_SN,
|
|
'orderSn' => $draft->ORDER_SN,
|
|
|
'amount' => (float) $draft->PAY_AMOUNT,
|
|
'amount' => (float) $draft->PAY_AMOUNT,
|
|
|
'currency' => $draft->CURRENCY,
|
|
'currency' => $draft->CURRENCY,
|
|
|
- 'reference' => $draft->PAYMENT_REFERENCE,
|
|
|
|
|
- 'authorizationUrl' => $payment['authorization_url'] ?? null,
|
|
|
|
|
- 'accessCode' => $payment['access_code'] ?? null,
|
|
|
|
|
];
|
|
];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -414,11 +384,10 @@ class GuestDeclarationOrderService
|
|
|
$expectedAmount = (int) round(((float) $draft->PAY_AMOUNT) * 100);
|
|
$expectedAmount = (int) round(((float) $draft->PAY_AMOUNT) * 100);
|
|
|
if ($draft->STATUS !== GuestDeclarationDraftStatus::PENDING_PAYMENT
|
|
if ($draft->STATUS !== GuestDeclarationDraftStatus::PENDING_PAYMENT
|
|
|
|| ($verifiedTransaction['status'] ?? null) !== 'success'
|
|
|| ($verifiedTransaction['status'] ?? null) !== 'success'
|
|
|
- || (string) ($verifiedTransaction['reference'] ?? '') !== (string) $draft->PAYMENT_REFERENCE
|
|
|
|
|
|| (int) ($verifiedTransaction['amount'] ?? -1) !== $expectedAmount
|
|
|| (int) ($verifiedTransaction['amount'] ?? -1) !== $expectedAmount
|
|
|
|| strtoupper((string) ($verifiedTransaction['currency'] ?? '')) !== strtoupper((string) $draft->CURRENCY)
|
|
|| strtoupper((string) ($verifiedTransaction['currency'] ?? '')) !== strtoupper((string) $draft->CURRENCY)
|
|
|
) {
|
|
) {
|
|
|
- if (($verifiedTransaction['reference'] ?? null) === $draft->PAYMENT_REFERENCE || $metadataMatches) {
|
|
|
|
|
|
|
+ if ($metadataMatches) {
|
|
|
GuestDeclarationDraft::updateAll(
|
|
GuestDeclarationDraft::updateAll(
|
|
|
['STATUS' => GuestDeclarationDraftStatus::MANUAL_REVIEW, 'UPDATED_AT' => time()],
|
|
['STATUS' => GuestDeclarationDraftStatus::MANUAL_REVIEW, 'UPDATED_AT' => time()],
|
|
|
['AND', ['ID' => $draft->ID], ['STATUS' => GuestDeclarationDraftStatus::PENDING_PAYMENT]]
|
|
['AND', ['ID' => $draft->ID], ['STATUS' => GuestDeclarationDraftStatus::PENDING_PAYMENT]]
|
|
@@ -522,18 +491,21 @@ class GuestDeclarationOrderService
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function markPaymentFailed(string $reference, array $verifiedTransaction): void
|
|
|
|
|
|
|
+ public function markPaymentFailed(string $orderSn, array $verifiedTransaction): void
|
|
|
{
|
|
{
|
|
|
- $draft = GuestDeclarationDraft::findOne(['PAYMENT_REFERENCE' => $reference]);
|
|
|
|
|
|
|
+ $draft = GuestDeclarationDraft::findOne(['ORDER_SN' => $orderSn]);
|
|
|
|
|
+ $fields = $verifiedTransaction['metadata']['custom_fields'] ?? [];
|
|
|
|
|
+ $metadataMatches = ($fields[0]['value'] ?? null) === $orderSn
|
|
|
|
|
+ && ($fields[1]['value'] ?? null) === 'guestUserDec';
|
|
|
if (!$draft || $draft->STATUS !== GuestDeclarationDraftStatus::PENDING_PAYMENT
|
|
if (!$draft || $draft->STATUS !== GuestDeclarationDraftStatus::PENDING_PAYMENT
|
|
|
|| ($verifiedTransaction['status'] ?? null) === 'success'
|
|
|| ($verifiedTransaction['status'] ?? null) === 'success'
|
|
|
- || (string) ($verifiedTransaction['reference'] ?? '') !== $reference
|
|
|
|
|
|
|
+ || !$metadataMatches
|
|
|
) {
|
|
) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
GuestDeclarationDraft::updateAll(
|
|
GuestDeclarationDraft::updateAll(
|
|
|
['STATUS' => GuestDeclarationDraftStatus::PAYMENT_FAILED, 'PAYMENT_REFERENCE' => null, 'PAYMENT_NOTE' => null, 'UPDATED_AT' => time()],
|
|
['STATUS' => GuestDeclarationDraftStatus::PAYMENT_FAILED, 'PAYMENT_REFERENCE' => null, 'PAYMENT_NOTE' => null, 'UPDATED_AT' => time()],
|
|
|
- ['AND', ['ID' => $draft->ID], ['STATUS' => GuestDeclarationDraftStatus::PENDING_PAYMENT], ['PAYMENT_REFERENCE' => $reference]]
|
|
|
|
|
|
|
+ ['AND', ['ID' => $draft->ID], ['STATUS' => GuestDeclarationDraftStatus::PENDING_PAYMENT]]
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
|