• paypal退款 java实现


    //直接上代码
    public class PaypalRefundConfig {
    
        private String clientId;
        private String clientSecret;
        private String endpointMode; // 
    
        public String getClientId() {
            return clientId;
        }
    
        public void setClientId(String clientId) {
            this.clientId = clientId;
        }
    
        public String getClientSecret() {
            return clientSecret;
        }
    
        public void setClientSecret(String clientSecret) {
            this.clientSecret = clientSecret;
        }
    
        public String getEndpointMode() {
            return endpointMode;
        }
    
        public void setEndpointMode(String endpointMode) {
            this.endpointMode = endpointMode;
        }
    
    }
    
    public class PaypalRefundRequest {
    
        private String     currency = "USD";
        private String     tradeNo;         // 交易号
        private String     saleId;          // Transaction ID
        private BigDecimal amount;          // 退款总金额
        private String     reason;          // 退款理由
    
        public String getTradeNo() {
            return tradeNo;
        }
    
        public void setTradeNo(String tradeNo) {
            this.tradeNo = tradeNo;
        }
    
        public String getSaleId() {
            return saleId;
        }
    
        public void setSaleId(String saleId) {
            this.saleId = saleId;
        }
    
        public String getCurrency() {
            return currency;
        }
    
        public void setCurrency(String currency) {
            this.currency = currency;
        }
    
        public BigDecimal getAmount() {
            return amount;
        }
    
        public void setAmount(BigDecimal amount) {
            this.amount = amount;
        }
    
        public String getReason() {
            return reason;
        }
    
        public void setReason(String reason) {
            this.reason = reason;
        }
    }
    //下面帮助类
    public class PaypalRefundHelper {
    
        public static PaypalRefundReturn refundSale(PaypalRefundConfig config, PaypalRefundRequest request) {
    
            APIContext apiContext = new APIContext(config.getClientId(), config.getClientSecret(), config.getEndpointMode());
    
            // ###Sale
            // A sale transaction.
            // Create a Sale object with the
            // given sale transaction id.
    
            // ###Refund
            // A refund transaction.
            // Use the amount to create
            // a refund object
            RefundRequest refund = new RefundRequest();
            // ###Amount
            // Create an Amount object to
            // represent the amount to be
            // refunded. Create the refund object, if the refund is partial
            Amount amount = new Amount();
            amount.setCurrency(request.getCurrency());
            amount.setTotal(PayUtil.format(request.getAmount()));
            refund.setAmount(amount);
            refund.setReason(request.getReason());
    
    
            try {
                if(request.getSaleId() == null) {
                   //注意这段代码,获取saleId
                    Payment payment = Payment.get(apiContext, request.getTradeNo());
                    Transaction transaction = payment.getTransactions().get(0);
                    RelatedResources resources = transaction.getRelatedResources().get(0);
                    String id = resources.getSale().getId();
                    request.setSaleId(id);
                }
    
                // ### Api Context
                // Pass in a `ApiContext` object to authenticate
                // the call and to send a unique request id
                // (that ensures idempotency). The SDK generates
                // a request id if you do not pass one explicitly.
    
                // Refund by posting to the APIService
                // using a valid AccessToken
                Sale sale = new Sale();
                sale.setId(request.getSaleId());
                DetailedRefund res = sale.refund(apiContext, refund);
                PaypalRefundReturn r = new PaypalRefundReturn();
                r.setState(res.getState());
                r.setAmount(res.getAmount().getTotal());
                r.setCurrency(res.getAmount().getCurrency());
                return r;
            } catch (PayPalRESTException e) {
                throw new IllegalStateException("failed to refund saleId=" + request.getTradeNo(), e);
            }
        }
    
    }
    //下面测试代码
    public static void main(String[] args) {
    
    //paypal退款很简单,只需要回去saleId就OK了(详见PaypalRefundHelper类)
      PaypalRefundConfig config = new PaypalRefundConfig();
            config.setClientId("******");
            config.setClientSecret("****");
            config.setEndpointMode("**");
    
            PaypalRefundRequest req = new PaypalRefundRequest();
            req.setCurrency("USD");
            req.setAmount(new BigDecimal("0.01"));//测试为0.01
            req.setReason("测试");
            //交易单号
            req.setTradeNo("121212");
            PaypalRefundReturn refund = PaypalRefundHelper.refundSale(config, req);
            System.out.println(refund);
    }
      
  • 相关阅读:
    ScrollVIEW 2000个ITEM不会卡
    嵌套ScrollView 左右滑动不影响上下滑动
    初学数据结构——栈和队列
    初学数据结构——单向循环链表和双向循环链表。
    初学数据结构——单链表
    bootstrap模态框垂直居中
    Javascript经典实例
    Javascript经典实例
    读书笔记-前言
    web中的中文字体的英文名称
  • 原文地址:https://www.cnblogs.com/dreammyone/p/8569276.html
Copyright © 2020-2023  润新知