首先注册商户账户,这个属于非技术流程 按步骤操作就可以了,不细讲了
首先需要创建一个应用
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无关,不在此多讲了
上面是个人的嵌入流程,不对的地方欢迎指正
技术交流群:208779755
个人公众号:海涛聊技术