#gary_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/1291569213269610597
đ Have more to share? Add more details, code, screenshots, videos, etc. below.
app.post('/webhook', express.raw({type: 'application/json'}), (request, response) => {
let event = request.body;
// Only verify the event if you have an endpoint secret defined.
// Otherwise use the basic event deserialized with JSON.parse
if (endpointSecret) {
// Get the signature sent by Stripe
const signature = request.headers['stripe-signature'];
try {
event = stripe.webhooks.constructEvent(
request.body,
signature,
endpointSecret
);
} catch (err) {
console.log(â ď¸ Webhook signature verification failed., err.message);
return response.sendStatus(400);
}
}
// Handle the event
switch (event.type) {
case 'payment_intent.succeeded':
console.log('PaymentIntent was successful!');
const paymentIntent = event.data.object;
//create a tax transaction for the successful payment
const transaction = stripe.tax.transactions.createFromCalculation({
calculation: tax_calculation,//'{{TAX_CALCULATION}}',
reference: paymentIntent.id,//'{{PAYMENT_INTENT_ID}}',
expand: ['line_items'],
});
break;
// ... handle other event types
default:
console.log(`Unhandled event type ${event.type}`);
}
// Return a 200 response to acknowledge receipt of the event
response.send();
});
In your code, I don't see anywhere that tax_calculation is defined.
You didn't declare tax_calculation in your code
app.post('/update-payment-sheet', async (req, res) => {
const calculation = await stripe.tax.calculations.create({
currency: 'usd',
line_items: [
{
amount: req.body.amount,
reference: 'L1',
},
],
customer_details: {
address: {
line1: '123 Dear Road',
line2: '',
city: 'Acme',
state: 'AL',
postal_code: '85255',
country: 'US',
},
address_source: 'shipping',
},
expand: ['line_items.data.tax_breakdown']
});
const paymentIntent = await stripe.paymentIntents.update(
req.body.paymentIntentID,
{
amount: calculation.amount_total,
metadata: {tax_calculation: calculation.id}
}
);
console.log("update payment sheet req",req.body)
res.json({
total: calculation.amount_total,
tax_amount: calculation.tax_amount_exclusive,
client_secret: paymentIntent.client_secret
});
//console.log("update payment sheet res",res.body)
});
I see you setting tax_calculation in the metadata. In you webhook event code, you should retrieve the tax_calculation from the metadata of the Payment Intent object in the event first before using it
Can you assist with how to do that?
In https://dashboard.stripe.com/test/events/evt_3Q601AJ15oFjjfNB0w5dg3rH, you can see that tax_calculation was included in the metadata.
You'd need to retrieve the metadata to get the tax_calculation from the paymentIntent object in your code.
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
This will look something like:
paymentIntent.metadata.{{the-metadata-key}}
Ok thank you for the code hint, very helpful! I will try and see if I can incorporate
I tried this code
const transaction = stripe.tax.transactions.createFromCalculation({
calculation: paymentIntent.metadata.tax_calculation,//'{{TAX_CALCULATION}}',
reference: paymentIntent.id,//'{{PAYMENT_INTENT_ID}}',
expand: ['line_items'],
});
and didnt get an error
Looks good to me! If the transaction is created successfully, then you will be all good
If i go to export transactions its not showing in the download... would it appear here in Test Mode? Or is there another way to check if the tax transaction posted successfully?
You can check the request history: https://dashboard.stripe.com/test/logs?method[0]=post&method[1]=delete&direction[0]=connect_in&direction[1]=self
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
Screenshots attached, do you think it posted the tax transaction successfully?
If the status code is 200, it means the request was made successfully.
In your code, the a transaction object is returned, this also means that a tax transaction is created successfully
Yay! Thanks for your help!