PayPal REST SDK completes the transaction but wait

2019-09-25 03:46发布

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:

  1. My transactions shouldn't be linked to shipping addresses,

  2. Transaction should be instant and paypal personal sandbox account shouldn't say this:

    enter image description here

  3. User can buy one or many features,

  4. Explain what the helper function at the end is doing

Thankyou in advance. I hope this code helps someone else too

2条回答
唯我独甜
2楼-- · 2019-09-25 04:26
  1. In the Classic API there was a flag for NOSHIPPING you could include in your request that would disable shipping address requirements during checkout. I know it's in REST somewhere, but I'm struggling to find it in the reference right now.

  2. Instead of "authorize" you should use "sale" for the intent parameter.

  3. Not sure what you're asking here..?? Just build your shopping cart so your user can add as many items as they want to their cart, and then pass those cart details into your PayPal payment request.

  4. It seems to be handling the actual pay call to PayPal (ie. paypal.payment.create)

查看更多
祖国的老花朵
3楼-- · 2019-09-25 04:36

This document shows the use of the no_shipping field in button code:

https://developer.paypal.com/docs/checkout/how-to/customize-flow/#pass-experience-profile-options

To call the API directly, you have to create a web experience profile object containing this field (input_fields.no_shipping):

https://developer.paypal.com/docs/api/payment-experience/v1/#web-profiles_create

Then reference it in your /payment call (the experience_profile_id field) https://developer.paypal.com/docs/api/payments/v1/#payment

查看更多
登录 后发表回答