#rezamirzad

1 messages ยท Page 1 of 1 (latest)

unborn wharfBOT
toxic valley
hasty igloo
#

yes

#

as of now, i have no way to tell my backend which product is being purchased in the checkout.

toxic valley
#

If so, when your frontend calls your backend, you need to send all the information you need via the request body

#

in the checkout.js, when calling /create-payment-intent you need to pass all information that you need

hasty igloo
#

can you direct my to the whole syntax?

all i have is:

fetch(${Env.REACT_APP_API_URL}/create-payment-intent, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ items: [{ id: "xl-tshirt" }] }), })

#

where do i pass the product id in the body of my post request?
and then, how do i use it on the backend?

toxic valley
#

Yes exactly there,
you send in the body your products ids, something like:

const items = [{ productId: "prod_123" }];

  const response = await fetch("/create-payment-intent", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ items }),
  });
#

And in your backend, you need to follow the definition of the /create-payment-intentin server.js

const { items } = req.body;
items[0].productId...
```
hasty igloo
#

ok cool
so, in my calculateOrderAmount, i get the item productId.
how do i retrieve the price for said productId?

toxic valley
hasty igloo
#

so, i would basically do a querty like this?

const price = await stripe.prices.search({ query: "active:'true' AND metadata['price_id']:items[0].productId'", });

toxic valley
#

Something like this:
const price = await stripe.prices.search({query: "active:'true' AND product:items[0].productId'"});

hasty igloo
#

so this
const price = await stripe.prices.search({ query: "active:'true' AND product:items[0].productId'", });
console.log(price);

returns:
{
object: 'search_result',
data: [],
has_more: false,
next_page: null,
url: '/v1/prices/search'
}

i do not see the price anywhere

toxic valley
hasty igloo
#

req_wtvmKwW5FSlnSu

toxic valley
#

You are sending this as a request Body:

{
  query: "active:'true' AND product:'items[0].priceId'",
}
```
You are searching price by product using a priceId?
Probably you should replace `items[0].priceId` with `items[0].priceId`. sorry I have a typo in my suggested message above:
const price = await stripe.prices.search({query: "active:'true' AND product:'"+items[0].priceId+"'"});
hasty igloo
#

so i give the priceId from my front?
it still returns no data

const price = await stripe.prices.search({ query: "active:'true' AND product:'" + items[0].priceId + "'", });

price
{
object: 'search_result',
data: [],
has_more: false,
next_page: null,
url: '/v1/prices/search'
}

although, i can see that i pass the priceId correctly

[ { priceId: 'price_XXXXXX' } ]

tawny night
#

What is the value of your items[0].priceId variable?

hasty igloo
#

req_NOeWTiIx2EQDlQ

#

price_1MQvcaGMR3u9Lz70hZdkwLVQ

tawny night
#

I don't get what you'd be searching for Price objects if you already have the price_xxx ID?

hasty igloo
#

ok so now i have my amount

{
object: 'search_result',
data: [
{
id: 'price_1MQvcaGMR3u9Lz70hZdkwLVQ',
object: 'price',
active: true,
billing_scheme: 'per_unit',
created: 1673886800,
currency: 'eur',
custom_unit_amount: null,
livemode: false,
lookup_key: null,
metadata: {},
nickname: null,
product: 'prod_NBICRRUIfpPLxd',
recurring: null,
tax_behavior: 'unspecified',
tiers_mode: null,
transform_quantity: null,
type: 'one_time',
unit_amount: 9900,
unit_amount_decimal: '9900'
}
],
has_more: false,
next_page: null,
url: '/v1/prices/search'
}

therefore, i want to pass it to
const paymentIntent = await stripe.paymentIntents.create({
setup_future_usage: 'off_session',
amount: price.data.unit_amount, //amount: price, currency: 'eur', automatic_payment_methods: { enabled: true, }, });

#

but price.data.unit_amount is undefined

tawny night
#

Because price.data is a list (array). You'd need to do amount: price.data[0].unit_amount

hasty igloo
#

aha, yes, i see now.

great, thank you

tawny night
hasty igloo
#

ok i get it.
so, when i get to my return_url, I check the status of the payment with the webhook?

tawny night
#

No, the webhook is a different process separate to your payment UI. It happens asynchronously. You setup a server/endpoint, and we send events to that endpoint (like payment_intent.suceeded) which indicate activity on your account that you can then action

#

There's no guarantee than any customer stays in your payment flow long enough to be redirected back to your site, so you shouldn't use that as confirmation of payment/success. Just display a nice order confirmation page, or something

hasty igloo
#

so, where and when do i use the webhook?

let's say, i redirect the client to the next url.

but now, i wanna make sure that the payment actually succeeded.

what do i need to do in order to get that confirmation?

tawny night
#

You don't 'use' the webhook. It just happens asynchonrously in the background, assuming correctly setup

hasty igloo
#

yes i did, and i already wrote the webhook post request on the back end

tawny night
#

i.e. your customer completes a payment, we flag that payment as successful and omit a payment_intent.succeeded event. Your webhook is configured to receive those events, so will required a POST HTTP request with the payment details in the body that you can action as required

hasty igloo
#

ok, but even though i installed the cli,

tawny night
#

How did you install it?

hasty igloo
tawny night
#

Did you move it to a global dir so that the stripe command is available everywhere?

#

It's easier to install via homebrew if you can

hasty igloo
#

yes it is working now

so, when i proceeed with the payment, i can see the webhook returning

idempotency_key: '3a095ed6-8698-42d6-b51f-1f4d6d5df7b6'

},
type: 'payment_intent.succeeded'
}

i want to create some data on my database.

i use the backend webhook, and just check if "succeeded" in order to trigger the graphql queries and resolvers?

tawny night
#

Well with the payment_intent.succeeded event, the status will always be succeeded

#

So on receipt of that event you can always infer that the payment was fine and the balance due to your Stripe balance

#

So yes, at that point you can write your custom fulfilment logic in your webhook

hasty igloo
#

i see

but i want my custom code to be executed only if the payment was received

what field of the webhook response do i have to check?

tawny night
#

What do you mean by received?

hasty igloo
#

i mean the client enters his card information on the checkout form.
stripe processes the payment.
i want to create the data on my data base, only when the payment is successful

but the payment_intent.succeeded is also triggered at the creation of the payment intent

if I got it correctly, what i want is the "paid" field, write?

tawny night
#

but the payment_intent.succeeded is also triggered at the creation of the payment intent
Shouldn't be, that'd be payment_intent.created

#

Even better, configure the webhook in Stripe to only listen for payment_intent.succeeded events as I suspect that's all you care about right now

hasty igloo
#

yes, i think I got it.
will come back if i have other questions

thank you ver much @tawny night ๐Ÿ‘

tawny night
#

np!

hasty igloo
#

can you explain me how is the webhook "payment_intent.succeeded" triggered when it is on my backend code, and i do not call it anywhere?

#

basically, if i do not run the stripe cli, the webhook is not called

tawny night
hasty igloo
#

my backend is on localhost:8888

#

and i want the listener to be active regardless of the stripe cli

tawny night
#

You need to setup a webhook at the URL where it will be deployed then

hasty igloo
#

aha, can not be on localhost then?
to test on localhost, it will need to be absolutely with the stripe cli?

#

like if my colleague wants to test and work on my git branch, he will need to set up the stripe cli, righht?

tawny night
hasty igloo
#

i see

thanks again

one more thing, can you give me the list of all parameters in confirmParams?

i am using the receipt_email with the email entered on the checkout form.

but i do not receive any email

tawny night
hasty igloo
#

i still do not get the email with this

tawny night