Skip to content

Commit 1f5e45c

Browse files
author
Pieter Poorthuis
committed
Removed deprecated BTC only fields, added multi currency fields
1 parent 0aac5e3 commit 1f5e45c

File tree

6 files changed

+117
-77
lines changed

6 files changed

+117
-77
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014-2015 BITPAY, INC.
3+
Copyright (c) 2014-2018 BITPAY, INC.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ To contribute to this project, please fork and submit a pull request.
6868

6969
The MIT License (MIT)
7070

71-
Copyright (c) 2014-2015 BitPay, Inc.
71+
Copyright (c) 2014-2018 BitPay, Inc.
7272

7373
Permission is hereby granted, free of charge, to any person obtaining a copy
7474
of this software and associated documentation files (the "Software"), to deal

src/controller/BitPay.java

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -433,40 +433,58 @@ public List<Invoice> getInvoices(String dateStart, String dateEnd) throws BitPay
433433

434434
return invoices;
435435
}
436+
/**
437+
* Checks whether a BitPay invoice has been paid in full.
438+
* Returns true if the amountPaid >= paymentTotals, returns false otherwise
439+
*
440+
* @param Invoice A Bitpay invoice object
441+
* @return true if the amountPaid >= paymentTotals, returns false otherwise
442+
*/
443+
444+
public boolean isFullyPaid(Invoice invoice) {
445+
long amountPaid = invoice.getAmountPaid();
446+
String transactionCurrency = invoice.getTransactionCurrency();
447+
// first check if invoice has a transactionCurrency. If not, this means the invoice has not been paid
448+
if (transactionCurrency == null){
449+
return false;
450+
}
451+
Hashtable <String, Long> paymentTotals = invoice.getPaymentTotals();
452+
if (amountPaid < paymentTotals.get(transactionCurrency)){
453+
return false;
454+
}
455+
return true;
456+
}
436457

