title: NodeJS function inside of Stripe Webhook does not fire when using Serverless
body: <p><strong>Objective</strong>: Host an endpoint with Serverless for a Stripe webhook that fires off an email when the <code>charge.refunded</code> event occurs.</p>
  <p><em>Issue</em>: NodeJS function does not fire properly inside of Serverless implementation of Stripe Webhook.</p>
  <p><em>Background</em>: I implemented the below code outside of a <a href="https://www.serverless.com/framework/docs/providers/aws/guide/quick-start/" rel="nofollow noreferrer">Serverless</a> implementation following this <a href="https://stripe.com/docs/webhooks/quickstart" rel="nofollow noreferrer">Stripe Webhook guide</a> and was able to successfully identify the event in my local dev env and fire off the email.</p>
  <p>However, once I moved and stripped out the server code (express) for my <code>handler.js</code> file within my Serverless app, the <code>sendEmail</code> function no longer fires as the function does not enter the <code>charge.refunded</code> case. I tested this by adding my generated endpoint for the webhook into Stripe and testing the event from there. Upon firing the <code>charge.refunded</code> event in Stripe, I get back a successful response and my Serverless logs pick up on the API call, yet <code>sendEmail</code> is never reached.</p>
  <p>Please help!</p>
  <p><strong>EDIT</strong>: Final Working Code Below</p>
  <pre><code>&quot;use strict&quot;;
  const stripe = require('stripe')('sk_test_');
  const formData = require('form-data');
  const Mailgun = require('mailgun.js');
  
  module.exports.webhook = async (event) =&gt; {
  
    async function sendEmail() {
  
      const mailgun = new Mailgun(formData);
  
      const mg = mailgun.client({
        username: 'api',
        key: process.env.MAILGUN_API_KEY,
        public_key: process.env.MAILGUN_PUBLIC_KEY
      });
  
      let messageParams = {
        from: 'mailgun@domain.com',
        to: 'me@domain.com',
        subject: 'Hello',
        text: 'Testing some Mailgun awesomeness!',
    }
  
      const result = await mg.messages.create(
        'sandbox-123.mailgun.org',
        messageParams
        ).then((msg) =&gt; console.log(msg))
        .catch((err) =&gt; console.log(err));
      
      return result
    }
  
    try {
  
    const eventBody = JSON.parse(event.body)
  
    switch (eventBody.type) {
      case &quot;charge.refunded&quot;:
        const charge = eventBody.data.object;
        await sendEmail()
        console.log('charge refunded!')
        break;
      default:
        // Unexpected event type
        console.log(`bad request:${request}`)
        console.log(`Unhandled event type ${event.type}.`);
    }
  
    return {
      statusCode: 200,
      body: JSON.stringify(
        {
          message: &quot;Your function executed successfully!&quot;,
          input: event,
        },
        null,
        2
      ),
    };
  
  
    } catch (err) {
      //console.log('ERROR:', err)
      return {
        statusCode: 400,
        body: JSON.stringify(
          {
            message: &quot;Your function errored&quot;,
            error: err,
          },
          null,
          2
        ),
      };
  
    }
    
  };
  
  </code></pre>
  <p><a href="https://stackoverflow.com/questions/65150532/stripe-webhook-is-giving-status-200-but-the-nodejs-function-is-not-firing">This question</a> seems similar to mine, but is unresolved.</p>
  <p>Edit: Here is the test request that Stripe sends for the event.</p>
  <pre><code>{
    &quot;created&quot;: 121351351231,
    &quot;livemode&quot;: false,
    &quot;id&quot;: &quot;evt_00000000000000&quot;,
    &quot;type&quot;: &quot;charge.refunded&quot;,
    &quot;object&quot;: &quot;event&quot;,
    &quot;request&quot;: null,
    &quot;pending_webhooks&quot;: 1,
    &quot;api_version&quot;: &quot;2020-08-27&quot;,
    &quot;data&quot;: {
      &quot;object&quot;: {
        &quot;id&quot;: &quot;ch_00000000000000&quot;,
        &quot;object&quot;: &quot;charge&quot;,
        &quot;amount&quot;: 2000,
        &quot;amount_captured&quot;: 2000,
        &quot;amount_refunded&quot;: 2000,
        &quot;application&quot;: null,
        &quot;application_fee&quot;: null,
        &quot;application_fee_amount&quot;: null,
        &quot;balance_transaction&quot;: &quot;txn_00000000000000&quot;,
        &quot;billing_details&quot;: {
          &quot;address&quot;: {
            &quot;city&quot;: null,
            &quot;country&quot;: null,
            &quot;line1&quot;: null,
            &quot;line2&quot;: null,
            &quot;postal_code&quot;: null,
            &quot;state&quot;: null
          },
          &quot;email&quot;: null,
          &quot;name&quot;: null,
          &quot;phone&quot;: null
        },
        &quot;calculated_statement_descriptor&quot;: &quot;Stripe&quot;,
        &quot;captured&quot;: true,
        &quot;created&quot;: 1639014132,
        &quot;currency&quot;: &quot;usd&quot;,
        &quot;customer&quot;: null,
        &quot;description&quot;: &quot;(created by Stripe CLI)&quot;,
        &quot;disputed&quot;: false,
        &quot;failure_code&quot;: null,
        &quot;failure_message&quot;: null,
        &quot;fraud_details&quot;: {
        },
        &quot;invoice&quot;: null,
        &quot;livemode&quot;: false,
        &quot;metadata&quot;: {
        },
        &quot;on_behalf_of&quot;: null,
        &quot;order&quot;: null,
        &quot;outcome&quot;: {
          &quot;network_status&quot;: &quot;approved_by_network&quot;,
          &quot;reason&quot;: null,
          &quot;risk_level&quot;: &quot;normal&quot;,
          &quot;risk_score&quot;: 54,
          &quot;seller_message&quot;: &quot;Payment complete.&quot;,
          &quot;type&quot;: &quot;authorized&quot;
        },
        &quot;paid&quot;: true,
        &quot;payment_intent&quot;: &quot;pi_00000000000000&quot;,
        &quot;payment_method&quot;: &quot;pm_00000000000000&quot;,
        &quot;payment_method_details&quot;: {
          &quot;card&quot;: {
            &quot;brand&quot;: &quot;visa&quot;,
            &quot;checks&quot;: {
              &quot;address_line1_check&quot;: null,
              &quot;address_postal_code_check&quot;: null,
              &quot;cvc_check&quot;: null
            },
            &quot;country&quot;: &quot;US&quot;,
            &quot;exp_month&quot;: 12,
            &quot;exp_year&quot;: 2022,
            &quot;fingerprint&quot;: &quot;skdjndsf54sdf&quot;,
            &quot;funding&quot;: &quot;credit&quot;,
            &quot;installments&quot;: null,
            &quot;last4&quot;: &quot;4242&quot;,
            &quot;network&quot;: &quot;visa&quot;,
            &quot;three_d_secure&quot;: null,
            &quot;wallet&quot;: null
          },
          &quot;type&quot;: &quot;card&quot;
        },
        &quot;receipt_email&quot;: null,
        &quot;receipt_number&quot;: null,
        &quot;receipt_url&quot;: &quot;https://pay.stripe.com/receipts/&quot;,
        &quot;refunded&quot;: true,
        &quot;refunds&quot;: {
          &quot;object&quot;: &quot;list&quot;,
          &quot;data&quot;: [
            {
              &quot;id&quot;: &quot;re_00000000000000&quot;,
              &quot;object&quot;: &quot;refund&quot;,
              &quot;amount&quot;: 2000,
              &quot;balance_transaction&quot;: &quot;txn_00000000000000&quot;,
              &quot;charge&quot;: &quot;ch_00000000000000&quot;,
              &quot;created&quot;: 12315364531,
              &quot;currency&quot;: &quot;usd&quot;,
              &quot;metadata&quot;: {
              },
              &quot;payment_intent&quot;: null,
              &quot;reason&quot;: null,
              &quot;receipt_number&quot;: &quot;1759-4625&quot;,
              &quot;source_transfer_reversal&quot;: null,
              &quot;status&quot;: &quot;succeeded&quot;,
              &quot;transfer_reversal&quot;: null
            }
          ],
          &quot;has_more&quot;: false,
          &quot;url&quot;: &quot;/v1/charges/ch_3K4beCGEYqkKFGst0peRnNQE/refunds&quot;
        },
        &quot;review&quot;: null,
        &quot;shipping&quot;: {
          &quot;address&quot;: {
            &quot;city&quot;: &quot;San Francisco&quot;,
            &quot;country&quot;: &quot;US&quot;,
            &quot;line1&quot;: &quot;510 Townsend St&quot;,
            &quot;line2&quot;: null,
            &quot;postal_code&quot;: &quot;94103&quot;,
            &quot;state&quot;: &quot;CA&quot;
          },
          &quot;carrier&quot;: null,
          &quot;name&quot;: &quot;Jenny Rosen&quot;,
          &quot;phone&quot;: null,
          &quot;tracking_number&quot;: null
        },
        &quot;source_transfer&quot;: null,
        &quot;statement_descriptor&quot;: null,
        &quot;statement_descriptor_suffix&quot;: null,
        &quot;status&quot;: &quot;succeeded&quot;,
        &quot;transfer_data&quot;: null,
        &quot;transfer_group&quot;: null,
        &quot;fee&quot;: 0
      }
    }
  }
  </code></pre>
  <p>Here is the abbreviated output of <code>console.log(event.body)</code>:</p>
  <pre><code>event.body {
      &quot;created&quot;: 0000000000,
      &quot;livemode&quot;: false,
      &quot;id&quot;: &quot;evt_00000000000000&quot;,
      &quot;type&quot;: &quot;charge.refunded&quot;,
      &quot;object&quot;: &quot;event&quot;,
      &quot;request&quot;: null,
      &quot;pending_webhooks&quot;: 1,
      &quot;api_version&quot;: &quot;2020-08-27&quot;,
      &quot;data&quot;: {
          &quot;object&quot;: {
              ...
              },
              &quot;description&quot;: &quot;(created by Stripe CLI)&quot;,
              &quot;disputed&quot;: false,
              &quot;outcome&quot;: {
                  &quot;network_status&quot;: &quot;approved_by_network&quot;,
                  &quot;reason&quot;: null,
                  &quot;risk_level&quot;: &quot;normal&quot;,
                  &quot;risk_score&quot;: 54,
                  &quot;seller_message&quot;: &quot;Payment complete.&quot;,
                  &quot;type&quot;: &quot;authorized&quot;
              },
              &quot;paid&quot;: true,
              &quot;payment_intent&quot;: &quot;pi_00000000000000&quot;,
              &quot;payment_method&quot;: &quot;pm_00000000000000&quot;,
              &quot;payment_method_details&quot;: {
                  &quot;card&quot;: {
                      &quot;brand&quot;: &quot;visa&quot;,
                      &quot;checks&quot;: {
                          &quot;address_line1_check&quot;: null,
                          &quot;address_postal_code_check&quot;: null,
                          &quot;cvc_check&quot;: null
                      },
                      &quot;country&quot;: &quot;US&quot;,
                      &quot;exp_month&quot;: 12,
                      &quot;exp_year&quot;: 2022,
                      &quot;fingerprint&quot;: &quot;000000&quot;,
                      &quot;funding&quot;: &quot;credit&quot;,
                      &quot;installments&quot;: null,
                      &quot;last4&quot;: &quot;4242&quot;,
                      &quot;network&quot;: &quot;visa&quot;,
                      &quot;three_d_secure&quot;: null,
                      &quot;wallet&quot;: null
                  },
                  &quot;type&quot;: &quot;card&quot;
              },
              &quot;receipt_email&quot;: null,
              &quot;receipt_number&quot;: &quot;1809-0178&quot;,
              &quot;receipt_url&quot;: &quot;https://pay.stripe.com/receipts/acct_/ch_/&quot;,
              &quot;refunded&quot;: true,
              &quot;refunds&quot;: {
                  &quot;object&quot;: &quot;list&quot;,
                  &quot;data&quot;: [
                      {
                          &quot;id&quot;: &quot;re_00000000000000&quot;,
                          &quot;object&quot;: &quot;refund&quot;,
                          &quot;amount&quot;: 2000,
                          &quot;balance_transaction&quot;: &quot;txn_00000000000000&quot;,
                          &quot;charge&quot;: &quot;ch_00000000000000&quot;,
                          &quot;created&quot;: 0000000,
                          &quot;currency&quot;: &quot;usd&quot;,
                          &quot;metadata&quot;: {},
                          &quot;payment_intent&quot;: null,
                          &quot;reason&quot;: null,
                          &quot;receipt_number&quot;: &quot;1917-1209&quot;,
                          &quot;source_transfer_reversal&quot;: null,
                          &quot;status&quot;: &quot;succeeded&quot;,
                          &quot;transfer_reversal&quot;: null
                      }
                  ],
                  &quot;has_more&quot;: false,
                  &quot;url&quot;: &quot;/v1/charges/ch_0000000/refunds&quot;
              },
              &quot;review&quot;: null,
              &quot;shipping&quot;: {
                  &quot;address&quot;: {
                      &quot;city&quot;: &quot;San Francisco&quot;,
                      &quot;country&quot;: &quot;US&quot;,
                      &quot;line1&quot;: &quot;510 Townsend St&quot;,
                      &quot;line2&quot;: null,
                      &quot;postal_code&quot;: &quot;94103&quot;,
                      &quot;state&quot;: &quot;CA&quot;
                  },
                  &quot;carrier&quot;: null,
                  &quot;name&quot;: &quot;Jenny Rosen&quot;,
                  &quot;phone&quot;: null,
                  &quot;tracking_number&quot;: null
              },
              &quot;source_transfer&quot;: null,
              &quot;statement_descriptor&quot;: null,
              &quot;statement_descriptor_suffix&quot;: null,
              &quot;status&quot;: &quot;succeeded&quot;,
              &quot;transfer_data&quot;: null,
              &quot;transfer_group&quot;: null,
              &quot;fee&quot;: 0
          }
      }
  }
  
  
  </code></pre>
  <p>The output for <code>console.log(JSON.stringify(JSON.parse(event.body).type))</code> is <code>&quot;charge.refunded&quot;</code> yet the case for it in the switch statement is not entered. Why?</p>
  <p><a href="https://i.stack.imgur.com/fFYTS.png" rel="nofollow noreferrer">
  Here is a picture from the Cloudwatch logs to confirm</a></p>