naseefo
naseefo

Reputation: 720

Error while running Paypal API using node js

I am running the following code in a script called pp.js. And I am running this on my index.html on loading of the page for test. I am bundling every source files using the latest webpack module.

    var paypal = require('paypal-rest-sdk');

    var user_config ={
        'mode': 'sandbox', //sandbox or live
        'client_id': 'xxxxxxx',
        'client_secret': 'xxxxxxx'
      }; 

    paypal.configure(user_config);

    var create_payment_json = {
        "intent": "sale",
        "payer": {
            "payment_method": "paypal"
        },
        "redirect_urls": {
            "return_url": "http://return.url",
            "cancel_url": "http://cancel.url"
        },
        "transactions": [{
            "item_list": {
                "items": [{
                    "name": "item",
                    "sku": "item",
                    "price": "1.00",
                    "currency": "USD",
                    "quantity": 1
                }]
            },
            "amount": {
                "currency": "USD",
                "total": "1.00"
            },
            "description": "This is the payment description."
        }]
    };

// TILL HERE THERE IS NO ERROR

    paypal.payment.create(create_payment_json, function (error, payment) {
        if (error) {
            console.log("There seems to be some error... I hope it can be corrected.");
            throw error;
        } else {
            console.log("Create Payment Response");
            console.log(payment);
        }
    });

When the last bit of the code is run i.e. paypal.payment.create(create_ ... I am getting the following error in the console of my browser.

Error thrown into the console

How do I rectify this?

Upvotes: 0

Views: 699

Answers (1)

jannis
jannis

Reputation: 5210

This code should be run server-side. The same issue in PayPal SDK tracker: https://github.com/paypal/PayPal-node-SDK/issues/220

Quote 1 (from the linked ticket)

Please see #149 (comment). It seems that you are trying to run this code in the browser which may be a security problem. If the user has access to your credentials or have an access token, they can do anything that you can do as a merchant (e.g. create more payments, refund people money, etc.).

Quote 2 (from the #149 ticket):

I did some more investigation. Apparently, this request.js:54 Uncaught Error: Invalid value for opts.mode. error comes from a version of stream-http/request.js. I'm guessing that you are trying to use browserify and using stream-http in the browser to simulate node.js's built-in http module.

Are you trying to run this SDK code in the browser (instead of in a server-side node.js process)?

This node.js SDK should only be used on a secured server. I have very limited experience using browserify. Unless I'm mistaken, you need to give this SDK access to your client ID and client secret to make this SDK work, and if your code runs in the browser, you will be exposing the credentials to any browser client. This will allow any customer to be able to do anything to your account (e.g. refund all of your payments).

If it's just that part of your code is intended to be run in the browser and another part is in the server but the code is all in one project, I recommend separating out the code into 2 different projects with different package.json files so you can have separate dependencies.

Upvotes: 1

Related Questions