You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Stripe集成到Flutter前端与PHP后端并使用自定义支付表单

Flutter + PHP 自定义Stripe支付表单实现方案

方案可行性

完全可行,逻辑和你之前Web端用Stripe JS+PHP的实现思路一致——前端用publishable key生成支付令牌(Token),后端用secret key完成支付扣费,这种模式适配Flutter/PHP的自定义支付场景,只要严格遵循Stripe的安全规范即可。


一、Flutter 前端实现步骤

  1. 添加Stripe依赖
    在pubspec.yaml中引入Flutter Stripe库:
dependencies:
  flutter_stripe: ^9.0.0 # 建议使用最新稳定版

执行flutter pub get完成安装。

  1. 初始化Stripe
    在App启动入口(如main.dart)用你的publishable key初始化:
import 'package:flutter_stripe/flutter_stripe.dart';

void main() {
  Stripe.publishableKey = '你的Stripe Publishable Key';
  runApp(const MyApp());
}
  1. 构建自定义支付表单并处理提交
    创建包含所需字段的表单,完成Token生成与后端请求:
import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';
import 'package:http/http.dart' as http;

class CustomPaymentForm extends StatefulWidget {
  const CustomPaymentForm({super.key});

  @override
  State<CustomPaymentForm> createState() => _CustomPaymentFormState();
}

class _CustomPaymentFormState extends State<CustomPaymentForm> {
  final _amountCtrl = TextEditingController();
  final _nameCtrl = TextEditingController();
  final _cardNumberCtrl = TextEditingController();
  final _expiryCtrl = TextEditingController(); // 格式:MM/YY
  final _cvvCtrl = TextEditingController();

  Future<void> _handlePaymentSubmit() async {
    try {
      // 1. 构造卡片参数
      final cardParams = CardParams(
        number: _cardNumberCtrl.text.replaceAll(' ', ''),
        expMonth: int.parse(_expiryCtrl.text.split('/')[0]),
        expYear: int.parse(_expiryCtrl.text.split('/')[1]),
        cvc: _cvvCtrl.text,
        name: _nameCtrl.text,
      );

      // 2. 生成Stripe Token
      final tokenResult = await Stripe.instance.createToken(cardParams);
      final paymentToken = tokenResult.token.id;

      // 3. 发送Token到PHP后端
      final response = await http.post(
        Uri.parse('你的后端支付接口地址'),
        body: {
          'token': paymentToken,
          'amount': _amountCtrl.text, // 注意:金额单位为分,如100代表$1.00
        },
      );

      // 4. 处理后端响应
      if (response.statusCode == 200) {
        // 支付成功逻辑(如弹窗提示、跳转订单页)
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('支付成功')),
        );
      } else {
        // 支付失败逻辑
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('支付失败:${response.body}')),
        );
      }
    } catch (e) {
      // 表单验证或Token生成失败处理
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('提交失败:$e')),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextField(controller: _amountCtrl, hintText: '金额(分)'),
            const SizedBox(height: 12),
            TextField(controller: _nameCtrl, hintText: '持卡人姓名'),
            const SizedBox(height: 12),
            TextField(controller: _cardNumberCtrl, hintText: '银行卡号'),
            const SizedBox(height: 12),
            TextField(controller: _expiryCtrl, hintText: '有效期(MM/YY)'),
            const SizedBox(height: 12),
            TextField(controller: _cvvCtrl, hintText: 'CVV码'),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: _handlePaymentSubmit,
              child: const Text('提交支付'),
            ),
          ],
        ),
      ),
    );
  }
}

二、PHP 后端实现步骤

  1. 安装Stripe PHP库
    通过Composer安装:
composer require stripe/stripe-php
  1. 编写支付处理接口
    创建process_payment.php文件,实现扣费逻辑:
<?php
require 'vendor/autoload.php';

// 初始化Stripe
\Stripe\Stripe::setApiKey('你的Stripe Secret Key');

// 获取前端参数
$token = $_POST['token'] ?? '';
$amount = $_POST['amount'] ?? 0;

if (empty($token) || $amount <= 0) {
    http_response_code(400);
    echo json_encode(['status' => 'error', 'message' => '参数无效']);
    exit;
}

try {
    // 创建Charge完成支付
    $charge = \Stripe\Charge::create([
        'amount' => (int)$amount,
        'currency' => 'usd', // 根据业务需求设置币种,如cny
        'source' => $token,
        'description' => '自定义支付订单',
    ]);

    // 返回成功响应
    http_response_code(200);
    echo json_encode([
        'status' => 'success',
        'charge_id' => $charge->id,
        'amount' => $charge->amount
    ]);
} catch (\Stripe\Exception\CardException $e) {
    // 卡片相关错误(如余额不足、卡号无效)
    http_response_code(400);
    echo json_encode(['status' => 'error', 'message' => $e->getError()->message]);
} catch (\Stripe\Exception\ApiErrorException $e) {
    // Stripe API通用错误
    http_response_code(500);
    echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
} catch (Exception $e) {
    // 系统其他错误
    http_response_code(500);
    echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
?>

三、方案适用性说明

这个方案完全适配Flutter/PHP的自定义支付场景:

  • 满足自定义表单需求,无需依赖Stripe预制UI组件
  • 遵循Stripe安全规范:前端仅生成Token(不存储/传输敏感支付数据),后端用Secret Key完成扣费,避免数据泄露风险
  • 可复用你之前Web端PHP的部分逻辑,降低开发成本

内容的提问来源于stack exchange,提问作者sonkhya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 20:22:45