Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PayPal API: response says that my JSON request is malformed when revising my subscription

I am trying to change a customer plan from "pro" to "expert".

I am currrently operating on the live PayPal API.

However, when I try to change it using the following code (written in .gs):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var url = "https://api.paypal.com/v1/billing/subscriptions/" + subID + "/revise";
var bearer = "XXXXXXXXX";
var options = {
      "method": "POST",
      "contentType": "application/json",
      "headers": {
        "Content-Type": "application/json",
        "Authorization": "Bearer " + bearer
      },
      "payload": JSON.stringify({
        "plan_id": "X-XXXXXXXXXXXXXXXXXXX"
      })
    };
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);

I get as a response:

 {"name":"INVALID_REQUEST","message":"Request is not well-formed, syntactically incorrect, or violates schema.","debug_id":"7df737cb91cff","details":[{"location":"body","issue":"MALFORMED_REQUEST_JSON","description":"The request JSON is not well formed."}],"links":[]}

From the PayPal Developer API section:

curl -v -X POST https://api-m.sandbox.paypal.com/v1/billing/subscriptions/I-BW452GLLEP1G/revise  
-H "Content-Type: application/json" \
-H "Authorization: Bearer <Access-Token>" \
-d '{
  "plan_id": "P-5ML4271244454362WXNWU5NQ"
}'

Everything seems right, why is it not working?

Note:
I had the same response when canceling a subscription and I solved it adding the JSON.stringify to the payload. But in this case, even if I add it, it does not solve the problem.

>Solution :

var url = "https://api.paypal.com/v1/billing/subscriptions/" + subID + "/revise";

Log this, you will likely find that subID is undefined

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading