#ZeroThreeEight
1 messages · Page 1 of 1 (latest)
Can I share the code and error?
"/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})
Could you please share the event ID that's failing?
"id": "evt_1NVv3WFM0KvL0X1JYFIc9bem",
The goal is to automatically cancel the subscription and update the database accordingly
I see that this event succeeded
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})
Probably Charge doesn't have a Customer object attached to it
Oh, the object here is a Dispute actually.
You can access your Customer via data.charge.customer. You will need to expand the charge property: https://stripe.com/docs/api/disputes/object#dispute_object-charge
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);
Yes, just make sure to expand the charge property, so it's not an ID but an object.
i can access the id with data.charge.customer.id right?
The charge.customer is an ID itself, unless you're expanding it too.
const customer = await stripe.customers.retrieve(data.charge.customer);
const id = customer.metadata.id;
This is what I just pushed.
is the id your internal ID?
Alright. Is it working now?