Refunds
The Refunds API allows you to create and manage refunds for charges. A refund can be for the full amount of the original charge or a partial amount. Refunds are processed against a specific payment_intent_id.
For more details on the payment process itself, please see the Payment Intents API Reference.
The Refund Object#
A Refund object contains detailed information about a specific refund transaction.
Attribute | Type | Description |
|---|---|---|
|
| Unique identifier for the refund object. |
|
|
|
|
| An arbitrary string attached to the object. |
|
| The amount of the refund. |
|
| Three-letter ISO currency code. |
|
| ID of the customer associated with this refund. |
|
| ID of the Payment Intent that was refunded. |
|
| (Optional) ID of the invoice associated with this refund. |
|
| (Optional) ID of the subscription associated with this refund. |
|
| A set of key-value pairs that you can attach to an object. |
|
| The status of the refund. Can be |
|
| (Optional) The reason for the refund. Can be |
|
| (Optional) If the refund fails, this provides a machine-readable failure reason. |
|
| Timestamp of when the object was created. |
|
| Timestamp of the last update to the object. |
Create a Refund#
Creates a new refund object.
Parameters
Name | Type | Description |
|---|---|---|
|
| Required. The identifier of the Payment Intent to refund. |
|
| The amount to refund. Cannot be greater than the original charge amount. If omitted, a full refund is issued. |
|
| An optional description for the refund. |
|
| The reason for the refund. Valid options include |
|
| A set of key-value pairs to store with the refund object. |
Returns
Returns the newly created Refund object.
Example
import payment from '@blocklet/payment-js';
async function createRefund(paymentIntentId) {
try {
const refund = await payment.refunds.create({
payment_intent_id: paymentIntentId,
amount: '500', // Refund 5.00 units of currency
reason: 'requested_by_customer',
metadata: { order_id: '6735' }
});
console.log('Refund created:', refund.id);
return refund;
} catch (error) {
console.error('Error creating refund:', error.message);
}
}
// Replace 'pi_xxxxxxxx' with a valid Payment Intent ID
createRefund('pi_xxxxxxxx');Example Response
{
"id": "re_1J3j4k5L6m7N8o9PqRsTuVwX",
"livemode": false,
"description": null,
"amount": "500",
"currency_id": "usd",
"customer_id": "cus_aBcDeFgHiJkLmNoP",
"payment_intent_id": "pi_xxxxxxxx",
"invoice_id": "in_1J3j4k5L6m7N8o9PqRsTuVwY",
"subscription_id": null,
"metadata": {
"order_id": "6735"
},
"status": "succeeded",
"reason": "requested_by_customer",
"failure_reason": null,
"created_at": "2023-10-27T10:00:00.000Z",
"updated_at": "2023-10-27T10:00:05.000Z"
}Retrieve a Refund#
Retrieves the details of an existing refund.
Parameters
Name | Type | Description |
|---|---|---|
|
| Required. The unique identifier of the refund to retrieve. |
Returns
Returns the Refund object if a valid ID was provided.
Example
import payment from '@blocklet/payment-js';
async function getRefundDetails(refundId) {
try {
const refund = await payment.refunds.retrieve(refundId);
console.log('Retrieved refund:', refund);
return refund;
} catch (error) {
console.error(`Error retrieving refund ${refundId}:`, error.message);
}
}
// Replace 're_xxxxxxxx' with a valid Refund ID
getRefundDetails('re_xxxxxxxx');Example Response
{
"id": "re_1J3j4k5L6m7N8o9PqRsTuVwX",
"livemode": false,
"description": null,
"amount": "500",
"currency_id": "usd",
"customer_id": "cus_aBcDeFgHiJkLmNoP",
"payment_intent_id": "pi_xxxxxxxx",
"status": "succeeded",
"reason": "requested_by_customer",
"created_at": "2023-10-27T10:00:00.000Z",
"updated_at": "2023-10-27T10:00:05.000Z"
}List Refunds#
Returns a paginated list of refunds. You can filter the list based on various parameters.
Parameters
Name | Type | Description |
|---|---|---|
|
| (Optional) Filter refunds by status (e.g., |
|
| (Optional) Only return refunds for the given invoice. |
|
| (Optional) Only return refunds for the given subscription. |
|
| (Optional) Only return refunds for the given currency. |
|
| (Optional) Only return refunds for the given customer. |
|
| (Optional) The page number for pagination, starting at 1. |
|
| (Optional) The number of items per page. Defaults to 20. |
|
| (Optional) Sort order for the results. Example: |
Returns
Returns a paginated object containing a count of total items and a list of Refund objects.
Example
import payment from '@blocklet/payment-js';
async function listSuccessfulRefunds() {
try {
const refunds = await payment.refunds.list({
status: 'succeeded',
pageSize: 5
});
console.log(`Found ${refunds.count} successful refunds.`);
refunds.list.forEach(refund => {
console.log(`- ID: ${refund.id}, Amount: ${refund.amount}`);
});
return refunds;
} catch (error) {
console.error('Error listing refunds:', error.message);
}
}
listSuccessfulRefunds();Example Response
{
"count": 25,
"list": [
{
"id": "re_1J3j4k5L6m7N8o9PqRsTuVwX",
"amount": "500",
"currency_id": "usd",
"status": "succeeded",
"created_at": "2023-10-27T10:00:00.000Z"
},
{
"id": "re_2K4k5l6M7n8O9p0QrStUvWxY",
"amount": "1200",
"currency_id": "usd",
"status": "succeeded",
"created_at": "2023-10-26T14:30:00.000Z"
}
]
}Search Refunds#
Returns a paginated list of refunds that match a search query.
Parameters
Name | Type | Description |
|---|---|---|
|
| Required. The search term to find refunds. This can be a customer email, a description, or an ID. |
|
| (Optional) The page number for pagination, starting at 1. |
|
| (Optional) The number of items per page. Defaults to 20. |
Returns
Returns a paginated object containing a count of total items and a list of matching Refund objects.
Example
import payment from '@blocklet/payment-js';
async function searchForRefund(query) {
try {
const results = await payment.refunds.search({ query: query });
console.log(`Found ${results.count} refunds matching '${query}'.`);
results.list.forEach(refund => {
console.log(`- ID: ${refund.id}, Status: ${refund.status}`);
});
return results;
} catch (error) {
console.error('Error searching refunds:', error.message);
}
}
searchForRefund('pi_xxxxxxxx');Example Response
{
"count": 1,
"list": [
{
"id": "re_1J3j4k5L6m7N8o9PqRsTuVwX",
"amount": "500",
"currency_id": "usd",
"payment_intent_id": "pi_xxxxxxxx",
"status": "succeeded",
"reason": "requested_by_customer",
"created_at": "2023-10-27T10:00:00.000Z"
}
]
}After managing refunds, you might want to explore how to handle Webhooks to receive real-time notifications for events like refund.succeeded.