fish 2 months ago
parent
commit
4a03557704
2 changed files with 269 additions and 32 deletions
  1. 220 0
      lib/models/payment.dart
  2. 49 32
      lib/service/pay_service.dart

+ 220 - 0
lib/models/payment.dart

@@ -0,0 +1,220 @@
+class OtherPayCreatedReponse {
+  String params;
+  String paymentId;
+  bool sandbox;
+
+  OtherPayCreatedReponse(this.params, this.paymentId, {this.sandbox = false});
+
+  toJson() => {
+        'params': params,
+        'payment_id': paymentId,
+        'sandbox': sandbox,
+      };
+
+  static OtherPayCreatedReponse fromJson(Map<String, dynamic> json) {
+    return OtherPayCreatedReponse(
+      json['params'],
+      json['payment_id'],
+      sandbox: json['sandbox'] ?? false,
+    );
+  }
+}
+
+class PaymentProduct {
+  String id;
+  String name;
+  int quota;
+  int retailPrice;
+  int retailPriceUSD;
+  String expirePolicy;
+  String expirePolicyText;
+  bool recommend;
+  String? description;
+  List<String> methods;
+
+  PaymentProduct({
+    required this.id,
+    required this.name,
+    required this.quota,
+    required this.retailPrice,
+    required this.expirePolicy,
+    required this.expirePolicyText,
+    this.recommend = false,
+    this.description,
+    this.retailPriceUSD = 0,
+    this.methods = const [],
+  });
+
+  String get retailPriceText => '¥${(retailPrice / 100).toStringAsFixed(0)}';
+
+  String get retailPriceUSDText =>
+      '\$${(retailPriceUSD / 100).toStringAsFixed(2)}';
+
+  /// 是否支持 Stripe 支付
+  bool get supportStripe => methods.contains('stripe') || methods.isEmpty;
+
+  toJson() => {
+        'id': id,
+        'name': name,
+        'quota': quota,
+        'retail_price': retailPrice,
+        'retail_price_usd': retailPriceUSD,
+        'expire_policy': expirePolicy,
+        'expire_policy_text': expirePolicyText,
+        'recommend': recommend,
+        'description': description,
+        'methods': methods,
+      };
+
+  static PaymentProduct fromJson(Map<String, dynamic> json) {
+    return PaymentProduct(
+      id: json['id'],
+      name: json['name'],
+      quota: json['quota'],
+      retailPrice: json['retail_price'],
+      retailPriceUSD: json['retail_price_usd'] ?? 0,
+      expirePolicy: json['expire_policy'],
+      expirePolicyText: json['expire_policy_text'],
+      recommend: json['recommend'] ?? false,
+      description: json['description'],
+      methods: ((json['methods'] ?? []) as List<dynamic>)
+          .map((e) => e.toString())
+          .toList(),
+    );
+  }
+}
+
+class PaymentProducts {
+  final List<PaymentProduct> consume;
+  final String? note;
+  final bool preferUSD;
+
+  PaymentProducts(this.consume, {this.note, this.preferUSD = false});
+
+  toJson() => {
+        'consume': consume,
+        'note': note,
+        'prefer_usd': preferUSD,
+      };
+
+  static PaymentProducts fromJson(Map<String, dynamic> json) {
+    return PaymentProducts(
+      (json['consume'] as List<dynamic>)
+          .map((e) => PaymentProduct.fromJson(e))
+          .toList(),
+      note: json['note'],
+      preferUSD: json['prefer_usd'] ?? false,
+    );
+  }
+}
+
+class PaymentStatus {
+  final bool success;
+  final String? note;
+
+  PaymentStatus(this.success, {this.note});
+
+  toJson() => {
+        'success': success,
+        'note': note,
+      };
+
+  static PaymentStatus fromJson(Map<String, dynamic> json) {
+    return PaymentStatus(
+      json['success'],
+      note: json['note'],
+    );
+  }
+}
+
+class WechatPaymentCreatedResponse {
+  final String paymentId;
+  final bool sandbox;
+  final String? codeUrl;
+  final String? prepayId;
+  final String? package;
+  final String? partnerId;
+  final String? appId;
+  final String? noncestr;
+  final String? timestamp;
+  final String? sign;
+
+  WechatPaymentCreatedResponse(
+    this.paymentId,
+    this.sandbox, {
+    this.codeUrl,
+    this.prepayId,
+    this.package,
+    this.partnerId,
+    this.appId,
+    this.noncestr,
+    this.timestamp,
+    this.sign,
+  });
+
+  toJson() => {
+        'payment_id': paymentId,
+        'sandbox': sandbox,
+        'code_url': codeUrl,
+        'prepay_id': prepayId,
+        'package': package,
+        'partner_id': partnerId,
+        'app_id': appId,
+        'noncestr': noncestr,
+        'timestamp': timestamp,
+        'sign': sign,
+      };
+
+  static WechatPaymentCreatedResponse fromJson(Map<String, dynamic> json) {
+    return WechatPaymentCreatedResponse(
+      json['payment_id'],
+      json['sandbox'] ?? false,
+      codeUrl: json['code_url'],
+      prepayId: json['prepay_id'],
+      package: json['package'],
+      partnerId: json['partner_id'],
+      appId: json['app_id'],
+      noncestr: json['noncestr'],
+      timestamp: json['timestamp'],
+      sign: json['sign'],
+    );
+  }
+}
+
+class StripePaymentCreatedResponse {
+  final String paymentId;
+  final String customer;
+  final String paymentIntent;
+  final String ephemeralKey;
+  final String publishableKey;
+  final String proxyUrl;
+
+  StripePaymentCreatedResponse(
+    this.paymentId,
+    this.customer,
+    this.paymentIntent,
+    this.ephemeralKey,
+    this.publishableKey,
+    this.proxyUrl,
+  );
+
+  toJson() => {
+        'payment_id': paymentId,
+        'customer': customer,
+        'payment_intent': paymentIntent,
+        'ephemeral_key': ephemeralKey,
+        'publishable_key': publishableKey,
+        'proxy_url': proxyUrl,
+      };
+
+  static StripePaymentCreatedResponse fromJson(Map<String, dynamic> json) {
+    return StripePaymentCreatedResponse(
+      json['payment_id'],
+      json['customer'],
+      json['payment_intent'],
+      json['ephemeral_key'],
+      json['publishable_key'],
+      json['proxy_url'] ?? '',
+    );
+  }
+}

