由于国内的支付方式都需要商家资质,沙盒环境都不能使用,因此在这里记录一下paypal的接入过程
前提准备
去开发环境获取clientId以及token
接着去curl生成一个产品

1 2 3 4 5 6 7 8 9 10 11
| curl -v -X POST https: -H "Content-Type: application/json" -H "Authorization: Bearer ACCESS-TOKEN" -H "PayPal-Request-Id: REQUEST-ID" -d '{ "name": "Video Streaming Service", "description": "A video streaming service", "type": "SERVICE", "category": "SOFTWARE", "image_url": "https://example.com/streaming.jpg", "home_url": "https://example.com/home" }'
|
这里curl完后会给一个token,方便后面的请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| { "id": "PROD-5FD60555F23244316", "name": "Video Streaming Service", "description": "A video streaming service", "create_time": "2023-01-21T16:04:39Z", "links": [ { "href": "https://api-m.sandbox.paypal.com/v1/catalogs/products/PROD-5FD60555F23244316", "rel": "self", "method": "GET" }, { "href": "https://api-m.sandbox.paypal.com/v1/catalogs/products/PROD-5FD60555F23244316", "rel": "edit", "method": "PATCH" } ] }
|
接着在去环境中curl生成一个计划表,这里可以理解成就是支付的前置页面,包含支付计划等
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| curl -v -k -X POST https: "product_id": "PROD-5FD60555F23244316", "name": "Basic Plan", "description": "Basic plan", "billing_cycles": [ { "frequency": { "interval_unit": "MONTH", "interval_count": 1 }, "tenure_type": "TRIAL", "sequence": 1, "total_cycles": 1 }, { "frequency": { "interval_unit": "MONTH", "interval_count": 1 }, "tenure_type": "REGULAR", "sequence": 2, "total_cycles": 12, "pricing_scheme": { "fixed_price": { "value": "10", "currency_code": "USD" } } } ], "payment_preferences": { "auto_bill_outstanding": true, "setup_fee": { "value": "10", "currency_code": "USD" }, "setup_fee_failure_action": "CONTINUE", "payment_failure_threshold": 3 }, "taxes": { "percentage": "10", "inclusive": false } }'
|
就比如上面的curl
- 有 1 个月的免费试用期,并可继续以 12 个月的固定价格订阅
- 包含 10 美元安装费
- 一个计费周期内收取所有未结余额
- 如果初始付款失败,则允许继续订阅
- 连续 3 次付款失败后暂停订阅
- 账单金额包含 10% 的税金
我们来看一下repsonse
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| { "id": "P-17M15335A8501272JLXLLNKI", "product_id": "PROD-5FD60555F23244316", "name": "Basic Plan", "status": "ACTIVE", "description": "Basic plan", "create_time": "2023-01-21T16:09:13Z", "links": [ { "href": "https://api-m.sandbox.paypal.com/v1/billing/plans/P-17M15335A8501272JLXLLNKI", "rel": "self", "method": "GET" }, { "href": "https://api-m.sandbox.paypal.com/v1/billing/plans/P-17M15335A8501272JLXLLNKI", "rel": "edit", "method": "PATCH" }, { "href": "https://api-m.sandbox.paypal.com/v1/billing/plans/P-17M15335A8501272JLXLLNKI/deactivate", "rel": "self", "method": "POST" } ] }
|
紧接着我们在前端生成一个支付页面
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <!DOCTYPE html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <script src="https://www.paypal.com/sdk/js?client-id=YOUR_CLIENT_ID&vault=true&intent=subscription"> </script> // Add your client_id <div id="paypal-button-container"></div> <script> paypal.Buttons({ createSubscription: function(data, actions) { return actions.subscription.create({ 'plan_id': 'YOUR_PLAN_ID' }); }, onApprove: function(data, actions) { alert('You have successfully subscribed to ' + data.subscriptionID); } }).render('#paypal-button-container'); </script> </body> </html>
|
以上的操作都需要modify一下clientId,和planId,总的来说官方文档写的很清晰,十分简单