I am trying to implement the pause subscription API for Paypal in my React app.
I don’t think I am really understanding properly how to do this.
First I am trying to generate an auth token, and then use that in the pause subscription API.
The original code from the documentation is a curl so I have changed it to axios, but I don’t think I am using it correctly.
Here is the code I am using:
async function cancel() {
const response = await axios.post(
'https://api-m.sandbox.paypal.com/v1/oauth2/token',
new URLSearchParams({
'grant_type': 'client_credentials'
}),
{
headers: {
'Accept': 'application/json',
'Accept-Language': 'en_US'
},
auth: {
username: 'MY CLIENT ID IS HERE',
password: 'MY SECRET IS HERE'
}
}
);
const response2 = await axios.post(
'https://api-m.sandbox.paypal.com/v1/billing/subscriptions/I-BW452GLLEP1G/suspend',
'',
{
headers: {
'Content-Type': 'application/json',
Authorization: response.data.access_token
}
}
);
}
This is the error the console returns:
2
Answers
Axios returns a promise. You have to use either .then() or
await
for it to resolve, and you must actually parse the resulting JSON string (axios does this automatically, fetch would not) and get the access_token string value out of it. Then you can use this value in the authorization header of your request.So, it may be as simple as adding an await:
And then:
I’m having a similar error also when trying to create a plant, product..
request:
response: