两种方式
- client(调用集成)
- server(调用api)
- 参考官方链接
client
直接用
let purchase_units_amount = {
// value不能小于小数点后两卫
currency_code: "USD",
value: "0.06",
breakdown: {
item_total: { // 原价
value: "0.08",
currency_code: "USD"
},
discount: { // 折扣
value: "0.02",
currency_code: "USD"
},
shipping: { // 运费
value: "0.01",
currency_code: "USD"
},
shipping_discount: { // 运费优惠
value: "0.01",
currency_code: "USD"
}
}
}
let purchase_units_item_list = [
{
name: "NeoPhone",
sku: "sku03",
unit_amount: {
value: "0.04",
currency_code: "USD"
},
quantity: "1"
},
{
name: "Fitness Watch",
sku: "sku04",
unit_amount: {
value: "0.04",
currency_code: "USD"
},
quantity: "1"
}
]
onShippingChange: (data, actions) => {
console.log("onShippingChange", data, actions);
},
// 按钮第一次呈现时调用
onInit: () => {
console.log("onInit");
},
// 点击付款按钮时调用 通常用于表单验证
onClick: () => {
console.log("onClick");
return true;
},
createOrder: function (data, actions) {
console.log(data);
// This function sets up the details of the transaction, including the amount and line item details.
return actions.order.create({
purchase_units: [{
amount: purchase_units_amount, // 金额
items: purchase_units_item_list, // 商品
description: '剁手', // 说明
}],
//商品的配置
// application_context: {
// brand_name: "LilySilk", // 品牌名
// shipping_preference: "SET_PROVIDED_ADDRESS", // GET_FROM_FILE / NO_SHIPPING / SET_PROVIDED_ADDRESS
// user_action: "PAY_NOW"
// }
});
},
onApprove: function (data, actions) {
console.log(data);
// This function captures the funds from the transaction.
return actions.order.capture().then(function (details) {
console.log(details);
// This function shows a transaction success message to your buyer.
alert('Transaction completed by ' + details.payer.name.given_name);
});
},
onError: (err) => {
// 参考资料:https://developer.paypal.com/docs/api/orders/v2/#orders_patch
console.log("onError", err);
}
调用api
- 拿到token (client_id和secret创建账号获取)
curl -v https://api-m.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "client_id:secret" \
-d "grant_type=client_credentials"
- 发请求时带上
axios.interceptors.request.use((config) => {
config.headers.authorization = 'Bearer xxxxxxxxxxx'
return config;
});
请求方式按这样来,需要什么调对应的接口即可
createOrder: function (data, actions) {
console.log(data)
return axios({
url: `${origin}/v2/checkout/orders`,
headers: {
'Content-type': 'application/json',
},
method: 'post',
data: {
'purchase_units': [{
'amount': {
'currency_code': 'USD',
'value': '0.06',
},
}],
'intent': 'CAPTURE'
}
});
},