Flutter Stripe Payment Error需要可发布密钥/缺少可发布密钥

1l5u6lss  于 2023-05-19  发布在  Flutter
关注(0)|答案(2)|浏览(159)

我已经创建了一个测试演示条纹支付,但当我通过点击按钮测试。我得到一个

API密钥是必需的,但我已经给出了。请帮我找出错误。很可能是我从main函数调用key的方式

import 'dart:convert';
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';
import 'package:http/http.dart' as http;

Future<void> main() async {
  Stripe.publishableKey =
  "pk_test_51JT7jkCTAUDjRNFVfafy4Gskx1KzUNk8nPj8T51zzCPE18fA17DOFO6MqSZVTCxhVCSWGwouDSe0yjcObAznHLW600VBoGyDcg";
  await Stripe.instance.applySettings();
  runApp(const PaymentScreen());
}

class PaymentScreen extends StatelessWidget {
  const PaymentScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Stripe Payment',
      theme: ThemeData(
        primarySwatch: Colors.teal,
      ),
      home: const PaymentPage(),
    );
  }
}

class PaymentPage extends StatelessWidget {
  const PaymentPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    Future<void> initPaymentSheet(context, {required String email, required int amount}) async {
      try {
        // 1. create payment intent on the server
        final response = await http.post(
            Uri.parse(
                'https://us-central1-stripe-checkout-flutter.cloudfunctions.net/stripePaymentIntentRequest'),
            body: {
              'email': email,
              'amount': amount.toString(),
            });

        final jsonResponse = jsonDecode(response.body);
        log(jsonResponse.toString());

        //2. initialize the payment sheet
        await Stripe.instance.initPaymentSheet(
          paymentSheetParameters: SetupPaymentSheetParameters(
            paymentIntentClientSecret: jsonResponse['paymentIntent'],
            merchantDisplayName: 'Flutter Stripe Store Demo',
            customerId: jsonResponse['customer'],
            customerEphemeralKeySecret: jsonResponse['ephemeralKey'],
            style: ThemeMode.light,
            testEnv: true,
            merchantCountryCode: 'SG',
          ),
        );

        await Stripe.instance.presentPaymentSheet();

        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('Payment completed!')),
        );
      } catch (e) {
        if (e is StripeException) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text('Error from Stripe: ${e.error.localizedMessage}'),
            ),
          );
        } else {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text('Error: $e')),
          );
        }
      }
    }

    return Scaffold(
      appBar: AppBar(
        title: const Text("Stripe Demo App"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
              ),
              onPressed: () async {
                await initPaymentSheet(context, email: "example@gmail.com", amount: 200000);
              },
              child: const Text(
                'Buy Token 2000 USD',
                style: TextStyle(color: Colors.white),
              ),
            )
          ],
        ),
      ),
    );
  }
}

你可以测试代码。除此之外,它没有其他错误

gg58donl

gg58donl1#

虽然我通常建议您将其作为issueflutter_stripe存储库一起归档,但与他们的示例相比,您可以考虑是否在初始化过程中丢失了一行:

void main() async {
  WidgetsFlutterBinding.ensureInitialized(); // <- you don't have this line

  // set the publishable key for Stripe - this is mandatory
  Stripe.publishableKey = stripePublishableKey;
  runApp(PaymentScreen());
}
zf9nrax1

zf9nrax12#

付款是不能够suceed类似的问题,我面临着与条纹:所以我在初始化flutter示例之前添加了merchantID和urlScreme。它将验证相同的商家

Stripe.publishableKey = "pk_test_RS3cXiyeMHhEmpqnojBPuukSZQdjA7S00GE5LjhtqLongSecureKey342423";
Stripe.merchantIdentifier = "acct_1IL78e48329489014f";

Stripe.stripeAccountId = "acct_1IL78e48329489014f";
Stripe.urlScheme = "flutterstripe";
await Stripe.instance.applySettings();

相关问题