็ฎไฝไธญๆ | English
koi_pay_kit
A universal, robust payment engine for Flutter. Supports any payment SDK through pluggable adapters with built-in order caching, crash recovery, polling verification, and guard mechanisms.
Why koi_pay_kit
Payment integration in Flutter is repetitive - every project rebuilds the same order caching, SDK invocation, result verification, and crash recovery logic.
koi_pay_kit solves this by providing a universal engine:
- ๐ Pluggable SDK adapters โ integrate any payment SDK (Alipay, WeChat Pay, Stripe, etc.)
- ๐ก๏ธ Pre-payment Guard โ prevents duplicate payments on the same channel
- ๐พ Crash recovery โ orders cached before SDK invocation, survives app kills
- ๐ Exponential backoff polling โ automatic verification for "processing" states
- ๐ฆ Channel isolation โ independent caches per business channel
- ๐ Silent reconciliation โ
recover()for startup self-healing - ๐ Observable โ optional
PaymentLoggercallback for monitoring - โฑ๏ธ Cache expiry โ auto-purge stale orders (default 24h)
Installation
Add the package to your pubspec.yaml:
dependencies:
koi_pay_kit: ^0.1.0
Then install dependencies:
flutter pub get
Quick Start
import 'package:koi_pay_kit/koi_pay_kit.dart';
import 'package:koi_pay_kit_alipay/koi_pay_kit_alipay.dart';
final engine = PaymentEngine(
backend: MyOrderBackend(apiClient),
sdks: {'alipay': AlipaySdk()},
cache: SharedPrefsCache(prefs),
);
// Pay
final sn = await engine.pay(
sdkId: 'alipay',
title: 'ๅ
ๅผ',
amountInCents: 10000, // 100.00 ๅ
channel: 2,
payload: {'days': 30, 'count': 5},
);
// Step 4: your business logic
await myApi.recharge(sn: sn);
await engine.clearOrder(2);
Architecture
โโโโโโโโโโโโโโโโโโโโโโโโ
โ Your App โ
โ โ UI / Providers โ
โ โ OrderBackend impl โ โ You implement this
โโโโโโโโโโโโโโโโโโโโโโโโค
โ koi_pay_kit โ
โ โ PaymentEngine โ โ Guard โ Cache โ Invoke โ Verify โ Poll
โโโโโโโโโโโโโโโโโโโโโโโโค
โ SDK Adapters โ
โ โ koi_pay_kit_alipayโ
โ โ koi_pay_kit_wechatโ โ Future
โ โ koi_pay_kit_stripeโ โ Future
โโโโโโโโโโโโโโโโโโโโโโโโ
Interfaces
| Interface | Purpose |
|---|---|
OrderBackend |
Server API: create orders + verify payments |
PaymentSdk |
SDK adapter: invoke native payment |
PaymentCache |
Persistence: read/write/delete cached orders |
Configuration
PaymentEngine(
// ...
config: PaymentConfig(
pollDelays: [2, 4, 8], // exponential backoff (seconds)
cacheExpireMs: 86400000, // 24 hours
enableGuard: true, // prevent duplicate payments
maxPollAttempts: 10, // safety limit
),
logger: (level, msg, [err]) => print('[$level] $msg'),
);
Recovery
Call recover() on page init to handle app kills during payment:
final recovery = await engine.recover(channel);
switch (recovery.status) {
case RecoveryStatus.paymentConfirmed:
await handleBusiness(recovery.cachedOrder!.sn);
await engine.clearOrder(channel);
case RecoveryStatus.expired:
case RecoveryStatus.paymentFailed:
case RecoveryStatus.noCachedOrder:
break; // nothing to do
case RecoveryStatus.needsManualRecovery:
showRecoveryBanner(recovery.cachedOrder!);
}
Main Public APIs
PaymentEngineโ core payment orchestratorPaymentSdkโ SDK adapter interfaceOrderBackendโ server API interfacePaymentCacheโ persistence interfaceSharedPrefsCacheโ built-in SharedPreferences implementationPaymentConfigโ engine configurationPaymentLoggerโ logging callbackPaymentOrder,OrderRequest,CachedOrderโ modelsSdkPayResult,VerifyResult,RecoveryResultโ result modelsPaymentPhase,PendingOrderException,PaymentExceptionโ flow types
Documentation
License
MIT. See LICENSE.
Libraries
- koi_pay_kit
- koi_pay_kit โ Universal Payment Engine koi_pay_kit โ ้็จๆฏไปๅผๆ