dart 抖动条纹支付出错

omhiaaxx  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(100)

在付款时出现此错误

交易失败错误'package:stripe_payment/src/stripe_payment. dart':Assert失败:第157行位置12:'intent.clientSecret!= null ':不为真。

这是我的支付代码类

import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:stripe_payment/stripe_payment.dart';

class StripeTransactionResponse {
  String message;
  bool success;
  String paymentConfirmId;
  String client_secret;
  StripeTransactionResponse(
      {this.message, this.success, this.client_secret, this.paymentConfirmId});
}

class StripeService {
  static String apiBase = 'https://api.stripe.com/v1';
  static String paymentApiUrl = '${StripeService.apiBase}/payment_intents';
  static String secret =
      'sk_test_51I3GcaKehTOEuQf6cFNlHAVWayKgaAgWyPgUdlSCfpFCqrTl9pBoLp9JrQLw6F4dOzRLKAfIVV8ZtTnjEzCw7QcI003MGowy0H';
  static Map<String, String> headers = {
    'Authorization': 'Bearer ${StripeService.secret}',
    'Content-Type': 'application/x-www-form-urlencoded'
  };

  static init() {
    StripePayment.setOptions(StripeOptions(
        publishableKey:
            "pk_test_51I3GcaKehTOEuQf6otthoRX3HgxJJQBbnNBtB6vUnQpaVQLwDM4jSB1XzI65anDcEAgppn99SmB6LKB5aEnKEDlv00uyvtp3zh",
        merchantId: "Test",
        androidPayMode: 'test'));
  }

  static Future<StripeTransactionResponse> payWithNewCard(
      double amount, String currency) async {
    try {
      var paymentMethod = await StripePayment.paymentRequestWithCardForm(
          CardFormPaymentRequest());

      var paymentIntentRes =
          await StripeService.createPaymentIntent(amount, currency);

      var response = await StripePayment.confirmPaymentIntent(PaymentIntent(
          clientSecret: paymentIntentRes['client_secret'],
          paymentMethodId: paymentMethod.id));

      if (response.status == 'succeeded') {
        return new StripeTransactionResponse(
          message: 'Transaction successful',
          success: true,
          paymentConfirmId: paymentIntentRes['id'],
          client_secret: paymentIntentRes['client_secret'],
        );
      }
    } catch (e) {
      return new StripeTransactionResponse(
          message: 'Transaction Failed Error ${e.toString()}', success: false);
    }
  }

  static Future<Map<String, dynamic>> createPaymentIntent(
      double amount, String currency) async {
    try {
      var amount1 = amount.round();
      Map<String, dynamic> body = {
        'amount': '$amount1',
        'currency': currency,
        'payment_method_types[]': 'card'
      };
      var respose = await http.post(StripeService.paymentApiUrl,
          body: body, headers: StripeService.headers);
      return jsonDecode(respose.body);
    } catch (e) {
      print("err charing user: " + e.toString());
    }
    return null;
  }
}

这是我的另一个代码,我在init方法中调用类,并调用付款按钮上的函数

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:lawyer_app/models/payement.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../homescreen.dart';
import '../userdrawerscreen.dart';
import 'package:http/http.dart' as http;

class AppointmentDetailForm extends StatefulWidget {
  String name;
  String email;
  String phone;
  String date;
  String time;
  String message;
  String lawyerid;
  AppointmentDetailForm(this.name, this.email, this.date, this.phone, this.time,
      this.message, this.lawyerid);
  @override
  _AppointmentDetailFormState createState() => _AppointmentDetailFormState();
}

class _AppointmentDetailFormState extends State<AppointmentDetailForm> {
  String fees;
  bool loader = false;

  var scret_key;
  var paymentConfirmId;

  Future<String> addPaymentdetailMethod(
    String secretkey,
    String publickey,
    String name,
    String email,
    String phone,
    String date,
    String time,
    String message,
    String fees,
    String lawyerid,
  ) async {
    String url = 'http://10xwebsolutions.com/lawyer/api/_complete_payment.php';

    final Map<String, dynamic> appointmentResponse = {
      'submit': 1,
      'secret_key': secretkey,
      'payment_confirm': publickey,
      'name': name,
      'email': email,
      "phone": phone,
      "date": date,
      "time": time,
      "message": message,
      "lawyer_id": lawyerid,
    };
    print(secretkey);
    print(publickey);
    print(name);
    print(email);
    print(phone);
    print(date);
    print(time);
    print(message);
    print(lawyerid);
    setState(() {
      loader = true;
    });

    final response = await http.post(url,
        body: jsonEncode(appointmentResponse),
        headers: {'content-type': 'application/json'});
    final Map<String, dynamic> responseData = json.decode(response.body);
    if (responseData['status'] == "true") {
      Fluttertoast.showToast(
          msg: "Booking Successfully",
          timeInSecForIosWeb: 1,
          backgroundColor: Colors.black,
          textColor: Colors.white,
          fontSize: 16.0);
      setState(() {
        loader = false;
      });
      Navigator.of(context)
          .push(MaterialPageRoute(builder: (context) => HomeScreen()));
    }

    if (responseData['status'] == "false") {
      Fluttertoast.showToast(
          msg: "Error..!!! Try again",
          timeInSecForIosWeb: 1,
          backgroundColor: Colors.black,
          textColor: Colors.white,
          fontSize: 16.0);
    }
  }

  @override
  void initState() {
    super.initState();
    StripeService.init();
    getPreference();
  }

