Skip to content

Commit 65546e6

Browse files
authored
Merge pull request #355 from swlodarski-sumoheavy/9.1.x
SP-1147 - use resource token in webhook resend requests
2 parents 925ffde + 1ed5196 commit 65546e6

File tree

9 files changed

+51
-34
lines changed

9 files changed

+51
-34
lines changed

examples/Merchant/InvoiceRequests.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public function requestInvoiceWebhookToBeResent(): void
8787
{
8888
$client = ClientProvider::create();
8989

90-
$client->requestInvoiceNotification('someInvoiceId');
90+
$invoiceId = 'someInvoiceId';
91+
$invoiceById = $client->getInvoice($invoiceId);
92+
$client->requestInvoiceNotification($invoiceId, $invoiceById->getToken());
9193
}
9294
}

examples/Merchant/RefundRequests.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public function requestRefundNotificationToBeResent(): void
5757
{
5858
$client = ClientProvider::create();
5959

60-
$client->sendRefundNotification('someRefundId');
60+
$refundId = 'someRefundId';
61+
$refund = $client->getRefund($refundId);
62+
$client->sendRefundNotification($refundId, $refund->getToken());
6163
}
6264
}

phpdoc.dist.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<output>docs</output>
1111
</paths>
1212

13-
<version number="9.1.4">
13+
<version number="9.1.5">
1414
<api format="php">
1515
<source dsn=".">
1616
<path>src</path>
@@ -24,4 +24,4 @@
2424

2525
<setting name="template.color" value="blue"/>
2626

27-
</phpdocumentor>
27+
</phpdocumentor>

src/BitPaySDK/Client.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,16 +272,18 @@ public function getInvoices(
272272
*
273273
* @see https://developer.bitpay.com/reference/retrieve-an-event-token Retrieve an Event Token
274274
*
275-
* @param string $invoiceId A BitPay invoice ID.
276-
* @return bool True if the webhook was successfully requested, false otherwise.
275+
* @param string $invoiceId A BitPay invoice ID.
276+
* @param string $invoiceToken The resource token for the invoiceId.
277+
* This token can be retrieved from the Bitpay's invoice object.
278+
* @return bool True if the webhook was successfully requested, false otherwise.
277279
* @throws BitPayApiException
278280
* @throws BitPayGenericException
279281
*/
280-
public function requestInvoiceNotification(string $invoiceId): bool
282+
public function requestInvoiceNotification(string $invoiceId, string $invoiceToken): bool
281283
{
282284
$invoiceClient = $this->getInvoiceClient();
283285

284-
return $invoiceClient->requestNotification($invoiceId);
286+
return $invoiceClient->requestNotification($invoiceId, $invoiceToken);
285287
}
286288

287289
/**
@@ -469,14 +471,16 @@ public function getRefundByGuid(string $guid): Refund
469471
* Request a Refund Notification to be Resent
470472
*
471473
* @param string $refundId A BitPay refund ID.
474+
* @param string $refundToken The resource token for the refundId.
475+
* This token can be retrieved from the Bitpay's refund object.
472476
* @return bool $result An updated Refund Object
473477
* @throws BitPayApiException
474478
*/
475-
public function sendRefundNotification(string $refundId): bool
479+
public function sendRefundNotification(string $refundId, string $refundToken): bool
476480
{
477481
$refundClient = $this->getRefundClient();
478482

479-
return $refundClient->sendNotification($refundId);
483+
return $refundClient->sendNotification($refundId, $refundToken);
480484
}
481485

482486
/**

src/BitPaySDK/Client/InvoiceClient.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,16 @@ public function getInvoices(
250250
/**
251251
* Request a BitPay Invoice Webhook.
252252
*
253-
* @param string $invoiceId A BitPay invoice ID.
254-
* @return bool True if the webhook was successfully requested, false otherwise.
253+
* @param string $invoiceId A BitPay invoice ID.
254+
* @param string $invoiceToken The resource token for the invoiceId.
255+
* This token can be retrieved from the Bitpay's invoice object.
256+
* @return bool True if the webhook was successfully requested, false otherwise.
255257
* @throws BitPayApiException
256258
* @throws BitPayGenericException
257259
*/
258-
public function requestNotification(string $invoiceId): bool
260+
public function requestNotification(string $invoiceId, string $invoiceToken): bool
259261
{
260-
$params = ['token' => $this->tokenCache->getTokenByFacade(Facade::MERCHANT)];
262+
$params = ['token' => $invoiceToken];
261263
$responseJson = $this->restCli->post("invoices/" . $invoiceId . "/notifications", $params);
262264

263265
return strtolower($responseJson) === "success";

src/BitPaySDK/Client/RefundClient.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,17 @@ public function getByGuid(string $guid): Refund
258258
/**
259259
* Send a refund notification.
260260
*
261-
* @param string $refundId A BitPay refund ID.
261+
* @param string $refundId A BitPay refund ID.
262+
* @param string $refundToken The resource token for the refundId.
263+
* This token can be retrieved from the Bitpay's refund object.
262264
* @return bool $result An updated Refund Object
263265
* @throws BitPayApiException
264266
* @throws Exception
265267
*/
266-
public function sendNotification(string $refundId): bool
268+
public function sendNotification(string $refundId, string $refundToken): bool
267269
{
268270
$params = [];
269-
$params["token"] = $this->tokenCache->getTokenByFacade(Facade::MERCHANT);
271+
$params["token"] = $refundToken;
270272

271273
$responseJson = $this->restCli->post("refunds/" . $refundId . "/notifications", $params, true);
272274

src/BitPaySDK/Env.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface Env
1717
public const TEST_URL = "https://test.bitpay.com/";
1818
public const PROD_URL = "https://bitpay.com/";
1919
public const BITPAY_API_VERSION = "2.0.0";
20-
public const BITPAY_PLUGIN_INFO = "BitPay_PHP_Client_v9.1.4";
20+
public const BITPAY_PLUGIN_INFO = "BitPay_PHP_Client_v9.1.5";
2121
public const BITPAY_API_FRAME = "std";
2222
public const BITPAY_API_FRAME_VERSION = "1.0.0";
2323
}

test/functional/BitPaySDK/RefundClientTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function testSendNotification(): void
6767
$invoices = $this->client->getInvoices($dateStart, $dateEnd, 'complete', null, 1);
6868
$refunds = $this->client->getRefunds($invoices[0]->getId());
6969

70-
self::assertTrue($this->client->sendRefundNotification($refunds[0]->getId()));
70+
self::assertTrue($this->client->sendRefundNotification($refunds[0]->getId(), $refunds[0]->getToken()));
7171
}
7272

7373
public function testCancelRefund(): void
@@ -108,4 +108,4 @@ private function getInvoiceExample(): Invoice
108108

109109
return $invoice;
110110
}
111-
}
111+
}

