-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from passions-app/master
[ADD] acknowledger for the new google play billing api version
- Loading branch information
Showing
3 changed files
with
170 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace ReceiptValidator\GooglePlay; | ||
|
||
/** | ||
* Class Acknowledger. | ||
*/ | ||
class Acknowledger | ||
{ | ||
const SUBSCRIPTION = 'SUBSCRIPTION'; | ||
const PRODUCT = 'PRODUCT'; | ||
|
||
/** | ||
* @var \Google_Service_AndroidPublisher | ||
*/ | ||
protected $androidPublisherService; | ||
/** | ||
* @var string | ||
*/ | ||
protected $packageName; | ||
/** | ||
* @var string | ||
*/ | ||
protected $purchaseToken; | ||
/** | ||
* @var string | ||
*/ | ||
protected $productId; | ||
|
||
/** | ||
* Acknowledger constructor. | ||
* | ||
* @param \Google_Service_AndroidPublisher $googleServiceAndroidPublisher | ||
* @param string $packageName | ||
* @param string $purchaseToken | ||
* @param string $productId | ||
*/ | ||
public function __construct( | ||
\Google_Service_AndroidPublisher $googleServiceAndroidPublisher, | ||
$packageName, | ||
$productId, | ||
$purchaseToken | ||
) { | ||
$this->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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace ReceiptValidator\Tests; | ||
|
||
use Google_Service_AndroidPublisher; | ||
use Google_Service_AndroidPublisher_Resource_PurchasesProducts; | ||
use Google_Service_AndroidPublisher_Resource_PurchasesSubscriptions; | ||
use PHPUnit\Framework\TestCase; | ||
use ReceiptValidator\GooglePlay\Acknowledger; | ||
|
||
/** | ||
* @group library | ||
*/ | ||
class GooglePlayAcknowledgerTest extends TestCase | ||
{ | ||
public function testValidate(): void | ||
{ | ||
$packageName = 'testPackage'; | ||
$productId = '15'; | ||
$purchaseToken = 'testPurchaseToken'; | ||
|
||
// mock objects | ||
$googleServiceAndroidPublisherMock = $this->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'); | ||
} | ||
} |