我有一个Web应用程序,UI上的AngularJS和后端的NodeJS。
我想实现Paypal In-Context Checkout功能,但我真的不明白这样做的步骤是什么,文档非常混乱。
有人可以解释一下实现这一目标的步骤,前端需要做什么以及后端有什么需要。 感谢。
答案 0 :(得分:0)
有三种方法可以做到这一点
1你可以使用客户端休息。 2你可以使用服务器端休息。 3你可以使用braintree sdk。
第一步paypal有checkout.js。 第二步使用paypal rest api基本上是后端任务。
第一步和第二步的过程非常相似。const paypal = require('paypal-rest-sdk');
const payments = paypal.v1.payments;
let env = new paypal.core.SandboxEnvironment('app-id', 'secret');
let client = new paypal.core.PayPalHttpClient(env);
创建付款发布请求,如下所示
var create_payment_json = {
'intent': 'sale',
'payer': {
'payment_method': 'paypal'
},
'redirect_urls': {
'return_url':
'http://localhost:4150/test/paymentSuccess',
'cancel_url': 'http://cancel.url'
},
'transactions': [{
'item_list': {
'items': [
{
'name': 'item',
'sku': 'item',
'price': '1.00',
'currency': 'USD',
'quantity': 1
}
]
},
'amount': {
'currency': 'USD',
'total': '1.00'
},
'description': 'This is the payment'
}]
};
let request = new payments.PaymentCreateRequest();
requestt.requestBody(create_payment_json);
let mrrrr;
client.execute(requestt).then((response) => {
console.log(response.statusCode);
console.log(response.result);
_.map(response.result.links, (value) => {
if (value.rel === 'approval_url') {
// reply.redirect(response.result.links[i].href);
mrrrr = value.href;
console.log('mrrrr', mrrrr);
// return reply.redirect(mrrrr);
}
});
}).catch((error) => {
console.error(error.statusCode);
console.error(error.message);
});
创建付款后,您将获得网址,重定向该网址并使用PayPal进行付款,它将使用查询参数付款ID和payerId返回成功网址。
现在获得成功写下面的代码
console.log('this is query parameters', request.query);
const payerID = request.query.PayerID;
const paymentID = request.query.paymentId;
const execute_payment_json = {
'payer_id': payerID
};
let ww = new payments.PaymentExecuteRequest(paymentID);
ww.requestBody(execute_payment_json);
client.execute(ww).then((response) => {
console.log('success', response.statusCode);
console.log('success', response.result);
}).catch((error) => {
console.error(error.statusCode);
console.error(error.message);
});