我想向我的客户出售一个/多个功能,并且应该立即付款,以便在交易完成后立即解锁一个功能。
我设法执行了交易,沙盒个人帐户显示了该交易,但沙盒商家帐户未显示任何要批准的交易。沙盒个人帐户显示:“这是临时授权,以确保您的付款方式能支付这笔款项。当爪牙商人的Test Store完成您的订单时,您的付款方式将被收取。”
这是代码:
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,您必须创建一个包含此字段(input_fields.no_shipping)的Web体验配置文件对象:
https://developer.paypal.com/docs/api/payment-experience/v1/#web-profiles_create
然后在您的/ paying呼叫中引用它(experience_profile_id字段)https://developer.paypal.com/docs/api/payments/v1/#payment
在Classic API中,您可以在请求中添加NOSHIPPING标记,该标记将在结帐时禁用送货地址要求。我知道它在REST的某个地方,但是我现在正在努力在参考中找到它。
代替“授权”,您应该对意图参数使用“销售”。
不知道您在这里问什么。只需构建您的购物车,您的用户就可以在购物车中添加任意数量的商品,然后将这些购物车详细信息传递到您的PayPal付款请求中。
似乎正在处理对PayPal的实际付款请求(即paypal.payment.create)