#derin_webhooks
1 messages ยท Page 1 of 1 (latest)
๐ Welcome to your new thread!
โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
๐ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1297748670430842951
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
I get the error that the amount refund is not accurate.
What is the error? What are the expected and actual behaviors?
const createRefund = async (req, res, next) => {
const { paymentIntentId, bookId } = req.body;
try {
const book = await Book.findById(bookId);
if (!book) {
return res
.status(404)
.json({ success: false, message: "Book not found" });
}
const refundAmount = book.price * 100;
const refund = await stripe.refunds.create({
payment_intent: paymentIntentId,
amount: refundAmount,
});
res.status(200).json({ success: true, refund });
} catch (error) {
console.error("Error creating refund:", error);
res
.status(500)
.json({ success: false, message: "Failed to create refund" });
}
};
const webhookRefund = (req, res, next) => {
const signature = req.headers["stripe-signature"];
let event;
try {
event = stripe.webhooks.constructEvent(
req.body,
signature,
process.env.STRIPE_REFUND_WEBHOOK_TEST_SECRET
);
} catch (err) {
console.error("Webhook signature verification failed:", err.message);
return res.status(400).send(Webhook Error: ${err.message});
}
if (event.type === "charge.refunded") {
const refund = event.data.object;
if (refund.status === "succeeded") {
const paymentIntentId = refund.payment_intent;
const { bookId } = req.body;
Purchase.findOneAndUpdate(
{ paymentIntentId, book: bookId },
{ refunded: true }
)
.then(() => {
console.log("Purchase marked as refunded");
})
.catch((err) => {
console.error("Error updating refund status:", err);
});
}
}
res.json({ received: true });
};
app.post(
"api/webhook-refund",
express.raw({ type: "application/json" }),
purchaseController.webhookRefund
);
The code looks fine to me. What is the expected behaviour?
Could you share your use case in an example?
The attachment shows the error on my DigitalOcean Runtime Log. The price of the product to refund is $6.99 and it says something about unrefunded amount being $6.54.
So I believe the refund amount should be after the Stripe fee, that I don't know how to derive.
In https://dashboard.stripe.com/test/logs/req_IfreU9oHvEGKG6, the amount is set to NaN instead of an integer
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
Your refund amount isn't calculated correctly, more specifically this line of code:
amount: refundAmount,
Somehow refundAmount became NaN when sending the request to Stripe
I'd recommend logging the refundAmount and check why the amount isn't an integer
Thank you. I will log the refundAmount to determine where the problem is