  void getPreference() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    setState(() {
      fees = sharedPreferences.getString("fees");
    });
  }

  @override
  Widget build(BuildContext context) {
    var height = MediaQuery.of(context).size.height / 100;
    var width1 = MediaQuery.of(context).size.width / 100;
    return Scaffold(
      drawer: DrawerScreen(),
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(70),
        child: AppBar(
          title: Container(
              padding: EdgeInsets.only(top: 5, right: 5),
              child: Text("Appointment Details")),
          backgroundColor: Color(0xff6762F1),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.only(
                bottomRight: Radius.circular(20),
                bottomLeft: Radius.circular(20)),
          ),
        ),
      ),
      body: SingleChildScrollView(
        child: Container(
          width: double.infinity,
          padding: EdgeInsets.all(30),
          color: Colors.white,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                width: double.infinity,
                height: 79.4 * height,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      "Full Name",
                      style: TextStyle(
                          color: Color(0xff6762F1),
                          fontSize: 14,
                          fontFamily: "Montserrat-Regular"),
                    ),
                    SizedBox(
                      height: 0.7 * height,
                    ),
                    Text(
                      widget.name,
                      style: TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                          fontFamily: "Montserrat-Regular"),
                    ),
                    Divider(
                      color: Colors.black,
                      height: 2.9 * height,
                    ),
                    Text(
                      "Email Address",
                      style: TextStyle(
                          color: Color(0xff6762F1),
                          fontSize: 14,
                          fontFamily: "Montserrat-Regular"),
                    ),
                    SizedBox(
                      height: 0.7 * height,
                    ),
                    Text(
                      widget.email,
                      style: TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                          fontFamily: "Montserrat-Regular"),
                    ),
                    Divider(
                      color: Colors.black,
                      height: 2.9 * height,
                    ),
                    Text(
                      "Mobile Number",
                      style: TextStyle(
                          color: Color(0xff6762F1),
                          fontSize: 14,
                          fontFamily: "Montserrat-Regular"),
                    ),
                    SizedBox(
                      height: 0.7 * height,
                    ),
                    Text(
                      widget.phone,
                      style: TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                          fontFamily: "Montserrat-Regular"),
                    ),
                    Divider(
                      color: Colors.black,
                      height: 2.9 * height,
                    ),
                    Text(
                      "Date",
                      style: TextStyle(
                          color: Color(0xff6762F1),
                          fontSize: 14,
                          fontFamily: "Montserrat-Regular"),
                    ),
                    SizedBox(
                      height: 0.7 * height,
                    ),
                    Text(
                      widget.date,
                      style: TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                          fontFamily: "Montserrat-Regular"),
                    ),
                    Divider(
                      color: Colors.black,
                      height: 2.9 * height,
                    ),
                    Text(
                      "Time",
                      style: TextStyle(
                          color: Color(0xff6762F1),
                          fontSize: 14,
                          fontFamily: "Montserrat-Regular"),
                    ),
                    SizedBox(
                      height: 0.7 * height,
                    ),
                    Text(
                      widget.time,
                      style: TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                          fontFamily: "Montserrat-Regular"),
                    ),
                    Divider(
                      color: Colors.black,
                      height: 2.9 * height,
                    ),
                    Text(
                      "Message",
                      style: TextStyle(
                          color: Color(0xff6762F1),
                          fontSize: 14,
                          fontFamily: "Montserrat-Regular"),
                    ),
                    SizedBox(
                      height: 0.7 * height,
                    ),
                    Text(
                      widget.message,
                      style: TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                          fontFamily: "Montserrat-Regular"),
                    ),
                    Divider(
                      color: Colors.black,
                      height: 2.9 * height,
                    ),
                    Center(
                      child: Column(
                        children: <Widget>[
                          SizedBox(
                            height: 5.8 * height,
                          ),
                          Text(
                            fees,
                            style: TextStyle(
                                color: Color(0xff6762F1),
                                fontSize: 26,
                                fontFamily: "Fonarto"),
                          ),
                          SizedBox(
                            height: 2.9 * height,
                          ),
                          Visibility(
                            visible: loader ?? true,
                            child: Column(
                              children: [
                                SizedBox(
                                  height: 2,
                                ),
                                SpinKitCubeGrid(
                                  color: Color(0xff6762F1),
                                  size: 30,
                                ),
                                Text(
                                  "Please wait...",
                                  style: TextStyle(
                                      fontSize: 20, color: Color(0xff6762F1)),
                                )
                              ],
                            ),
                          ),
                          SizedBox(
                            height: 4 * height,
                          ),
                          Container(
                            height: 7 * height,
                            width: 82 * width1,
                            child: FlatButton(
                              child: Text(
                                "MAKE PAYMENT",
                                style: TextStyle(
                                    color: Colors.white,
                                    fontWeight: FontWeight.bold,
                                    fontSize: 14,
                                    fontFamily: "Montserrat-Bold"),
                              ),
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(50),
                              ),
                              onPressed: () async {
                                var response =
                                    await StripeService.payWithNewCard(
                                        2.0, "USD");
                                print(response.message);
                                print(response.toString() + "check");
                                print(response.success.toString());
                                if (response.success == true) {
                                  print("okay");

                                  // addPaymentdetailMethod(
                                  //     scret_key,
                                  //     paymentConfirmId,
                                  //     widget.name,
                                  //     widget.email,
                                  //     widget.phone,
                                  //     widget.date,
                                  //     widget.time,
                                  //     widget.message,
                                  //     fees,
                                  //     widget.lawyerid);
                                } else {
                                  print("failed");
                                }
                              },
                              color: Colors.black,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class LawyerDetailAndCancel {}
hc2pp10m

hc2pp10m1#

错误'intent.clientSecret != null': is not true.似乎在有效负载无效时发生。确保可发布密钥和秘密密钥正确。此外,stripe_payment已停产。更好的做法是迁移/使用flutter_stripe

相关问题