437458
/**
438459
* Request a full refund for a BitPay invoice. The invoice full price and currency type are used in the request.
439460
*
440-
* @param invoiceId The id of the BitPay invoice for which a refund request should be made.
441-
* @param bitcoinAddress The bitcoin address to which the refund should will be made. If left empty ("") and the invoice contains a refund address then the request may success, otherwise it will fail.
461+
* @param invoiceId The id of the BitPay invoice for which a refund request should be made.
462+
* @param refundEmail The email of the buyer to which the refund email will be sent
442463
* @return A BitPay RefundRequest object with the new Refund object.
443464
* @throws BitPayException
444465
*/
445-
public RefundHelper requestRefund(String invoiceId, String bitcoinAddress) throws BitPayException {
466+
public RefundHelper requestRefund(String invoiceId, String refundEmail) throws BitPayException {
446467
Invoice invoice = this.getInvoice(invoiceId, this.getAccessToken(FACADE_MERCHANT));
447-
return this.requestRefund(invoice, bitcoinAddress, invoice.getPrice(), invoice.getCurrency());
468+
return this.requestRefund(invoice, refundEmail, invoice.getPrice(), invoice.getCurrency());
448469
}
449470

450471
/**
451472
* Request a refund for a BitPay invoice.
452473
*
453474
* @param invoice A BitPay invoice object for which a refund request should be made. Must have been obtained using the merchant facade.
454-
* @param bitcoinAddress The bitcoin address to which the refund should will be made. If left empty ("") and the invoice contains a refund address then the request may success, otherwise it will fail.
475+
* @param refundEmail The email of the buyer to which the refund email will be sent
455476
* @param amount The amount of money to refund. If zero then a request for 100% of the invoice value is created.
456477
* @param currency The three digit currency code specifying the exchange rate to use when calculating the refund bitcoin amount. If this value is "BTC" then no exchange rate calculation is performed.
457478
* @return A BitPay RefundRequest object with the new Refund object.
458479
* @throws BitPayException
459480
*/
460-
public RefundHelper requestRefund(Invoice invoice, String bitcoinAddress, Double amount, String currency) throws BitPayException {
461-
if (bitcoinAddress == null && !invoice.getFlags().getRefundable()) {
462-
throw new BitPayException("Error - cannot refund an invoice without a refund address");
463-
}
464-
481+
public RefundHelper requestRefund(Invoice invoice, String refundEmail, Double amount, String currency) throws BitPayException {
482+
465483
Refund refund = new Refund();
466484
refund.setToken(invoice.getToken());
467485
refund.setGuid(this.getGuid());
468486
refund.setAmount(amount);
469-
refund.setBitcoinAddress(bitcoinAddress);
487+
refund.setRefundEmail(refundEmail);
470488
refund.setCurrency(currency);
471489

472490
ObjectMapper mapper = new ObjectMapper();
@@ -489,7 +507,13 @@ public RefundHelper requestRefund(Invoice invoice, String bitcoinAddress, Double
489507
throw new BitPayException("Error - failed to deserialize BitPay server response (Refund) : " + e.getMessage());
490508
}
491509

492-
this.cacheAccessToken(refund.getId(), refund.getToken());
510+
try {
511+
this.cacheAccessToken(refund.getId(), refund.getToken());
512+
} catch (java.lang.NullPointerException e) {
513+
// When using the email address to refund, BitPay does not instantly return a request id
514+
// Instead BitPay returns 'success':'true'.
515+
// BitPay only returns a supportRequestId when the customer has entered his BTC/BCH refund address
516+
}
493517
return new RefundHelper(refund, invoice);
494518
}
495519

src/model/Invoice.java

Lines changed: 55 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,20 @@ public class Invoice {
3939
private String _id;
4040
private String _url;
4141
private String _status;
42-
private String _btcPrice;
4342
private String _invoiceTime;
4443
private long _expirationTime;
4544
private long _currentTime;
46-
private String _btcPaid;
47-
private String _btcDue;
4845
private List<InvoiceTransaction> _transactions;
49-
private String _rate;
50-
private Hashtable<String, String> _exRates;
5146
private String _exceptionStatus;
5247
private InvoicePaymentUrls _paymentUrls = new InvoicePaymentUrls();
53-
private InvoiceFlags _flags = new InvoiceFlags();
5448
private boolean _extendedNotifications = false;
49+
50+
private String _transactionCurrency;
51+
private long _amountPaid;
52+
private Hashtable<String, Hashtable <String, String> > _exchangeRates;
53+
private Hashtable<String, Long> _paymentTotals;
54+
private Hashtable<String, Long> _paymentSubtotals;
55+
5556

5657
/**
5758
* Constructor, create an empty Invoice object.
@@ -290,16 +291,6 @@ public void setStatus(String _status) {
290291
this._status = _status;
291292
}
292293

293-
@JsonIgnore
294-
public String getBtcPrice() {
295-
return _btcPrice;
296-
}
297-
298-
@JsonProperty("btcPrice")
299-
public void setBtcPrice(String _btcPrice) {
300-
this._btcPrice = _btcPrice;
301-
}
302-
303294
@JsonIgnore
304295
public String getInvoiceTime() {
305296
return _invoiceTime;
@@ -330,26 +321,6 @@ public void setCurrentTime(long _currentTime) {
330321
this._currentTime = _currentTime;
331322
}
332323

333-
@JsonIgnore
334-
public String getBtcPaid() {
335-
return _btcPaid;
336-
}
337-
338-
@JsonProperty("btcPaid")
339-
public void setBtcPaid(String _btcPaid) {
340-
this._btcPaid = _btcPaid;
341-
}
342-
343-
@JsonIgnore
344-
public String getBtcDue() {
345-
return _btcDue;
346-
}
347-
348-
@JsonProperty("btcDue")
349-
public void setBtcDue(String _btcDue) {
350-
this._btcDue = _btcDue;
351-
}
352-
353324
@JsonIgnore
354325
public List<InvoiceTransaction> getTransactions() {
355326
return _transactions;
@@ -360,26 +331,6 @@ public void setTransactions(List<InvoiceTransaction> _transactions) {
360331
this._transactions = _transactions;
361332
}
362333

363-
@JsonIgnore
364-
public String getRate() {
365-
return _rate;
366-
}
367-
368-
@JsonProperty("rate")
369-
public void setRate(String _rate) {
370-
this._rate = _rate;
371-
}
372-
373-
@JsonIgnore
374-
public Hashtable<String, String> getExRates() {
375-
return _exRates;
376-
}
377-
378-
@JsonProperty("exRates")
379-
public void setExRates(Hashtable<String, String> _exRates) {
380-
this._exRates = _exRates;
381-
}
382-
383334
@JsonIgnore
384335
public String getExceptionStatus() {
385336
return _exceptionStatus;
@@ -399,15 +350,56 @@ public InvoicePaymentUrls getPaymentUrls() {
399350
public void setPaymentUrls(InvoicePaymentUrls _paymentUrls) {
400351
this._paymentUrls = _paymentUrls;
401352
}
402-
403-
@JsonIgnore
404-
public InvoiceFlags getFlags() {
405-
return _flags;
353+
354+
@JsonIgnore
355+
public String getTransactionCurrency() {
356+
return _transactionCurrency;
406357
}
407358

408-
@JsonProperty("flags")
409-
public void setFlags(InvoiceFlags _flags) {
410-
this._flags = _flags;
359+
@JsonProperty("transactionCurrency")
360+
public void setTransactionCurrency(String _transactionCurrency) {
361+
this._transactionCurrency = _transactionCurrency;
411362
}
363+
364+
@JsonIgnore
365+
public long getAmountPaid() {
366+
return _amountPaid;
367+
}
368+
369+
@JsonProperty("amountPaid")
370+
public void setAmountPaid(long _amountPaid) {
371+
this._amountPaid = _amountPaid;
372+
}
373+
374+
@JsonIgnore
375+
public Hashtable<String, Hashtable <String, String> > getExchangeRates() {
376+
return _exchangeRates;
377+
}
378+
379+
@JsonProperty("exchangeRates")
380+
public void setExchangeRates(Hashtable<String, Hashtable <String, String> > _exchangeRates) {
381+
this._exchangeRates = _exchangeRates;
382+
}
383+
384+
@JsonIgnore
385+
public Hashtable<String, Long> getPaymentTotals() {
386+
return _paymentTotals;
387+
}
388+
389+
@JsonProperty("paymentTotals")
390+
public void setPaymentTotals(Hashtable<String, Long> _paymentTotals) {
391+
this._paymentTotals = _paymentTotals;
392+
}
393+
394+
@JsonIgnore
395+
public Hashtable<String, Long> getPaymentSubtotals() {
396+
return _paymentSubtotals;
397+
}
398+
399+
@JsonProperty("paymentSubtotals")
400+
public void setPaymentSubtotals(Hashtable<String, Long> _paymentSubtotals) {
401+
this._paymentSubtotals = _paymentSubtotals;
402+
}
403+
412404

413405
}

src/model/Refund.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class Refund {
1313

1414
private Double _amount = 0.0;
1515
private String _bitcoinAddress = "";
16+
private String _refundEmail = "";
1617
private String _currency = "";
1718

1819
private String _id;
@@ -91,6 +92,18 @@ public void setBitcoinAddress(String bitcoinAddress) {
9192
this._bitcoinAddress = bitcoinAddress;
9293
}
9394

95+
@JsonProperty("refundEmail")
96+
public String getRefundEmail() {
97+
return _refundEmail;
98+
}
99+
100+
@JsonProperty("refundEmail")
101+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
102+
public void setRefundEmail(String refundEmail) {
103+
this._refundEmail = refundEmail;
104+
}
105+
106+
94107
@JsonProperty("currency")
95108
public String getCurrency() {
96109
return _currency;

src/model/RefundParams.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class RefundParams {
1010
private String _currency = "";
1111
private Double _amount = 0.0;
1212
private String _bitcoinAddress = "";
13+
private String _refundEmail = "";
1314
private String _invoiceId = "";
1415

1516
public RefundParams() {}
@@ -64,6 +65,16 @@ public void setBitcoinAddress(String bitcoinAddress) {
6465
this._bitcoinAddress = bitcoinAddress;
6566
}
6667

68+
@JsonIgnore
69+
public String getRefundEmail() {
70+
return _refundEmail;
71+
}
72+
73+
@JsonProperty("refundEmail")
74+
public void setRefundEmail(String refundEmail) {
75+
this._refundEmail = refundEmail;
76+
}
77+
6778
@JsonIgnore
6879
public String getInvoiceId() {
6980
return _invoiceId;

0 commit comments

Comments
 (0)