Skip to content

Commit

Permalink
Fix scenario with late S2S notification inconsistency
Browse files Browse the repository at this point in the history
The late notification would cause system to create a payment for
already expired subscription. The created subscription would have
invalid start/end times and we wouldn't even be able to generate
revenue for such payment.

remp/crm#2679
  • Loading branch information
rootpd committed Dec 12, 2022
1 parent 6913ea1 commit d532090
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/Hermes/ServerToServerNotificationWebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,27 @@ private function process($parsedNotification): bool
* @throws \Exception Thrown when quantity is different than '1'. Only one subscription per purchase is allowed.
* @throws DoNotRetryException Thrown by ServerToServerNotificationProcessor when processing failed and it shouldn't be retried.
*/
private function createPayment(LatestReceiptInfo $latestReceiptInfo): ActiveRow
private function createPayment(LatestReceiptInfo $latestReceiptInfo): ?ActiveRow
{
// only one subscription per purchase
if ($latestReceiptInfo->getQuantity() !== 1) {
throw new \Exception("Unable to handle `quantity` different than 1 for notification with OriginalTransactionId " .
"[{$latestReceiptInfo->getOriginalTransactionId()}]");
}

$subscriptionStartDate = $this->serverToServerNotificationProcessor->getSubscriptionStartAt($latestReceiptInfo);
$subscriptionEndDate = $this->serverToServerNotificationProcessor->getSubscriptionEndAt($latestReceiptInfo);
if ($subscriptionEndDate < new \DateTime()) {
return null;
}

$metas = [
AppleAppstoreModule::META_KEY_ORIGINAL_TRANSACTION_ID => $latestReceiptInfo->getOriginalTransactionId(),
AppleAppstoreModule::META_KEY_PRODUCT_ID => $latestReceiptInfo->getProductId(),
AppleAppstoreModule::META_KEY_TRANSACTION_ID => $latestReceiptInfo->getTransactionId(),
];

$subscriptionType = $this->serverToServerNotificationProcessor->getSubscriptionType($latestReceiptInfo);
$recurrentCharge = false;
$paymentItemContainer = (new PaymentItemContainer())
->addItems(SubscriptionTypePaymentItem::fromSubscriptionType($subscriptionType));

Expand All @@ -215,21 +220,15 @@ private function createPayment(LatestReceiptInfo $latestReceiptInfo): ActiveRow
}

$payment = $this->paymentsRepository->add(
$subscriptionType,
$paymentGateway,
$this->serverToServerNotificationProcessor->getUser($latestReceiptInfo),
$paymentItemContainer,
'',
$subscriptionType->price,
$this->serverToServerNotificationProcessor->getSubscriptionStartAt($latestReceiptInfo),
$this->serverToServerNotificationProcessor->getSubscriptionEndAt($latestReceiptInfo),
null,
0,
null,
null,
null,
$recurrentCharge,
$metas
subscriptionType: $subscriptionType,
paymentGateway: $paymentGateway,
user: $this->serverToServerNotificationProcessor->getUser($latestReceiptInfo),
paymentItemContainer: $paymentItemContainer,
amount: $subscriptionType->price,
subscriptionStartAt: $subscriptionStartDate,
subscriptionEndAt: $subscriptionEndDate,
recurrentCharge: false,
metaData: $metas,
);

$payment = $this->paymentsRepository->updateStatus($payment, PaymentsRepository::STATUS_PREPAID);
Expand Down

0 comments on commit d532090

Please sign in to comment.