skip to Main Content

I was adding stripe payment in my project . I was using react-stripe-checkout and at backend was using node js with stripe installed.
At frontend I have inside render() :

const payNow= async token=>{
    try{
        const response = await axios({
            url : `/mycardpay/${localStorage.getItem('slno')}`,
            method : 'post',
            data : {
                amount : this.calculateTotalAmount(),
                token,
            }
        });

        if(response.data.message == 'Success'){
            console.log(response.data)
        }

    }catch(err){
        console.log(err)
    }
} 

than added :

<StripeCheckout   
    stripeKey="pk_stripekey..."  
    label='Pay Now'  
    name='Pay With Credit Card'  
    billingAddress   
    amount={this.calculateTotalAmount()*100}   
    description={`Your Total Amount $${this.calculateTotalAmount()}`}  
    token={payNow} 
/>

AT my backend I have :

const Stripe = require('stripe')(process.env.Secret_Key); 

async function paymentCard(req, res, next){
    try{
        const {usersl} = req.params;
        const {token, amount} = req.body;
        
        try{
            console.log(amount)
            console.log(token)
            console.log(token.card.last4)
            const customer = await Stripe.customers.create({
                email: token.email, // Use the email from the token
                source: token.id, // Use the token ID as the payment method source
              });
              
            await Stripe.paymentIntents.create({
                amount: amount, // Amount in cents
                currency: 'inr',
                payment_method_types: ['card'],
                payment_method: token.card.id, // Use the card ID from the token's card object
                customer: customer.id, 
                confirm: true,
                    
            })

It is the portion of my code that handles stripe payment and take the api to stripe server .
I am getting perfectly fine token.

{
  id: 'tok_1Ng5esSI10A4W78jlyIUAMLX',
  object: 'token',
  card: {
    id: 'card_1Ng5erSI10A4W78j5WRiJWUm',
    object: 'card',
    address_city: 'dhaka',
    address_country: 'Bangladesh',
    address_line1: 'asdad',
    address_line1_check: 'unchecked',
    address_line2: null,
    address_state: null,
    address_zip: '1244',
    address_zip_check: 'unchecked',
    brand: 'Visa',
    country: 'US',
    cvc_check: 'unchecked',
    dynamic_last4: null,
    exp_month: 4,
    exp_year: 2026,
    funding: 'credit',
    last4: '4242',
    name: 'asdad asd',
    tokenization_method: null,
    wallet: null
  },
  client_ip: '103.110.161.4',
  created: 1692276638,
  email: '[email protected]',
  livemode: false,
  type: 'card',
  used: false
}

Why in stripe website at payment i see it not succeded, I see it incomplete

I am expecting it to be completed.

2

Answers


  1. Chosen as BEST ANSWER

    Well, I just solved it finally. Latest stripe is way to secured and old methods in other tuts not work cause it sends require_action in backend after paymentIntents. So, we add another mode at frontend react js name @stripe/stripe-js.And add just few lines. At backend inside /payment (I used /mycardpay) rout :

     const customer = await Stripe.customers.create({
                    email: token.email, // Use the email from the token
                    source: token.id, // Use the token ID as the payment method source
                  });
    
                
                const paymentIntent = await Stripe.paymentIntents.create({
                    amount: amount, // Amount in cents
                    currency: 'inr',
                    payment_method_types: ['card'],
                    payment_method: token.card.id, // Use the card ID from the token's card object
                    customer: customer.id, 
                    confirm: true,
                       
                })
    
    
    
                if (paymentIntent.status === 'requires_action') {
                    // Return client_secret and requires_action to the frontend
                    console.log('1')
                    return res.status(200).json({
                        requires_action: true,
                        client_secret: paymentIntent.client_secret,
                        payment_method: paymentIntent.payment_method,
                    });
                } else if (paymentIntent.status === 'succeeded') {
                    console.log('2')
                    return res.status(200).json({
                        message: 'Success',
                    });
                } else {
                    // Handle other paymentIntent statuses
                    console.log('3')
                    return res.status(500).json({
                        message: 'Payment failed',
                    });
                }
            
    

    Than at Frontend we similiarly use react-js-checkout at start to start the payment.But, inside token={payNow} we add the process to handle require_action.

    import { loadStripe } from '@stripe/stripe-js';
    const payNow= async token=>{
                    const stripePromise = loadStripe('pk_test_........');
                    try{
                        const response = await axios({
                            url : `/mycardpay/${localStorage.getItem('slno')}`,
                            method : 'post',
                            data : {
                                amount : this.calculateTotalAmount(),
                                token,
                            }
                        });
            
                    if (response.data.requires_action) {
                        console.log('1')
                        const { client_secret,  payment_method} = response.data;
                        const stripe = await stripePromise;
                        
                        // Handle the customer action using Stripe.js
                        const result = await stripe.confirmCardPayment(client_secret, {
                            payment_method: payment_method.id,
                        });
        
                        if (result.error) {
                            console.log('1')
                            // Handle any error during customer action
                            console.error(result.error);
                        } else {
                            console.log('2')
                            // Payment completed successfully
                            console.log('Payment successful:', result.paymentIntent);
                        }
                    } else if (response.data.message === 'Success') {
                        console.log('3')
                        // Handle success without customer action
                        console.log('Payment successful:', response.data);
                    }
        
                }catch(err){
                    console.log(err)
                }
            }
    

    And start the whole process with your method to call .

    <StripeCheckout   
    stripeKey="pk_Test......"  
    label='Pay Now'  
    name='Pay With Credit Card'  
    billingAddress   
    amount={this.calculateTotalAmount()*100}   
    description={`Your Total Amount $${this.calculateTotalAmount()}`}  
    token={payNow} 
    />
    

    this.calculatetotalAmount() is just my function that calculate all product data cost it's nothing much.


  2. Looking at this packages github source, it appears to be several years out of date and is an implementation of Legacy Checkout which Stripe deprecated several years ago.

    Depending on what exactly you’re trying to achieve, you should either integrate using Stripe’s modern Checkout product, or look at leveraging @stripe/react-stripe-js to build a custom payment form (minimal example).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search