Home > Mobile >  stripe webhook with firebase showing error
stripe webhook with firebase showing error

Time:05-07

it is showing Unexpected value for STRIPE_SIGNING_SECRET error even after checking it many times in the env file

the terminal shows everything created but it does not reach firebase database I am thinking there is a error in the code

the stripe dashboard also says connected

I am using the forward to local host line in git terminal

webhook code

import { buffer } from "micro";
import * as admin from 'firebase-admin'

//secure a connection to Firebase from backend
const serviceAccount = require('../../../permissions.json');
const app = !admin.apps.length ? admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
}) 
  : admin.app();

// establish connection to stripe
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);

const endpointSecret = process.env.STRIPE_SIGNING_SECRET;
if (typeof endpointSecret !== "string") {
    console.error("Unexpected value for STRIPE_SIGNING_SECRET");
    // potentially throw an error here
  }

const fulfillOrder = async (session) => {
    //console.log('Fulfilling order', session)

    return app
    .firestore()
    .collection("user")
    .doc(session.metadata.email)
    .collection("orders")
    .doc(session.id)
    .set({
        amount: session.amount_total / 100,
        amount_shipping: session.amount_total_details.amount_shipping / 100,
        images: JSON.parse(session.metadata.images),
        timestamp: admin.firestore.FieldValue.serverTimestamp(),
    })
    .then(() => {
        console.log(`success: order ${session.id} had been added to db`);
    });
};

export default async (req, res) =>{
    if(req.method === 'post'){
        const requestBuffer = await buffer(req);
        const payload = requestBuffer.toString();
        const sig = req.headers["stripe-signature"];

        let event;

        // verify that the event posted came from stripe 
        try{
            event = stripe.webhooks.constructEvent(
                 payload,
                 sig,
                 endpointSecret);
        } catch (err) {
            console.log('ERROR', err.message)
            return res.status(400).send(`Webhook error: ${err.message}`)
        }

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

            //fulfill the order...
            return fulfillOrder(session)
            .then(() => res.status(200))
            .catch((err) => res.status(400).send(`Webhook error: ${err.message}`));
        }
    }
};

export const config = {
    api: {
        bodyParser: false,
        externalResolver: true,
    },
};

firebase rules

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow write: if false;
      allow read: if true;
    }
  }
}

file folder

console

CodePudding user response:

const endpointSecret = process.env.STRIPE_SIGNNING_SECRET;

Typo: STRIPE_SIGNNING_SECRET

To avoid the next issue, fix the other typo:

const sig = req.headers["stripe-signatur"];

stripe-signature

  • Related