avrono
avrono

Reputation: 1680

Using Paypal Rest API with Node

I am having a problem debugging my paypal integration with Node and the rest api https://github.com/paypal/rest-api-sdk-nodejs

The code snippet is as follows :

var config_opts = {
    'host': 'api.sandbox.paypal.com',
    'port': '',
    'client_id': 'my id',
    'client_secret': 'my secret'
 };

  var create_payment_json = {
    "intent": "sale",
    "payer": {
    "payment_method": "paypal"
},
"redirect_urls": {
    "return_url": "http://localhost:3000",
    "cancel_url": "http://localhost:3000"
},
"transactions": [{
    "amount": {
        "currency": "USD",
        "total": "10.00"
    },
    "description": "This is the payment description."
}]
  };

     paypal_sdk.payment.create(create_payment_json, config_opts, function (err, res) {
if (err) {
    console.log( err );
}

if (res) {
    console.log("Create Payment Response");
    console.log(res);
}  
 });

The response I get is as follows:

{ id: 'PAY-55J119327J2030636KLXMVJI',
  create_time: '2014-02-02T22:45:57Z',
  update_time: '2014-02-02T22:45:57Z',
  state: 'created',
  intent: 'sale',
  payer: 
   { payment_method: 'paypal',
     payer_info: { shipping_address: {} } },
  transactions: 
   [ { amount: [Object],
       description: 'This is the payment description.' } ],
  links: 
   [ { href: 'https://api.sandbox.paypal.com/v1/payments/payment/PAY-  55J119327J2030636KLXMVJI',
       rel: 'self',
       method: 'GET' },
 {     href: 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-90D3081342725343U',
       rel: 'approval_url',
       method: 'REDIRECT' },
 {     href: 'https://api.sandbox.paypal.com/v1/payments/payment/PAY-55J119327J2030636KLXMVJI/execute',
       rel: 'execute',
       method: 'POST' } ] }

I am wondering if there is some additional step in authenticating that I am missing ?

Upvotes: 3

Views: 4792

Answers (2)

sdabrutas
sdabrutas

Reputation: 1517

First, you have to declare a proper return_url (not just localhost:3000). I suggest that you change your return_url to "http://localhost:3000/success". Then, insert this one above your payment POST METHOD:

app.get('/success', function(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');
            }
        }
    });
});

This GET method will execute your payment. But, before it can execute, you must redirect first your server's response to paypal sandbox for payment confirmation.

Edit the content of your paypal_sdk.payment.create() function like this one:

paypal_sdk.payment.create(create_payment_json, config_opts, function (err, res) {
    if (err) {
       console.log( err );
    }

    else {
        console.log("Create Payment Response");
        console.log(res);

        //you forgot to redirect your response to paypal sandbox
        var redirectUrl;
        for(var i=0; i < payment.links.length; i++) {
            var link = payment.links[i];
            if (link.method === 'REDIRECT') {
                redirectUrl = link.href;
            }
        }
        res.redirect(redirectUrl);
    }  
});

I hope this can help (especially to those who are still looking for answers)

Upvotes: 3

js_gandalf
js_gandalf

Reputation: 891

You must go to Dashboard -> Rest API transactions.

See the attached screenshot:

enter image description here

Upvotes: 1

Related Questions