/stripe-guides

How to confirm a payment intent with Stripe?

Learn how to confirm a payment intent with Stripe, including server-side and client-side steps, code examples, and best practices for secure payment processing.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free consultation

How to confirm a payment intent with Stripe?

How to Confirm a Payment Intent with Stripe

 

Introduction

 

Confirming a payment intent is a crucial step in the Stripe payment process. Payment intents represent a customer's intent to pay and must be confirmed to complete the transaction. This tutorial will guide you through the process of confirming a payment intent with Stripe, covering both server-side and client-side implementations.

 

Step 1: Set Up Your Stripe Account

 

Before you can confirm payment intents, you need to set up your Stripe account and get your API keys:

  • Sign up for a Stripe account at Stripe Dashboard.
  • Navigate to Developers → API keys in your Stripe dashboard.
  • Copy your publishable key (for client-side) and secret key (for server-side).

 

Step 2: Install Stripe Libraries

 

Depending on your tech stack, you'll need to install the appropriate Stripe libraries:

For server-side (Node.js):

npm install stripe

For client-side:

<script src="https://js.stripe.com/v3/"></script>

 

Step 3: Create a Payment Intent on the Server

 

Before confirming a payment intent, you need to create one. Here's how to do it in Node.js:

const stripe = require('stripe')('sk_test_YOUR_SECRET_KEY');

async function createPaymentIntent(amount, currency, paymentMethodId) {
  try {
    const paymentIntent = await stripe.paymentIntents.create({
      amount: amount, // Amount in smallest currency unit (e.g., cents for USD)
      currency: currency, // e.g., 'usd', 'eur'
      payment\_method: paymentMethodId,
      confirmation\_method: 'manual', // Confirm manually later
      // Optional: Set other parameters
      // receipt\_email: customer.email,
      // customer: customer.id,
      // metadata: { order\_id: '123456' }
    });
    
    return paymentIntent;
  } catch (error) {
    console.error('Error creating payment intent:', error);
    throw error;
  }
}

 

Step 4: Confirm a Payment Intent on the Server

 

To confirm a payment intent on the server side:

async function confirmPaymentIntent(paymentIntentId) {
  try {
    const paymentIntent = await stripe.paymentIntents.confirm(paymentIntentId, {
      // Optional: You can specify a return\_url for redirect-based payments
      // return\_url: 'https://yourwebsite.com/payment-success',
    });
    
    return paymentIntent;
  } catch (error) {
    console.error('Error confirming payment intent:', error);
    throw error;
  }
}

 

Step 5: Implement a Complete Server-Side Payment Flow

 

Here's a more complete example using Express.js:

const express = require('express');
const stripe = require('stripe')('sk_test_YOUR_SECRET_KEY');
const app = express();

app.use(express.json());

// Endpoint to create a payment intent
app.post('/create-payment-intent', async (req, res) => {
  try {
    const { amount, currency, paymentMethodId } = req.body;
    
    const paymentIntent = await stripe.paymentIntents.create({
      amount,
      currency,
      payment\_method: paymentMethodId,
      confirmation\_method: 'manual',
    });
    
    res.json({ clientSecret: paymentIntent.client\_secret });
  } catch (error) {
    res.status(400).json({ error: error.message });
  }
});

// Endpoint to confirm a payment intent
app.post('/confirm-payment-intent', async (req, res) => {
  try {
    const { paymentIntentId } = req.body;
    
    const paymentIntent = await stripe.paymentIntents.confirm(paymentIntentId);
    
    res.json({ success: true, paymentIntent });
  } catch (error) {
    res.status(400).json({ error: error.message });
  }
});

app.listen(3000, () => console.log('Server running on port 3000'));

 

Step 6: Confirm a Payment Intent on the Client Side

 

For client-side confirmation (which is more common in web applications), you'll need to use Stripe.js:

<!DOCTYPE html>
<html>
<head>
  <title>Stripe Payment</title>
  <script src="https://js.stripe.com/v3/"></script>
</head>
<body>
  <form id="payment-form">
    <div id="card-element">
      
Linkedin logoDribbble logo

We are a software development agency specializing in web apps, mobile apps, business apps and AI apps.

@2024 Rapid Developers LLC. All rights reserved | Terms of Service | Privacy Policy