test/unit/BitPaySDK/ClientTest.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class ClientTest extends TestCase
4747
private const PAYOUT_TOKEN = 'kQLZ7C9YKPSnMCC4EJwrqRHXuQkLzL1W8DfZCh37DHb';
4848
private const CORRUPT_JSON_STRING = '{"code":"USD""name":"US Dollar","rate":21205.85}';
4949
private const TEST_INVOICE_ID = 'UZjwcYkWAKfTMn9J1yyfs4';
50+
private const TEST_INVOICE_TOKEN = 'cM78LHk17Q8fktDE6QLBBFfvH1QKBhRkHibTLcxhgzsu3VDRvSyu3CGi17DuwYxhT';
5051
private const TEST_INVOICE_GUID = 'chc9kj52-04g0-4b6f-941d-3a844e352758';
5152
private const CORRECT_JSON_STRING = '[
5253
{ "currency": "EUR", "balance": 0 },
@@ -2796,14 +2797,15 @@ public function testGetRefundByGuidShouldCatchRestCliJsonMapperException()
27962797
public function testSendRefundNotification()
27972798
{
27982799
$exampleRefundId = 'testId';
2799-
$params['token'] = self::MERCHANT_TOKEN;
2800+
$exampleRefundToken = 'testToken';
2801+
$params['token'] = $exampleRefundToken;
28002802

28012803
$restCliMock = $this->getRestCliMock();
28022804
$restCliMock->expects(self::once())->method('post')
28032805
->with("refunds/" . $exampleRefundId . "/notifications", $params, true)
28042806
->willReturn('{"status":"success"}');
28052807
$client = $this->getClient($restCliMock);
2806-
$result = $client->sendRefundNotification($exampleRefundId);
2808+
$result = $client->sendRefundNotification($exampleRefundId, $exampleRefundToken);
28072809

28082810
self::assertIsBool($result);
28092811
}
@@ -2814,7 +2816,8 @@ public function testSendRefundNotification()
28142816
public function testSendRefundNotificationShouldCatchRestCliBitPayException()
28152817
{
28162818
$exampleRefundId = 'testId';
2817-
$params['token'] = self::MERCHANT_TOKEN;
2819+
$exampleRefundToken = 'testToken';
2820+
$params['token'] = $exampleRefundToken;
28182821

28192822
$restCliMock = $this->getRestCliMock();
28202823
$restCliMock->expects(self::once())->method('post')
@@ -2823,7 +2826,7 @@ public function testSendRefundNotificationShouldCatchRestCliBitPayException()
28232826
$client = $this->getClient($restCliMock);
28242827
$this->expectException(BitPayApiException::class);
28252828

2826-
$client->sendRefundNotification($exampleRefundId);
2829+
$client->sendRefundNotification($exampleRefundId, $exampleRefundToken);
28272830
}
28282831

