通貨と金額
Omise APIで金額を正しく指定する方法、通貨単位の理解、マルチ通貨取引の処理方法について解説します。
概要
Omise APIを使用する際、すべての金額は対象通貨の最小単位で指定する必要があります。これにより、小数値を使用した場合に発生する浮動小数点エラーを防ぎ、精度を確保できます。
最小通貨単位
各通貨には最小単位(補助単位とも呼ばれます)があります:
| 通貨 | コード | 最小単位 | 小数桁数 | 例 |
|---|---|---|---|---|
| タイバーツ | THB | サタン | 2 | ฿100.00 = 10000 |
| 日本円 | JPY | 円 | 0 | ¥100 = 100 |
| シンガポールドル | SGD | セント | 2 | S$100.00 = 10000 |
| マレーシアリンギット | MYR | セン | 2 | RM100.00 = 10000 |
| 米ドル | USD | セント | 2 | $100.00 = 10000 |
| ユーロ | EUR | セント | 2 | €100.00 = 10000 |
例
// タイバーツ: ฿1,000.00 = 100,000サタン
const amount = 100000;
const currency = 'thb';
// 日本円: ¥1,000 = 1,000円(補助単位なし)
const amount = 1000;
const currency = 'jpy';
// シンガポールドル: S$50.50 = 5,050セント
const amount = 5050;
const currency = 'sgd';
よくある間違い
// ❌ 間違い - 小数値を使用している
const charge = await omise.charges.create({
amount: 1000.00, // これは฿1,000ではなく฿10.00です!
currency: 'thb'
});
// ✅ 正しい - 最小単位を使用している
const charge = await omise.charges.create({
amount: 100000, // これは฿1,000.00です
currency: 'thb'
});
金額の変換
表示金額から最小単位への変換
function toSmallestUnit(amount, currency) {
const zeroDecimalCurrencies = ['jpy', 'krw', 'vnd'];
if (zeroDecimalCurrencies.includes(currency.toLowerCase())) {
return Math.round(amount);
}
return Math.round(amount * 100);
}
// 使用例
toSmallestUnit(1000.00, 'thb'); // 100000
toSmallestUnit(1000, 'jpy'); // 1000
toSmallestUnit(99.99, 'sgd'); // 9999
最小単位から表示金額への変換
function toDisplayAmount(amount, currency) {
const zeroDecimalCurrencies = ['jpy', 'krw', 'vnd'];
if (zeroDecimalCurrencies.includes(currency.toLowerCase())) {
return amount;
}
return amount / 100;
}
// 使用例
toDisplayAmount(100000, 'thb'); // 1000.00
toDisplayAmount(1000, 'jpy'); // 1000
toDisplayAmount(9999, 'sgd'); // 99.99
表示用フォーマット
function formatCurrency(amount, currency) {
const displayAmount = toDisplayAmount(amount, currency);
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency: currency.toUpperCase()
}).format(displayAmount);
}
// 使用例
formatCurrency(100000, 'thb'); // "฿1,000.00"
formatCurrency(1000, 'jpy'); // "¥1,000"
formatCurrency(9999, 'sgd'); // "S$99.99"
対応通貨
課金(クレジット/デビットカード)
課金に使用できる通貨は、アカウントの登録国によって異なります:
| 国 | 対応通貨 |
|---|---|
| タイ | THB |
| 日本 | JPY |
| シンガポール | SGD, USD |
| マレーシア | MYR |
課金限度額
| 国 | 通貨 | 最小金額 | 最大金額 |
|---|---|---|---|
| タイ | THB | ฿20 (2000) | ฿150,000 (15000000) |
| 日本 | JPY | ¥100 (100) | ¥6,000,000 (6000000) |
| シンガポール | SGD | S$1 (100) | S$99,999 (9999900) |
| マレーシア | MYR | RM1 (100) | RM30,000 (3000000) |