+ 49 - 32
lib/service/pay_service.dart

@@ -2,38 +2,55 @@
 // import 'package:flutter_alipay/flutter_alipay.dart';
 // import 'package:stripe_payment/stripe_payment.dart';
 
-// class PayService {
-//   PayService._();
+class PayService {
+  PayService._();
 
-//   void wechatPay() async {
-//     bool isInstalled = await fluwx.isWeChatAppInstalled();
-//     if (isInstalled) {
-//       fluwx
-//           .pay(fluwx.PayInfo(
-//         appId: 'your_app_id',
-//         partnerId: 'your_partner_id',
-//         prepayId: 'your_prepay_id',
-//         packageValue: 'Sign=WXPay',
-//         nonceStr: 'your_nonce_str',
-//         timeStamp: 'your_timestamp',
-//         sign: 'your_sign',
-//       ))
-//           .then((response) {
-//         // 处理支付结果
-//       });
-//     } else {
-//       // alert('未安装微信');
-//     }
-//   }
+  /// 发起微信支付
+  Future<WechatPaymentCreatedResponse> createWechatPayment({
+    required String productId,
+    String? source,
+  }) async {
+    return sendPostRequest(
+      '/v1/payment/wechatpay/',
+      (resp) {
+        return WechatPaymentCreatedResponse.fromJson(resp.data);
+      },
+      formData: {
+        'product_id': productId,
+        'source': source,
+      },
+    );
+  }
 
-//   void alipay() async {
-//     String orderString = 'your_order_string'; // 从服务器获取
-//     var result = await FlutterAlipay.pay(orderString);
-//   }
+  void wechatPay() async {
+    bool isInstalled = await fluwx.isWeChatAppInstalled();
+    if (isInstalled) {
+      fluwx
+          .pay(fluwx.PayInfo(
+        appId: 'your_app_id',
+        partnerId: 'your_partner_id',
+        prepayId: 'your_prepay_id',
+        packageValue: 'Sign=WXPay',
+        nonceStr: 'your_nonce_str',
+        timeStamp: 'your_timestamp',
+        sign: 'your_sign',
+      ))
+          .then((response) {
+        // 处理支付结果
+      });
+    } else {
+      // alert('未安装微信');
+    }
+  }
 
-//   void stripe() async {
-//     var token = await StripePayment.paymentRequestWithCardForm(
-//         CardFormPaymentRequest());
-//     // 发送 token 到服务器进行支付处理
-//   }
-// }
+  void alipay() async {
+    String orderString = 'your_order_string'; // 从服务器获取
+    var result = await FlutterAlipay.pay(orderString);
+  }
+
+  void stripe() async {
+    var token = await StripePayment.paymentRequestWithCardForm(
+        CardFormPaymentRequest());
+    // 发送 token 到服务器进行支付处理
+  }
+}