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);
}