#gary_webhooks

1 messages ¡ Page 1 of 1 (latest)

old treeBOT
#

👋 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.

signal crag
#

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();
});

gloomy gale
#

In your code, I don't see anywhere that tax_calculation is defined.

#

You didn't declare tax_calculation in your code

signal crag
#

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)

});

gloomy gale
#

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

signal crag
#

Can you assist with how to do that?

gloomy gale
#

This will look something like:

paymentIntent.metadata.{{the-metadata-key}}
signal crag
#

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

gloomy gale
#

Looks good to me! If the transaction is created successfully, then you will be all good

signal crag
#

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?

gloomy gale
signal crag
#

Screenshots attached, do you think it posted the tax transaction successfully?

gloomy gale
#

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

signal crag
#

Yay! Thanks for your help!