|
4 | 4 |
|
5 | 5 | package com.bitpay.sdk; |
6 | 6 |
|
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; |
8 | 37 | import com.bitpay.sdk.model.Bill.Bill; |
9 | 38 | import com.bitpay.sdk.model.Facade; |
10 | 39 | import com.bitpay.sdk.model.Invoice.Invoice; |
|
27 | 56 | import com.fasterxml.jackson.databind.JsonNode; |
28 | 57 | import com.fasterxml.jackson.databind.ObjectMapper; |
29 | 58 | 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; |
30 | 76 | import org.apache.http.HttpEntity; |
31 | 77 | import org.apache.http.HttpHost; |
32 | 78 | import org.apache.http.HttpResponse; |
|
45 | 91 | import org.apache.http.util.EntityUtils; |
46 | 92 | import org.bitcoinj.core.ECKey; |
47 | 93 |
|
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 | | - |
57 | 94 | /** |
58 | 95 | * The type Client. Class is responsible for API calls. |
59 | 96 | * |
@@ -562,51 +599,70 @@ public Invoice cancelInvoice(String invoiceId, Boolean forceCancel) throws Invoi |
562 | 599 | * @throws BitPayException BitPayException class |
563 | 600 | */ |
564 | 601 | 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 | | - if (invoiceId == null && amount == null) { |
568 | | - throw new RefundCreationException(null ,"Invoice ID, amount and currency are required to issue a refund."); |
569 | | - } |
570 | | - if (invoiceId != null) { |
571 | | - params.put("invoiceId", invoiceId); |
572 | | - } |
573 | | - if (amount != null) { |
574 | | - params.put("amount", amount); |
575 | | - } |
576 | | - if (preview != null) { |
577 | | - params.put("preview", preview); |
578 | | - } |
579 | | - if (immediate != null) { |
580 | | - params.put("immediate", immediate); |
581 | | - } |
582 | | - if (buyerPaysRefundFee != null) { |
583 | | - params.put("buyerPaysRefundFee", buyerPaysRefundFee); |
584 | | - } |
585 | | - if (reference != null) { |
586 | | - params.put("reference", reference); |
587 | | - } |
| 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); |
588 | 609 |
|
589 | | - Refund refund; |
590 | | - ObjectMapper mapper = new ObjectMapper(); |
| 610 | + final Map<String, Object> params = this.createBasicParamsForCreateRefund(refund); |
591 | 611 |
|
592 | | - String json; |
| 612 | + return createRefundByParams(params); |
| 613 | + } |
593 | 614 |
|
594 | | - try { |
595 | | - json = mapper.writeValueAsString(params); |
596 | | - } catch (JsonProcessingException e) { |
597 | | - throw new RefundCreationException(null ,"failed to serialize Refund object : " + e.getMessage()); |
598 | | - } |
| 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); |
599 | 647 |
|
600 | | - try { |
601 | | - HttpResponse response = this.post("refunds/", json, true); |
602 | | - refund = new ObjectMapper().readValue(this.responseToJsonString(response), Refund.class); |
603 | | - } catch (BitPayException ex) { |
604 | | - throw new RefundCreationException(ex.getStatusCode(), ex.getReasonPhrase()); |
605 | | - } catch (Exception e) { |
606 | | - throw new RefundCreationException(null, "failed to deserialize BitPay server response (Refund) : " + e.getMessage()); |
607 | | - } |
| 648 | + final Map<String, Object> params = this.createBasicParamsForCreateRefund(refund); |
608 | 649 |
|
609 | | - 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); |
610 | 666 | } |
611 | 667 |
|
612 | 668 | /** |
@@ -1960,6 +2016,69 @@ private void LoadAccessTokens() throws BitPayException { |
1960 | 2016 | } |
1961 | 2017 | } |
1962 | 2018 |
|
| 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 | + |
1963 | 2082 | /** |
1964 | 2083 | * Retrieve a token associated with a known resource. The token is used to access other related resources. |
1965 | 2084 | * |
|
0 commit comments