我想向我的客户销售功能/功能,支付应该是即时的,以便在交易完成时立即解锁功能。
我设法执行交易,沙箱个人帐户显示该交易,但沙盒商家帐户没有显示任何批准它的内容。沙箱个人帐户显示“这是一项临时授权,以确保您的付款方式将涵盖付款。当jawad商家的测试商店完成您的订单时,将收取您的付款方式。”
这是代码:
var express = require('express');
const router = express.Router();
var paypal = require('paypal-rest-sdk');
paypal.configure({
'mode': 'sandbox', //sandbox or live
'client_id': 'client id',
'client_secret': 'client secret'
});
// payment process
router.use('/buy', (req, res) => {
// payment object
var payment = {
"intent": "authorize",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://localhost:4040/xyz/paypal/success",
"cancel_url": "http://localhost:4040/xyz/paypal/err"
},
"transactions": [{
"item_list": {
"items": [{
"name": 'item1',
"sku": "item",
"price": '39',
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"total": 39.00,
"currency": "USD"
},
"description": "A feature "
}]
}
// calling the create Pay method
createPay(payment)
.then((transaction) => {
var id = transaction.id;
var links = transaction.links;
var counter = links.length;
while (counter--) {
if (links[counter].method == 'REDIRECT') {
// redirecting to paypal where user approves the transaction
return res.redirect(links[counter].href)
}
}
})
.catch((err) => {
console.log(err);
res.redirect('/err');
});
});
// success page
router.use('/success', (req, res) => {
var paymentId = req.query.paymentId;
var payerId = { 'payer_id': req.query.PayerID };
paypal.payment.execute(paymentId, payerId, function(error, payment){
if(error){
console.error(error);
} else {
if (payment.state === 'approved'){
res.send('payment completed successfully');
console.log(payment);
} else {
res.send('payment not successful');
}
}
});
})
// error page
router.use('/err', (req, res) => {
console.log(req.query);
res.redirect('https://soundcloud.com/');
})
// helper functions
var createPay = (payment) => {
return new Promise((resolve, reject) => {
paypal.payment.create(payment, function (err, payment) {
if (err) {
reject(err);
}
else {
resolve(payment);
console.log(payment)
}
});
});
}
module.exports = router;
帮助我以下内容:
先感谢您。我希望这段代码也可以帮助别人
本文档显示了按钮代码中no_shipping字段的使用:
https://developer.paypal.com/docs/checkout/how-to/customize-flow/#pass-experience-profile-options
要直接调用API,您必须创建一个包含此字段的Web体验配置文件对象(input_fields.no_shipping):
https://developer.paypal.com/docs/api/payment-experience/v1/#web-profiles_create
然后在您的/付款电话(experience_profile_id字段)https://developer.paypal.com/docs/api/payments/v1/#payment中引用它