#theglobe-checkout-coupon

1 messages · Page 1 of 1 (latest)

fluid socketBOT
thorny socket
#

theglobe-checkout-coupon

#

Hey @signal furnace ! When you get the Event you have to separately call the Retrieve Checkout Session API so that you can expand in that call based on https://stripe.com/docs/expand

signal furnace
#

I am catching a raw_date webhook from Stripe with the checkout.session.completed event using Zapier. Then I have another action where I am trying to get that raw_data and use the expand to get the coupon code

#

but it's not working

thorny socket
#

I understand that part which is what I tried to explain. After you get the Event on your endpoint, it will not have the data you want, you have to write custom code to retrieve the data from the API and expand there

signal furnace
#

yes, I wrote. Here it is part of it: const lineItems = session.line_items.data;

  // Find the line item with the coupon
  const lineItemWithCoupon = lineItems.find(item => item.discount_amounts && item.discount_amounts[0]?.coupon?.name === '95%OFF');

  if (lineItemWithCoupon) {
    const couponName = lineItemWithCoupon.discount_amounts[0].coupon.name;
    return { output: couponName };
  } else {
    return { error: 'Coupon not found in line items' };
  }
} else {
  const errorBody = await response.text();
  console.error(`Failed to retrieve session: ${response.status} ${response.statusText}`, errorBody);
  return { error: `Failed to retrieve session: ${response.status} ${response.statusText}` };
}

} catch (error) {
console.error(error);
return { error: error.message };
}
};

output = await retrieveSession();

#

but it does not get the line_items data

thorny socket
#

output = await retrieveSession(); what does that do?

#

right now your code you shared expects the coupon name to be here. Where did you write the code to expand that property?

signal furnace
#

here it is:
const retrieveSession = async () => {
try {
const response = await fetch(https://api.stripe.com/v1/checkout/sessions/${sessionId}, {
headers: {
Authorization: Bearer ${stripeSecretKey},
'Content-Type': 'application/x-www-form-urlencoded',
},
method: 'GET',
});

if (response.ok) {
  const session = await response.json();
  console.log(session); // Log the entire session object for inspection

  const lineItems = session.line_items.data;

  // Find the line item with the coupon
  const lineItemWithCoupon = lineItems.find(item => item.discount_amounts && item.discount_amounts[0]?.coupon?.name === '95%OFF');

  if (lineItemWithCoupon) {
    const couponName = lineItemWithCoupon.discount_amounts[0].coupon.name;
    return { output: couponName };
  } else {
    return { error: 'Coupon not found in line items' };
  }
} else {
  const errorBody = await response.text();
  console.error(`Failed to retrieve session: ${response.status} ${response.statusText}`, errorBody);
  return { error: `Failed to retrieve session: ${response.status} ${response.statusText}` };
}

} catch (error) {
console.error(error);
return { error: error.message };
}
};

output = await retrieveSession();

thorny socket
#

okay so at the beginning of that code you call the Retrieve Session API right?

signal furnace
#

yes

thorny socket
#

where did you expand the discount/coupon part?

signal furnace
#

so I was trying to expand using this code const response = await fetch(https://api.stripe.com/v1/checkout/sessions/${sessionId}?expand[]=line_items, {

thorny socket
#

you need to URL encode those parameters since you are making the raw request yourself

#

and you need to expand more than line_items right?

#

you need to expand line_items.data.discounts

signal furnace
#

the code is the 95OFF. it's here: "line_items": [
{
"id": "li_1Nersdfdas484",
"object": "item",
"adjustable_quantity": null,
"cross_sell_from": null,
"description": "Documents ",
"discount_amounts": [
{
"amount": 37715,
"coupon": {
"object": "coupon",
"amount_off": null,
"currency": null,
"duration": "forever",
"has_applies_to_products": false,
"name": "95%OFF",
"percent_off": 95
},
"currency": "usd",
"intervals": null,
"promotion_code": {
"object": "promotion_code",
"code": "95OFF"
}
}
],

thorny socket
#

that is not returned by the API though right?

signal furnace
#

no, i found on the checkout.session object

thorny socket
#

where?

signal furnace
#

Response body
{
"id": "ppage_1xxx",
"object": "checkout.session",
"account_settings": {
"account_id": "acct_xxxx",
"assets": {
"icon":

#

It's the first event that shows to me when there's a sale

thorny socket
#

yeah that's just a purely internal API though

#

which means you have to expand line_items.data.discounts to get that. So try that, dump the exact data you get from the API and then you should be unblocked

signal furnace
#

ok I will try that again. Thanks for your help. I appreciated

thorny socket
#

sure thing!