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);
}
  

 

转载于:https://www.cnblogs.com/dreammyone/p/8569276.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>