Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SP-1147 - use resource token in webhook resend requests #355

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
4 changes: 2 additions & 2 deletions phpdoc.dist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<output>docs</output>
</paths>

<version number="9.1.4">
<version number="9.1.5">
<api format="php">
<source dsn=".">
<path>src</path>
Expand All @@ -24,4 +24,4 @@

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

</phpdocumentor>
</phpdocumentor>
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 refundId.
* 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 refundId.
* 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
2 changes: 1 addition & 1 deletion src/BitPaySDK/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface Env
public const TEST_URL = "https://test.bitpay.com/";
public const PROD_URL = "https://bitpay.com/";
public const BITPAY_API_VERSION = "2.0.0";
public const BITPAY_PLUGIN_INFO = "BitPay_PHP_Client_v9.1.4";
public const BITPAY_PLUGIN_INFO = "BitPay_PHP_Client_v9.1.5";
public const BITPAY_API_FRAME = "std";
public const BITPAY_API_FRAME_VERSION = "1.0.0";
}
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
Loading