28292832
/**
@@ -2832,7 +2835,8 @@ public function testSendRefundNotificationShouldCatchRestCliBitPayException()
28322835
public function testSendRefundNotificationShouldCatchRestCliException()
28332836
{
28342837
$exampleRefundId = 'testId';
2835-
$params['token'] = self::MERCHANT_TOKEN;
2838+
$exampleRefundToken = 'testToken';
2839+
$params['token'] = $exampleRefundToken;
28362840

28372841
$restCliMock = $this->getRestCliMock();
28382842
$restCliMock->expects(self::once())->method('post')
@@ -2841,7 +2845,7 @@ public function testSendRefundNotificationShouldCatchRestCliException()
28412845
$client = $this->getClient($restCliMock);
28422846
$this->expectException(BitPayApiException::class);
28432847

2844-
$client->sendRefundNotification($exampleRefundId);
2848+
$client->sendRefundNotification($exampleRefundId, $exampleRefundToken);
28452849
}
28462850

28472851
/**
@@ -2850,15 +2854,16 @@ public function testSendRefundNotificationShouldCatchRestCliException()
28502854
public function testSendRefundNotificationShouldCatchJsonMapperException()
28512855
{
28522856
$exampleRefundId = 'testId';
2853-
$params['token'] = self::MERCHANT_TOKEN;
2857+
$exampleRefundToken = 'testToken';
2858+
$params['token'] = $exampleRefundToken;
28542859

28552860
$restCliMock = $this->getRestCliMock();
28562861
$restCliMock->expects(self::once())->method('post')
28572862
->with("refunds/" . $exampleRefundId . "/notifications", $params, true)
28582863
->willReturn(file_get_contents(__DIR__ . '/jsonResponse/false.json', true));
28592864
$client = $this->getClient($restCliMock);
28602865

2861-
self::assertFalse($client->sendRefundNotification($exampleRefundId));
2866+
self::assertFalse($client->sendRefundNotification($exampleRefundId, $exampleRefundToken));
28622867
}
28632868

28642869
public function testGetInvoice()
@@ -3037,7 +3042,7 @@ public function testGetInvoicesShouldCatchJsonMapperException()
30373042
public function testRequestInvoiceNotificationShouldReturnTrueOnSuccess()
30383043
{
30393044
$invoiceId = self::TEST_INVOICE_ID;
3040-
$params['token'] = self::MERCHANT_TOKEN;
3045+
$params['token'] = self::TEST_INVOICE_TOKEN;
30413046
$expectedSuccessResponse = 'success';
30423047
$restCliMock = $this->getRestCliMock();
30433048

@@ -3049,13 +3054,13 @@ public function testRequestInvoiceNotificationShouldReturnTrueOnSuccess()
30493054

30503055
$testedObject = $this->getClient($restCliMock);
30513056

3052-
$result = $testedObject->requestInvoiceNotification($invoiceId);
3057+
$result = $testedObject->requestInvoiceNotification($invoiceId, self::TEST_INVOICE_TOKEN);
30533058
self::assertTrue($result);
30543059
}
30553060

30563061
public function testRequestInvoiceNotificationShouldReturnFalseOnFailure()
30573062
{
3058-
$params['token'] = self::MERCHANT_TOKEN;
3063+
$params['token'] = self::TEST_INVOICE_TOKEN;
30593064
$expectedFailResponse = 'fail';
30603065
$restCliMock = $this->getRestCliMock();
30613066

@@ -3066,13 +3071,13 @@ public function testRequestInvoiceNotificationShouldReturnFalseOnFailure()
30663071
->willReturn($expectedFailResponse);
30673072
$testedObject = $this->getClient($restCliMock);
30683073

3069-
$result = $testedObject->requestInvoiceNotification(self::TEST_INVOICE_ID);
3074+
$result = $testedObject->requestInvoiceNotification(self::TEST_INVOICE_ID, self::TEST_INVOICE_TOKEN);
30703075
self::assertFalse($result);
30713076
}
30723077

30733078
public function testRequestInvoiceNotificationShouldCatchJsonMapperException()
30743079
{
3075-
$params['token'] = self::MERCHANT_TOKEN;
3080+
$params['token'] = self::TEST_INVOICE_TOKEN;
30763081
$restCliMock = $this->getRestCliMock();
30773082

30783083
$restCliMock
@@ -3084,7 +3089,7 @@ public function testRequestInvoiceNotificationShouldCatchJsonMapperException()
30843089
$testedObject = $this->getClient($restCliMock);
30853090

30863091
$this->expectException(BitPayGenericException::class);
3087-
$testedObject->requestInvoiceNotification(self::TEST_INVOICE_ID);
3092+
$testedObject->requestInvoiceNotification(self::TEST_INVOICE_ID, self::TEST_INVOICE_TOKEN);
30883093
}
30893094

30903095
public function testCancelInvoice()

0 commit comments

Comments
 (0)