Skip to content

Commit

Permalink
Merge pull request #110 from passions-app/master
Browse files Browse the repository at this point in the history
[ADD] fix error on acknowledgement of an already acknowledged purchase
  • Loading branch information
Stafox authored Aug 3, 2020
2 parents 4679611 + efe7bf0 commit b88a5fb
Show file tree
Hide file tree
Showing 2 changed files with 304 additions and 52 deletions.
116 changes: 90 additions & 26 deletions src/GooglePlay/Acknowledger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@

namespace ReceiptValidator\GooglePlay;

use Exception;
use Google_Service_AndroidPublisher;
use ReceiptValidator\RunTimeException;

/**
* Class Acknowledger.
*/
class Acknowledger
{
// Do acknowledge only in case if it have not done
const ACKNOWLEDGE_STRATEGY_IMPLICIT = 'strategy_implicit';
// Try to do acknowledge directly (exception will be returned in case when acknowledge already was done)
const ACKNOWLEDGE_STRATEGY_EXPLICIT = 'strategy_explicit';

const SUBSCRIPTION = 'SUBSCRIPTION';
const PRODUCT = 'PRODUCT';

/**
* @var \Google_Service_AndroidPublisher
* @var Google_Service_AndroidPublisher
*/
protected $androidPublisherService;
/**
Expand All @@ -26,60 +35,115 @@ class Acknowledger
* @var string
*/
protected $productId;
/**
* @var string
*/
protected $strategy;

/**
* Acknowledger constructor.
*
* @param \Google_Service_AndroidPublisher $googleServiceAndroidPublisher
* @param string $packageName
* @param string $purchaseToken
* @param string $productId
* @param Google_Service_AndroidPublisher $googleServiceAndroidPublisher
* @param string $packageName
* @param string $purchaseToken
* @param string $productId
* @param string $strategy
*
* @throws RunTimeException
*/
public function __construct(
\Google_Service_AndroidPublisher $googleServiceAndroidPublisher,
Google_Service_AndroidPublisher $googleServiceAndroidPublisher,
$packageName,
$productId,
$purchaseToken
$purchaseToken,
$strategy = self::ACKNOWLEDGE_STRATEGY_EXPLICIT
) {
if (!in_array($strategy, [self::ACKNOWLEDGE_STRATEGY_EXPLICIT, self::ACKNOWLEDGE_STRATEGY_IMPLICIT])) {
throw new RuntimeException(sprintf('Invalid strategy provided %s', $strategy));
}

$this->androidPublisherService = $googleServiceAndroidPublisher;
$this->packageName = $packageName;
$this->purchaseToken = $purchaseToken;
$this->productId = $productId;
$this->strategy = $strategy;
}

/**
* @param string $type
* @param string $developerPayload
*
* @throws RunTimeException
*
* @return bool
*/
public function acknowledge(string $type = self::SUBSCRIPTION, string $developerPayload = '')
{
try {
switch ($type) {
case self::SUBSCRIPTION:
$this->androidPublisherService->purchases_subscriptions->acknowledge(
$this->packageName,
$this->productId,
$this->purchaseToken,
new \Google_Service_AndroidPublisher_SubscriptionPurchasesAcknowledgeRequest(
['developerPayload' => $developerPayload]
)
);
if ($this->strategy === self::ACKNOWLEDGE_STRATEGY_EXPLICIT) {
// Here exception might be thrown as previously, so no BC break here
$this->androidPublisherService->purchases_subscriptions->acknowledge(
$this->packageName,
$this->productId,
$this->purchaseToken,
new \Google_Service_AndroidPublisher_SubscriptionPurchasesAcknowledgeRequest(
['developerPayload' => $developerPayload]
)
);
} elseif ($this->strategy === self::ACKNOWLEDGE_STRATEGY_IMPLICIT) {
$subscriptionPurchase = $this->androidPublisherService->purchases_subscriptions->get(
$this->packageName,
$this->productId,
$this->purchaseToken
);

if ($subscriptionPurchase->getAcknowledgementState() !== AbstractResponse::ACKNOWLEDGEMENT_STATE_DONE) {
$this->androidPublisherService->purchases_subscriptions->acknowledge(
$this->packageName,
$this->productId,
$this->purchaseToken,
new \Google_Service_AndroidPublisher_SubscriptionPurchasesAcknowledgeRequest(
['developerPayload' => $developerPayload]
)
);
}
}
break;
case self::PRODUCT:
$this->androidPublisherService->purchases_products->acknowledge(
$this->packageName,
$this->productId,
$this->purchaseToken,
new \Google_Service_AndroidPublisher_ProductPurchasesAcknowledgeRequest(
['developerPayload' => $developerPayload]
)
);
if ($this->strategy === self::ACKNOWLEDGE_STRATEGY_EXPLICIT) {
// Here exception might be thrown as previously, so no BC break here
$this->androidPublisherService->purchases_products->acknowledge(
$this->packageName,
$this->productId,
$this->purchaseToken,
new \Google_Service_AndroidPublisher_ProductPurchasesAcknowledgeRequest(
['developerPayload' => $developerPayload]
)
);
} elseif ($this->strategy === self::ACKNOWLEDGE_STRATEGY_IMPLICIT) {
$productPurchase = $this->androidPublisherService->purchases_products->get(
$this->packageName,
$this->productId,
$this->purchaseToken
);

if ($productPurchase->getAcknowledgementState() !== AbstractResponse::ACKNOWLEDGEMENT_STATE_DONE) {
$this->androidPublisherService->purchases_products->acknowledge(
$this->packageName,
$this->productId,
$this->purchaseToken,
new \Google_Service_AndroidPublisher_ProductPurchasesAcknowledgeRequest(
['developerPayload' => $developerPayload]
)
);
}
}
break;
default:
throw new \RuntimeException(
\sprintf(
sprintf(
'Invalid type provided : %s expected %s',
$type,
implode(',', [self::PRODUCT, self::SUBSCRIPTION])
Expand All @@ -88,8 +152,8 @@ public function acknowledge(string $type = self::SUBSCRIPTION, string $developer
}

return true;
} catch (\Exception $e) {
throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
} catch (Exception $e) {
throw new \RuntimeException($e->getCode(), $e->getCode(), $e);
}
}
}
Loading

0 comments on commit b88a5fb

Please sign in to comment.