Skip to content

Commit 0ad8239

Browse files
SP-349 Java - Updates for POST /refunds
1 parent 95c4f2d commit 0ad8239

File tree

1 file changed

+169
-51
lines changed

1 file changed

+169
-51
lines changed

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

Lines changed: 169 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,36 @@
44

55
package com.bitpay.sdk;
66

7-
import com.bitpay.sdk.exceptions.*;
7+
import com.bitpay.sdk.exceptions.BillCreationException;
8+
import com.bitpay.sdk.exceptions.BillDeliveryException;
9+
import com.bitpay.sdk.exceptions.BillQueryException;
10+
import com.bitpay.sdk.exceptions.BillUpdateException;
11+
import com.bitpay.sdk.exceptions.BitPayException;
12+
import com.bitpay.sdk.exceptions.InvoiceCancellationException;
13+
import com.bitpay.sdk.exceptions.InvoiceCreationException;
14+
import com.bitpay.sdk.exceptions.InvoiceQueryException;
15+
import com.bitpay.sdk.exceptions.InvoiceUpdateException;
16+
import com.bitpay.sdk.exceptions.LedgerQueryException;
17+
import com.bitpay.sdk.exceptions.PayoutBatchCancellationException;
18+
import com.bitpay.sdk.exceptions.PayoutBatchCreationException;
19+
import com.bitpay.sdk.exceptions.PayoutBatchNotificationException;
20+
import com.bitpay.sdk.exceptions.PayoutBatchQueryException;
21+
import com.bitpay.sdk.exceptions.PayoutCancellationException;
22+
import com.bitpay.sdk.exceptions.PayoutCreationException;
23+
import com.bitpay.sdk.exceptions.PayoutNotificationException;
24+
import com.bitpay.sdk.exceptions.PayoutQueryException;
25+
import com.bitpay.sdk.exceptions.PayoutRecipientCancellationException;
26+
import com.bitpay.sdk.exceptions.PayoutRecipientCreationException;
27+
import com.bitpay.sdk.exceptions.PayoutRecipientNotificationException;
28+
import com.bitpay.sdk.exceptions.PayoutRecipientQueryException;
29+
import com.bitpay.sdk.exceptions.PayoutRecipientUpdateException;
30+
import com.bitpay.sdk.exceptions.RateQueryException;
31+
import com.bitpay.sdk.exceptions.RefundCancellationException;
32+
import com.bitpay.sdk.exceptions.RefundCreationException;
33+
import com.bitpay.sdk.exceptions.RefundQueryException;
34+
import com.bitpay.sdk.exceptions.RefundUpdateException;
35+
import com.bitpay.sdk.exceptions.SettlementQueryException;
36+
import com.bitpay.sdk.exceptions.WalletQueryException;
837
import com.bitpay.sdk.model.Bill.Bill;
938
import com.bitpay.sdk.model.Facade;
1039
import com.bitpay.sdk.model.Invoice.Invoice;
@@ -27,6 +56,23 @@
2756
import com.fasterxml.jackson.databind.JsonNode;
2857
import com.fasterxml.jackson.databind.ObjectMapper;
2958
import com.fasterxml.jackson.databind.node.ObjectNode;
59+
import java.io.File;
60+
import java.io.UnsupportedEncodingException;
61+
import java.net.URI;
62+
import java.net.URISyntaxException;
63+
import java.nio.charset.StandardCharsets;
64+
import java.nio.file.Files;
65+
import java.nio.file.Paths;
66+
import java.util.ArrayList;
67+
import java.util.Arrays;
68+
import java.util.HashMap;
69+
import java.util.Hashtable;
70+
import java.util.Iterator;
71+
import java.util.List;
72+
import java.util.Locale;
73+
import java.util.Map;
74+
import java.util.Objects;
75+
import java.util.UUID;
3076
import org.apache.http.HttpEntity;
3177
import org.apache.http.HttpHost;
3278
import org.apache.http.HttpResponse;
@@ -45,15 +91,6 @@
4591
import org.apache.http.util.EntityUtils;
4692
import org.bitcoinj.core.ECKey;
4793

