diff --git a/src/GooglePlay/AbstractResponse.php b/src/GooglePlay/AbstractResponse.php index a7e1c9c..ed16f8f 100644 --- a/src/GooglePlay/AbstractResponse.php +++ b/src/GooglePlay/AbstractResponse.php @@ -3,8 +3,7 @@ namespace ReceiptValidator\GooglePlay; /** - * Class AbstractResponse - * @package ReceiptValidator\GooglePlay + * Class AbstractResponse. */ abstract class AbstractResponse { @@ -12,6 +11,8 @@ abstract class AbstractResponse const CONSUMPTION_STATE_CONSUMED = 1; const PURCHASE_STATE_PURCHASED = 0; const PURCHASE_STATE_CANCELED = 1; + const ACKNOWLEDGEMENT_STATE_YET_TO_BE = 0; + const ACKNOWLEDGEMENT_STATE_DONE = 1; /** * @var \Google_Service_AndroidPublisher_ProductPurchase|\Google_Service_AndroidPublisher_SubscriptionPurchase @@ -19,7 +20,7 @@ abstract class AbstractResponse protected $response; /** - * Constructor + * Constructor. * * @param \Google_Service_AndroidPublisher_ProductPurchase|\Google_Service_AndroidPublisher_SubscriptionPurchase $response */ @@ -36,6 +37,22 @@ public function getDeveloperPayload() return $this->response->getDeveloperPayload(); } + /** + * @return int + */ + public function getAcknowledgementState() + { + return $this->response->acknowledgementState; + } + + /** + * @return bool + */ + public function isAcknowledged() + { + return $this->response->acknowledgementState === static::ACKNOWLEDGEMENT_STATE_DONE; + } + /** * @return string */ diff --git a/src/GooglePlay/Acknowledger.php b/src/GooglePlay/Acknowledger.php new file mode 100644 index 0000000..44fb667 --- /dev/null +++ b/src/GooglePlay/Acknowledger.php @@ -0,0 +1,95 @@ +androidPublisherService = $googleServiceAndroidPublisher; + $this->packageName = $packageName; + $this->purchaseToken = $purchaseToken; + $this->productId = $productId; + } + + /** + * @param string $type + * @param string $developerPayload + * + * @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] + ) + ); + break; + case self::PRODUCT: + $this->androidPublisherService->purchases_products->acknowledge( + $this->packageName, + $this->productId, + $this->purchaseToken, + new \Google_Service_AndroidPublisher_ProductPurchasesAcknowledgeRequest( + ['developerPayload' => $developerPayload] + ) + ); + break; + default: + throw new \RuntimeException( + \sprintf( + 'Invalid type provided : %s expected %s', + $type, + implode(',', [self::PRODUCT, self::SUBSCRIPTION]) + ) + ); + } + + return true; + } catch (\Exception $e) { + throw new \RuntimeException($e->getMessage(), $e); + } + } +} diff --git a/tests/GooglePlay/GooglePlayAcknowledgerTest.php b/tests/GooglePlay/GooglePlayAcknowledgerTest.php new file mode 100755 index 0000000..6a4eda7 --- /dev/null +++ b/tests/GooglePlay/GooglePlayAcknowledgerTest.php @@ -0,0 +1,55 @@ +getMockBuilder(Google_Service_AndroidPublisher::class) + ->disableOriginalConstructor()->getMock(); + $productPurchaseMock = $this->getMockBuilder(Google_Service_AndroidPublisher_Resource_PurchasesProducts::class) + ->disableOriginalConstructor()->getMock(); + $subscriptionPurchaseMock = $this->getMockBuilder(Google_Service_AndroidPublisher_Resource_PurchasesSubscriptions::class) + ->disableOriginalConstructor()->getMock(); + + // mock expectations + $googleServiceAndroidPublisherMock->purchases_products = $productPurchaseMock; + $googleServiceAndroidPublisherMock->purchases_subscriptions = $subscriptionPurchaseMock; + + $productPurchaseMock->expects($this->once())->method('acknowledge') + ->with( + $packageName, + $productId, + $purchaseToken, + new \Google_Service_AndroidPublisher_ProductPurchasesAcknowledgeRequest(['developerPayload' => 'bar']) + ); + + $subscriptionPurchaseMock->expects($this->once())->method('acknowledge') + ->with( + $packageName, + $productId, + $purchaseToken, + new \Google_Service_AndroidPublisher_SubscriptionPurchasesAcknowledgeRequest(['developerPayload' => 'foo']) + ); + + $googlePlayAcknowledger = new Acknowledger($googleServiceAndroidPublisherMock, $packageName, $productId, $purchaseToken); + + $googlePlayAcknowledger->acknowledge(Acknowledger::SUBSCRIPTION, 'foo'); + $googlePlayAcknowledger->acknowledge(Acknowledger::PRODUCT, 'bar'); + } +}