Payment Intents
A Payment Intent is an object that represents your intention to collect payment from a customer, tracking the lifecycle of the payment process from creation to completion. It is a central part of the PaymentKit API, guiding you through the necessary steps to confirm and capture a payment.
Most integrations will use a Checkout Session to create and handle Payment Intents automatically. However, you can use this API for more direct control over the payment lifecycle.
The Payment Intent Lifecycle#
A Payment Intent progresses through various statuses as it moves from creation to completion. Understanding this flow is key to managing payments effectively.
The Payment Intent Object#
The PaymentIntent object contains detailed information about a transaction. Here are some of its key attributes:
Attribute | Type | Description |
|---|---|---|
|
| Unique identifier for the Payment Intent, prefixed with |
|
| The amount intended to be collected. |
|
| The three-letter ISO currency code (or custom currency ID). |
|
| ID of the Customer this Payment Intent belongs to, if one exists. |
|
| The current status of the Payment Intent. Can be |
|
| Defines how the payment is captured. Can be |
|
| Defines how the payment is confirmed. Can be |
|
| The last payment error encountered, if any. Contains details about the failure. |
|
| A set of key-value pairs that you can attach to an object. |
|
| The timestamp when the Payment Intent was created. |
Create a Payment Intent#
Creates a new Payment Intent.
Parameters
Name | Type | Description |
|---|---|---|
|
| Required. The transaction amount as a string. |
|
| Required. The currency of the payment. |
|
| Optional. The ID of the customer associated with this payment. |
|
| Optional. An arbitrary string attached to the object. |
|
| Optional. The ID of the PaymentMethod to attach to this PaymentIntent. |
|
| Optional. The list of payment method types (e.g., |
|
| Optional. Controls when the funds will be captured. Can be |
|
| Optional. A set of key-value pairs to store additional information. |
Returns
Returns the created PaymentIntent object.
Example
async function createPaymentIntent() {
try {
const paymentIntent = await payment.paymentIntents.create({
amount: '2000',
currency_id: 'usd',
payment_method_types: ['stripe'],
description: 'Charge for services rendered',
});
console.log('Payment Intent created:', paymentIntent);
} catch (error) {
console.error('Error:', error.message);
}
}
createPaymentIntent();Example Response
{
"id": "pi_1J2a3b4c5d6e7f8g",
"livemode": false,
"description": "Charge for services rendered",
"amount": "2000",
"amount_received": "0",
"amount_capturable": "0",
"currency_id": "usd",
"customer_id": null,
"payment_method_id": null,
"status": "requires_payment_method",
"capture_method": "automatic",
"confirmation_method": "automatic",
"payment_method_types": [
"stripe"
],
"metadata": {},
"created_at": "2023-10-27T10:00:00.000Z",
"updated_at": "2023-10-27T10:00:00.000Z"
}Retrieve a Payment Intent#
Retrieves the details of an existing Payment Intent.
Parameters
Name | Type | Description |
|---|---|---|
|
| Required. The unique identifier of the Payment Intent to retrieve. |
Returns
Returns the PaymentIntent object, with some fields expanded into full objects.
Example
async function retrievePaymentIntent(id) {
try {
const paymentIntent = await payment.paymentIntents.retrieve(id);
console.log('Retrieved Payment Intent:', paymentIntent.status);
} catch (error) {
console.error('Error:', error.message);
}
}
retrievePaymentIntent('pi_1J2a3b4c5d6e7f8g');Example Response
{
"id": "pi_1J2a3b4c5d6e7f8g",
"livemode": false,
"description": "Charge for services rendered",
"amount": "2000",
"status": "succeeded",
"currency_id": "usd",
"customer_id": "cus_a1b2c3d4e5f6",
// ... other fields
}Update a Payment Intent#
Updates properties on a Payment Intent object. You can update details like metadata or description.
Parameters
Name | Type | Description |
|---|---|---|
|
| Required. The ID of the Payment Intent to update. |
|
| Required. An object containing the fields to update. |
Updatable Fields in data
Name | Type | Description |
|---|---|---|
|
| An arbitrary string attached to the object. |
|
| A set of key-value pairs to store additional information. |
|
| The email address that the receipt for a successful payment will be sent to. |
Returns
Returns the updated PaymentIntent object.
Example
async function updatePaymentIntent(id) {
try {
const paymentIntent = await payment.paymentIntents.update(id, {
metadata: { order_id: '6735' }
});
console.log('Payment Intent updated:', paymentIntent.metadata);
} catch (error) {
console.error('Error:', error.message);
}
}
updatePaymentIntent('pi_1J2a3b4c5d6e7f8g');Example Response
{
"id": "pi_1J2a3b4c5d6e7f8g",
"metadata": {
"order_id": "6735"
},
// ... other fields
}List all Payment Intents#
Returns a paginated list of your Payment Intents. The list can be filtered by various parameters.
Parameters
Name | Type | Description |
|---|---|---|
|
| Optional. Only return Payment Intents with the given status. |
|
| Optional. Only return Payment Intents for the given invoice. |
|
| Optional. Only return Payment Intents for the given customer. |
|
| Optional. Filter by custom metadata fields. |
|
| Optional. The page number for pagination. |
|
| Optional. The number of items per page (default: 20). |
|
| Optional. Sort order (e.g., |
Returns
A paginated object containing a list of PaymentIntent objects and the total count.
Example
async function listSuccessfulPayments() {
try {
const paymentIntents = await payment.paymentIntents.list({
status: 'succeeded',
pageSize: 10
});
console.log(`Found ${paymentIntents.count} successful payments.`);
paymentIntents.list.forEach(pi => console.log(pi.id));
} catch (error) {
console.error('Error:', error.message);
}
}
listSuccessfulPayments();Example Response
{
"count": 42,
"list": [
{
"id": "pi_1J2a3b4c5d6e7f8g",
"status": "succeeded",
"amount": "2000",
// ... other fields
},
{
"id": "pi_zY9x8w7v6u5t4s3r",
"status": "succeeded",
"amount": "5000",
// ... other fields
}
]
}Search Payment Intents#
Performs a free-text search for Payment Intents based on a query string.
Parameters
Name | Type | Description |
|---|---|---|
|
| Required. The search query. |
|
| Optional. The page number for pagination. |
|
| Optional. The number of items per page (default: 20). |
Returns
A paginated object containing a list of matching PaymentIntent objects and the total count.
Example
async function searchPayments(query) {
try {
const results = await payment.paymentIntents.search({ query: query });
console.log(`Found ${results.count} results for '${query}'.`);
} catch (error) {
console.error('Error:', error.message);
}
}
searchPayments('order_id:6735');Example Response
{
"count": 1,
"list": [
{
"id": "pi_1J2a3b4c5d6e7f8g",
"status": "succeeded",
"metadata": {
"order_id": "6735"
},
// ... other fields
}
]
}Refund a Payment Intent#
Creates a Refund object for a charged Payment Intent. This can be a full or partial refund.
Parameters
Name | Type | Description |
|---|---|---|
|
| Required. The ID of the Payment Intent to refund. |
|
| Required. An object containing refund details. |
Fields in data
Name | Type | Description |
|---|---|---|
|
| Optional. The amount to refund. If not provided, a full refund is issued. |
|
| Optional. The reason for the refund. Can be |
|
| Optional. An internal note about the refund. |
Returns
Returns an expanded Refund object.
Example
async function refundPayment(id) {
try {
const refund = await payment.paymentIntents.refund(id, {
amount: '500',
reason: 'requested_by_customer',
description: 'Customer requested partial refund.'
});
console.log('Refund processed:', refund.id);
} catch (error) {
console.error('Error:', error.message);
}
}
refundPayment('pi_1J2a3b4c5d6e7f8g');Example Response
{
"id": "re_a1b2c3d4e5f6g7h8",
"amount": "500",
"currency_id": "usd",
"payment_intent_id": "pi_1J2a3b4c5d6e7f8g",
"reason": "requested_by_customer",
"status": "succeeded",
"created_at": "2023-10-27T12:00:00.000Z",
// ... other fields
}Now that you understand how to manage the payment lifecycle with Payment Intents, you may want to explore the Refunds API in more detail or learn about creating reusable Payment Links.