3535public 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}
0 commit comments