48-
import java.io.File;
49-
import java.io.UnsupportedEncodingException;
50-
import java.net.URI;
51-
import java.net.URISyntaxException;
52-
import java.nio.charset.StandardCharsets;
53-
import java.nio.file.Files;
54-
import java.nio.file.Paths;
55-
import java.util.*;
56-
5794
/**
5895
* The type Client. Class is responsible for API calls.
5996
*
@@ -562,52 +599,70 @@ public Invoice cancelInvoice(String invoiceId, Boolean forceCancel) throws Invoi
562599
* @throws BitPayException BitPayException class
563600
*/
564601
public Refund createRefund(String invoiceId, Double amount, Boolean preview, Boolean immediate, Boolean buyerPaysRefundFee, String reference) throws RefundCreationException, BitPayException {
565-
final Map<String, Object> params = new HashMap<>();
566-
params.put("token", this.getAccessToken(Facade.Merchant));
567-
params.put("guid", this.getGuid());
568-
if (invoiceId == null && amount == null) {
569-
throw new RefundCreationException(null ,"Invoice ID, amount and currency are required to issue a refund.");
570-
}
571-
if (invoiceId != null) {
572-
params.put("invoiceId", invoiceId);
573-
}
574-
if (amount != null) {
575-
params.put("amount", amount);
576-
}
577-
if (preview != null) {
578-
params.put("preview", preview);
579-
}
580-
if (immediate != null) {
581-
params.put("immediate", immediate);
582-
}
583-
if (buyerPaysRefundFee != null) {
584-
params.put("buyerPaysRefundFee", buyerPaysRefundFee);
585-
}
586-
if (reference != null) {
587-
params.put("reference", reference);
588-
}
602+
Refund refund = new Refund();
603+
refund.setInvoice(invoiceId);
604+
refund.setAmount(amount);
605+
refund.setPreview(preview);
606+
refund.setImmediate(immediate);
607+
refund.setBuyerPaysRefundFee(buyerPaysRefundFee);
608+
refund.setReference(reference);
589609

590-
Refund refund;
591-
ObjectMapper mapper = new ObjectMapper();
610+
final Map<String, Object> params = this.createBasicParamsForCreateRefund(refund);
592611

593-
String json;
612+
return createRefundByParams(params);
613+
}
594614

595-
try {
596-
json = mapper.writeValueAsString(params);
597-
} catch (JsonProcessingException e) {
598-
throw new RefundCreationException(null ,"failed to serialize Refund object : " + e.getMessage());
599-
}
615+
/**
616+
* Create a refund for a BitPay invoice.
617+
*
618+
* @param invoiceId The BitPay invoice Id having the associated refund to be created.
619+
* @param amount Amount to be refunded in the currency indicated.
620+
* @param preview Whether to create the refund request as a preview (which will not be acted on until status is updated)
621+
* @param immediate Whether funds should be removed from merchant ledger immediately on submission or at time of processing
622+
* @param buyerPaysRefundFee Whether the buyer should pay the refund fee (default is merchant)
623+
* @param reference Present only if specified. Used as reference label for the refund. Max str length = 100
624+
* @param guid Guid
625+
* @return An updated Refund Object
626+
* @throws RefundCreationException RefundCreationException class
627+
* @throws BitPayException BitPayException class
628+
* @since 8.7.0
629+
*/
630+
public Refund createRefund(
631+
String invoiceId,
632+
Double amount,
633+
Boolean preview,
634+
Boolean immediate,
635+
Boolean buyerPaysRefundFee,
636+
String reference,
637+
String guid
638+
) throws RefundCreationException, BitPayException {
639+
Refund refund = new Refund();
640+
refund.setInvoice(invoiceId);
641+
refund.setAmount(amount);
642+
refund.setPreview(preview);
643+
refund.setImmediate(immediate);
644+
refund.setBuyerPaysRefundFee(buyerPaysRefundFee);
645+
refund.setReference(reference);
646+
refund.setGuid(guid);
600647

601-
try {
602-
HttpResponse response = this.post("refunds/", json, true);
603-
refund = new ObjectMapper().readValue(this.responseToJsonString(response), Refund.class);
604-
} catch (BitPayException ex) {
605-
throw new RefundCreationException(ex.getStatusCode(), ex.getReasonPhrase());
606-
} catch (Exception e) {
607-
throw new RefundCreationException(null, "failed to deserialize BitPay server response (Refund) : " + e.getMessage());
608-
}
648+
final Map<String, Object> params = this.createBasicParamsForCreateRefund(refund);
609649

610-
return refund;
650+
return createRefundByParams(params);
651+
}
652+
653+
/**
654+
* Create a refund for a BitPay invoice.
655+
*
656+
* @param refund Refund class which provided data - invoice id, amount, preview, immediate, buyerPaysRefundFee, reference and guid for create request
657+
* @return An updated Refund Object
658+
* @throws RefundCreationException RefundCreationException class
659+
* @throws BitPayException BitPayException class
660+
* @since 8.7.0
661+
*/
662+
public Refund createRefund(Refund refund) throws
663+
RefundCreationException, BitPayException {
664+
final Map<String, Object> params = this.createBasicParamsForCreateRefund(refund);
665+
return createRefundByParams(params);
611666
}
612667

