skip to Main Content

Hi I’m trying to use firebase functions to cancel my apps paypal subscription, this code works since when i check the paypal subscription from paypal api they are canceled. But the problem is that the firebase functions always return “Error: could not handle the request” and in logs it shows error below message. How can i change my code so it dosen’t return this error and forwards the response from paypal api to my app or atleast return 200 code if paypal api returns 204 or 422?

Error: Incorrect statusCode: 422
    at ClientRequest.h.request (/srv/node_modules/bent/src/nodejs.js:132:23)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)
    at HTTPParser.parserOnIncomingClient (_http_client.js:552:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:119:17)
    at TLSSocket.socketOnData (_http_client.js:454:20)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)

My code

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
const bent = require('bent');

admin.initializeApp();

exports.cancelPaypalSubscription = functions.https.onRequest(async (req, res) => {
  const id = req.query.id;
  const post = bent(
    'https://api.sandbox.paypal.com/v1/billing/subscriptions/' + id,
    'POST',
    {'Authorization': 'Basic ' + functions.config().paypal.key,
     'Content-Type': 'application/json'
    });
  return await post('/cancel', {reason: "User account deleted"});
});

2

Answers


  1. The PayPal API returns a 422 HTTP status code, which means that the request you’re making is not correct according to the API.

    From quickly scanning the API documentation on the subscription API, it seems that you can’t POST to a specific subscription URL. To cancel a subscription you need to post to https://api.sandbox.paypal.com/v1/billing/subscriptions/I-EXCCE2B4J0D5/cancel, so with /cancel added to the end.

    Login or Signup to reply.
  2. Sounds like there is an unhandled exception when you call post() method. Try to wrap it in try/catch:

    try {
      await post('/cancel', {reason: "User account deleted"});
    } catch (err) {
      // handle error
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search