支付货币
支付货币 API 允许你管理应用程序接受支付的各种货币。每种货币都与特定的支付方式相关联,并定义了其名称、符号和小数精度等属性。
这是一个核心资源,会被其他对象(例如价格)引用。
支付货币对象#
PaymentCurrency 对象包含有关受支持货币的详细信息。
Attribute | Type | Description |
|---|---|---|
|
| 货币对象的唯一标识符。 |
|
| 此货币关联的支付方式的 ID。 |
|
| 此货币是否处于活动状态且可用于支付。 |
|
| 如果对象存在于生产模式中,则为 |
|
| 如果为 |
|
| 指示这是否是相关支付方式的基础货币。 |
|
| 货币的全名(例如,“Ethereum”)。 |
|
| 货币的描述。 |
|
| 货币图片或徽标的 URL。 |
|
| 货币符号(例如,“ETH”)。 |
|
| 货币支持的小数位数。 |
|
| 小数点后的最大位数。 |
|
| 单次支付所需的最小金额,以字符串形式表示。 |
|
| 单次支付允许的最大金额,以字符串形式表示。 |
|
| 基于代币的货币的合约地址(如果适用)。 |
|
| 货币类型。可以是 |
|
| 货币金库的配置。包含 |
|
| 可以附加到对象的一组键值对。 |
|
| 创建货币时的时间戳。 |
|
| 货币最后更新的时间戳。 |
创建货币#
创建你的应用程序可以接受的新支付货币。
参数
Name | Type | Description |
|---|---|---|
|
| 必填。 货币的全名。 |
|
| 必填。 此货币所属支付方式的 ID。 |
|
| 必填。 货币的符号。 |
|
| 必填。 货币徽标的 URL。 |
|
| 货币的可选描述。 |
|
| 小数位数。默认为 |
|
| 货币类型。可以是 |
|
| 货币是否处于活动状态。默认为 |
|
| 最小支付金额。默认为 |
|
| 最大支付金额。默认为一个非常大的数字字符串。 |
|
| 基于代币的货币的合约地址。 |
示例
import payment from '@blocklet/payment-js';
async function createCurrency() {
try {
const currency = await payment.paymentCurrencies.create({
name: 'ArcBlock Token',
symbol: 'ABT',
logo: 'https://www.arcblock.io/images/abt-logo.png',
description: 'The native token of the ArcBlock platform.',
payment_method_id: 'pm_xxxxxxxxxxxx',
decimal: 18,
type: 'standard'
});
console.log('已创建货币:', currency);
} catch (error) {
console.error('创建货币时出错:', error);
}
}
createCurrency();响应示例
{
"id": "curr_1234567890",
"payment_method_id": "pm_xxxxxxxxxxxx",
"active": true,
"livemode": false,
"locked": false,
"is_base_currency": false,
"name": "ArcBlock Token",
"description": "The native token of the ArcBlock platform.",
"logo": "https://www.arcblock.io/images/abt-logo.png",
"symbol": "ABT",
"decimal": 18,
"maximum_precision": 8,
"minimum_payment_amount": "0",
"maximum_payment_amount": "10000000000000000000000000000",
"contract": null,
"metadata": {},
"created_at": "2023-10-27T10:00:00.000Z",
"updated_at": "2023-10-27T10:00:00.000Z",
"vault_config": null,
"type": "standard"
}检索货币#
检索现有支付货币的详细信息。
参数
Name | Type | Description |
|---|---|---|
|
| 必填。 要检索的货币的唯一标识符。 |
示例
import payment from '@blocklet/payment-js';
async function getCurrency(currencyId) {
try {
const currency = await payment.paymentCurrencies.retrieve(currencyId);
console.log('已检索货币:', currency.name);
} catch (error) {
console.error('检索货币时出错:', error);
}
}
getCurrency('curr_1234567890');响应示例
{
"id": "curr_1234567890",
"payment_method_id": "pm_xxxxxxxxxxxx",
"active": true,
"livemode": false,
"locked": false,
"is_base_currency": false,
"name": "ArcBlock Token",
"description": "The native token of the ArcBlock platform.",
"logo": "https://www.arcblock.io/images/abt-logo.png",
"symbol": "ABT",
"decimal": 18,
"maximum_precision": 8,
"minimum_payment_amount": "0",
"maximum_payment_amount": "10000000000000000000000000000",
"contract": null,
"metadata": {},
"created_at": "2023-10-27T10:00:00.000Z",
"updated_at": "2023-10-27T10:00:00.000Z",
"vault_config": null,
"type": "standard"
}更新货币#
通过设置传入参数的值来更新指定的支付货币。任何未提供的参数将保持不变。
参数
第一个参数是要更新的货币的 id。第二个参数是包含要更新字段的对象。
Name | Type | Description |
|---|---|---|
|
| 货币的全名。 |
|
| 货币的描述。 |
|
| 货币徽标的 URL。 |
|
| 货币是否处于活动状态。 |
|
| 最小支付金额。 |
|
| 最大支付金额。 |
|
| 要与货币一同存储的一组键值对。 |
示例
import payment from '@blocklet/payment-js';
async function updateCurrency(currencyId) {
try {
const currency = await payment.paymentCurrencies.update(currencyId, {
description: 'An updated description for the native token of ArcBlock.'
});
console.log('已更新货币:', currency.description);
} catch (error) {
console.error('更新货币时出错:', error);
}
}
updateCurrency('curr_1234567890');响应示例
{
"id": "curr_1234567890",
"payment_method_id": "pm_xxxxxxxxxxxx",
"active": true,
"livemode": false,
"locked": false,
"is_base_currency": false,
"name": "ArcBlock Token",
"description": "An updated description for the native token of ArcBlock.",
"logo": "https://www.arcblock.io/images/abt-logo.png",
"symbol": "ABT",
"decimal": 18,
"maximum_precision": 8,
"minimum_payment_amount": "0",
"maximum_payment_amount": "10000000000000000000000000000",
"contract": null,
"metadata": {},
"created_at": "2023-10-27T10:00:00.000Z",
"updated_at": "2023-10-27T11:00:00.000Z",
"vault_config": null,
"type": "standard"
}列出货币#
返回你的支付货币列表。货币按创建日期排序返回,最新创建的货币排在最前面。
参数
Name | Type | Description |
|---|---|---|
|
| 分页的页码。默认为 |
|
| 每页的项目数。默认为 |
|
| 结果的排序顺序。示例: |
示例
import payment from '@blocklet/payment-js';
async function listCurrencies() {
try {
const currencyList = await payment.paymentCurrencies.list({ pageSize: 5 });
console.log(`找到 ${currencyList.length} 种货币:`);
currencyList.forEach(currency => {
console.log(`- ${currency.name} (${currency.symbol})`);
});
} catch (error) {
console.error('列出货币时出错:', error);
}
}
listCurrencies();响应示例
list 方法返回一个 PaymentCurrency 对象数组。
[
{
"id": "curr_1234567890",
"name": "ArcBlock Token",
"symbol": "ABT",
"active": true,
"livemode": false,
"logo": "https://www.arcblock.io/images/abt-logo.png"
},
{
"id": "curr_abcdefghij",
"name": "Ethereum",
"symbol": "ETH",
"active": true,
"livemode": false,
"logo": "https://example.com/eth_logo.png"
}
]