#ZeroThreeEight

1 messages · Page 1 of 1 (latest)

fervent sandBOT
pulsar sonnet
#

Can I share the code and error?

limpid maple
#

Hi! Let me help you with this.

#

Yes please

pulsar sonnet
#
  "/webhook",
  express.raw({ type: "application/json" }),
  async (req, res) => {
    const sig = req.headers["stripe-signature"];

    let data;
    let event;

    try {
      event = stripe.webhooks.constructEvent(req.rawBody, sig, endpointSecret);

      data = event.data.object;
      eventType = event.type;
    } catch (err) {
      console.log(`Webhook Error: ${err.message}`);
      res.status(400).send(`Webhook Error: ${err.message}`);
      return;
    }

    // Handle the event
    if (eventType === "charge.dispute.created") {
      console.log("Chargeback functie gestart");
      try {
        const customer = await stripe.customers.retrieve(data.customer);

        // Update database
        const id = customer.metadata.id;
        const ref = db.collection("users").doc(id);
        const get = await ref.get();
        const userData = get.data();

        console.log("Met ID: " + id);

        const updatedData = {
          ...userData,
          eindDatum: moment().format("DD-MM-YYYY"),
          isBetaald: false,
        };
        const update = ref.update(updatedData);
        const sheet = await axios.put(
          `${process.env.SHEET_BEST_API}/id/${id}`,
          {
            ...updatedData,
          }
        );

        // Unsubscribe with subscription ID
        try {
          const subscriptionId = userData.subscriptionId;
          const deleted = await stripe.subscriptions.del(subscriptionId);
          console.log("Subscription successfully canceled");
        } catch (error) {
          console.error("Error canceling subscription:", error);
        }
      } catch (error) {
        console.error("Error handling chargeback:", error);
      }
    }

    if (eventType === "checkout.session.completed") {
      try {
        ("Checkout session completed!");
        const customer = await stripe.customers.retrieve(data.customer);



#

I shared the first part of my checkout.session.completed. Because that one works (access to id)

#

Error:

Chargeback error: Error: Stripe: Argument "customer" must be a string, but got: undefined (on API request to GET /v1/customers/{customer})

limpid maple
#

Could you please share the event ID that's failing?

pulsar sonnet
#

"id": "evt_1NVv3WFM0KvL0X1JYFIc9bem",

#

The goal is to automatically cancel the subscription and update the database accordingly

limpid maple
#

I see that this event succeeded

pulsar sonnet
#

Can you please look at the error I provided. Thats where the problem is

#

Error:

Chargeback error: Error: Stripe: Argument "customer" must be a string, but got: undefined (on API request to GET /v1/customers/{customer})

limpid maple
#

Probably Charge doesn't have a Customer object attached to it

pulsar sonnet
#

I see

#

What workaround would you suggest?

limpid maple
pulsar sonnet
#

awesome

#

Thank you

#

So: Can I just replace the

const customer = await stripe.customers.retrieve(data.customer);

for

const customer = await stripe.customers.retrieve(data.charge.customer);

limpid maple
#

Yes, just make sure to expand the charge property, so it's not an ID but an object.

pulsar sonnet
limpid maple
#

The charge.customer is an ID itself, unless you're expanding it too.

pulsar sonnet
#

const customer = await stripe.customers.retrieve(data.charge.customer);

const id = customer.metadata.id;

#

This is what I just pushed.

limpid maple
#

is the id your internal ID?

pulsar sonnet
#

yes, thats the one I need to update my database

#

its in the metadata

limpid maple
#

Alright. Is it working now?

fervent sandBOT