Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling of next subscription type for trial periods #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/VerifyPurchaseApiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()}].",
Expand Down
8 changes: 8 additions & 0 deletions src/models/LatestReceiptInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ 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) {
return null;
}
if ($followNextSubscriptionType && $appStoreSubscriptionType->subscription_type->next_subscription_type_id !== null) {
return $appStoreSubscriptionType->subscription_type->next_subscription_type;
}
return $appStoreSubscriptionType->subscription_type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down