From 09355cc012f6a248a2bbc6d6bfd773e48feb4b59 Mon Sep 17 00:00:00 2001 From: Tibor Mikoczy Date: Fri, 14 Jan 2022 13:08:35 +0100 Subject: [PATCH 1/2] Added handling of next subscription type for trial subscriptions --- src/api/VerifyPurchaseApiHandler.php | 2 +- src/models/LatestReceiptInfo.php | 8 ++++++++ .../AppleAppstoreSubscriptionTypesRepository.php | 8 ++++++-- .../ServerToServerNotificationProcessor.php | 2 +- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/api/VerifyPurchaseApiHandler.php b/src/api/VerifyPurchaseApiHandler.php index a36a2d6..55e8c0c 100644 --- a/src/api/VerifyPurchaseApiHandler.php +++ b/src/api/VerifyPurchaseApiHandler.php @@ -269,7 +269,7 @@ private function createPayment( ?string $articleID ): JsonResponse { $subscriptionType = $this->appleAppstoreSubscriptionTypesRepository - ->findSubscriptionTypeByAppleAppstoreProductId($latestReceipt->getProductId()); + ->findSubscriptionTypeByAppleAppstoreProductId($latestReceipt->getProductId(), !$latestReceipt->isTrialPeriod()); if (!$subscriptionType) { Debugger::log( "Unable to find SubscriptionType by product ID [{$latestReceipt->getProductId()}] from transaction [{$latestReceipt->getOriginalTransactionId()}].", diff --git a/src/models/LatestReceiptInfo.php b/src/models/LatestReceiptInfo.php index f7d655b..e181352 100644 --- a/src/models/LatestReceiptInfo.php +++ b/src/models/LatestReceiptInfo.php @@ -55,4 +55,12 @@ public function getTransactionId(): string { return $this->latestReceiptInfo->transaction_id; } + + public function isTrialPeriod(): bool + { + if (!isset($this->latestReceiptInfo->is_trial_period)) { + return false; + } + return filter_var($this->latestReceiptInfo->is_trial_period, FILTER_VALIDATE_BOOLEAN); + } } diff --git a/src/models/Repository/AppleAppstoreSubscriptionTypesRepository.php b/src/models/Repository/AppleAppstoreSubscriptionTypesRepository.php index 9177714..6bb882e 100644 --- a/src/models/Repository/AppleAppstoreSubscriptionTypesRepository.php +++ b/src/models/Repository/AppleAppstoreSubscriptionTypesRepository.php @@ -30,8 +30,12 @@ final public function update(IRow &$row, $data) /** * @param string $appleAppstoreProductId Identification of product in Apple App Store */ - final public function findSubscriptionTypeByAppleAppstoreProductId(string $appleAppstoreProductId): ?ActiveRow + final public function findSubscriptionTypeByAppleAppstoreProductId(string $appleAppstoreProductId, bool $followNextSubscriptionType = true): ?ActiveRow { - return ($this->findBy('product_id', $appleAppstoreProductId))->subscription_type ?? null; + $appStoreSubscriptionType = $this->findBy('product_id', $appleAppstoreProductId); + if (!$appStoreSubscriptionType instanceof ActiveRow) { + return null; + } + return $followNextSubscriptionType && $appStoreSubscriptionType->subscription_type->next_subscription_type_id !== null ? $appStoreSubscriptionType->subscription_type->next_subscription_type : $appStoreSubscriptionType->subscription_type; } } diff --git a/src/models/ServerToServerNotificationProcessor/ServerToServerNotificationProcessor.php b/src/models/ServerToServerNotificationProcessor/ServerToServerNotificationProcessor.php index 04a76c1..56cdeff 100644 --- a/src/models/ServerToServerNotificationProcessor/ServerToServerNotificationProcessor.php +++ b/src/models/ServerToServerNotificationProcessor/ServerToServerNotificationProcessor.php @@ -42,7 +42,7 @@ public function __construct( public function getSubscriptionType(LatestReceiptInfo $latestReceiptInfo): ActiveRow { $appleAppstoreProductId = $latestReceiptInfo->getProductId(); - $subscriptionType = $this->appleAppstoreSubscriptionTypesRepository->findSubscriptionTypeByAppleAppstoreProductId($appleAppstoreProductId); + $subscriptionType = $this->appleAppstoreSubscriptionTypesRepository->findSubscriptionTypeByAppleAppstoreProductId($appleAppstoreProductId, !$latestReceiptInfo->isTrialPeriod()); if (!$subscriptionType) { throw new \Exception("Unable to find SubscriptionType by product ID [{$appleAppstoreProductId}] provided by ServerToServerNotification."); } From b6dd5aaf6e821c68c1aa2036e8a1f7d5c30548cb Mon Sep 17 00:00:00 2001 From: Tibor Mikoczy Date: Wed, 19 Jan 2022 10:17:12 +0100 Subject: [PATCH 2/2] Changes --- .../AppleAppstoreSubscriptionTypesRepository.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/models/Repository/AppleAppstoreSubscriptionTypesRepository.php b/src/models/Repository/AppleAppstoreSubscriptionTypesRepository.php index 6bb882e..4bc6b0a 100644 --- a/src/models/Repository/AppleAppstoreSubscriptionTypesRepository.php +++ b/src/models/Repository/AppleAppstoreSubscriptionTypesRepository.php @@ -33,9 +33,12 @@ final public function update(IRow &$row, $data) final public function findSubscriptionTypeByAppleAppstoreProductId(string $appleAppstoreProductId, bool $followNextSubscriptionType = true): ?ActiveRow { $appStoreSubscriptionType = $this->findBy('product_id', $appleAppstoreProductId); - if (!$appStoreSubscriptionType instanceof ActiveRow) { + if (!$appStoreSubscriptionType) { return null; } - return $followNextSubscriptionType && $appStoreSubscriptionType->subscription_type->next_subscription_type_id !== null ? $appStoreSubscriptionType->subscription_type->next_subscription_type : $appStoreSubscriptionType->subscription_type; + if ($followNextSubscriptionType && $appStoreSubscriptionType->subscription_type->next_subscription_type_id !== null) { + return $appStoreSubscriptionType->subscription_type->next_subscription_type; + } + return $appStoreSubscriptionType->subscription_type; } }