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

 

posted @ 2018-03-14 18:15  令狐る侠  阅读(3818)  评论(1编辑  收藏  举报