Used to check for browser translation.
用于检测浏览器翻译。
ブラウザの翻訳を検出する
API 参考

退款


退款 API 允许您创建和管理收费的退款。退款可以是原始收费的全部金额,也可以是部分金额。退款是针对特定的 payment_intent_id 处理的。

有关支付流程本身的更多详情,请参阅 Payment Intents API 参考

Refund 对象#

Refund 对象包含有关特定退款交易的详细信息。

属性

类型

描述

id

string

退款对象的唯一标识符。

livemode

boolean

如果对象是在生产模式下创建的,则为 true;如果在测试模式下创建的,则为 false

description

string

附加到对象的任意字符串。

amount

string

退款金额。

currency_id

string

三位字母的 ISO 货币代码。

customer_id

string

与此退款关联的客户 ID。

payment_intent_id

string

被退款的 Payment Intent 的 ID。

invoice_id

string

(可选)与此退款关联的发票 ID。

subscription_id

string

(可选)与此退款关联的订阅 ID。

metadata

object

可以附加到对象的一组键值对。

status

string

退款状态。可以是 pendingrequires_actionfailedcanceledsucceeded

reason

string

(可选)退款原因。可以是 duplicaterequested_by_customerrequested_by_adminfraudulentexpired_uncaptured_charge

failure_reason

string

(可选)如果退款失败,此字段提供一个机器可读的失败原因。

created_at

Date

对象创建时的时间戳。

updated_at

Date

对象最后更新的时间戳。

创建退款#

创建一个新的退款对象。

参数

名称

类型

描述

payment_intent_id

string

必需。 要退款的 Payment Intent 的标识符。

amount

string

要退款的金额。不能大于原始收费金额。如果省略,将进行全额退款。

description

string

退款的可选描述。

reason

string

退款原因。有效选项包括 duplicaterequested_by_customer 等。

metadata

object

与退款对象一起存储的一组键值对。

返回

返回新创建的 Refund 对象。

示例

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

async function createRefund(paymentIntentId) {
try {
const refund = await payment.refunds.create({
payment_intent_id: paymentIntentId,
amount: '500', // 退款 5.00 个货币单位
reason: 'requested_by_customer',
metadata: { order_id: '6735' }
});
console.log('退款已创建:', refund.id);
return refund;
} catch (error) {
console.error('创建退款时出错:', error.message);
}
}

// 将 'pi_xxxxxxxx' 替换为有效的 Payment Intent ID
createRefund('pi_xxxxxxxx');

响应示例

{
"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"
}

检索退款#

检索现有退款的详细信息。

参数

名称

类型

描述

id

string

必需。 要检索的退款的唯一标识符。

返回

如果提供了有效的 ID,则返回 Refund 对象。

示例

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

async function getRefundDetails(refundId) {
try {
const refund = await payment.refunds.retrieve(refundId);
console.log('已检索退款:', refund);
return refund;
} catch (error) {
console.error(`检索退款 ${refundId} 时出错:`, error.message);
}
}

// 将 're_xxxxxxxx' 替换为有效的退款 ID
getRefundDetails('re_xxxxxxxx');

响应示例

{
"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"
}

列出退款#

返回一个分页的退款列表。您可以根据各种参数筛选列表。

参数

名称

类型

描述

status

string

(可选)按状态(例如 succeededfailed)筛选退款。

invoice_id

string

(可选)仅返回指定发票的退款。

subscription_id

string

(可选)仅返回指定订阅的退款。

currency_id

string

(可选)仅返回指定货币的退款。

customer_id

string

(可选)仅返回指定客户的退款。

page

number

(可选)用于分页的页码,从 1 开始。

pageSize

number

(可选)每页的项目数。默认为 20。

order

string[] or string

(可选)结果的排序顺序。示例:['created_at', 'DESC']

返回

返回一个分页对象,其中包含项目总数 countRefund 对象列表 list

示例

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

async function listSuccessfulRefunds() {
try {
const refunds = await payment.refunds.list({
status: 'succeeded',
pageSize: 5
});
console.log(`找到 ${refunds.count} 条成功的退款。`);
refunds.list.forEach(refund => {
console.log(`- ID: ${refund.id}, 金额: ${refund.amount}`);
});
return refunds;
} catch (error) {
console.error('列出退款时出错:', error.message);
}
}

listSuccessfulRefunds();

响应示例

{
"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"
}
]
}

搜索退款#

返回与搜索查询匹配的分页退款列表。

参数

名称

类型

描述

query

string

必需。 用于查找退款的搜索词。可以是客户电子邮件、描述或 ID。

page

number

(可选)用于分页的页码,从 1 开始。

pageSize

number

(可选)每页的项目数。默认为 20。

返回

返回一个分页对象,其中包含项目总数 count 和匹配的 Refund 对象列表 list

示例

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

async function searchForRefund(query) {
try {
const results = await payment.refunds.search({ query: query });
console.log(`找到 ${results.count} 条与 '${query}' 匹配的退款。`);
results.list.forEach(refund => {
console.log(`- ID: ${refund.id}, 状态: ${refund.status}`);
});
return results;
} catch (error) {
console.error('搜索退款时出错:', error.message);
}
}

searchForRefund('pi_xxxxxxxx');

响应示例

{
"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"
}
]
}


管理退款后,您可能想了解如何处理 Webhooks 以接收 refund.succeeded 等事件的实时通知。