jsongoogle-apps-scriptpaypalpaypal-subscriptions

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):

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