I want to sell a feature/features to my client and the payment should be instant, so that it unlocks a feature immediately whenever the transaction is completed.
I managed to execute the transaction and the sandbox personal account shows that transaction but the sandbox merchant account doesn't show anything to approve it. sandbox personal account shows that " This is a temporary authorization to make sure your payment method will cover the payment. Your payment method will be charged when jawad merchant's Test Store completes your order."
this is the code :
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;
Help me with the following:
My transactions shouldn't be linked to shipping addresses,
Transaction should be instant and paypal personal sandbox account shouldn't say this:
User can buy one or many features,
Explain what the helper function at the end is doing
Thankyou in advance. I hope this code helps someone else too