koi_pay_kit 0.1.0 copy "koi_pay_kit: ^0.1.0" to clipboard
koi_pay_kit: ^0.1.0 copied to clipboard

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.

example/example.dart

// ignore_for_file: avoid_print

import 'package:koi_pay_kit/koi_pay_kit.dart';

/// 示例 OrderBackend 实现
/// Example [OrderBackend] implementation
class ExampleOrderBackend implements OrderBackend {
  @override
  Future<PaymentOrder> createOrder(OrderRequest request) async {
    // 你的服务端 API / Your server API
    return PaymentOrder(
      orderSn: 'SN${DateTime.now().millisecondsSinceEpoch}',
      sdkPaymentData: 'alipay_signed_string',
    );
  }

  @override
  Future<VerifyResult> verifyPayment(String orderSn) async {
    // 查询服务端支付状态 / Query server payment status
    return const VerifyResult(
      status: VerifyStatus.success,
      info: 'ok',
    );
  }
}

/// 示例 PaymentSdk 实现
/// Example [PaymentSdk] implementation
class ExampleSdk implements PaymentSdk {
  @override
  String get id => 'example';

  @override
  String get displayName => 'Example Pay';

  @override
  Future<bool> get isAvailable async => true;

  @override
  Future<SdkPayResult> invoke(Object paymentData) async {
    return const SdkPayResult(
      status: SdkResultStatus.success,
      rawResult: {'code': '0'},
    );
  }
}

void main() async {
  print('=========================');
  print(' koi_pay_kit 示例程序');
  print('=========================\n');

  // 1. 创建引擎 / Create engine
  final engine = PaymentEngine(
    backend: ExampleOrderBackend(),
    sdks: {'example': ExampleSdk()},
    cache: InMemoryCache(),
    logger: (level, msg, [err]) => print('[$level] $msg'),
  );

  // 2. 启动时静默对账 / Silent reconciliation on startup
  final recovery = await engine.recover(0);
  print('Recovery status: ${recovery.status}');

  // 3. 发起支付 / Initiate payment
  try {
    final sn = await engine.pay(
      sdkId: 'example',
      title: '示例充值',
      amountInCents: 10000, // 100.00 元
      channel: 0,
      payload: {'days': 30},
      onPhaseChanged: (phase) => print('Phase: $phase'),
    );

    print('支付成功 (Payment succeeded): sn=$sn');

    // 4. 业务逻辑完成后清除缓存 / Clear cache after business logic
    await engine.clearOrder(0);
  } on PendingOrderException catch (e) {
    print('未完成订单: ${e.message}');
  } on PaymentException catch (e) {
    print('支付失败: ${e.message}');
  }

  // 5. 释放引擎 / Dispose engine
  engine.dispose();
}

/// 内存缓存实现(仅用于示例)
/// In-memory cache implementation (example only)
class InMemoryCache implements PaymentCache {
  final _store = <String, String>{};

  @override
  Future<String?> read(String key) async => _store[key];

  @override
  Future<void> write(String key, String value) async => _store[key] = value;

  @override
  Future<void> delete(String key) async => _store.remove(key);
}
0
likes
150
points
16
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

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.

Repository (GitHub)
View/report issues

Topics

#payment #alipay #wechat-pay #in-app-purchase

License

MIT (license)

Dependencies

equatable, flutter, shared_preferences

More

Packages that depend on koi_pay_kit