#ZeroThreeEight
1 messages · Page 1 of 1 (latest)
Hi Hanzo, allow me to share the code and error
Sure
Jul 25 01:32:22 PM Error handling chargeback: Error: Stripe: Argument "customer" must be a string, but got: undefined (on API request to `GET /v1/customers/{customer}`)
Jul 25 01:32:22 PM at /opt/render/project/src/node_modules/stripe/cjs/StripeResource.js:99:23
Jul 25 01:32:22 PM at Array.reduce (<anonymous>)
Jul 25 01:32:22 PM at Constructor._getRequestOpts (/opt/render/project/src/node_modules/stripe/cjs/StripeResource.js:96:35)
Jul 25 01:32:22 PM at /opt/render/project/src/node_modules/stripe/cjs/StripeResource.js:143:29
Jul 25 01:32:22 PM at new Promise (<anonymous>)
Jul 25 01:32:22 PM at Constructor._makeRequest (/opt/render/project/src/node_modules/stripe/cjs/StripeResource.js:139:16)
Jul 25 01:32:22 PM at Constructor.retrieve (/opt/render/project/src/node_modules/stripe/cjs/StripeMethod.js:31:83)
Jul 25 01:32:22 PM at /opt/render/project/src/server.js:511:49
Jul 25 01:32:22 PM at Layer.handle [as handle_request] (/opt/render/project/src/node_modules/express/lib/router/layer.js:95:5)
Jul 25 01:32:22 PM at next (/opt/render/project/src/node_modules/express/lib/router/route.js:144:13)```
So thaths chargeback: Error: Stripe: Argument "customer" must be a string, but got: undefined (on API request to GET /v1/customers/{customer})
// Handle the event
if (eventType === "charge.dispute.created") {
console.log("Chargeback functie gestart");
try {
const customer = await stripe.customers.retrieve(data.charge.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: "Chargeback",
};
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);
}
}
This is the full code of the webhook
Im using the same way to get the customer in order to cancel the subscription and update my database accordingly
Can you try printing data.charge.customer before you call retrieve function?
The thing is, I dont have access to my DEV server atm
/ test environment
So i cant reproduce the cahrgeback
Expanding the data.customer with charge was the recommendation of your colleague earlier
But it doenst seem to fix the job
I need access to the metadata (id property) to cancel the subscription after a chargeback and update my db
And im already in produdction
charge.dispute.created event delivers a dispute object as payload
https://stripe.com/docs/api/disputes/object
On the dispute object, charge is an expandable property.
So you can't directly call .retrieve(data.charge.customer)
You'd need to make a separate request to expand data.charge and then access the customer ID
https://stripe.com/docs/api/expanding_objects
K, thanks
👍