Home > Net >  Get undefined when stripe return result
Get undefined when stripe return result

Time:02-18

Hello guy I am doing payment with stripe, I am already success payment (checked on dashboard.striped.com) but i can't get the result return. Below is my code enter image description here Node

app.post('/payment', async (req, res) => {
  const { token,
    totalPrice } = req.body
  const idempontencyKey = uuidv4()
  return stripe.customers.create({
    email:token.email,
    source:token.id
  }).then(customer => {
    stripe.charges.create({
      amount: Math.ceil(totalPrice),
      currency: 'usd',
      customer:customer.id,
    },{idempotencyKey: idempontencyKey})
  }).then(result => {
    res.status(200).json(result) // result in here is undefined
  }).catch(err => console.log(err))
});

Here is my checkout component

<StripeCheckout stripeKey='pk_test_51KU0KNHzr2aPULLHERi1fWVBHp2Oxy1BTeBG0gfJ6O9DeRwuiGN1csKbpJMMPLvZjHTv5bo2qKguE8RQZnpCekjD00KDxRcNhm' token={makePayment} name='Buy product' amount={order.totalPrice} >
   <ButtonComponent>Pay ${order.totalPrice}</ButtonComponent>
</StripeCheckout>

And this is makePayment

const makePayment = async (token) => {
    const config = {
      headers: {
        'Content-Type': 'application/json'
      }
    }
    const data = await axios.post('/payment', {
      token,
      totalPrice: order.totalPrice
    }, config)
    console.log('data',data.data) // this return empty string
  }

Anyone have any ideas? Thank you!!!

CodePudding user response:

The part

}).then(customer => {
  stripe.charges.create({
    amount: Math.ceil(totalPrice),
    currency: 'usd',
    customer:customer.id,
  },{idempotencyKey: idempontencyKey})
})

does not return anything, so the next .then will receive undefined.

Change it to

}).then(customer => {
  return stripe.charges.create({
    amount: Math.ceil(totalPrice),
    currency: 'usd',
    customer:customer.id,
  },{idempotencyKey: idempontencyKey})
})

CodePudding user response:

In this then, you just call create charges function but return nothing. So that in the next then, it receive nothing.

}).then(customer => {
  return stripe.charges.create({
    amount: Math.ceil(totalPrice),
    currency: 'usd',
    customer:customer.id,
  },{idempotencyKey: idempontencyKey})
})
  • Related