【发布时间】:2015-11-11 14:45:35
【问题描述】:
我正在使用 PayPal Express Checkout,我需要做的是稍后分别授权和捕获付款,我当前的 PayPal 流程是,
1) 我使用以下代码创建付款:
var paypal = require('paypal-rest-sdk');
function createPayPal(req, res, itemsArray, redirectsTos) {
var payment = {
"intent": "sale",
"payer": {},
"transactions": [{
"amount": {
"currency": sails.config.currency,
"total": itemsArray.totalArrayAmount,
"details": {
"subtotal": itemsArray.totalArrayAmount,
"fee": sails.config.PayPalCreateFee
}
},
"invoice_number": req.session.invNum,
"item_list": {
"items": itemsArray.itemsArray
}
}]
};
sails.log.info('payment obj :', JSON.stringify(payment))
payment.payer.payment_method = sails.config.PayPalPaymentMethod;
payment.redirect_urls = {
"return_url": res.locals.return_url_buy,
"cancel_url": res.locals.cancel_url_buy
};
paypal.payment.create(payment, function(error, payment) {
if (error) {
sails.log.error(error);
redirectsTos(({
message: 'failure',
redirect: '/paypal/error'
}), null);
} else {
sails.log.info('Payment ID = ', payment.id);
sails.log.info('User ID = ', req.session.userSession);
var redirectUrl;
for (var i = 0; i < payment.links.length; i++) {
var link = payment.links[i];
if (link.method === 'REDIRECT') {
redirectUrl = link.href;
sails.log.info('goto:', redirectUrl)
redirectsTos(null, ({
message: 'success',
redirect: redirectUrl
}));
}
}
}
});
}
Paypal 将订单信息和重定向 urls 返回给我,我将用户重定向到 links 对象中的 href。然后当支付流返回给我的网站时,它会发送给我
{
paymentId: 'PAY-5FB60654T5508144abcxyzZLQ',
token: 'EC-26U68825EW2123428',
PayerID: 'QSABTRW6AHYH6'
}
然后我使用以下代码执行付款。
function executePayPal(req, paymentId, payerId, executedPayPal) {
sails.log.info('in executedPayPal');
var details = {
"payer_id": payerId
};
var payment = paypal.payment.execute(paymentId, details, function(error, payment) {
if (error) {
sails.log.error('error in payment id in executePayPal function of paypal controller', error);
var err = JSON.stringify(error);
var errParsed = JSON.parse(err);
crashHandlingService.appCrash(errParsed, 101202);
executedPayPal(({
message: 'failure',
redirect: '/paypal/error/'
}), null);
} else {
executedPayPal(({
message: 'success',
redirect: '/paypal/success/'
}), null);
}
});
}
现在这段代码主要做的是
- 创建付款,
- 将用户重定向到贝宝页面 C
- 获取付款。
而我真正想要实现的是
- 授权支付->
-
捕获付款,以便稍后在某些 cronJob 或服务中捕获付款。
在上面的流程中间将用户重定向到paypal页面,我真的不知道如何授权,重定向然后捕获付款。
所以请在这方面指导我。
注意:我已阅读以下贝宝文档但无法理解。请记住,我需要在 paypal 页面上显示付款详情,并在付款页面上显示优惠券代码及其折扣。
https://developer.paypal.com/docs/integration/direct/capture-payment/#authorize-the-payment https://developer.paypal.com/docs/classic/express-checkout/ht_ec-singleAuthPayment-curl-etc/
提前致谢:)。
【问题讨论】:
-
我想我已经找到了解决方案,通过使用以下链接我可以做到但不确定,我要去测试它。 github.com/paypal/PayPal-node-SDK/blob/…