Paystack Subscription with Google Cloud Functions

Viewed 185

I'm tryng to inteegrate Paystack subscription based payment in to a react app using firebase and google cloud functions. I added the the node, from the paystack documentation, code to my cloud function. Once i run the code i get en error ReferenceError: resp is not defined.

Paystack doc for reference https://paystack.com/docs/payments/subscriptions/#create-a-subscription

exports.payment = functions.https.onCall((data) => {
    const params = JSON.stringify({
      "email": "customer@email.com",
      "plan": "PLN_xxxxxxxxx"
    })
    const options = {
      hostname: 'api.paystack.co',
      port: 443,
      path: '/transaction/initialize',
      method: 'POST',
      headers: {
        Authorization: 'Bearer SECRET_KEY',
        'Content-Type': 'application/json'
      }
    }
    const req = https.request(options, res => {
      let data = ''
      resp.on('data', (chunk) => {
        data += chunk
      });
      resp.on('end', () => {
        console.log(JSON.parse(data))
      })
    }).on('error', error => {
      console.error(error)
    })
    req.write(params)
    req.end()
})

Thanks

1 Answers

On this line:

const req = https.request(options, res => {

the parameter is called res. However, on subsequent lines it seems to be mistyped as resp.

So try swapping resp out for res (or vice-versa). Something like:

const req = https.request(options, res => {
  let data = ''
  res.on('data', (chunk) => {
    data += chunk
  });
  res.on('end', () => {
    console.log(JSON.parse(data))
  })
}).on('error', error => {
  console.error(error)
})
Related