#imzikkado

1 messages · Page 1 of 1 (latest)

glass cipherBOT
flat kettle
#

hi, do you have some example code/example JSON responses etc?

turbid nexus
#

whait...

#
app.post("/checkout", async (req, res) => {
  if (!req.isAuthenticated()) {
    return res.status(401).end()
  }



  console.log('Checkout: start')
  const session = await stripe.checkout.sessions.create({
    payment_method_types: ['card'],
    line_items: [
      {

        price_data: {
          currency: "brl",
          product_data: {
            name: "RPSide - 1 Month",
          },
          unit_amount: 3000,
        },
        quantity: 1
      },
    ],
    metadata: {
      itemType: '30d',
      userId: req.session.passport.id
    },
    mode: "payment",
    success_url: "http://localhost:8080/painel",
    cancel_url: "http://localhost:8080/planos"
  });


  console.log('Checkout: end')
  res.json({ id: session.id });
});

app.post('/webhook', express.raw({ type: 'application/json' }), async (req, res) => {

  const stripePayload = (req as any).rawBody || req.body;
  const stripeSignature = req.headers['stripe-signature'] as string;
  if (stripeSignature == null) { console.log('No stripe signature found!'); }

  const secret = 'whsec_1.........................'

  const event = stripe.webhooks.constructEvent(stripePayload, stripeSignature?.toString(), secret);


  console.log(event)



  if (event.type === 'payment_intent.succeeded') {
    const paymentIntent = event.data.object;
    

    console.log(paymentIntent)


    new User({
      userId: 'customer',
      expire: AddTimeToDate('1d', true)
    })//.save()
    console.log('Cliente adicionado ao banco de dados');

    res.status(200).end();
  } else {
    console.log(event.type)
    res.status(400).end();
  }
});

flat kettle
#

you're setting metadata on the CheckoutSession object, that does not copy to the PaymentIntent object which is what your webook is inspecting.

turbid nexus
#
{
  id: 'pi_3OJZQyKdtAoK1yx10p0tCf0a',
  object: 'payment_intent',
  amount: 3000,
  amount_capturable: 0,
  amount_details: { tip: {} },
  amount_received: 3000,
  application: null,
  application_fee_amount: null,
  automatic_payment_methods: null,
  canceled_at: null,
  cancellation_reason: null,
  capture_method: 'automatic',
  client_secret: 'pi_3OJZQyKdtAoK1yx10p0tCf0a_secret_VSagVFTmIYhwFs1z8bnbQQG6p',
  confirmation_method: 'automatic',
  created: 1701685888,
  currency: 'brl',
  customer: null,
  description: null,
  invoice: null,
  last_payment_error: null,
  latest_charge: 'ch_3OJZQyKdtAoK1yx10Lw2lmaD',
  livemode: false,
  metadata: {},
  next_action: null,
  on_behalf_of: null,
  payment_method: 'pm_1OJZQyKdtAoK1yx1eKrYxg0s',
  payment_method_configuration_details: null,
  payment_method_options: {
    card: {
      installments: null,
      mandate_options: null,
      network: null,
      request_three_d_secure: 'automatic'
    }
  },
  payment_method_types: [ 'card' ],
  processing: null,
  receipt_email: null,
  review: null,
  setup_future_usage: null,
  shipping: null,
  source: null,
  statement_descriptor: null,
  statement_descriptor_suffix: null,
  status: 'succeeded',
  transfer_data: null,
  transfer_group: null
}
flat kettle
#

either set the metadata on th PaymentIntent using payment_intent_data.metadata on the CheckoutSession, or listen to the checkout.session.completed webhook event instead

turbid nexus
#

so instead of using "payment_intent.succeeded" I would use "checkout.session.completed"?

flat kettle
turbid nexus
flat kettle
#

yes

turbid nexus
#

ok

#

The checkout.session.completed event does not actually appear to be called when the payment is approved

flat kettle
#

it is, if you're using Checkout. Happy to clarify if you have some example payment ID involved

turbid nexus
#

I will have to store everything.

flat kettle
#

not sure what you mean

turbid nexus
#

I have to generate the payment ID and store it in the database, give the ID to the customer

flat kettle
#

sorry I don't understand

#

what I meant was, if you are claiming you're not getting a checkout.session.completed event, show me the ID like a PaymentIntent pi_xxx that you are getting when you test this, so I can have a look at your account logs,, and I'll show you that there is a checkout.session.completed event and the likely reason you don't see it.

turbid nexus
#

I'll take a look as soon as I get off work.

#

but I am receiving the checkout.session.completed event

#

My question was just whether this event is triggered when the payment is approved, but you said yes, so it's ok...

shell pond
#

By "payment is approved" you mean payment succeeds?

#

If you use Stripe Checkout, both checkout.session.completed and payment_intent.succeeded events are triggered, when the payment is successful. The former contains the Checkout Session object (which has your metadata), and the latter contains the PaymentIntent object, which won't have the same metadata if you use the code that you provided above. Now, the question is what you're actually trying to achieve.

turbid nexus
#

Aaaaaa okkk

#

Now I understand, there are 2 events, one brings the metadata and the other is when it is actually approved

shell pond
#

Not exactly. Both are happening at the same time, PaymentIntent is just a more low level API.

#

But because you set the metadata on you Checkout Session object when creating it, the payload of checkout.session.completed (the payload is the Checkout Session object itself) will contain the metadata. If you set the payment_intent_data.metadata value when creating the Checkout Session, then the PaymentIntent object will contain it.