613668
/**
@@ -1961,6 +2016,69 @@ private void LoadAccessTokens() throws BitPayException {
19612016
}
19622017
}
19632018

2019+
private Map<String, Object> createBasicParamsForCreateRefund(Refund refund) throws BitPayException {
2020+
String guid = Objects.isNull(refund.getGuid()) ? this.getGuid() : refund.getGuid();
2021+
String invoiceId = refund.getInvoice();
2022+
Double amount = refund.getAmount();
2023+
Boolean preview = refund.getPreview();
2024+
Boolean immediate = refund.getImmediate();
2025+
Boolean buyerPaysRefundFee = refund.getBuyerPaysRefundFee();
2026+
String reference = refund.getReference();
2027+
2028+
if (invoiceId == null && amount == null) {
2029+
throw new RefundCreationException(null, "Invoice ID, amount and currency are required to issue a refund.");
2030+
}
2031+
2032+
final Map<String, Object> params = new HashMap<>();
2033+
params.put("token", this.getAccessToken(Facade.Merchant));
2034+
if (invoiceId != null) {
2035+
params.put("invoiceId", invoiceId);
2036+
}
2037+
if (amount != null) {
2038+
params.put("amount", amount);
2039+
}
2040+
if (preview != null) {
2041+
params.put("preview", preview);
2042+
}
2043+
if (immediate != null) {
2044+
params.put("immediate", immediate);
2045+
}
2046+
if (buyerPaysRefundFee != null) {
2047+
params.put("buyerPaysRefundFee", buyerPaysRefundFee);
2048+
}
2049+
if (reference != null) {
2050+
params.put("reference", reference);
2051+
}
2052+
params.put("guid", guid);
2053+
2054+
return params;
2055+
}
2056+
2057+
private Refund createRefundByParams(Map<String, Object> params) throws RefundCreationException {
2058+
Refund refund;
2059+
ObjectMapper mapper = new ObjectMapper();
2060+
2061+
String json;
2062+
2063+
try {
2064+
json = mapper.writeValueAsString(params);
2065+
} catch (JsonProcessingException e) {
2066+
throw new RefundCreationException(null, "failed to serialize Refund object : " + e.getMessage());
2067+
}
2068+
2069+
try {
2070+
HttpResponse response = this.post("refunds/", json, true);
2071+
refund = new ObjectMapper().readValue(this.responseToJsonString(response), Refund.class);
2072+
} catch (BitPayException ex) {
2073+
throw new RefundCreationException(ex.getStatusCode(), ex.getReasonPhrase());
2074+
} catch (Exception e) {
2075+
throw new RefundCreationException(null,
2076+
"failed to deserialize BitPay server response (Refund) : " + e.getMessage());
2077+
}
2078+
2079+
return refund;
2080+
}
2081+
19642082
/**
19652083
* Retrieve a token associated with a known resource. The token is used to access other related resources.
19662084
*

0 commit comments

Comments
 (0)