เอกสารอ้างอิง API ของ Omise.js
เอกสารอ้างอิงที่สมบูรณ์สำหรับเมธอด พารามิเตอร์ และออบเจ็กต์การตอบกลับทั้งหมดของ Omise.js
การเริ่มต้นใช้งาน
Omise.setPublicKey()
ตั้งค่า Omise public key ของคุณก่อนเรียก API ใดๆ
Signature:
Omise.setPublicKey(publicKey: string): void
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
publicKey | string | ✓ | Your Omise public key starting with pkey_ |
Example:
Omise.setPublicKey("pkey_test_5xs8t3mho5d4pbbkjz1dk");
หมายเหตุ:
- ต้องเรียกก่อนเมธอดอื่นๆ ของ Omise.js
- ใ ช้ test keys (
pkey_test_...) สำหรับการพัฒนา - ใช้ live keys (
pkey_live_...) สำหรับการใช้งานจริง - Key ถูกเก็บไว้ในหน่วยความจำเท่านั้น ไม่ถูกบันทึก
การสร้าง Token
Omise.createToken()
สร้างการแทนค่าแบบ token ของข้อมูลการชำระเงิน
Signature:
Omise.createToken(
type: string,
data: object,
callback: (statusCode: number, response: object) => void
): void
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
type | string | ✓ | Token type: "card" or "tokenization" |
data | object | ✓ | Card data or tokenization data |
callback | function | ✓ | Callback function receiving (statusCode, response) |
Token บัตร
สร้าง token จากข้อมูลบัตร
ออบเจ็กต์ข้อมูลบัตร:
{
name: string, // Cardholder name
number: string, // Card number (13-19 digits)
expiration_month: number, // Month (1-12)
expiration_year: number, // Full year (e.g., 2027)
security_code: string, // CVV/CVC (3-4 digits)
// Optional billing address
city?: string,
postal_code?: string,
country?: string // ISO 3166-1 alpha-2 (e.g., "TH")
}
Example:
Omise.createToken("card", {
name: "John Doe",
number: "4242424242424242",
expiration_month: 12,
expiration_year: 2027,
security_code: "123",
city: "Bangkok",
postal_code: "10110",
country: "TH"
}, function(statusCode, response) {
if (statusCode === 200) {
console.log("Token ID:", response.id);
// response.id = "tokn_test_5xs8t3mho5d4pbbkjz1dk"
} else {
console.error("Error:", response.message);
}
});
Token กระเป๋าเงินดิจิทัล
สร้าง token จากข้อมูลกระเป๋าเงินดิจิทัล (Google Pay, Apple Pay)
ออบเจ็กต์ข้อมูลการสร้าง Token:
{
method: string, // "googlepay" or "applepay"
data: object // Token data from payment provider
}
Google Pay Example:
const googlePayToken = {
signature: "...",
protocolVersion: "ECv2",
signedMessage: "..."
};
Omise.createToken("tokenization", {
method: "googlepay",
data: googlePayToken
}, callback);
Apple Pay Example:
const applePayToken = {
paymentData: {...},
transactionIdentifier: "...",
paymentMethod: {...}
};
Omise.createToken("tokenization", {
method: "applepay",
data: applePayToken
}, callback);
การตอบกลับ Token
การตอบกลับที่สำเร็จ (Status 200):
{
object: "token",
id: "tokn_test_5xs8t3mho5d4pbbkjz1dk",
livemode: false,
location: "/tokens/tokn_test_5xs8t3mho5d4pbbkjz1dk",
used: false,
charge_status: "unknown",
card: {
object: "card",
id: "card_test_5xs8t3mho5d4pbbkjz1dk",
livemode: false,
location: null,
country: "us",
city: null,
postal_code: null,
financing: "",
bank: "",
last_digits: "4242",
brand: "Visa",
expiration_month: 12,
expiration_year: 2027,
fingerprint: "XjOdjaoKvKLxNFvbxdPo7hpO8VRQVOM5bZt9e1D4NG0=",
name: "John Doe",
security_code_check: true,
tokenization_method: null,
created_at: "2025-02-06T10:30:45Z"
},
created_at: "2025-02-06T10:30:45Z"
}
การตอบกลับข้อผิดพลาด (Status 400+):
{
object: "error",
location: "https://docs.omise.co/api-errors#invalid-card",
code: "invalid_card",
message: "number is invalid"
}
การสร้าง Source
Omise.createSource()
สร้าง source การชำระเงินสำหรับวิธีการชำระเงินทางเลือก
Signature:
Omise.createSource(
type: string,
data: object,
callback: (statusCode: number, response: object) => void
): void
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
type | string | ✓ | Payment method type |
data | object | ✓ | Source data including amount and currency |
callback | function | ✓ | Callback function receiving (statusCode, response) |
วิธีการชำระเงินที่รองรับ
PromptPay
Omise.createSource("promptpay", {
amount: 50000, // Required
currency: "THB" // Required
}, callback);
PayNow (Singapore)
Omise.createSource("paynow", {
amount: 5000, // Required
currency: "SGD" // Required
}, callback);
TrueMoney Wallet
Omise.createSource("truemoney", {
amount: 30000, // Required
currency: "THB", // Required
phone_number: "+66876543210" // Required
}, callback);
Rabbit LINE Pay
Omise.createSource("rabbit_linepay", {
amount: 20000, // Required
currency: "THB" // Required
}, callback);
Mobile Banking
// SCB Easy App
Omise.createSource("mobile_banking_scb", {
amount: 40000,
currency: "THB"
}, callback);
// KBank Mobile Banking
Omise.createSource("mobile_banking_kbank", {
amount: 40000,
currency: "THB"
}, callback);
// Bangkok Bank Mobile Banking
Omise.createSource("mobile_banking_bay", {
amount: 40000,
currency: "THB"
}, callback);
// Krung Thai Mobile Banking
Omise.createSource("mobile_banking_ktb", {
amount: 40000,
currency: "THB"
}, callback);
E-Wallets
// GrabPay
Omise.createSource("grabpay", {
amount: 10000,
currency: "SGD" // or "THB", "MYR"
}, callback);
// ShopeePay
Omise.createSource("shopeepay", {
amount: 25000,
currency: "THB"
}, callback);
// Boost (Malaysia)
Omise.createSource("boost", {
amount: 3000,
currency: "MYR"
}, callback);
// Touch 'n Go (Malaysia)
Omise.createSource("touch_n_go", {
amount: 5000,
currency: "MYR"
}, callback);
// Alipay
Omise.createSource("alipay", {
amount: 50000,
currency: "THB"
}, callback);
// WeChat Pay
Omise.createSource("wechat_pay", {
amount: 50000,
currency: "THB"
}, callback);
Bank Transfers
// FPX (Malaysia)
Omise.createSource("fpx", {
amount: 10000,
currency: "MYR",
email: "customer@example.com", // Required
bank: "test" // Bank code (see FPX docs)
}, callback);
// DuitNow QR (Malaysia)
Omise.createSource("duitnow_qr", {
amount: 5000,
currency: "MYR"
}, callback);
การผ่อนชำระ
// Krungthai Card Installment
Omise.createSource("installment_kbank", {
amount: 50000,
currency: "THB",
installment_term: 6 // 3, 6, 10 months
}, callback);
// Bay Installment
Omise.createSource("installment_bay", {
amount: 50000,
currency: "THB",
installment_term: 10
}, callback);
// First Choice Installment
Omise.createSource("installment_first_choice", {
amount: 30000,
currency: "THB",
installment_term: 6
}, callback);
// KTC Installment
Omise.createSource("installment_ktc", {
amount: 50000,
currency: "THB",
installment_term: 6
}, callback);
// SCB Installment
Omise.createSource("installment_scb", {
amount: 50000,
currency: "THB",
installment_term: 10
}, callback);
Buy Now Pay Later (BNPL)
// Atome
Omise.createSource("atome", {
amount: 50000,
currency: "THB",
phone_number: "+66876543210", // Required
email: "customer@example.com", // Required
name: "John Doe", // Required
shipping: { // Required for physical goods
address: {
country: "TH",
city: "Bangkok",
postal_code: "10110",
state: "Bangkok",
street1: "123 Main St"
}
},
items: [ // Optional but recommended
{
sku: "PROD-001",
name: "Product Name",
quantity: 1,
amount: 50000,
currency: "THB"
}
]
}, callback);
การตอบกลับ Source
การตอบกลับที่สำเร็จ (Status 200):
{
object: "source",
id: "src_test_5xs8t3mho5d4pbbkjz1dk",
livemode: false,
location: "/sources/src_test_5xs8t3mho5d4pbbkjz1dk",
type: "promptpay",
flow: "redirect",
amount: 50000,
currency: "THB",
charge_status: "unknown",
references: {
qr_code: "00020101021..." // For QR-based payments
},
created_at: "2025-02-06T10:30:45Z"
}
OmiseCard (ฟอร์มสำเร็จรูป)
OmiseCard.configure()
กำหนดค่าฟอร์มการชำระเงินสำเร็จรูป
Signature:
OmiseCard.configure(options: object): void
Options:
{
// Required
publicKey: string,
amount: number,
currency: string,
// Payment methods
defaultPaymentMethod?: string,
otherPaymentMethods?: string[],
// UI customization
frameLabel?: string,
frameDescription?: string,
submitLabel?: string,
buttonLabel?: string,
locale?: string, // "en", "th", "ja"
image?: string, // Logo URL
// Customer data
name?: string,
email?: string,
phoneNumber?: string,
// Other
location?: "yes" | "no",
metadata?: object,
// Callbacks
onCreateTokenSuccess: (nonce: string) => void,
onFormClosed?: () => void
}
Example:
OmiseCard.configure({
publicKey: "pkey_test_5xs8t3mho5d4pbbkjz1dk",
amount: 50000,
currency: "THB",
defaultPaymentMethod: "credit_card",
otherPaymentMethods: ["promptpay", "truemoney"],
frameLabel: "ACME Store",
submitLabel: "Pay Now",
locale: "en",
onCreateTokenSuccess: (nonce) => {
console.log("Created:", nonce);
}
});
OmiseCard.open()
เปิดฟอร์มการชำระเงินแบบ modal
Signature:
OmiseCard.open(): void
Example:
document.getElementById('pay-button').addEventListener('click', () => {
OmiseCard.open();
});
OmiseCard.close()
ปิดฟอร์มการชำระเงินแบบ modal
Signature:
OmiseCard.close(): void
Example:
OmiseCard.close();
รหัสข้อผิดพลาด
ข้อผิดพลาดในการสร้าง Token
| Code | Message | Solution |
|---|---|---|
invalid_card | number is invalid | Check card number format |
invalid_card | expiration date cannot be in the past | Use future date |
invalid_card | security code failed verification | Check CVV |
empty_card | name can't be blank | Provide cardholder name |
invalid_expiration_month | expiration month is invalid | Use 1-12 |
invalid_expiration_year | expiration year is invalid | Use 4-digit year |
invalid_security_code | security code is invalid | Provide 3-4 digit CVV |
invalid_number | number is invalid | Check card number |
invalid_country | country is invalid | Use ISO 3166-1 alpha-2 |
public_key_is_required | public key is required | Call setPublicKey first |
ข้อผิดพลาดในการสร้าง Source
| Code | Message | Solution |
|---|---|---|
invalid_amount | amount is invalid | Amount must be positive integer |
invalid_currency | currency is invalid | Use correct currency for method |
phone_number_required | phone number is required | Provide phone number |
email_required | email is required | Provide email address |
invalid_phone_number | phone number is invalid | Use E.164 format (+66...) |
invalid_email | email is invalid | Provide valid email |