Dart SDK
The Omise Dart SDK provides a comprehensive server-side integration for the Omise payment API. Built for backend services, cloud functions, and CLI tools, it offers full API access with type-safe request/response models and async support.
概要
The Dart SDK enables you to:
- Full API access - Complete control over charges, customers, cards, and more
- Server-side operations - Create charges, manage customers, handle refunds
- Type-safe models - Fully typed request and response objects
- Async/await support - Modern Dart async patterns
- Null safety - Built with sound null safety
- Webhook handling - Verify and process webhook events
- Error handling - Comprehensive exception types
Key Features
- Null-safe Dart 2.12+ API
- Future-based async operations
- Complete API coverage
- Type-safe request builders
- Webhook signature verification
- Automatic request retry logic
- Custom HTTP client support
- Comprehensive error handling
要件
- Dart 2.12 or later (null safety)
- Server-side or CLI application
- Not suitable for client-side/browser use (requires secret key)
インストール
Add to pubspec.yaml
dependencies:
omise_dart: ^3.0.0
Install Package
dart pub add omise_dart
Or manually:
dart pub get
クイックスタート
1. Import the Package
import 'package:omise_dart/omise_dart.dart';
2. Initialize the Client
void main() async {
final omise = Omise(
publicKey: 'pkey_test_5xyzyx5xyzyx5xyzyx5',
secretKey: 'skey_test_5xyzyx5xyzyx5xyzyx5',
);
}
3. Create a Charge
Future<void> createCharge() async {
final omise = Omise(
publicKey: 'pkey_test_5xyzyx5xyzyx5xyzyx5',
secretKey: 'skey_test_5xyzyx5xyzyx5xyzyx5',
);
try {
final charge = await omise.charges.create(
amount: 100000, // 1,000.00 THB
currency: 'thb',
card: 'tokn_test_5xyzyx5xyzyx5xyzyx5',
description: 'Order #1234',
metadata: {
'order_id': '1234',
'customer_email': 'john@example.com',
},
);
print('Charge created: ${charge.id}');
print('Status: ${charge.status}');
if (charge.paid) {
print('Payment successful!');
}
} catch (error) {
print('Error: $error');
}
}
構成
Client Configuration
// Basic configuration
final omise = Omise(
publicKey: 'pkey_test_5xyzyx5xyzyx5xyzyx5',
secretKey: 'skey_test_5xyzyx5xyzyx5xyzyx5',
);
// Advanced configuration
final omise = Omise(
publicKey: 'pkey_test_5xyzyx5xyzyx5xyzyx5',
secretKey: 'skey_test_5xyzyx5xyzyx5xyzyx5',
apiVersion: '2019-05-29',
timeout: Duration(seconds: 60),
debugMode: true,
);
Environment-based Configuration
import 'dart:io';
class Config {
static String get publicKey {
return Platform.environment['OMISE_PUBLIC_KEY'] ??
'pkey_test_5xyzyx5xyzyx5xyzyx5';
}
static String get secretKey {
return Platform.environment['OMISE_SECRET_KEY'] ??
'skey_test_5xyzyx5xyzyx5xyzyx5';
}
}
final omise = Omise(
publicKey: Config.publicKey,
secretKey: Config.secretKey,
);
Custom HTTP Client
import 'package:http/http.dart' as http;
final customClient = http.Client();
final omise = Omise(
publicKey: 'pkey_test_...',
secretKey: 'skey_test_...',
httpClient: customClient,
);
// Don't forget to close the client when done
void cleanup() {
customClient.close();
}
課金
Create a Charge
// Charge a card token
Future<Charge> chargeCard(String tokenId, int amount) async {
return await omise.charges.create(
amount: amount,
currency: 'thb',
card: tokenId,
description: 'Payment for order',
returnUri: 'https://example.com/payment/callback',
);
}
// Charge a customer's default card
Future<Charge> chargeCustomer(String customerId, int amount) async {
return await omise.charges.create(
amount: amount,
currency: 'thb',
customer: customerId,
description: 'Subscription payment',
);
}
// Charge with a source
Future<Charge> chargeSource(String sourceId, int amount) async {
return await omise.charges.create(
amount: amount,
currency: 'thb',
source: sourceId,
returnUri: 'https://example.com/payment/callback',
);
}
Create Charge with Metadata
Future<Charge> createChargeWithMetadata() async {
return await omise.charges.create(
amount: 100000,
currency: 'thb',
card: 'tokn_test_5xyzyx5xyzyx5xyzyx5',
description: 'Order #1234',
metadata: {
'order_id': '1234',
'customer_email': 'john@example.com',
'customer_name': 'John Doe',
'shipping_method': 'express',
},
);
}
Retrieve a Charge
Future<Charge> getCharge(String chargeId) async {
return await omise.charges.retrieve(chargeId);
}
// Check charge status
Future<void> checkChargeStatus(String chargeId) async {
final charge = await omise.charges.retrieve(chargeId);
print('Charge ID: ${charge.id}');
print('Status: ${charge.status}');
print('Paid: ${charge.paid}');
print('Amount: ${charge.amount}');
if (charge.authorized) {
print('Charge is authorized');
}
if (charge.captured) {
print('Charge is captured');
}
if (charge.reversed) {
print('Charge is reversed');
}
}
List Charges
Future<ChargeList> listCharges({
int limit = 20,
int offset = 0,
}) async {
return await omise.charges.list(
limit: limit,
offset: offset,
);
}
// List all charges
Future<void> listAllCharges() async {
var offset = 0;
const limit = 100;
while (true) {
final charges = await omise.charges.list(
limit: limit,
offset: offset,
);
for (final charge in charges.data) {
print('Charge: ${charge.id} - ${charge.amount}');
}
if (charges.data.length < limit) break;
offset += limit;
}
}
Update a Charge
Future<Charge> updateCharge(String chargeId) async {
return await omise.charges.update(
chargeId,
description: 'Updated description',
metadata: {
'updated_at': DateTime.now().toIso8601String(),
},
);
}
Capture a Charge
Future<Charge> captureCharge(String chargeId) async {
return await omise.charges.capture(chargeId);
}
// Partial capture
Future<Charge> partialCapture(String chargeId, int amount) async {
return await omise.charges.capture(
chargeId,
captureAmount: amount,
);
}
Reverse a Charge
Future<Charge> reverseCharge(String chargeId) async {
return await omise.charges.reverse(chargeId);
}
顧客
Create a Customer
Future<Customer> createCustomer({
required String email,
String? description,
Map<String, dynamic>? metadata,
}) async {
return await omise.customers.create(
email: email,
description: description,
metadata: metadata,
);
}
// 使用方法
final customer = await createCustomer(
email: 'john@example.com',
description: 'John Doe',
metadata: {
'phone': '+66812345678',
'address': '123 Wireless Road, Bangkok',
},
);
Retrieve a Customer
Future<Customer> getCustomer(String customerId) async {
return await omise.customers.retrieve(customerId);
}
Update a Customer
Future<Customer> updateCustomer(String customerId) async {
return await omise.customers.update(
customerId,
email: 'newemail@example.com',
description: 'Updated customer',
metadata: {
'last_updated': DateTime.now().toIso8601String(),
},
);
}
List Customers
Future<CustomerList> listCustomers({
int limit = 20,
int offset = 0,
}) async {
return await omise.customers.list(
limit: limit,
offset: offset,
);
}
Delete a Customer
Future<DeletedCustomer> deleteCustomer(String customerId) async {
return await omise.customers.delete(customerId);
}
カード
Create a Card
Future<Card> addCardToCustomer(
String customerId,
String tokenId,
) async {
return await omise.customers.cards.create(
customerId,
card: tokenId,
);
}
List Customer Cards
Future<CardList> listCustomerCards(String customerId) async {
return await omise.customers.cards.list(customerId);
}
// Get default card
Future<Card?> getDefaultCard(String customerId) async {
final customer = await omise.customers.retrieve(customerId);
if (customer.defaultCard != null) {
return await omise.customers.cards.retrieve(
customerId,
customer.defaultCard!,
);
}
return null;
}
Update a Card
Future<Card> updateCard(
String customerId,
String cardId, {
String? name,
int? expirationMonth,
int? expirationYear,
}) async {
return await omise.customers.cards.update(
customerId,
cardId,
name: name,
expirationMonth: expirationMonth,
expirationYear: expirationYear,
);
}
Delete a Card
Future<DeletedCard> deleteCard(
String customerId,
String cardId,
) async {
return await omise.customers.cards.delete(customerId, cardId);
}
返金
Create a Refund
Future<Refund> createRefund(String chargeId) async {
// 全額返金
return await omise.refunds.create(chargeId);
}
// 部分払い戻し
Future<Refund> partialRefund(String chargeId, int amount) async {
return await omise.refunds.create(
chargeId,
amount: amount,
);
}
// Refund with metadata
Future<Refund> refundWithReason(
String chargeId,
String reason,
) async {
return await omise.refunds.create(
chargeId,
metadata: {
'reason': reason,
'refunded_by': 'system',
'refunded_at': DateTime.now().toIso8601String(),
},
);
}
Retrieve a Refund
Future<Refund> getRefund(String chargeId, String refundId) async {
return await omise.refunds.retrieve(chargeId, refundId);
}
List Refunds
Future<RefundList> listRefunds(String chargeId) async {
return await omise.refunds.list(chargeId);
}
// List all refunds for a charge
Future<void> listAllRefunds(String chargeId) async {
final refunds = await omise.refunds.list(chargeId);
for (final refund in refunds.data) {
print('Refund: ${refund.id}');
print('Amount: ${refund.amount}');
print('Status: ${refund.status}');
}
}
ソース
Create a Source
// インターネットバンキング
Future<Source> createInternetBankingSource(int amount) async {
return await omise.sources.create(
amount: amount,
currency: 'thb',
type: 'internet_banking_bay',
);
}
// PromptPay
Future<Source> createPromptPaySource(int amount) async {
return await omise.sources.create(
amount: amount,
currency: 'thb',
type: 'promptpay',
);
}
// TrueMoney Wallet
Future<Source> createTrueMoneySource(
int amount,
String phoneNumber,
) async {
return await omise.sources.create(
amount: amount,
currency: 'thb',
type: 'truemoney',
phoneNumber: phoneNumber,
);
}
// インストール
Future<Source> createInstallmentSource(
int amount,
int installmentTerms,
) async {
return await omise.sources.create(
amount: amount,
currency: 'thb',
type: 'installment_bay',
installmentTerms: installmentTerms,
);
}
Retrieve a Source
Future<Source> getSource(String sourceId) async {
return await omise.sources.retrieve(sourceId);
}
// Poll source until it's paid
Future<Source> waitForSourcePayment(
String sourceId, {
Duration interval = const Duration(seconds: 3),
Duration timeout = const Duration(minutes: 10),
}) async {
final deadline = DateTime.now().add(timeout);
while (DateTime.now().isBefore(deadline)) {
final source = await omise.sources.retrieve(sourceId);
if (source.status == 'successful' || source.status == 'failed') {
return source;
}
await Future.delayed(interval);
}
throw TimeoutException('Source payment timeout');
}
トークン
Create a Token
// Note: Tokens should be created client-side for PCI compliance
// This is only for server-to-server scenarios
Future<Token> createToken({
required String name,
required String number,
required int expirationMonth,
required int expirationYear,
required String securityCode,
}) async {
return await omise.tokens.create(
name: name,
number: number,
expirationMonth: expirationMonth,
expirationYear: expirationYear,
securityCode: securityCode,
);
}
Retrieve a Token
Future<Token> getToken(String tokenId) async {
return await omise.tokens.retrieve(tokenId);
}
送金
Create a Transfer
Future<Transfer> createTransfer(int amount) async {
return await omise.transfers.create(
amount: amount,
);
}
// Transfer with recipient
Future<Transfer> transferToRecipient(
String recipientId,
int amount,
) async {
return await omise.transfers.create(
amount: amount,
recipient: recipientId,
);
}
Retrieve a Transfer
Future<Transfer> getTransfer(String transferId) async {
return await omise.transfers.retrieve(transferId);
}
List Transfers
Future<TransferList> listTransfers({
int limit = 20,
int offset = 0,
}) async {
return await omise.transfers.list(
limit: limit,
offset: offset,
);
}
Update a Transfer
Future<Transfer> updateTransfer(String transferId) async {
return await omise.transfers.update(
transferId,
amount: 50000, // Update transfer amount
);
}
Destroy a Transfer
Future<DeletedTransfer> deleteTransfer(String transferId) async {
return await omise.transfers.destroy(transferId);
}