因为公司要接入 paypal 支付方式,自己捣鼓搞了好久,都没弄好。
本次使用的是最简单的按钮配置,支付方式配置其实支持有很多种。
账号注册
这个就不讲了可以参考:这篇最详细 如果不想现在过去就用鼠标右键点击打开新标签页。
支付方式对接选择。
一共有很多种,但是这里我选择的是最简单的一种。按钮操作。
沙盒测试
注册账户并生成沙盒账户及测试app
paypal 支付对接其实就是两个账号互相转账。
- 注册商家账户
- 注册个人账户
- 生成 app (个人用户和商家用户,发起支付请求都是同过app 来绑定的,据我估计应该就是 paypal 来绑定是那个机构发起的支付)
依旧参考这篇
最终结果要是这样 - 这里的沙盒账户最好自己创建两个,不要用系统自带的。注意一个是商业,一个是个人
按钮配置页
先选择按钮配置的 api 里面有教程。
其实这里官方文档讲的已经很详细了。官方文档
我的页面是
<head>
<meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Ensures optimal rendering on mobile devices. -->
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> <!-- Optimal Internet Explorer compatibility -->
</head>
<body>
<script
src="https://www.paypal.com/sdk/js?client-id=sb"> // Required. Replace SB_CLIENT_ID with your sandbox client ID. 官方文档已经说的很清楚了这里是app 的client ID 所以不要在傻不拉几的一直病急乱投医了。
</script>
<div id="paypal-button-container"></div>
<script>
paypal.Buttons({
createOrder: function(data, actions) {
// This function sets up the details of the transaction, including the amount and line item details.
return actions.order.create({
purchase_units: [{
amount: {
value: '100.02'
},
// invoice_id It can't be repeated, mean is : The API caller-provided external invoice number for this order. Appears in both the payer's transaction history and the emails that the payer receives.
"invoice_id":"CCCCCCCCCC" // 这里可以你自己定义,类似于商品的编号。不能重复,放心回调会返回的。
}]
});
},
// 支付成功后的函数
onApprove: function(data, actions) {
// This function captures the funds from the transaction.
return actions.order.capture().then(function(details) {
// This function shows a transaction success message to your buyer.
alert('Transaction completed by callback info' + details.payer.name.given_name);
alert('Transaction completed by callback info: ' + data.orderID);
});
},
// 支付取消后的函数
onCancel: function (data) {
console.log("Cancel payment!");
window.location.href= "https://www.baidu.com";
//cancel return url;
},
//支付失败的函数
onError: function (data) {
console.log("payment failed!");
window.location.href= "https://www.baidu.com";
// error return url;
}
}).render('#paypal-button-container');
//This function displays Smart Payment Buttons on your web page.
</script>
</body>
</html>
沙盒支付
打开页面点支付按钮,登录个人账户点击支付。如果账户里面还有钱的话。
什么?你还不知道账户在哪里 在这里
支付完成记得登录主账户查看钱有没有加。
沙盒IPN 回调设置
回调是异步的。
非常感谢这个博客 给我的启发
还有这位兄弟
先登录商家的沙盒用户 沙盒登录地址
注意:要是公网 ip 地址
然后再次发送支付请求,就会得到支付完后的请求体。
请求体里面是:
mc_gross=10.01&protection_eligibility=Eligible&address_status=confirmed&payer_id=UGP233DJP2RF6&address_street=NO+1+Nan+Jin+Road&payment_date=04%3A18%3A52+Jan+06%2C+2021+PST&payment_status=Completed&charset=gb2312&address_zip=200000&first_name=John&mc_fee=0.64&address_country_code=CN&address_name=Doe+John¬ify_version=3.9&custom=&payer_status=verified&business=sb-eksxx4542815%40business.example.com&address_country=China&address_city=Shanghai&quantity=1&verify_sign=A68859pDcJKhpisZpaMkuQEJHzSPATI19GwzSqQalG-4vkgJ2elYAp00&payer_email=sb-bs3404551534%40personal.example.com&txn_id=4GS170274N9213147&payment_type=instant&last_name=Doe&address_state=Shanghai&receiver_email=sb-eksxx4542815%40business.example.com&payment_fee=0.64&shipping_discount=0.00&insurance_amount=0.00&receiver_id=GZX82WGTLDAL6&txn_type=express_checkout&item_name=&discount=0.00&mc_currency=USD&item_number=&residence_country=CN&test_ipn=1&shipping_method=Default&transaction_subject=&payment_gross=10.01&ipn_track_id=38aa83d901e58
官方好像还要我们回复一下要校验是否交易成功。以下是官方要求。
收到通知后需要进行通知确认
为了确保付款已进入您的PayPal账户,您必须验证用作“receiver_email”的电子邮件地址是否已在您的PayPal账户中注册并得到确认。
服务器收到即时付款通知后,您将需要通过构建一个发送到PayPal的HTTP POST对其进行确认。您的POST应发送到https://www.paypal.com/cgi-bin/webscr
(当您收到时)。您还需要将一个值为“_notify-validate”的名为“cmd”变量(例如,cmd=_notify-validate)附加到POST字符串。
PayPal将回复该POST,并在回复的正文中包含一个单词“VERIFIED”或“INVALID”。当您收到VERIFIED回复时,您需要在实施订单之前执行若干检查:
确认“payment_status”为“Completed”,因为系统也会为其他结果(如“Pending”或“Failed”)发送 IPN。
检查“txn_id”是否未重复,以防止欺诈者重复使用旧的已完成的交易
验证“receiver_email”是已在您的PayPal账户中注册的电子邮件地址,以防止将付款发送到欺诈者的账户
检查其他交易详情(如物品号和价格),以确认价格未改变
完成了以上检查后,您可以使用 IPN 数据更新您的数据库,并处理购物。
如果收到“无效”通知,则应将其视为可疑通知,并应对其进行调查。
一般回调的步骤都是,
- 解析请求
- 找到官方接口访问发送过来的 txn_id 去查询支付状态,是否是支付成功
- 找到自己库里的支付记录
- 以上都符合,异步确认订单。
下篇写根据paypal交易号查询交易状态的操作。
扫描二维码关注公众号,回复:
14753194 查看本文章