Skip to content

Commit 6c79393

Browse files
authored
Merge pull request #335 from swlodarski-sumoheavy/10.1.x
SP-1153: use resource token in webhook resend requests
2 parents 7bbf82c + 55a5d46 commit 6c79393

File tree

12 files changed

+74
-33
lines changed

12 files changed

+74
-33
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.bitpay</groupId>
88
<artifactId>bitpay_sdk</artifactId>
9-
<version>10.1.2</version>
9+
<version>10.1.3</version>
1010
<packaging>jar</packaging>
1111

1212
<name>BitPay</name>

src/main/java/com/bitpay/sdk/Client.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -735,14 +735,19 @@ public Invoice cancelInvoiceByGuid(
735735
* The intent of this call is to address issues when BitPay sends a webhook but the client doesn't receive it,
736736
* so the client can request that BitPay resend it.
737737
*
738-
* @param invoiceId The id of the invoice for which you want the last webhook to be resent.
738+
* @param invoiceId The id of the invoice for which you want the last webhook to be resent.
739+
* @param invoiceToken The resource token for the invoiceId.
740+
* This token can be retrieved from the Bitpay's invoice object.
739741
* @return Boolean status of request
740742
* @throws BitPayApiException BitPayApiException class
741743
* @throws BitPayGenericException BitPayGenericException class
742744
* @see <a href="https://developer.bitpay.com/reference/request-an-invoice-webhook-to-be-resent">Request an Invoice Webhook to be Resent</a>
743745
*/
744-
public Boolean requestInvoiceWebhookToBeResent(String invoiceId) throws BitPayApiException, BitPayGenericException {
745-
return this.getInvoiceClient().requestInvoiceWebhookToBeResent(invoiceId);
746+
public Boolean requestInvoiceWebhookToBeResent(
747+
String invoiceId,
748+
String invoiceToken
749+
) throws BitPayApiException, BitPayGenericException {
750+
return this.getInvoiceClient().requestInvoiceWebhookToBeResent(invoiceId, invoiceToken);
746751
}
747752

748753
/**
@@ -907,14 +912,19 @@ public Refund updateRefundByGuid(String guid, String status) throws BitPayGeneri
907912
/**
908913
* Send a refund notification.
909914
*
910-
* @param refundId A BitPay refund ID.
915+
* @param refundId A BitPay refund ID.
916+
* @param refundToken The resource token for the refundId.
917+
* This token can be retrieved from the Bitpay's refund object.
911918
* @return An updated Refund Object
912919
* @throws BitPayGenericException BitPayGenericException class
913920
* @throws BitPayApiException BitPayApiException class
914921
* @see <a href="https://developer.bitpay.com/reference/request-a-refund-notification-to-be-resent">Request a Refund Notification to be Resent</a>
915922
*/
916-
public Boolean sendRefundNotification(String refundId) throws BitPayGenericException, BitPayApiException {
917-
return this.getRefundClient().sendRefundNotification(refundId);
923+
public Boolean sendRefundNotification(
924+
String refundId,
925+
String refundToken
926+
) throws BitPayGenericException, BitPayApiException {
927+
return this.getRefundClient().sendRefundNotification(refundId, refundToken);
918928
}
919929

920930
/**

src/main/java/com/bitpay/sdk/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class Config {
3434
/**
3535
* BitPay Plugin Info Version.
3636
*/
37-
public static final String BITPAY_PLUGIN_INFO = "BitPay_Java_Client_v10.1.2";
37+
public static final String BITPAY_PLUGIN_INFO = "BitPay_Java_Client_v10.1.3";
3838
/**
3939
* BitPay Api Frame.
4040
*/

src/main/java/com/bitpay/sdk/client/InvoiceClient.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,14 +453,19 @@ public Invoice cancelByGuid(String guid, Boolean forceCancel) throws BitPayApiEx
453453
/**
454454
* Request an Invoice Webhook to be Resent.
455455
*
456-
* @param invoiceId Invoice ID
456+
* @param invoiceId Invoice ID
457+
* @param invoiceToken The resource token for the invoiceId.
458+
* This token can be retrieved from the Bitpay's invoice object.
457459
* @return Boolean
458460
* @throws BitPayApiException BitPayApiException class
459461
* @throws BitPayGenericException BitPayGenericException class
460462
*/
461-
public Boolean requestInvoiceWebhookToBeResent(String invoiceId) throws BitPayApiException, BitPayGenericException {
463+
public Boolean requestInvoiceWebhookToBeResent(
464+
String invoiceId,
465+
String invoiceToken
466+
) throws BitPayApiException, BitPayGenericException {
462467
final Map<String, String> params = new HashMap<>();
463-
params.put("token", this.accessTokens.getAccessToken(Facade.MERCHANT));
468+
params.put("token", invoiceToken);
464469

465470
JsonMapper mapper = JsonMapperFactory.create();
466471
String json = null;

src/main/java/com/bitpay/sdk/client/RefundClient.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,18 +272,23 @@ public Refund updateByGuid(
272272
/**
273273
* Send a refund notification.
274274
*
275-
* @param refundId A BitPay refund ID.
275+
* @param refundId A BitPay refund ID.
276+
* @param refundToken The resource token for the refundId.
277+
* This token can be retrieved from the Bitpay's refund object.
276278
* @return An updated Refund Object
277279
* @throws BitPayGenericException BitPayGenericException class
278280
* @throws BitPayApiException BitPayApiException class
279281
*/
280-
public Boolean sendRefundNotification(final String refundId) throws BitPayApiException, BitPayGenericException {
282+
public Boolean sendRefundNotification(
283+
final String refundId,
284+
final String refundToken
285+
) throws BitPayApiException, BitPayGenericException {
281286
if (Objects.isNull(refundId)) {
282287
BitPayExceptionProvider.throwMissingParameterException();
283288
}
284289

285290
final Map<String, String> params = new HashMap<>();
286-
params.put("token", this.accessTokens.getAccessToken(Facade.MERCHANT));
291+
params.put("token", refundToken);
287292

288293
final JsonMapper mapper = JsonMapperFactory.create();
289294
String json = null;

src/main/java/com/bitpay/sdk/model/invoice/Refund.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class Refund {
4848
protected String transactionCurrency = ModelConfiguration.DEFAULT_NON_SENT_VALUE;
4949
protected String id;
5050
protected String status = ModelConfiguration.DEFAULT_NON_SENT_VALUE;
51+
protected String token;
5152

5253

5354
/**
@@ -543,4 +544,29 @@ public String getType() {
543544
public void setType(final String type) {
544545
this.type = type;
545546
}
547+
548+
/**
549+
* Gets refund resource token.
550+
* This token is derived from the API token initially used to create the refund and is tied
551+
* to the specific resource id created.
552+
*
553+
* @return the token
554+
*/
555+
@JsonProperty("token")
556+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
557+
public String getToken() {
558+
return this.token;
559+
}
560+
561+
/**
562+
* Sets refund resource token.
563+
* This token is derived from the API token initially used to create the refund and is tied
564+
* to the specific resource id created.
565+
*
566+
* @param token the token
567+
*/
568+
@JsonProperty("token")
569+
public void setToken(final String token) {
570+
this.token = token;
571+
}
546572
}

src/test/java/com/bitpay/sdk/ClientTest.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -636,21 +636,19 @@ public void it_should_throws_exception_for_cancel_invoice_for_invalid_state_of_c
636636
@Test
637637
public void it_should_request_invoice_webhook_to_be_resent() throws BitPayApiException, BitPayGenericException {
638638
// given
639-
final String merchantToken = "merchantToken";
640639
final String invoiceId = "UZjwcYkWAKfTMn9J1yyfs4";
641-
final String requestJson = "{\"token\":\"merchantToken\"}";
640+
final String invoiceToken = "cM78LHk17Q8fktDE6QLBBFfvH1QKBhRkHibTLcxhgzsu3VDRvSyu3CGi17DuwYxhT";
641+
final String requestJson = String.format("{\"token\":\"%s\"}", invoiceToken);
642642

643643
Mockito.when(this.bitPayClient.post(
644644
ArgumentMatchers.eq("invoices/" + invoiceId + "/notifications"), ArgumentMatchers.eq(requestJson))
645645
).thenReturn(this.getHttpResponseWithSpecificBody("{\"data\": \"Success\"}"));
646-
Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken);
647646
Client testedClass = this.getTestedClass();
648647

649648
// when
650-
Boolean result = testedClass.requestInvoiceWebhookToBeResent(invoiceId);
649+
Boolean result = testedClass.requestInvoiceWebhookToBeResent(invoiceId, invoiceToken);
651650

652651
// then
653-
Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT);
654652
Mockito.verify(this.bitPayClient, Mockito.times(1)).post("invoices/" + invoiceId + "/notifications", requestJson);
655653
Assertions.assertTrue(result);
656654
}
@@ -791,7 +789,7 @@ public void it_should_throws_bitpay_api_exception_for_invalid_sendRefundNotifica
791789
)).thenThrow(new BitPayApiException("error message", "500"));
792790

793791
// when
794-
testedClass.sendRefundNotification("1");
792+
testedClass.sendRefundNotification("1", "cM78LHk17Q8fktDE6QLBBFfvH1QKBhRkHibTLcxhgzsu3VDRvSyu3CGi17DuwYxhT");
795793
}
796794
);
797795

@@ -1046,27 +1044,23 @@ public void it_should_update_refund_by_guid() throws BitPayApiException, BitPayG
10461044
@Test
10471045
public void it_should_test_sendRefundNotification() throws BitPayApiException, BitPayGenericException {
10481046
// given
1049-
final String merchantToken = "merchantToken";
10501047
final String refundId = "WoE46gSLkJQS48RJEiNw3L";
1048+
final String refundToken = "cM78LHk17Q8fktDE6QLBBFfvH1QKBhRkHibTLcxhgzsu3VDRvSyu3CGi17DuwYxhT";
10511049
final String getRefundsJsonConvertedResponse = getPreparedJsonDataFromFile("refundNotificationResponse.json");
10521050

1053-
final List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
1054-
params.add(new BasicNameValuePair("token", merchantToken));
1055-
final String requestedJson = "{\"token\":\"merchantToken\"}";
1051+
final String requestedJson = String.format("{\"token\":\"%s\"}", refundToken);
10561052

10571053
Mockito.when(this.bitPayClient.post(
10581054
"refunds/" + refundId + "/notifications",
10591055
requestedJson,
10601056
true
10611057
)).thenReturn(this.getHttpResponseWithSpecificBody(getRefundsJsonConvertedResponse));
1062-
Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken);
10631058
Client testedClass = this.getTestedClass();
10641059

10651060
// when
1066-
Boolean result = testedClass.sendRefundNotification(refundId);
1061+
Boolean result = testedClass.sendRefundNotification(refundId, refundToken);
10671062

10681063
// then
1069-
Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT);
10701064
Mockito.verify(this.bitPayClient, Mockito.times(1)).post(
10711065
"refunds/" + refundId + "/notifications",
10721066
requestedJson,

src/test/java/com/bitpay/sdk/ConfigTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void it_should_returns_bitpay_api_version() {
5858

5959
@Test
6060
public void it_should_returns_bitpay_plugin_info() {
61-
Assertions.assertTrue(Config.BITPAY_PLUGIN_INFO.contains("BitPay_Java_Client_v10.1.2"));
61+
Assertions.assertTrue(Config.BITPAY_PLUGIN_INFO.contains("BitPay_Java_Client_v10.1.3"));
6262
}
6363

6464
@Test

src/test/java/com/bitpay/sdk/client/InvoiceClientTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,12 @@ public void it_should_request_invoice_webhook_to_be_resent() throws BitPayExcept
436436
this.addServerJsonResponse(
437437
"/invoices/Hpqc63wvE1ZjzeeH4kEycF/notifications",
438438
"POST",
439-
"{\"token\":\"someMerchantToken\"}",
439+
"{\"token\":\"cM78LHk17Q8fktDE6QLBBFfvH1QKBhRkHibTLcxhgzsu3VDRvSyu3CGi17DuwYxhT\"}",
440440
getPreparedJsonDataFromFile("invoiceWebhookResponse.json")
441441
);
442442

443443
// when
444-
Boolean result = this.getTestedClass(accessTokens).requestInvoiceWebhookToBeResent("Hpqc63wvE1ZjzeeH4kEycF");
444+
Boolean result = this.getTestedClass(accessTokens).requestInvoiceWebhookToBeResent("Hpqc63wvE1ZjzeeH4kEycF", "cM78LHk17Q8fktDE6QLBBFfvH1QKBhRkHibTLcxhgzsu3VDRvSyu3CGi17DuwYxhT");
445445
Assertions.assertTrue(result);
446446
}
447447

src/test/java/com/bitpay/sdk/client/RefundClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public void it_should_send_refund_notification() throws BitPayException {
236236
);
237237

238238
// when
239-
Boolean result = this.getTestedClass().sendRefundNotification("WoE46gSLkJQS48RJEiNw3L");
239+
Boolean result = this.getTestedClass().sendRefundNotification("WoE46gSLkJQS48RJEiNw3L", "cM78LHk17Q8fktDE6QLBBFfvH1QKBhRkHibTLcxhgzsu3VDRvSyu3CGi17DuwYxhT");
240240

241241
// then
242242
Assertions.assertTrue(result);

0 commit comments

Comments
 (0)