简体   繁体   中英

PayPal pause subcription API in react returning error

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:


在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

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:

const response = await axios.post(

And then:

      Authorization: response.data.access_token

I'm having a similar error also when trying to create a plant, product..

request:

const response = await axios.post(
      'https://api.sandbox.paypal.com/v1/billing/plans',
      {
          // 'product_id': 'PROD-XXCD1234QWER65782',
          'name': `Plan - ${paiement_cycle} subscription for ${company} with total amount USD${parsed_price}`,
          'description': `${paiement_cycle} subscription for ${company} with total amount ${parsed_price}. subscribed by ${name}`,
          'status': 'ACTIVE',
          'billing_cycles': [
              {
                  'frequency': {
                      'interval_unit': 'MONTH',
                      'interval_count': 1
                  },
                  // 'tenure_type': 'REGULAR',
                  // 'sequence': 1,
                  'pricing_scheme': {
                      'fixed_price': {
                          'value': parsed_price*100,
                          'currency_code': 'USD'
                      }
                  }
              },
          ],
      },
      {
          headers: {
            'Content-Type': 'application/json',
            'Authorization': `${token_type} ${access_token}`,
              'PayPal-Request-Id': `${pro_seq}`
          }
        }
    );

response:

    {
  "name": "NOT_AUTHORIZED",
  "message": "Authorization failed due to insufficient permissions.",
  "debug_id": "be19a801e3232",
  "details": [
    {
      "issue": "PERMISSION_DENIED",
      "description": "You do not have permission to access or perform operations on this resource."
    }
  ],
  "links": [
    {
      "href": "https://developer.paypal.com/docs/api/v1/billing/subscriptions#NOT_AUTHORIZED",
      "rel": "information_link",
      "method": "GET"
    }
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM