Skip to content

Commit

Permalink
SP-1147 - use resource token in invoice and refund webhook resend req…
Browse files Browse the repository at this point in the history
…uests
  • Loading branch information
swlodarski-sumoheavy committed Jan 23, 2025
1 parent 925ffde commit ad3902f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 31 deletions.
4 changes: 3 additions & 1 deletion examples/Merchant/InvoiceRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public function requestInvoiceWebhookToBeResent(): void
{
$client = ClientProvider::create();

$client->requestInvoiceNotification('someInvoiceId');
$invoiceId = 'someInvoiceId';
$invoiceById = $client->getInvoice($invoiceId);
$client->requestInvoiceNotification($invoiceId, $invoiceById->getToken());
}
}
4 changes: 3 additions & 1 deletion examples/Merchant/RefundRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public function requestRefundNotificationToBeResent(): void
{
$client = ClientProvider::create();

$client->sendRefundNotification('someRefundId');
$refundId = 'someRefundId';
$refund = $client->getRefund($refundId);
$client->sendRefundNotification($refundId, $refund->getToken());
}
}
16 changes: 10 additions & 6 deletions src/BitPaySDK/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,18 @@ public function getInvoices(
*
* @see https://developer.bitpay.com/reference/retrieve-an-event-token Retrieve an Event Token
*
* @param string $invoiceId A BitPay invoice ID.
* @return bool True if the webhook was successfully requested, false otherwise.
* @param string $invoiceId A BitPay invoice ID.
* @param string $invoiceToken The resource token for the invoiceId.
* This token can be retrieved from the Bitpay's invoice object.
* @return bool True if the webhook was successfully requested, false otherwise.
* @throws BitPayApiException
* @throws BitPayGenericException
*/
public function requestInvoiceNotification(string $invoiceId): bool
public function requestInvoiceNotification(string $invoiceId, string $invoiceToken): bool
{
$invoiceClient = $this->getInvoiceClient();

return $invoiceClient->requestNotification($invoiceId);
return $invoiceClient->requestNotification($invoiceId, $invoiceToken);
}

/**
Expand Down Expand Up @@ -469,14 +471,16 @@ public function getRefundByGuid(string $guid): Refund
* Request a Refund Notification to be Resent
*
* @param string $refundId A BitPay refund ID.
* @param string $refundToken The resource token for the invoirefundIdceId.
* This token can be retrieved from the Bitpay's refund object.
* @return bool $result An updated Refund Object
* @throws BitPayApiException
*/
public function sendRefundNotification(string $refundId): bool
public function sendRefundNotification(string $refundId, string $refundToken): bool
{
$refundClient = $this->getRefundClient();

return $refundClient->sendNotification($refundId);
return $refundClient->sendNotification($refundId, $refundToken);
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/BitPaySDK/Client/InvoiceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,16 @@ public function getInvoices(
/**
* Request a BitPay Invoice Webhook.
*
* @param string $invoiceId A BitPay invoice ID.
* @return bool True if the webhook was successfully requested, false otherwise.
* @param string $invoiceId A BitPay invoice ID.
* @param string $invoiceToken The resource token for the invoiceId.
* This token can be retrieved from the Bitpay's invoice object.
* @return bool True if the webhook was successfully requested, false otherwise.
* @throws BitPayApiException
* @throws BitPayGenericException
*/
public function requestNotification(string $invoiceId): bool
public function requestNotification(string $invoiceId, string $invoiceToken): bool
{
$params = ['token' => $this->tokenCache->getTokenByFacade(Facade::MERCHANT)];
$params = ['token' => $invoiceToken];
$responseJson = $this->restCli->post("invoices/" . $invoiceId . "/notifications", $params);

return strtolower($responseJson) === "success";
Expand Down
8 changes: 5 additions & 3 deletions src/BitPaySDK/Client/RefundClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,17 @@ public function getByGuid(string $guid): Refund
/**
* Send a refund notification.
*
* @param string $refundId A BitPay refund ID.
* @param string $refundId A BitPay refund ID.
* @param string $refundToken The resource token for the invoirefundIdceId.
* This token can be retrieved from the Bitpay's refund object.
* @return bool $result An updated Refund Object
* @throws BitPayApiException
* @throws Exception
*/
public function sendNotification(string $refundId): bool
public function sendNotification(string $refundId, string $refundToken): bool
{
$params = [];
$params["token"] = $this->tokenCache->getTokenByFacade(Facade::MERCHANT);
$params["token"] = $refundToken;

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

Expand Down
4 changes: 2 additions & 2 deletions test/functional/BitPaySDK/RefundClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testSendNotification(): void
$invoices = $this->client->getInvoices($dateStart, $dateEnd, 'complete', null, 1);
$refunds = $this->client->getRefunds($invoices[0]->getId());

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

public function testCancelRefund(): void
Expand Down Expand Up @@ -108,4 +108,4 @@ private function getInvoiceExample(): Invoice

return $invoice;
}
}
}
33 changes: 19 additions & 14 deletions test/unit/BitPaySDK/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class ClientTest extends TestCase
private const PAYOUT_TOKEN = 'kQLZ7C9YKPSnMCC4EJwrqRHXuQkLzL1W8DfZCh37DHb';
private const CORRUPT_JSON_STRING = '{"code":"USD""name":"US Dollar","rate":21205.85}';
private const TEST_INVOICE_ID = 'UZjwcYkWAKfTMn9J1yyfs4';
private const TEST_INVOICE_TOKEN = 'cM78LHk17Q8fktDE6QLBBFfvH1QKBhRkHibTLcxhgzsu3VDRvSyu3CGi17DuwYxhT';
private const TEST_INVOICE_GUID = 'chc9kj52-04g0-4b6f-941d-3a844e352758';
private const CORRECT_JSON_STRING = '[
{ "currency": "EUR", "balance": 0 },
Expand Down Expand Up @@ -2796,14 +2797,15 @@ public function testGetRefundByGuidShouldCatchRestCliJsonMapperException()
public function testSendRefundNotification()
{
$exampleRefundId = 'testId';
$params['token'] = self::MERCHANT_TOKEN;
$exampleRefundToken = 'testToken';
$params['token'] = $exampleRefundToken;

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

self::assertIsBool($result);
}
Expand All @@ -2814,7 +2816,8 @@ public function testSendRefundNotification()
public function testSendRefundNotificationShouldCatchRestCliBitPayException()
{
$exampleRefundId = 'testId';
$params['token'] = self::MERCHANT_TOKEN;
$exampleRefundToken = 'testToken';
$params['token'] = $exampleRefundToken;

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

$client->sendRefundNotification($exampleRefundId);
$client->sendRefundNotification($exampleRefundId, $exampleRefundToken);
}

/**
Expand All @@ -2832,7 +2835,8 @@ public function testSendRefundNotificationShouldCatchRestCliBitPayException()
public function testSendRefundNotificationShouldCatchRestCliException()
{
$exampleRefundId = 'testId';
$params['token'] = self::MERCHANT_TOKEN;
$exampleRefundToken = 'testToken';
$params['token'] = $exampleRefundToken;

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

$client->sendRefundNotification($exampleRefundId);
$client->sendRefundNotification($exampleRefundId, $exampleRefundToken);
}

/**
Expand All @@ -2850,15 +2854,16 @@ public function testSendRefundNotificationShouldCatchRestCliException()
public function testSendRefundNotificationShouldCatchJsonMapperException()
{
$exampleRefundId = 'testId';
$params['token'] = self::MERCHANT_TOKEN;
$exampleRefundToken = 'testToken';
$params['token'] = $exampleRefundToken;

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

self::assertFalse($client->sendRefundNotification($exampleRefundId));
self::assertFalse($client->sendRefundNotification($exampleRefundId, $exampleRefundToken));
}

public function testGetInvoice()
Expand Down Expand Up @@ -3037,7 +3042,7 @@ public function testGetInvoicesShouldCatchJsonMapperException()
public function testRequestInvoiceNotificationShouldReturnTrueOnSuccess()
{
$invoiceId = self::TEST_INVOICE_ID;
$params['token'] = self::MERCHANT_TOKEN;
$params['token'] = self::TEST_INVOICE_TOKEN;
$expectedSuccessResponse = 'success';
$restCliMock = $this->getRestCliMock();

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

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

$result = $testedObject->requestInvoiceNotification($invoiceId);
$result = $testedObject->requestInvoiceNotification($invoiceId, self::TEST_INVOICE_TOKEN);
self::assertTrue($result);
}

public function testRequestInvoiceNotificationShouldReturnFalseOnFailure()
{
$params['token'] = self::MERCHANT_TOKEN;
$params['token'] = self::TEST_INVOICE_TOKEN;
$expectedFailResponse = 'fail';
$restCliMock = $this->getRestCliMock();

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

$result = $testedObject->requestInvoiceNotification(self::TEST_INVOICE_ID);
$result = $testedObject->requestInvoiceNotification(self::TEST_INVOICE_ID, self::TEST_INVOICE_TOKEN);
self::assertFalse($result);
}

public function testRequestInvoiceNotificationShouldCatchJsonMapperException()
{
$params['token'] = self::MERCHANT_TOKEN;
$params['token'] = self::TEST_INVOICE_TOKEN;
$restCliMock = $this->getRestCliMock();

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

$this->expectException(BitPayGenericException::class);
$testedObject->requestInvoiceNotification(self::TEST_INVOICE_ID);
$testedObject->requestInvoiceNotification(self::TEST_INVOICE_ID, self::TEST_INVOICE_TOKEN);
}

public function testCancelInvoice()
Expand Down

0 comments on commit ad3902f

Please sign in to comment.