stripe not forwarding events to my local end point

  Kiến thức lập trình

I have followed the stripes webhooks cli to test on a local enviroment. The first two steps checked out but when it comes to trigering events, it wount trigger my local end point even tho the CMD says trigger succeeded. My webhook is listening on port 4242

// Match the raw body to content type application/json
app.post('/webhook', bodyParser.raw({ type: 'application/json' }), (request, response) => {
  let event;

  try {
    event = stripe.webhooks.constructEvent(
      request.body,
      request.headers['stripe-signature'],
      process.env.STRIPE_WEBHOOK_SECRET
    );
  } catch (err) {
    console.log(err);
    console.log(`âš ï¸  Webhook signature verification failed.`);
    console.log(
      `âš ï¸  Check the env file and enter the correct webhook secret.`
    );
    return response.sendStatus(400);
  }

  // Extract the object from the event.
  const dataObject = event.data.object;

  //store the stripe customer
  const customerId = dataObject.customer

  // Handle the event
  // Review important events for Billing webhooks
  // https://stripe.com/docs/billing/webhooks
  // Remove comment to see the various objects sent for this sample
  switch (event.type) {
    case 'invoice.paid':

      console.log("invoice paid")

      //set users subscription to active
      Billing.find({ stripeCustomerId: customerId })
        .then(data => {

          data[0].Subscription = "active"
          data[0].save()
        })



      // Used to provision services after the trial has ended.
      // The status of the invoice will show up as paid. Store the status in your
      // database to reference when a user accesses your service to avoid hitting rate limits.
      break;
    case 'invoice.payment_failed':

      //check to see if the subscription is inactive
      Billing.find({ stripeCustomerId: customerId })
        .then(data => {

          data[0].Subscription = "past_due"
          data[0].save()
        })

      console.log("invoice failed")
      break;

    case 'customer.subscription.deleted':

      //check to see if the subscription is inactive
      Billing.find({ stripeCustomerId: customerId })
        .then(data => {

          data[0].Subscription = "inactive"
          data[0].save()
        })

      console.log("subscription delted")
      break;
    default:
    // Unexpected event type
  }
  response.sendStatus(200);
});

I need to figure out if my code is the problem as to why it is not forwarding events to my localhost:5000/webhook

New contributor

Max Kodi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT