java paypal

作者:原创时间:2022-04-19
文档

java paypal是什么,让我们一起了解一下?

paypal支付的IPN和PDT两种通知方式,IPN异步通知,可能会有时延,但可靠性高,当接入方主机不可达时,有重试机制保证IPN通知尽量抵达接入方服务器。接入方收到IPN通知后,需要对其确认。

接入方收到IPN通知后,需要对其确认。确认方法为,把接收到的IPN通知原封不动的作为请求体,调用IPN确认接口。PDT通知是是实时的,但可靠性不高,因为只会通知一次,没有重试机制,一旦接入方出现主机不可达,这样的消息将会被丢失。官方推荐,IPN通知和PDT通知最好混合使用,以满足时效性和可靠性的保证。我们采用了IPN和PDT两种通知机制。

paypal流程有如下几步:

1、创建应用程序

2、启用与palPal的连接

3、应用评论

4、构建按钮

5、获取授权码

6、获取访问令牌

7、为access_token交换refresh_token

8、获取客户信息

9、测试集成

10、上线

通过springboot环境,java如下:

@Controller
@RequestMapping("/paypal")
public class PaymentController {
    private Logger log = LoggerFactory.getLogger(getClass());
 
    @Autowired
    private PaypalService paypalService;
 
    // 回调地址
    private static Integer orderId = null;
    private static String SUCCESS_URL = "https://www.baidu.com";
    private static String FAIL_URL = "http://www.163.com";
    private static String CANCEL_URL = "https://www.autohome.com.cn/shenzhen/?pvareaid=1001299";
 
    @RequestMapping(method = RequestMethod.GET)
    public String index(){
        return "index";
    }
 
   /**
     * 订单支付
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/payment")
    public String payment(HttpServletRequest request, @RequestBody PaymentEx paymentEx){
 
        if (null == paymentEx || null == paymentEx.getOrderId() || null == paymentEx.getOrderAddressEx() || null == paymentEx.getOrderItemExList()) {
            return "redirect:" + FAIL_URL;
        }
        orderId = paymentEx.getOrderId();
        SUCCESS_URL = paymentEx.getSuccessUrl();
        FAIL_URL = paymentEx.getFailUrl();
        CANCEL_URL = paymentEx.getCancelUrl();
        // 调用本地 successPay方法
        String successUrl = URLUtils.getBaseURl(request) + "/" + "paypal/successPay";
        // 调用本地 cancel方法
        String cancelUrl = URLUtils.getBaseURl(request) + "/" + "paypal/cancelPay";
        try {
            Payment payment = paypalService.createPayment(paymentEx, successUrl,cancelUrl);
            for(Links links : payment.getLinks()){
                if(links.getRel().equals("approval_url")){
                    // 客户付款登陆地址
                    return "redirect:" + links.getHref();
                }
            }
        } catch (PayPalRESTException e) {
            log.error(e.getMessage());
            return "redirect:" + FAIL_URL;
        }
        return "redirect:" + FAIL_URL;
    }
 
    /**
     * 取消支付
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/cancelPay")
    public String cancelPay(){
        return "redirect:" + CANCEL_URL;
    }
 
    /**
     * 支付成功
     * @param paymentId
     * @param payerId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/successPay")
    public String successPay(@RequestParam("paymentId") String paymentId, @RequestParam("PayerID") String payerId){
        try {
            /**
             * 支付
             */
            Payment payment = paypalService.executePayment(paymentId, payerId);
 
            /**
             * 保存支付信息
             */
            ShopOrderPayment orderPayment = new ShopOrderPayment();
            orderPayment.setOrderId(orderId);
            // 订单总价
           String total = payment.getTransactions().get(0).getAmount().getTotal();
            orderPayment.setAmountPaid(new BigDecimal(total));
            // 交易号
           String transationId = payment.getTransactions().get(0).getRelatedResources().get(0).getSale().getId();
            orderPayment.setTransactionId(transationId);
            paypalService.savePayment(orderPayment);
 
            /**
             * 支付成功重定向页面
             */
            if(payment.getState().equals("approved")){
                return "redirect:" + SUCCESS_URL;
            }
        } catch (PayPalRESTException e) {
            log.error(e.getMessage());
            return "redirect:" + FAIL_URL;
        }
        return "redirect:" + FAIL_URL;
    }
}

以上就是小编今天的分享了,希望可以帮助到大家。

显示全文
java phoenix java ping java pipeline java rsa公钥加密 java rsa私钥加密 java rtp java runnable java runtime.exec java rxjava java sandbox java script java selector java selenium java semaphore java separator java sequence java serializable java serializable接口 java serialversionuid java setlayout java path java rsa加密 java rotate java reverse java reverse() java retrofit java resume java resultset java result java rest java requests java partial java request java parser java repeat java parse java pandas java override java render java region