Tan Han Wei
Tan Han Wei

Reputation: 19

How to create a Wix Webhook Endpoint for Stripe with wix-http-functions?

I'm using this guide under section:

"6: Confirm the payment is successful" to test webhook from Stripe Dashboard.

UPDATE: I have installed stripe, express and body-parser in Wix's Package Manager. This is my latest code for http-function.js in Wix:

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

const key = require('stripe')('sk_test_XXXX'); //use your own Secret Key

const endpointSecret = 'whsec_XXXXXX'; //stripe webhook signing secret

const app = require('express')();

const bodyParser = require('body-parser');

let responseBody = {
          "status": 200,
        };

export function post_checkoutUpdate(request) {


//console.log("Running the function"); // this one works

    app.post('/webhook', bodyParser.raw({type: 'application/json'}), (request, response) => {
  const sig = request.headers['stripe-signature'];

  console('Inside app.post'); //the code doesn't reach here

  let event;

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


  // Handle the checkout.session.completed event
  if (event.type === 'checkout.session.completed') {
    const session = event.data.object;

   console('Checked'); //the code doesn't reach here too

  }

  // Return a response to acknowledge receipt of the event
  response.json({received: true});
});

app.listen(8000, () => console.log('Running on port 8000'));

return response(responseBody);

}

From Stripe Dashboard, I have sent a test event to this webhook endpoint: https://www<myowndomain.com>/_functions/checkoutUpdate

Now, the test webhook was sent successfully and I can receive HTTP status code: 200 (OK) from the server to Stripe. However, the code doesn't run app.post and I receive the following error from Wix Site Monitoring Tool:

"jsonPayload":{
"message":"["(node:1) [DEP0097] DeprecationWarning: Using a domain property in MakeCallback is deprecated. Use the async_context variant of MakeCallback or the AsyncResource class instead."]"
}

Any help would be greatly appreciated, thanks!

Upvotes: 0

Views: 1126

Answers (2)

AbdullahA
AbdullahA

Reputation: 173

You don't need express, this is how I done it:

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);
}

Upvotes: 1

Meredith Hassett
Meredith Hassett

Reputation: 291

You can incorporate try-catch's into your code and turn on Site Monitoring to start recording the backend logs. Once we get a more detailed response, we can start debugging what is happening.

Upvotes: 1

Related Questions