• 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);
    }
      
  • 相关阅读:
    HDU 4819 Mosaic --二维线段树(树套树)
    Codeforces Round #274 Div.1 C Riding in a Lift --DP
    ZOJ 3829 Known Notation --贪心+找规律
    JAVA成员变量和静态变量的区别
    spring注解
    js中double类型的数据加减的时候出错
    js在页面间传值的方法记录
    将表中数据转换成java entity实例
    最近都写APP的接口,有苦说不出啊.
    写于2018年第一场雪----记我的第一年工作
  • 原文地址:https://www.cnblogs.com/dreammyone/p/8569276.html
Copyright © 2020-2023  润新知