Checkout
Stripe Checkout is a prebuilt, hosted payment page optimized for conversion. Whether you offer one-time purchases or subscriptions, you can use Checkout to easily and securely accept payments online.

Accept a payment

The easiest way to use Checkout to accept a single payment is to create a checkout session in the server and then use the Stripe Service to redirect in the client.
To see more details on a full example, please check the official Stripe Docs.
checkout.component.ts
checkout.component.html
server.js
1
import { Component } from '@angular/core';
2
import { HttpClient } from '@angular/common/http';
3
import { switchMap } from 'rxjs/operators';
4
5
import { StripeService } from 'ngx-stripe';
6
7
@Component({
8
selector: 'app-checkout',
9
templateUrl: './checkout.component.html'
10
})
11
export class CheckoutComponent {
12
constructor(
13
private http: HttpClient,
14
private stripeService: StripeService
15
) {}
16
17
checkout() {
18
// Check the server.js tab to see an example implementation
19
this.http.post('/create-checkout-session', {})
20
.pipe(
21
switchMap(session => {
22
return this.stripeService.redirectToCheckout({ sessionId: session.id })
23
})
24
)
25
.subscribe(result => {
26
// If `redirectToCheckout` fails due to a browser or network
27
// error, you should display the localized error message to your
28
// customer using `error.message`.
29
if (result.error) {
30
alert(result.error.message);
31
}
32
});
33
}
34
}
Copied!
1
<button (click)="checkout()">
2
GO TO CHECKOUT
3
</button>
Copied!
1
// This example sets up an endpoint using the Express framework.
2
// Watch this video to get started: https://youtu.be/rPR2aJ6XnAc.
3
4
const express = require('express');
5
const app = express();
6
const stripe = require('stripe')('***your secret key***');
7
8
app.post('/create-checkout-session', async (req, res) => {
9
const session = await stripe.checkout.sessions.create({
10
payment_method_types: ['card'],
11
line_items: [
12
{
13
price_data: {
14
currency: 'usd',
15
product_data: {
16
name: 'T-shirt',
17
},
18
unit_amount: 2000,
19
},
20
quantity: 1,
21
},
22
],
23
mode: 'payment',
24
success_url: 'https://example.com/success',
25
cancel_url: 'https://example.com/cancel',
26
});
27
28
res.json({ id: session.id });
29
});
30
31
app.listen(4242, () => console.log(`Listening on port ${4242}!`));
Copied!

Create a Subscription

In a very similar way as the previous example, you can use Checkout to create a subscription with different pricing options.
As you can see in the server tab, the priceId comes in the body and we use it to create the session. This allows the user to choose the price. In the example below we use different buttons, but it would be easy to just change the priceId base on any other condition, like a select field for example.
Also, notice the mode attribute in the server.js tab. It was payment in the previous example but it is subscription in this one.
Again, please visit the Stripe Docs site for more details.
checkout.component.ts
checkout.component.html
server.js
1
import { Component } from '@angular/core';
2
import { HttpClient } from '@angular/common/http';
3
import { switchMap } from 'rxjs/operators';
4
5
import { StripeService } from 'ngx-stripe';
6
7
@Component({
8
selector: 'app-checkout',
9
templateUrl: './checkout.component.html'
10
})
11
export class CheckoutComponent {
12
constructor(
13
private http: HttpClient,
14
private stripeService: StripeService
15
) {}
16
17
checkout() {
18
// Check the server.js tab to see an example implementation
19
this.https.post('/create-checkout-session', { priceId })
20
.pipe(
21
switchMap(session => {
22
return this.stripeService.redirectToCheckout({ sessionId: session.id })
23
})
24
)
25
.subscribe(result => {
26
// If `redirectToCheckout` fails due to a browser or network
27
// error, you should display the localized error message to your
28
// customer using `error.message`.
29
if (result.error) {
30
alert(result.error.message);
31
}
32
});
33
}
34
}
Copied!
1
<div>Sign Up for the awesome product</div>
2
3
<button (click)="checkout('price_Z0FvDp6vZvdwRZ')">
4
10$ / month
5
</button>
6
7
<button (click)="checkout('price_Z2FvDp6vZvdwRZ')">
8
25$ / month
9
</button>
Copied!
1
// This example sets up an endpoint using the Express framework.
2
// Watch this video to get started: https://youtu.be/rPR2aJ6XnAc.
3
const express = require('express');
4
const app = express();
5
const stripe = require('stripe')('***your_secret_key****')
6
7
app.post('/create-checkout-session', async (req, res) => {
8
const { priceId } = req.body;
9
10
// See https://stripe.com/docs/api/checkout/sessions/create
11
// for additional parameters to pass.
12
try {
13
const session = await stripe.checkout.sessions.create({
14
mode: 'subscription',
15
payment_method_types: ['card'],
16
line_items: [
17
{
18
price: priceId,
19
// For metered billing, do not pass quantity
20
quantity: 1,
21
},
22
],
23
// {CHECKOUT_SESSION_ID} is a string literal; do not change it!
24
// the actual Session ID is returned in the query parameter when your customer
25
// is redirected to the success page.
26
success_url: 'https://example.com/success',
27
cancel_url: 'https://example.com/cancel',
28
});
29
30
res.send({
31
sessionId: session.id,
32
});
33
} catch (e) {
34
res.status(400);
35
return res.send({
36
error: {
37
message: e.message,
38
}
39
});
40
}
41
});
Copied!

Customizing Checkout

There is a lot of elements that you can customize about your Checkout integration:
  • Branding
  • Policies and contact information
  • Customizing the submit button
Please check the docs for more information.

Customize your success page

You can use details from a Checkout Session to display an order confirmation page for your customer after the payment. To use the details from a Checkout Session:
  1. 1.
    Modify the success_url to pass the Checkout Session ID to the client side
  2. 2.
    Look up the Checkout Session using the ID on your success page
  3. 3.
    Use the Checkout Session to customize what's displayed on your success page
Here is a minimal example build the html with express (definitely better ways to do it)
1
// This example sets up an endpoint using the Express framework.
2
// Watch this video to get started: https://youtu.be/rPR2aJ6XnAc.
3
4
const express = require('express');
5
const app = express();
6
7
// Set your secret key. Remember to switch to your live secret key in production.
8
// See your keys here: https://dashboard.stripe.com/apikeys
9
const stripe = require('stripe')('***your secret key****');
10
11
app.post('/order/success', async (req, res) => {
12
const session = await stripe.checkout.sessions.retrieve(req.query.session_id);
13
const customer = await stripe.customers.retrieve(session.customer);
14
15
res.send(`
16
<html>
17
<body>
18
<h1>Thanks for your order, ${customer.name}!</h1>
19
</body>
20
</html>
21
`);
22
});
23
24
app.listen(4242, () => console.log(`Listening on port ${4242}!`));
Copied!
Last modified 1mo ago