Skip to content

Commit

Permalink
Adds fallback when processing DID_CHANGE_RENEWAL_STATUS notification …
Browse files Browse the repository at this point in the history
…type

In case of missing payment, new payment is created.

remp/heldpesk#1130
  • Loading branch information
miroc committed Mar 4, 2022
1 parent f6063dc commit 68fe25c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/Hermes/MissingPaymentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Crm\AppleAppstoreModule\Hermes;

class MissingPaymentException extends \Exception
{
}
10 changes: 8 additions & 2 deletions src/Hermes/ServerToServerNotificationWebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,13 @@ private function process($parsedNotification): bool
return $this->changeSubscriptionTypeOfNextPayment($latestReceiptInfo);

case ServerToServerNotification::NOTIFICATION_TYPE_DID_CHANGE_RENEWAL_STATUS:
return $this->changeRenewalStatus($stsNotification, $latestReceiptInfo);
try {
return $this->changeRenewalStatus($stsNotification, $latestReceiptInfo);
} catch (MissingPaymentException $exception) {
Debugger::log($exception->getMessage(), Debugger::ERROR);
// if payment is missing, fallback is to create payment again
return $this->createPayment($latestReceiptInfo);
}

case ServerToServerNotification::NOTIFICATION_TYPE_DID_FAIL_TO_RENEW:
$this->handleFailedRenewal($stsNotification, $latestReceiptInfo);
Expand Down Expand Up @@ -457,7 +463,7 @@ private function changeRenewalStatus(
$originalTransactionID
);
if (empty($paymentMetas)) {
throw new \Exception("Unable to find (recurrent or non-recurrent) payment with `original_transaction_id` [{$originalTransactionID}]. Unable to change renewal status.");
throw new MissingPaymentException("Unable to find (recurrent or non-recurrent) payment with `original_transaction_id` [{$originalTransactionID}]. Unable to change renewal status.");
}

$lastPayment = reset($paymentMetas)->payment;
Expand Down

0 comments on commit 68fe25c

Please sign in to comment.