Checkout Sessions
A Checkout Session represents your customer's session as they are making a payment. It is the primary mechanism for collecting payment details to create a one-time payment or a subscription. After you create a Session, you redirect the customer to a secure, hosted payment page. Once the payment is complete, the customer is redirected back to your application.
Related guides: One-Time Payments, Subscriptions.
The Checkout Session Object#
A Checkout Session object contains all the details about the payment flow, including line items, payment status, and customer information.
Attribute | Type | Description |
|---|---|---|
| string | Unique identifier for the object. |
| string | The URL to which you should redirect your customer to complete the payment. |
| string | The status of the Checkout Session. Can be |
| string | The payment status of the session. Can be |
| string | The mode of the Checkout Session, which determines the type of payment being collected. Can be |
| string | Total of all line item prices. |
| string | The total amount after discounts and taxes are applied. |
| string | The ID of the currency used for the payment. |
| string | The ID of the Customer for this session. |
| array | A list of items the customer is purchasing. |
| object | A set of key-value pairs that you can attach to an object. |
| string | The URL to redirect to on successful payment. |
| string | The URL to redirect to if the customer cancels the session. |
| number | The Unix timestamp at which the Checkout Session will expire. |
Create a Checkout Session#
Creates a Session for a customer to enter their payment details.
Create a Checkout Session
const session = await payment.checkoutSessions.create({
success_url: 'https://example.com/success',
cancel_url: 'https://example.com/cancel',
line_items: [
{
price_id: 'price_12345',
quantity: 1,
},
],
mode: 'payment', // or 'subscription'
});Parameters
Name | Type | Description |
|---|---|---|
| array | Required. A list of line item objects. See the Line Item Properties table below for details. |
| string | Required. The URL to which the customer will be directed after a successful payment. |
| string | Required. The URL to which the customer will be directed if they cancel the payment. |
| boolean | Enables the use of promotion codes. Defaults to |
| string | Specifies whether to collect the customer's billing address. Can be |
| string | A unique string to reference the Checkout Session. This can be used to reconcile the session with your internal systems. |
| string | The ID of an existing Customer to associate with this session. |
| string | Determines how to handle customer creation. Can be |
| number | A Unix timestamp after which the session will expire. Defaults to 24 hours from creation. |
| object | A set of key-value pairs to store additional information about the object. |
| object | Data that will be used to create the underlying PaymentIntent. See the Payment Intent Data Properties table below. |
| object | Data that will be used to create a subscription if the session is in |
| boolean | If |
Line Item Properties
Name | Type | Description |
|---|---|---|
| string | The ID of the Price object. |
| number | The quantity of the line item being purchased. Defaults to 1. |
| object | Allows the customer to adjust the quantity of this line item on the checkout page. Contains |
Subscription Data Properties
Name | Type | Description |
|---|---|---|
| string | An optional description for the subscription. |
| number | The number of trial days for the subscription. |
| object | A set of key-value pairs to store on the subscription. |
Returns
The created Checkout Session object.
Response
{
"id": "cs_123456789",
"object": "checkout.session",
"url": "https://checkout.example.com/pay/cs_123456789",
"status": "open",
"payment_status": "unpaid",
"mode": "payment",
"success_url": "https://example.com/success",
"cancel_url": "https://example.com/cancel",
"line_items": [
{
"price_id": "price_12345",
"quantity": 1
}
],
"expires_at": 1678886400,
// ... other properties
}Retrieve a Checkout Session#
Retrieves the details of an existing Checkout Session.
Retrieve a Checkout Session
const session = await payment.checkoutSessions.retrieve('cs_123456789');Parameters
Name | Type | Description |
|---|---|---|
| string | Required. The unique identifier of the Checkout Session to retrieve. |
Returns
The retrieved Checkout Session object.
Response
{
"id": "cs_123456789",
"object": "checkout.session",
"status": "complete",
"payment_status": "paid",
// ... other properties
}Update a Checkout Session#
Updates a Checkout Session object by setting the values of the parameters passed. Any parameters not provided will be left unchanged.
Update a Checkout Session
const session = await payment.checkoutSessions.update('cs_123456789', {
metadata: {
order_id: '6735'
}
});Parameters
Name | Type | Description |
|---|---|---|
| string | Required. The ID of the session to update. |
| object | A set of key-value pairs to store on the object. This is the only field that can be updated. |
Returns
The updated Checkout Session object.
List all Checkout Sessions#
Returns a list of your Checkout Sessions. The sessions are returned sorted by creation date, with the most recent sessions appearing first.
List Checkout Sessions
const sessions = await payment.checkoutSessions.list({
limit: 5,
status: 'open'
});Parameters
Name | Type | Description |
|---|---|---|
| string | Filter sessions by status. Can be |
| string | Filter sessions by payment status. Can be |
| string | Only return sessions for the given customer ID. |
| string | Only return sessions for the given customer DID. |
| string | Only return the session for the given PaymentIntent. |
| string | Only return sessions created from the given Payment Link. |
| string | Only return the session for the given subscription. |
| string | Filter by custom metadata fields. For example, |
| number | The page number for pagination. Defaults to |
| number | The number of items per page. Defaults to |
Returns
A paginated object containing a list of Checkout Session objects.
Response
{
"count": 15,
"list": [
{
"id": "cs_123456789",
"object": "checkout.session",
"status": "open",
// ... other properties
},
{
"id": "cs_987654321",
"object": "checkout.session",
"status": "open",
// ... other properties
}
]
}Expire a Checkout Session#
Manually expires a Checkout Session. This action is irreversible.
Expire a Checkout Session
const session = await payment.checkoutSessions.expire('cs_123456789');Parameters
Name | Type | Description |
|---|---|---|
| string | Required. The ID of the Checkout Session to expire. |
Returns
The expired Checkout Session object with a status of expired.
Response
{
"id": "cs_123456789",
"object": "checkout.session",
"status": "expired",
// ... other properties
}