最新版的PayPal快速集成方法(个人经验记录)


声明:本文转载自https://my.oschina.net/u/817581/blog/1631357,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

首先注册商户账户,这个属于非技术流程 按步骤操作就可以了,不细讲了

首先需要创建一个应用

https://developer.paypal.com/developer/applications/

在这里创建一个应用 然后可以获得

clientId 和 clientSecret ,在下面会用到,接下来是创建一个测试账号,同样是下面会用到

https://developer.paypal.com/developer/accounts/

下来来讲PayPal的代码嵌入:

有三种方式

1.Server Side Express Checkout using REST

2.Client Side Express Checkout using REST

3.Express Checkout using Braintree SDK

因为我们有现有的支付体系,只是想额外支持PayPal这种支付方式,所以我们就采用了第一种方式

先说一下页面部分的实现

<!DOCTYPE html>   <head>     <meta http-equiv="X-UA-Compatible" content="IE=edge" />     <meta name="viewport" content="width=device-width, initial-scale=1">     <script src="https://www.paypalobjects.com/api/checkout.js"></script>    </head>      <body>     <div id="paypal-button-container"></div>      <script>         paypal.Button.render({              env: 'sandbox', // sandbox | production              // Show the buyer a 'Pay Now' button in the checkout flow             commit: true,              // payment() is called when the button is clicked             payment: function() {                  // 你自己的订单生成地址,在这个接口里面 调用PayPal接口生成订单 同时 生成自己系统的订单                 var CREATE_URL = 'XXX/wapi/pay/paypalCreate';                  // Make a call to your server to set up the payment                 return paypal.request.post(CREATE_URL)                     .then(function(res) {                     	//这里是生成 PayPal订单后  PayPal的接口 返回的PayPal的 订单id                         return res.paymentID;                     });             },              // onAuthorize() is called when the buyer approves the payment             onAuthorize: function(data, actions) {                  // 用户在PayPal确认付款后的回调地址  换成自己的接口 订单执行(完成)接口 这里要调用 PayPal的 订单确认接口,同时把自己系统的订单变成支付完成状态                 //这里只讲 快速集成,不讲 如何保证数据的一致性 各个系统自己实现                 var EXECUTE_URL = 'XXXX/wapi/pay/paypalExecute';                  // Set up the data you need to pass to your server                 var data = {                     paymentID: data.paymentID,//订单id                     payerID: data.payerID//支付用户id                 };                  // Make a call to your server to execute the payment                 return paypal.request.post(EXECUTE_URL, data)                     .then(function (res) {                         window.alert('Payment Complete!');                     });             }          }, '#paypal-button-container');     </script>   </body> </html> 

到这里页面就实现完成了,其他业务逻辑可以自己补充,

接下来就是页面上用到的两个接口的实现

首先需要下载用到的jar包文件 :https://github.com/paypal/PayPal-Java-SDK/wiki/Installation 最新的文件下载地址,我使用的是

<dependency> 	<groupId>com.paypal.sdk</groupId> 	<artifactId>rest-api-sdk</artifactId> 	<version>LATEST</version> </dependency>

接下里是两个接口,这里用到一开始获得的 clientId 和 clientSecret

     /** 	 * paypal订单生成 	 * @return 	 */ 	@RequestMapping(value = "/paypalCreate",produces = MediaType.APPLICATION_JSON_VALUE) 	@ResponseBody 	public String paypalCreate(HttpServletRequest request, HttpServletResponse response) { 		APIContext apiContext = new APIContext(clientId, clientSecret, "sandbox");//上线记得改为正     式环境 		Payment payment = new Payment();//交易对象 		Payer payr = new Payer();//支付信息 		payr.setPaymentMethod("paypal");//支付方式 		payment.setPayer(payr); 		payment.setIntent("sale");//交易类型 		RedirectUrls redirectUrls = new RedirectUrls();//回跳信息 		redirectUrls.setCancelUrl("url");//交易取消回跳地址 		redirectUrls.setReturnUrl("url");//交易成功回跳地址 		payment.setRedirectUrls(redirectUrls ); 		 		List<Transaction> transactions = new ArrayList<Transaction>(); //商品信息 		Transaction transaction = new Transaction(); 		Amount amount = new Amount();//总计费用 		amount.setTotal("3.00"); 		amount.setCurrency("USD"); 		transaction.setAmount(amount ); 		transaction.setDescription("测试订单"); 		transaction.setInvoiceNumber("自己系统的订单号"); 		transactions.add(transaction); 		payment.setTransactions(transactions); 		String payId = null; 		try { 			Payment pay = payment.create(apiContext); 			payId = pay.getId(); 		} catch (PayPalRESTException e) { 			logger.error("paypal完成支付出现异常,异常信息:" + e.getMessage()); 		} 		Map result = new HashMap(); 		result.put("paymentID", payId); 		return JSON.toJSONString(result); 	}      /** 	 * paypal订单执行 	 * @return 	 */     @RequestMapping(value = "/paypalExecute") 	@ResponseBody 	public String paypalExecute(HttpServletRequest request, HttpServletResponse response) { 		String payId = request.getParameter("paymentID"); 		String payerId = request.getParameter("payerID"); 		APIContext apiContext = new APIContext(clientId, clientSecret, "sandbox"); 		PaymentExecution paymentExecution = new PaymentExecution(); 		paymentExecution.setPayerId(payerId);//付款人 		 		Payment pymnt = new Payment(); 		pymnt.setId(payId); 		String result = "failed"; 		try { 			Payment executedPayment = pymnt.execute(apiContext, paymentExecution); 			if("approved".equals(executedPayment.getState())){ 				result = "success"; 			} 		} catch (PayPalRESTException e) { 			// TODO Auto-generated catch block 			e.printStackTrace(); 		} 		Map resultM = new HashMap(); 		resultM.put("result", result); 		return JSON.toJSONString(resultM); 	}

上面是PayPal完成支付的逻辑,这两个接口里面应该补充自己系统的逻辑代码,直接省略了

到这里就完成了,PayPal的快速集成,如果想让自己的支付安全 还要考虑下数据一致性问题,和支付失败的回滚问题,这个就是自由支付系统的逻辑了 和 嵌入PayPal无关,不在此多讲了

上面是个人的嵌入流程,不对的地方欢迎指正

本文发表于2018年03月08日 22:45
(c)注:本文转载自https://my.oschina.net/u/817581/blog/1631357,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 2542 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

万稳万当,不如一默。任何一句话,你不说出来便是那句话的主人,你说了出来,便是那句话的奴隶。

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1