Accept a payment

Securely accept card payments online.

Overview

Collecting payments on your website consists of:

  • Collecting card information
  • Creating a PaymentIntent object to track a payment
  • Submitting the payment to Stripe for processing

In this guide we'll build a website that uses Stripe Elements to collect card information from the user, create a PaymentIntent from a Node backend, and submit the payment to Stripe.

We'll build this demo:

Try

1

Create an HTML file

We'll collect the card information using a library of prebuilt UI components: Stripe Elements.

For that we need some empty DOM nodes. So we create an html file with three elements.

  • #card-element will be the container of the card input element
  • #submit is the button to trigger the payment
  • and #card-errors is where we'll put the error messages

2

Add some styles

We add some styles to make the demo look a bit better.

3

Include scripts

To use the prebuild UI components, we need to include the Stripe.js script on our page.

We also include the client.js script, that's where we'll initialize the UI components and handle the user events.

4

Create a card element

The first thing we do is create an instance of Elements, create a card element and mount it to the DOM.

The card Element simplifies the form and minimizes the number of required fields by inserting a single, flexible input field that securely collects all necessary card and billing details. Otherwise, combine cardNumber, cardExpiry, and cardCvc Elements for a flexible, multi-input card form.

5

Display errors

Elements validates user input as it is typed. To help your customers catch mistakes, listen to change events on the card Element and display any errors.

6

Handle the sumbit button

The next thing we need to do is to confirm the payment when the user clicks the submit button. But for that first we need to create a PaymentIntent, and we can't do that on the client, we need a server.

7

Install the Stripe library

For easy access to the Stripe API from our application we'll use the official Stripe library for Node.

We also install express and body-parser.

8

And we add the boilerplate to serve index.html and client.js.

9

Create a PaymentIntent

And now we are ready to create the PaymentIntent.

Stripe uses a PaymentIntent object to represent your intent to collect payment from a customer, tracking charge attempts and payment state changes throughout the process.

We only need to pass the currency and amount for the payment. The amount represents how much to charge in the smallest currency unit (e.g., 98000 cents to charge $980.00 or 100 to charge ¥100, a zero-decimal currency).

Included in the returned PaymentIntent is a client secret, which we’ll use to securely complete the payment process instead of passing the entire PaymentIntent object. We send the client secret back to the client so we can use to confirm the payment.

10

Get the client secret

Now we can handle the user's click to the "Pay" button. We call the endpoint we created in the previous step and get the paymentClientSecret.

11

Submit the payment to Stripe

With the `paymentClientSecret` we can now call the `confirmCardPayment` method, which combines the info from the PaymentIntent (amount and currency) with the info from the card element (card number, CVC, expiration date, postal code), and submit the payment to Stripe.

When the payment completes successfully, the value of the returned PaymentIntent’s status property is succeeded. Check the status of a PaymentIntent in the Dashboard or by inspecting the status property on the object. If the payment is not successful, inspect the returned error to determine the cause.

12

Test the integration

By this point you should have a basic card integration that collects card details and makes a payment.

There are several test cards you can use in test mode to make sure this integration is ready:

4242424242424242
Succeeds and immediately processes the payment.
4000002500003155
Requires authentication. Stripe will trigger a modal asking for the customer to authenticate.
4000000000009995
Always fails with a decline code of insufficient_funds.

For the full list of test cards see our guide on testing.

<html>
<head>
<title>Stripe Payment</title>
</head>
<body>
<div id="card-element">
<!-- Elements will create inputs here -->
</div>
​
<button id="submit">Pay $980.00</button>
​
<p id="card-errors" role="alert">
<!-- We'll put the error messages here -->
</p>
</body>
</html>
​
<!-- We'll put the error messages here -->
<script src="https://js.stripe.com/v3/"></script>
​
<script src="https://js.stripe.com/v3/"></script>
CONTINUE WITH

Save a card after a payment

Learn how to save card details after a payment.

Place a hold on a card

Separate authorization and capture to create a charge now, but capture funds later.

Set up recurring payments

Learn how to save card details and charge your customers later.

See Also

You can learn more about the Payment Intents API and Elements: