Skip to main content

Refunds

Refund objects represent the return of funds to a customer for a previous charge. You can create new refunds, retrieve details for an existing one, and list all refunds for accounting purposes. When a refund is created, it is initially in a pending state and will be processed asynchronously.

The Refund Object

A typical Refund object contains the following key attributes:

  • id: Unique identifier for the refund object.
  • amount: The total amount refunded, in the smallest currency unit (e.g., cents).
  • currency_id: The identifier of the currency used for the refund.
  • status: The current status of the refund. Can be pending, succeeded, failed, or canceled.
  • reason: The reason for the refund. Possible values include duplicate, requested_by_customer, requested_by_admin, fraudulent, or expired_uncaptured_charge.
  • payment_intent_id: The ID of the Payment Intent that was refunded.
  • customer_id: The ID of the Customer to whom the refund was issued.
  • description: A description of the refund.
  • metadata: A set of key-value pairs that you can attach to the object for your own reference.
  • created_at: The timestamp when the refund was created.

Create a Refund

Creates a Refund object to refund a charge that has previously been created. This can be for the full amount of the charge or a partial amount.

Create a Refund

javascript
import payment from '@blocklet/payment-js';

async function issueRefund() {
  try {
    const refund = await payment.refunds.create({
      payment_intent_id: 'pi_xxxxxxxxxxxx',
      amount: '50.00', // Note: amount is a string representing the value in major units (e.g., dollars)
      currency_id: 'usd_xxxxxxxx',
      customer_id: 'cus_xxxxxxxx',
      payment_method_id: 'pm_xxxxxxxx',
      reason: 'requested_by_customer',
      description: 'Refund for order #54321',
      metadata: { order_id: '54321' }
    });
    console.log('Refund initiated:', refund);
  } catch (error) {
    console.error('Error creating refund:', error.message);
  }
}

issueRefund();

Parameters

NameTypeDescription
amountstringRequired. A positive string representing how much of this charge to refund. Cannot refund more than the charge's remaining refundable amount.
currency_idstringRequired. The identifier of the currency for the refund.
customer_idstringRequired. The identifier of the customer associated with the refund.
payment_method_idstringRequired. The identifier of the payment method used for the original charge.
payment_intent_idstringRequired. The identifier of the PaymentIntent to refund.
reasonstringRequired. String indicating the reason for the refund. Supported values are duplicate, requested_by_customer, requested_by_admin, fraudulent, expired_uncaptured_charge.
descriptionstringRequired. An arbitrary string attached to the object. Often useful for displaying to users. Maximum 200 characters.
metadataobjectOptional. A set of key-value pairs that you can attach to a refund object. This can be useful for storing additional information in a structured format.
invoice_idstringOptional. The ID of the invoice that's associated with this refund.
subscription_idstringOptional. The ID of the subscription that's associated with this refund.

Returns

Returns the created Refund object.

Response Example

json
{
  "id": "ref_xxxxxxxxxxxx",
  "object": "refund",
  "amount": "5000",
  "currency_id": "usd_xxxxxxxx",
  "customer_id": "cus_xxxxxxxx",
  "payment_intent_id": "pi_xxxxxxxxxxxx",
  "reason": "requested_by_customer",
  "status": "pending",
  "description": "Refund for order #54321",
  "metadata": {
    "order_id": "54321"
  },
  "created_at": "2023-10-27T10:00:00.000Z"
}

Retrieve a Refund

Retrieves the details of an existing refund by its unique ID.

Retrieve a Refund

javascript
import payment from '@blocklet/payment-js';

async function getRefundDetails(refundId) {
  try {
    const refund = await payment.refunds.retrieve(refundId);
    console.log('Refund details:', refund);
  } catch (error) {
    console.error(`Error retrieving refund ${refundId}:`, error.message);
  }
}

getRefundDetails('ref_xxxxxxxxxxxx');

Parameters

NameTypeDescription
idstringRequired. The unique identifier of the refund to retrieve.

Returns

Returns the Refund object if a valid ID was provided. Throws an error otherwise.

Response Example

json
{
  "id": "ref_xxxxxxxxxxxx",
  "object": "refund",
  "amount": "5000",
  "currency_id": "usd_xxxxxxxx",
  "customer_id": "cus_xxxxxxxx",
  "payment_intent_id": "pi_xxxxxxxxxxxx",
  "reason": "requested_by_customer",
  "status": "succeeded",
  "description": "Refund for order #54321",
  "metadata": {
    "order_id": "54321"
  },
  "created_at": "2023-10-27T10:00:00.000Z",
  "customer": { ... },
  "paymentCurrency": { ... },
  "paymentIntent": { ... },
  "paymentMethod": { ... }
}

List all Refunds

Returns a paginated list of all refunds. You can filter the list based on various criteria.

List Refunds

javascript
import payment from '@blocklet/payment-js';

async function listSuccessfulRefunds() {
  try {
    const response = await payment.refunds.list({
      status: 'succeeded',
      pageSize: 10
    });
    console.log(`Found ${response.count} successful refunds.`);
    response.list.forEach(refund => {
      console.log(`- Refund ID: ${refund.id}, Amount: ${refund.amount}`);
    });
  } catch (error) {
    console.error('Error listing refunds:', error.message);
  }
}

listSuccessfulRefunds();

Parameters

NameTypeDescription
pagenumberOptional. The page number for pagination, starting from 1. Defaults to 1.
pageSizenumberOptional. The number of objects to return per page. Defaults to 20.
statusstringOptional. A comma-separated string to filter refunds by status (e.g., 'succeeded,pending').
invoice_idstringOptional. Only return refunds for the given invoice.
subscription_idstringOptional. Only return refunds for the given subscription.
currency_idstringOptional. Only return refunds for the given currency.
customer_idstringOptional. Only return refunds for the given customer.

Returns

Returns a paginated object containing a list of Refund objects, the total count, and paging information.

Response Example

json
{
  "count": 15,
  "list": [
    {
      "id": "ref_xxxxxxxxxxxx",
      "object": "refund",
      "amount": "5000",
      "status": "succeeded",
      // ... other refund fields
    }
    // ... more refund objects
  ],
  "paging": {
    "page": 1,
    "pageSize": 10
  }
}

Search for Refunds

Performs a search query against your refunds. This is useful for building custom search functionality.

Search Refunds

javascript
import payment from '@blocklet/payment-js';

async function searchForRefund(query) {
  try {
    const response = await payment.refunds.search({ q: query });
    console.log(`Search results for '${query}':`);
    response.list.forEach(refund => {
      console.log(`- Refund ID: ${refund.id}`);
    });
  } catch (error) {
    console.error('Error searching refunds:', error.message);
  }
}

searchForRefund('customer_id:cus_xxxxxxxx');

Parameters

NameTypeDescription
qstringRequired. The search query string. Supports key-value filtering (e.g., status:succeeded).
pagenumberOptional. The page number for pagination. Defaults to 1.
pageSizenumberOptional. The number of objects to return per page. Defaults to 20.
ostringOptional. Specifies the order of the results. Use asc for ascending or desc for descending. Defaults to desc based on created_at.

Returns

Returns a paginated object containing a list of matching Refund objects, the total count, and paging information.

Response Example

json
{
  "count": 1,
  "list": [
    {
      "id": "ref_xxxxxxxxxxxx",
      "object": "refund",
      "amount": "5000",
      "customer_id": "cus_xxxxxxxx",
      "status": "succeeded",
      // ... other refund fields
    }
  ],
  "paging": {
    "page": 1,
    "pageSize": 20
  }
}