Skip to content

Commit

Permalink
Merge pull request #108 from passions-app/master
Browse files Browse the repository at this point in the history
[ADD] acknowledger for the new google play billing api version
  • Loading branch information
Stafox authored Nov 19, 2019
2 parents ae63882 + c546714 commit 60bf231
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/GooglePlay/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
namespace ReceiptValidator\GooglePlay;

/**
* Class AbstractResponse
* @package ReceiptValidator\GooglePlay
* Class AbstractResponse.
*/
abstract class AbstractResponse
{
const CONSUMPTION_STATE_YET_TO_BE_CONSUMED = 0;
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
*/
protected $response;

/**
* Constructor
* Constructor.
*
* @param \Google_Service_AndroidPublisher_ProductPurchase|\Google_Service_AndroidPublisher_SubscriptionPurchase $response
*/
Expand All @@ -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
*/
Expand Down
95 changes: 95 additions & 0 deletions src/GooglePlay/Acknowledger.php
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);
}
}
}
55 changes: 55 additions & 0 deletions tests/GooglePlay/GooglePlayAcknowledgerTest.php
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');
}
}

0 comments on commit 60bf231

Please sign in to comment.