Skip to content

Commit 7eae0f1

Browse files
committed
Subscription and Product facades can supply custom client on initialization
1 parent 3d113e2 commit 7eae0f1

File tree

6 files changed

+39
-8
lines changed

6 files changed

+39
-8
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"php": ">=7.1",
2222
"ext-json": "*",
2323
"imdhemy/appstore-iap": "^0.3",
24-
"imdhemy/google-play-billing": "^0.3",
24+
"imdhemy/google-play-billing": "^0.6",
2525
"nesbot/carbon": "^2.41"
2626
},
2727
"require-dev": {

src/Facades/Product.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
namespace Imdhemy\Purchases\Facades;
55

6+
use GuzzleHttp\ClientInterface;
67
use Illuminate\Support\Facades\Facade;
78

89
/**
9-
* @method static \Imdhemy\Purchases\Product googlePlay()
10+
* @method static \Imdhemy\Purchases\Product googlePlay(?ClientInterface $client = null)
1011
* @method static \Imdhemy\Purchases\Product appStore()
1112
*/
1213
class Product extends Facade

src/Facades/Subscription.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
namespace Imdhemy\Purchases\Facades;
55

6+
use GuzzleHttp\ClientInterface;
67
use Illuminate\Support\Facades\Facade;
78

89
/**
9-
* @method static \Imdhemy\Purchases\Subscription googlePlay()
10+
* @method static \Imdhemy\Purchases\Subscription googlePlay(?ClientInterface $client = null)
1011
* @method static \Imdhemy\Purchases\Subscription appStore()
1112
*/
1213
class Subscription extends Facade

src/Product.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
namespace Imdhemy\Purchases;
55

66
use GuzzleHttp\Client;
7+
use GuzzleHttp\ClientInterface;
78
use GuzzleHttp\Exception\GuzzleException;
89
use Imdhemy\AppStore\ClientFactory as AppStoreClientFactory;
10+
use Imdhemy\AppStore\Exceptions\InvalidReceiptException;
911
use Imdhemy\AppStore\Receipts\ReceiptResponse;
1012
use Imdhemy\AppStore\Receipts\Verifier;
1113
use Imdhemy\GooglePlay\ClientFactory as GooglePlayClientFactory;
@@ -45,11 +47,12 @@ class Product
4547
protected $password;
4648

4749
/**
50+
* @param ClientInterface|null $client
4851
* @return self
4952
*/
50-
public function googlePlay(): self
53+
public function googlePlay(?ClientInterface $client = null): self
5154
{
52-
$this->client = GooglePlayClientFactory::create([GooglePlayClientFactory::SCOPE_ANDROID_PUBLISHER]);
55+
$this->client = $client ?? GooglePlayClientFactory::create([GooglePlayClientFactory::SCOPE_ANDROID_PUBLISHER]);
5356
$this->packageName = config('purchase.google_play_package_name');
5457

5558
return $this;
@@ -121,7 +124,7 @@ public function acknowledge(?string $developerPayload = null): void
121124

122125
/**
123126
* @return ReceiptResponse
124-
* @throws GuzzleException
127+
* @throws GuzzleException|InvalidReceiptException
125128
*/
126129
public function verifyReceipt(): ReceiptResponse
127130
{

src/Subscription.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Imdhemy\Purchases;
55

66
use GuzzleHttp\Client;
7+
use GuzzleHttp\ClientInterface;
78
use GuzzleHttp\Exception\GuzzleException;
89
use Imdhemy\AppStore\ClientFactory as AppStoreClientFactory;
910
use Imdhemy\AppStore\Exceptions\InvalidReceiptException;
@@ -69,12 +70,13 @@ class Subscription
6970
protected $isGoogle = false;
7071

7172
/**
73+
* @param ClientInterface|null $client
7274
* @return self
7375
*/
74-
public function googlePlay(): self
76+
public function googlePlay(?ClientInterface $client = null): self
7577
{
7678
$this->isGoogle = true;
77-
$this->client = GooglePlayClientFactory::create([GooglePlayClientFactory::SCOPE_ANDROID_PUBLISHER]);
79+
$this->client = $client ?? GooglePlayClientFactory::create([GooglePlayClientFactory::SCOPE_ANDROID_PUBLISHER]);
7880
$this->packageName = config('purchase.google_play_package_name');
7981

8082
return $this;

tests/Facades/SubscriptionTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Imdhemy\Purchases\Tests\Facades;
44

5+
use Exception;
56
use GuzzleHttp\Exception\GuzzleException;
67
use Imdhemy\AppStore\Exceptions\InvalidReceiptException;
78
use Imdhemy\AppStore\Receipts\ReceiptResponse;
9+
use Imdhemy\GooglePlay\ClientFactory;
810
use Imdhemy\GooglePlay\Subscriptions\SubscriptionPurchase;
911
use Imdhemy\Purchases\Contracts\SubscriptionContract;
1012
use Imdhemy\Purchases\Facades\Subscription;
@@ -93,4 +95,26 @@ public function test_it_handles_the_21007_error_from_the_app_store()
9395
$response = Subscription::appStore()->receiptData($receiptData)->password($password)->verifyRenewable();
9496
$this->assertTrue($response->getStatus()->isValid());
9597
}
98+
99+
/**
100+
* @test
101+
* @throws Exception
102+
* @throws GuzzleException
103+
*/
104+
public function test_custom_client_can_be_set_on_google_play()
105+
{
106+
$jsonStream = file_get_contents(__DIR__ . '/../../google-app-credentials.json');
107+
$jsonKey = json_decode($jsonStream, true);
108+
$client = ClientFactory::createWithJsonKey($jsonKey, [ClientFactory::SCOPE_ANDROID_PUBLISHER]);
109+
$subscription = Subscription::googlePlay($client)
110+
->packageName('com.twigano.fashion')
111+
->id($this->itemId)
112+
->token($this->token);
113+
114+
$getResponse = $subscription->get();
115+
$stdSubscription = $subscription->toStd();
116+
117+
$this->assertInstanceOf(SubscriptionPurchase::class, $getResponse);
118+
$this->assertInstanceOf(SubscriptionContract::class, $stdSubscription);
119+
}
96120
}

0 commit comments

Comments
 (0)