Skip to content
This repository has been archived by the owner on May 12, 2020. It is now read-only.

Can't access sandbox - Client Authentication failed #141

Closed
RayKahn opened this issue Mar 4, 2016 · 6 comments
Closed

Can't access sandbox - Client Authentication failed #141

RayKahn opened this issue Mar 4, 2016 · 6 comments

Comments

@RayKahn
Copy link

RayKahn commented Mar 4, 2016

Hi,
I am trying to integrate paypal-rest-sdk with my node application. The following is my code:

var paypal = require('paypal-rest-sdk');
paypal.configure(Config.paypal_info.api);

Where api is:

'api': {
'host' : 'api.sandbox.paypal.com',
'mode': 'sandbox',
'client_id' : 'my client id based on Business-Pro account ',
'client_secret' : 'client secret here'
}

And my payment object is:

var paymentJSON = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": Config.paypal_info.return_url,
"cancel_url": Config.paypal_info.cancel_url
},
"transactions": [{
"item_list": {
"items": [{
"name": "Activation",
"sku": pProviderID,
"price": Config.paypal_info.price,
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"currency": "USD",
"total": Config.paypal_info.price,
},
"description": ""
}]
};
paypal.payment.create(paymentJSON, function (error, payment) {...});

Yet I keep getting the following error:
{"error":"invalid_client","error_description":"Client Authentication failed"}

The rest of error message is:
{"Authorization":"Basic cmF5LmthaG4tZmFjaWxpdGF0b3JfYXBpMS52b29temkuY29tOjVHS0RKTTZYREhGN1dCS1k=","Accept":"application/json","Content-Type":"application/x-www-form-urlencoded","Content-Length":29,"PayPal-Request-Id":"33a71ad3-c2e5-4ce8-af58-a367b70a8d5b","User-Agent":"PayPalSDK/PayPal-node-SDK 1.6.8 (node v4.2.2-x64-darwin; OpenSSL 1.0.2d)"}'
'grant_type=client_credentials'
paypal-debug-id: 56c2c2b7647f9, 56c2c2b7647f9

Can't figure out what the issue is.

Thanks in advance.

@randstraw
Copy link

You are using your sandbox credentials? Also no extra characters in your config string?

@RayKahn
Copy link
Author

RayKahn commented Mar 4, 2016

I am using the sandbox credentials. And I don't see any extra characters in
my config either.

On Fri, Mar 4, 2016 at 12:09 PM Randy notifications@github.com wrote:

You are using your sandbox credentials? Also no extra characters in your
config string?


Reply to this email directly or view it on GitHub
#141 (comment)
.

@RayKahn
Copy link
Author

RayKahn commented Mar 4, 2016

I was able to resolve the issue.
It's not enough to have a developer account; I had to also create an app account (of course this is NOT clear at all). After I created an app account I used the client_id and client_secret to create a payment. Although my "app" is a web application, I think what I have done is correct. Unless someone says otherwise.

@RayKahn RayKahn closed this as completed Mar 4, 2016
@randstraw
Copy link

Yes you need to Create REST API app here: https://developer.paypal.com/developer/applications/

@RayKahn
Copy link
Author

RayKahn commented Mar 4, 2016

Thanks for your help.

On Fri, Mar 4, 2016 at 12:44 PM Randy notifications@github.com wrote:

Yes you need to Create REST API app here:
https://developer.paypal.com/developer/applications/


Reply to this email directly or view it on GitHub
#141 (comment)
.

@ompurwar
Copy link

ompurwar commented Jan 2, 2020

Use the below code in helper.js make sure to change

process.env.NODE_ENV

Reference:
https://developer.paypal.com/docs/checkout/integrate/#replace-sandbox-credentials-with-live-credentials
https://developer.paypal.com/docs/checkout/reference/server-integration/setup-sdk/


'use strict';

const checkoutNodeJssdk = require('@paypal/checkout-server-sdk');


function client() {
    return new checkoutNodeJssdk.core.PayPalHttpClient(environment());
}


function environment() {
    let clientId = process.env.PAYPAL_CLIENT_ID;
    let clientSecret = process.env.PAYPAL_CLIENT_SECRET;
    let productionBaseUrl = 'https://api.paypal.com';

    if (process.env.NODE_ENV === 'x' || process.env.NODE_ENV === 'q') {
        return new checkoutNodeJssdk.core.SandboxEnvironment(
            clientId, clientSecret
        );
    }
    if (process.env.NODE_ENV === 'p') {
        return new checkoutNodeJssdk.core.PayPalEnvironment(
            clientId, clientSecret,productionBaseUrl
        );
    }
}

async function prettyPrint(jsonData, pre = "") {
    let pretty = "";
    function capitalize(string) {
        return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
    }
    for (let key in jsonData) {
        if (jsonData.hasOwnProperty(key)) {
            if (isNaN(key))
                pretty += pre + capitalize(key) + ": ";
            else
                pretty += pre + (parseInt(key) + 1) + ": ";
            if (typeof jsonData[key] === "object") {
                pretty += "\n";
                pretty += await prettyPrint(jsonData[key], pre + "    ");
            }
            else {
                pretty += jsonData[key] + "\n";
            }

        }
    }
    return pretty;
}

module.exports = { client: client, prettyPrint: prettyPrint };

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants