#Yharoo

1 messages · Page 1 of 1 (latest)

drifting questBOT
gusty blaze
wooden sedge
#

ahh had this issue before. Do you use server.use(express.json()); in your project?

gusty blaze
#

i'd suggest looking through the demo code

#

no, we don't use express.json iirc, cause that modifies the request body

wooden sedge
#

@true elbow example:
make sure you set this up in express before you do server.use(express.json())

async function getStripe() {
  const API_KEY = sk_test_myapikey
  const stripe = new Stripe(API_KEY, {
    apiVersion: "2022-11-15"
  });
  return stripe;
}

async function verifySubscriptionUpdateWebHook(request) {
  const event = (await getStripe()).webhooks.constructEvent(
    request.body,
    request.headers["stripe-signature"],
    "whsec_updateendpointsecret"
  );
  return event;
}

function stripeSubscriptionUpdateWebHook({ server }) {
  server.post(
    "/api/v1/public/stripe-subscription-updated",
    bodyParser.raw({ type: "*/*" }),
    async (req, res) => {
      try {
        const event: any = await verifySubscriptionUpdateWebHook(req);
        if (event.type === "customer.subscription.updated" && event.data && event.data.object && event.data.object.customer) {
          // Get customer id from session
          const customerId = extractCustomerId(event);
          // Update userbilling object with new object
          // return true and not wait for our end to update
          const service = new UserBillingService();
          service.updateSubscriptionByCustomerId(customerId, event);
        } else if (event.type === "customer.subscription.created" && event.data && event.data.object && event.data.object.customer) {
          const customerId = extractCustomerId(event);
          const customer: any = await getStripeCustomer(customerId);
          if(customer) {
            const email = customer.email;
            if(email) {
              const user = await User.findOne({ email });
              updateCustomerPlan(req, user, event.data.object);
           }
          }
        }
        res.json({ received: true });
      } catch (err) {
        res.status(400).send(`Webhook Error: ${err.message}`);
      } finally {
        console.log(log);
      }
    }
  );
#

@true elbow see bodyParser.raw({ type: "*/*" }) in the code above

true elbow
#

Hey, thanks for reply

#

I am not using express, I'm using next js

#

it uses its built in server for backend

wooden sedge
#

ahh okay my bad

true elbow
#

not ur fault.

#

Should i just use express?

gusty blaze
#

might be helpful