Skip to content

Commit fb1a21f

Browse files
authored
Merge pull request #214 from mwarzybok-sumoheavy/feature/SP-418
SP-418 Java 9.0.0 Functional test suite
2 parents 4674eb9 + 1b160db commit fb1a21f

File tree

7 files changed

+520
-32
lines changed

7 files changed

+520
-32
lines changed

pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
<version>2.22.2</version>
130130
<configuration>
131131
<argLine>@{argLine} -Duser.timezone=UTC</argLine>
132+
<excludes>
133+
<exclude>**/integration/ClientIntegrationTest.java</exclude>
134+
</excludes>
132135
</configuration>
133136
</plugin>
134137
</plugins>

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public String getAccessToken(String key) throws BitPayException {
295295
* @return Map|null
296296
* @throws BitPayException the bit pay exception
297297
*/
298-
public Map getCurrencyInfo(String currencyCode) throws BitPayException {
298+
public Map<String, Object> getCurrencyInfo(String currencyCode) throws BitPayException {
299299
CurrencyClient client = new CurrencyClient(this.bitPayClient);
300300
return client.getCurrencyInfo(currencyCode);
301301
}
@@ -341,6 +341,19 @@ public Invoice getInvoice(String invoiceId) throws BitPayException {
341341
}
342342
}
343343

344+
/**
345+
* Retrieve a BitPay invoice by guid using the specified facade. The client must have been previously authorized for the specified facade.
346+
*
347+
* @param guid The guid of the invoice to retrieve.
348+
* @return A BitPay Invoice object.
349+
* @throws InvoiceQueryException InvoiceQueryException class
350+
*/
351+
public Invoice getInvoiceByGuid(String guid) throws InvoiceQueryException {
352+
Facade facade = getFacadeBasedOnAccessToken();
353+
boolean signRequest = isSignRequest(facade);
354+
return this.getInvoiceClient().getInvoiceByGuid(guid, facade, signRequest);
355+
}
356+
344357
/**
345358
* Retrieve a BitPay invoice by guid using the specified facade. The client must have been previously authorized for the specified facade.
346359
*
@@ -806,7 +819,7 @@ public String deliverBill(String billId, String billToken, boolean signRequest)
806819
* @since 8.8.0
807820
*/
808821
public Rate getRate(String baseCurrency, String currency) throws RateQueryException {
809-
return this.getRatesClient().getRate(baseCurrency, currency);
822+
return this.getRateClient().getRate(baseCurrency, currency);
810823
}
811824

812825
/**
@@ -829,7 +842,7 @@ public Rates getRates() throws RateQueryException {
829842
* @since 8.8.0
830843
*/
831844
public Rates getRates(String baseCurrency) throws RateQueryException {
832-
return this.getRatesClient().getRates(baseCurrency);
845+
return this.getRateClient().getRates(baseCurrency);
833846
}
834847

835848
/**

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

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
public class InvoiceClient {
3636

3737
private final BitPayClient bitPayClient;
38-
private AccessTokens accessTokens;
39-
private GuidGenerator guidGenerator;
38+
private final AccessTokens accessTokens;
39+
private final GuidGenerator guidGenerator;
4040

4141
/**
4242
* Instantiates a new Invoice client.
@@ -90,7 +90,8 @@ public Invoice createInvoice(Invoice invoice, Facade facade, Boolean signRequest
9090
} catch (BitPayException ex) {
9191
throw new InvoiceCreationException(ex.getStatusCode(), ex.getReasonPhrase());
9292
} catch (Exception e) {
93-
throw new InvoiceCreationException(null, "failed to deserialize BitPay server response (Invoice) : " + e.getMessage());
93+
throw new InvoiceCreationException(null,
94+
"failed to deserialize BitPay server response (Invoice) : " + e.getMessage());
9495
}
9596

9697
return invoice;
@@ -119,9 +120,11 @@ public Invoice getInvoice(String invoiceId, Facade facade, Boolean signRequest)
119120
} catch (BitPayException ex) {
120121
throw new InvoiceQueryException(ex.getStatusCode(), ex.getReasonPhrase());
121122
} catch (JsonProcessingException e) {
122-
throw new InvoiceQueryException(null, "failed to deserialize BitPay server response (Invoice) : " + e.getMessage());
123+
throw new InvoiceQueryException(null,
124+
"failed to deserialize BitPay server response (Invoice) : " + e.getMessage());
123125
} catch (Exception e) {
124-
throw new InvoiceQueryException(null, "failed to deserialize BitPay server response (Invoice) : " + e.getMessage());
126+
throw new InvoiceQueryException(null,
127+
"failed to deserialize BitPay server response (Invoice) : " + e.getMessage());
125128
}
126129

127130
return invoice;
@@ -151,9 +154,11 @@ public Invoice getInvoiceByGuid(String guid, Facade facade, Boolean signRequest)
151154
} catch (BitPayException ex) {
152155
throw new InvoiceQueryException(ex.getStatusCode(), ex.getReasonPhrase());
153156
} catch (JsonProcessingException e) {
154-
throw new InvoiceQueryException(null, "failed to deserialize BitPay server response (Invoice) : " + e.getMessage());
157+
throw new InvoiceQueryException(null,
158+
"failed to deserialize BitPay server response (Invoice) : " + e.getMessage());
155159
} catch (Exception e) {
156-
throw new InvoiceQueryException(null, "failed to deserialize BitPay server response (Invoice) : " + e.getMessage());
160+
throw new InvoiceQueryException(null,
161+
"failed to deserialize BitPay server response (Invoice) : " + e.getMessage());
157162
}
158163

159164
return invoice;
@@ -172,17 +177,18 @@ public Invoice getInvoiceByGuid(String guid, Facade facade, Boolean signRequest)
172177
* @throws BitPayException BitPayException class
173178
* @throws InvoiceQueryException InvoiceQueryException class
174179
*/
175-
public List<Invoice> getInvoices(String dateStart, String dateEnd, String status, String orderId, Integer limit, Integer offset) throws BitPayException, InvoiceQueryException {
180+
public List<Invoice> getInvoices(String dateStart, String dateEnd, String status, String orderId, Integer limit,
181+
Integer offset) throws BitPayException, InvoiceQueryException {
176182
if (Objects.isNull(dateStart) || Objects.isNull(dateEnd)) {
177183
throw new InvoiceQueryException(null, "missing required parameter");
178184
}
179185

180186
final List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
181187
ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT));
182-
ParameterAdder.execute(params,"dateStart", dateStart);
183-
ParameterAdder.execute(params,"dateEnd", dateEnd);
184-
ParameterAdder.execute(params,"orderId", orderId);
185-
ParameterAdder.execute(params,"status", status);
188+
ParameterAdder.execute(params, "dateStart", dateStart);
189+
ParameterAdder.execute(params, "dateEnd", dateEnd);
190+
ParameterAdder.execute(params, "orderId", orderId);
191+
ParameterAdder.execute(params, "status", status);
186192
if (Objects.nonNull(limit)) {
187193
ParameterAdder.execute(params, "limit", limit.toString());
188194
}
@@ -194,13 +200,16 @@ public List<Invoice> getInvoices(String dateStart, String dateEnd, String status
194200

195201
try {
196202
HttpResponse response = this.bitPayClient.get("invoices", params);
197-
invoices = Arrays.asList(new ObjectMapper().readValue(this.bitPayClient.responseToJsonString(response), Invoice[].class));
203+
invoices = Arrays.asList(
204+
new ObjectMapper().readValue(this.bitPayClient.responseToJsonString(response), Invoice[].class));
198205
} catch (BitPayException ex) {
199206
throw new InvoiceQueryException(ex.getStatusCode(), ex.getReasonPhrase());
200207
} catch (JsonProcessingException e) {
201-
throw new InvoiceQueryException(null, "failed to deserialize BitPay server response (Invoices) : " + e.getMessage());
208+
throw new InvoiceQueryException(null,
209+
"failed to deserialize BitPay server response (Invoices) : " + e.getMessage());
202210
} catch (Exception e) {
203-
throw new InvoiceQueryException(null, "failed to deserialize BitPay server response (Invoices) : " + e.getMessage());
211+
throw new InvoiceQueryException(null,
212+
"failed to deserialize BitPay server response (Invoices) : " + e.getMessage());
204213
}
205214

206215
return invoices;
@@ -219,13 +228,16 @@ public InvoiceEventToken getInvoiceEventToken(String invoiceId) throws BitPayExc
219228

220229
try {
221230
HttpResponse response = this.bitPayClient.get("invoices/" + invoiceId + "/events", params);
222-
return JsonMapperFactory.create().readValue(this.bitPayClient.responseToJsonString(response), InvoiceEventToken.class);
231+
return JsonMapperFactory.create()
232+
.readValue(this.bitPayClient.responseToJsonString(response), InvoiceEventToken.class);
223233
} catch (BitPayException ex) {
224234
throw new InvoiceQueryException(ex.getStatusCode(), ex.getReasonPhrase());
225235
} catch (JsonProcessingException e) {
226-
throw new InvoiceQueryException(null, "failed to deserialize BitPay server response (Invoices) : " + e.getMessage());
236+
throw new InvoiceQueryException(null,
237+
"failed to deserialize BitPay server response (Invoices) : " + e.getMessage());
227238
} catch (Exception e) {
228-
throw new InvoiceQueryException(null, "failed to deserialize BitPay server response (Invoices) : " + e.getMessage());
239+
throw new InvoiceQueryException(null,
240+
"failed to deserialize BitPay server response (Invoices) : " + e.getMessage());
229241
}
230242
}
231243

@@ -254,10 +266,9 @@ public Invoice updateInvoice(
254266

255267
final Map<String, Object> params = new HashMap<>();
256268
params.put("token", this.accessTokens.getAccessToken(Facade.MERCHANT));
257-
if (buyerSms == null && smsCode == null) {
258-
throw new InvoiceUpdateException(null,
259-
"Updating the invoice requires Mobile Phone Number for SMS reception.");
260-
}
269+
validateRequiredField(buyerSms, buyerEmail);
270+
validateSmsCode(buyerSms, smsCode, autoVerify);
271+
261272
if (buyerSms != null) {
262273
params.put("buyerSms", buyerSms);
263274
}
@@ -294,6 +305,27 @@ public Invoice updateInvoice(
294305
return invoice;
295306
}
296307

308+
private void validateSmsCode(String buyerSms, String smsCode, Boolean autoVerify) throws InvoiceUpdateException {
309+
if (Objects.isNull(autoVerify)) {
310+
return;
311+
}
312+
313+
if (autoVerify) {
314+
return;
315+
}
316+
317+
if (Objects.nonNull(buyerSms) && Objects.nonNull(smsCode)) {
318+
return;
319+
}
320+
321+
if (Objects.isNull(buyerSms) && Objects.isNull(smsCode)) {
322+
return;
323+
}
324+
325+
throw new InvoiceUpdateException(null,
326+
"If provided alongside a valid SMS, will bypass the need to complete an SMS challenge");
327+
}
328+
297329
/**
298330
* Pay a BitPay invoice with a mock transaction.
299331
*
@@ -321,7 +353,8 @@ public Invoice payInvoice(String invoiceId, String status) throws BitPayExceptio
321353

322354
try {
323355
HttpResponse response = this.bitPayClient.update("invoices/pay/" + invoiceId, json);
324-
invoice = JsonMapperFactory.create().readValue(this.bitPayClient.responseToJsonString(response), Invoice.class);
356+
invoice =
357+
JsonMapperFactory.create().readValue(this.bitPayClient.responseToJsonString(response), Invoice.class);
325358
} catch (BitPayException ex) {
326359
throw new InvoiceUpdateException(ex.getStatusCode(), ex.getReasonPhrase());
327360
} catch (Exception e) {
@@ -362,9 +395,9 @@ public Invoice cancelInvoice(String invoiceId) throws InvoiceCancellationExcepti
362395
public Invoice cancelInvoice(String invoiceId, Boolean forceCancel)
363396
throws InvoiceCancellationException, BitPayException {
364397
final List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
365-
ParameterAdder.execute(params,"token", this.accessTokens.getAccessToken(Facade.MERCHANT));
398+
ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT));
366399
if (forceCancel) {
367-
ParameterAdder.execute(params,"forceCancel", forceCancel.toString());
400+
ParameterAdder.execute(params, "forceCancel", forceCancel.toString());
368401
}
369402
Invoice invoice;
370403

@@ -387,9 +420,9 @@ public Invoice cancelInvoiceByGuid(String guid, Boolean forceCancel) throws BitP
387420
}
388421

389422
final List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
390-
ParameterAdder.execute(params,"token", this.accessTokens.getAccessToken(Facade.MERCHANT));
423+
ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT));
391424
if (forceCancel.equals(true)) {
392-
ParameterAdder.execute(params,"forceCancel", forceCancel.toString());
425+
ParameterAdder.execute(params, "forceCancel", forceCancel.toString());
393426
}
394427
Invoice invoice;
395428

@@ -427,4 +460,15 @@ public Boolean requestInvoiceWebhookToBeResent(String invoiceId) throws BitPayEx
427460
throw new BitPayException(null, "failed to process request : " + e.getMessage());
428461
}
429462
}
463+
464+
private void validateRequiredField(String buyerSms, String buyerEmail) throws InvoiceUpdateException {
465+
if (buyerSms == null && buyerEmail == null) {
466+
throw new InvoiceUpdateException(null,
467+
"Updating the invoice requires Mobile Phone Number for SMS reception.");
468+
}
469+
470+
if (Objects.nonNull(buyerSms) && Objects.nonNull(buyerEmail)) {
471+
throw new InvoiceUpdateException(null, "Updating an invoice will require EITHER an SMS or E-mail)");
472+
}
473+
}
430474
}

src/main/java/com/bitpay/sdk/model/Payout/PayoutRecipient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public class PayoutRecipient {
4949
*
5050
* @param email string Recipient email address to which the invite shall be sent.
5151
* @param label string Recipient nickname assigned by the merchant (Optional).
52-
* @param notificationURL string URL to which BitPay sends webhook notifications to inform the merchant about the status of a given recipient. HTTPS is mandatory (Optional).
52+
* @param notificationURL string URL to which BitPay sends webhook notifications to inform the merchant about the
53+
* status of a given recipient. HTTPS is mandatory (Optional).
5354
*/
5455
public PayoutRecipient(String email, String label, String notificationURL) {
5556
this.email = email;

src/setup/java/BitPaySetup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static void main(String[] args) {
8989
try {
9090
ObjectMapper mapper = new ObjectMapper();
9191

92-
client = Client.createClient(new PrivateKey(privateKey), new AccessTokens());
92+
client = new Client(env, new PrivateKey(privateKey), new AccessTokens(), null, null);
9393
pairingCodeMerchant = client.authorizeClient(Facade.MERCHANT);
9494
pairingCodePayout = client.authorizeClient(Facade.PAYOUT);
9595

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BitPay.config.json
2+
bitpay_private_prod.key
3+
email.txt

0 commit comments

Comments
 (0)