Webhooks and Stripe API

Good Day,

We have “fully” integrated Stripe into our website, but I’m having trouble with webhook events coming from Stripe. I would like to run functions based on a successful Payment Intent or a Subscription is expired/cancelled.

I have scoured YouTube, Google, and this Forum for any information and can’t see to find someone who has done this. Does anyone know of a link or video of someone getting this to work? I’ve watched and read the forum posts on webhooks already and attempted to get it to work, but no success.

Here is my code and information, if someone wants to help:
webhook endpoint entered on Stripe - https://[mysite]/_functions/webhook

import { printToConsole } from 'backend/payStripe';
import Stripe from 'stripe';

const stripe = new Stripe('[my_secret_key]');

const endpointSecret = '[my_webhook_secret_key]';

export function webhook(request, response) {
    const sig = request.headers['stripe-signature'];
    
    let event;

    try {
        event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
    } catch (err) {
        response.status(400).send(`Webhook Error: ${err.message}`);
        return;
    }

    // Handle the event
    if(event.type === 'payment_intent.succeeded') {
        const paymentIntent = event.data.object;
        printToConsole(paymentIntent);
    }
    else {
        printToConsole(`Unhandled event type ${event.type}`);
    }
    response.send();
}

Response according to Stripe:
404 ERR

Hello Michael,

Please see the following URL with a code example the second answer is the one that you will want to use.
https://webcache.googleusercontent.com/search?q=cache:lz805C0GCA0J:https://www.anycodings.com/1questions/2343999/how-to-create-a-wix-webhook-endpoint-for-stripe-with-wix-http-functions&cd=2&hl=en&ct=clnk&gl=au

Note: I have had to use a cache version of the site to get the site to load due to bot protection that appears on desktop chrome. This does not appear when using mobile browsers.

Excerpt from above reference.

import { ok, badRequest, response } from'wix-http-functions'; 
import stripe from'stripe'; 
import wixData from'wix-data';  

const key = require('stripe')('sk_test_XXXX'); 
//use your own Secret Key
const endpointSecret = 'whsec_XXXXXX'; 
//stripe webhook signing secret
let responseBody = {    "status": 200, };  
export async function post_checkoutUpdate(request) {      
    const sig = request.headers['stripe-signature'];     
    let event;     
    const rawBody = await request.body.text()      
    try {         
        event = key.webhooks.constructEvent(
            rawBody, 
            sig, 
            endpointSecret
        );     
    } catch (err) {         
        return response.status = 400     
    }     
    // Handle the checkout.session.completed event
    if (event.type === 'checkout.session.completed') {             
        const session = event.data.object;       
        console('Checked');    
    }     
    // Return a response to acknowledge receipt of the event         
    response.status = 200
    return response(responseBody); 
}