ข้ามไปยังเนื้อหาหลัก

เอกสารอ้างอิง API ของ Omise.js

เอกสารอ้างอิงที่สมบูรณ์สำหรับเมธอด พารามิเตอร์ และออบเจ็กต์การตอบกลับทั้งหมดของ Omise.js

การเริ่มต้นใช้งาน

Omise.setPublicKey()

ตั้งค่า Omise public key ของคุณก่อนเรียก API ใดๆ

Signature:

Omise.setPublicKey(publicKey: string): void

Parameters:

ParameterTypeRequiredDescription
publicKeystringYour 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:

ParameterTypeRequiredDescription
typestringToken type: "card" or "tokenization"
dataobjectCard data or tokenization data
callbackfunctionCallback 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:

ParameterTypeRequiredDescription
typestringPayment method type
dataobjectSource data including amount and currency
callbackfunctionCallback 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

CodeMessageSolution
invalid_cardnumber is invalidCheck card number format
invalid_cardexpiration date cannot be in the pastUse future date
invalid_cardsecurity code failed verificationCheck CVV
empty_cardname can't be blankProvide cardholder name
invalid_expiration_monthexpiration month is invalidUse 1-12
invalid_expiration_yearexpiration year is invalidUse 4-digit year
invalid_security_codesecurity code is invalidProvide 3-4 digit CVV
invalid_numbernumber is invalidCheck card number
invalid_countrycountry is invalidUse ISO 3166-1 alpha-2
public_key_is_requiredpublic key is requiredCall setPublicKey first

ข้อผิดพลาดในการสร้าง Source

CodeMessageSolution
invalid_amountamount is invalidAmount must be positive integer
invalid_currencycurrency is invalidUse correct currency for method
phone_number_requiredphone number is requiredProvide phone number
email_requiredemail is requiredProvide email address
invalid_phone_numberphone number is invalidUse E.164 format (+66...)
invalid_emailemail is invalidProvide valid email

HTTP Status Codes

StatusMeaningAction
200SuccessProcess the response
400Bad RequestFix the request data
401UnauthorizedCheck public key
402Payment RequiredContact Omise support
404Not FoundCheck endpoint URL
429Too Many RequestsImplement rate limiting
500Server ErrorRetry or contact support
503บริการ UnavailableRetry later

คำจำกัดความของประเภท

TypeScript

declare global {
interface Window {
Omise: {
setPublicKey(publicKey: string): void;
createToken(
type: 'card',
data: CardData,
callback: TokenCallback
): void;
createToken(
type: 'tokenization',
data: TokenizationData,
callback: TokenCallback
): void;
createSource(
type: string,
data: SourceData,
callback: SourceCallback
): void;
};
OmiseCard: {
configure(options: OmiseCardOptions): void;
open(): void;
close(): void;
};
}
}

interface CardData {
name: string;
number: string;
expiration_month: number;
expiration_year: number;
security_code: string;
city?: string;
postal_code?: string;
country?: string;
}

interface TokenizationData {
method: 'googlepay' | 'applepay';
data: any;
}

interface SourceData {
amount: number;
currency: string;
[key: string]: any;
}

type TokenCallback = (statusCode: number, response: TokenResponse | ErrorResponse) => void;
type SourceCallback = (statusCode: number, response: SourceResponse | ErrorResponse) => void;

interface TokenResponse {
object: 'token';
id: string;
livemode: boolean;
used: boolean;
card: CardObject;
created_at: string;
}

interface SourceResponse {
object: 'source';
id: string;
livemode: boolean;
type: string;
flow: string;
amount: number;
currency: string;
created_at: string;
}

interface ErrorResponse {
object: 'error';
code: string;
message: string;
}

interface OmiseCardOptions {
publicKey: string;
amount: number;
currency: string;
defaultPaymentMethod?: string;
otherPaymentMethods?: string[];
frameLabel?: string;
frameDescription?: string;
submitLabel?: string;
buttonLabel?: string;
locale?: 'en' | 'th' | 'ja';
image?: string;
name?: string;
email?: string;
phoneNumber?: string;
location?: 'yes' | 'no';
metadata?: Record<string, any>;
onCreateTokenSuccess: (nonce: string) => void;
onFormClosed?: () => void;
}

ความเข้ากันได้ของเบราว์เซอร์

FeatureChromeFirefoxSafariEdge
createToken60+55+11+79+
createSource60+55+11+79+
OmiseCard60+55+11+79+

Mobile:

  • iOS Safari 11+
  • Chrome for Android 60+

ทรัพยากรที่เกี่ยวข้อง


การสนับสนุน