flutter 条带状态:'需要来源',永远

qfe3c7zg  于 2022-11-25  发布在  Flutter
关注(0)|答案(1)|浏览(88)

我有一个关于智能板的项目。当购买成功时,我想做一些事情,当购买不成功时,我想做另一件事情。我的查询做得很好,但条带的状态总是“requires_source”
下面是我的代码。我使用firebase数据库,函数(节点)和Flutter。

`const functions = require("firebase-functions");
const stripe = require("stripe")("mykey");
const express = require('express');
const app = express();
app.use(express.static('public'));
let admin = require('firebase-admin');

admin.initializeApp(functions.config());

exports.stripePaymentIntentRequest = functions.https.onRequest(async (req, res) => {

    var db = admin.firestore();

    try {
        let customerId;

        //Gets the customer who's email id matches the one sent by the client
        const customerList = await stripe.customers.list({
            email: req.body.email,
            limit: 1
        });

        //Checks the if the customer exists, if not creates a new customer
        if (customerList.data.length !== 0) {
            customerId = customerList.data[0].id;
        }
        else {
            const customer = await stripe.customers.create({
                email: req.body.email
            });
            customerId = customer.data.id;
        }

        //Creates a temporary secret key linked with the customer
        const ephemeralKey = await stripe.ephemeralKeys.create(
            { customer: customerId },
            { apiVersion: '2020-08-27' }
        );

        //Creates a new payment intent with amount passed in from the client
        const paymentIntent = await stripe.paymentIntents.create({
            amount: parseInt(req.body.amount),
            currency: req.body.currency,
            metadata:{
            userid: (req.body.userid),
            bookid: (req.body.bookid),
            bookimage: (req.body.bookimage),
            booklevel: (req.body.booklevel),
            bookname: (req.body.bookname),
            smartboard: (req.body.smartboard),
            purchaseurl: (req.body.purchaseurl),
            },
            customer: customerId,
            description: 'NATIONAL ENGLISH BOOK APP'
        })

        if(paymentIntent.status == 'succeeded'){

        var packetRef = await db.collection('highschool_preview_info').doc(req.body.bookid).get();
        let afterbookid = packetRef.data().bookid;
        let afterbookimage = packetRef.data().bookimage;
        let afterbooklevel = packetRef.data().booklevel;
        let afterbookname = packetRef.data().bookname;
        let aftersmartboard = packetRef.data().smartboard;
        let afterpurchaseurl = packetRef.data().purchaseurl;

        const satisdb = await db.collection('Person').doc(req.body.userid).collection('myBooks').doc(req.body.bookid).set({
        'bookid': afterbookid,
        'bookimage': afterbookimage,
        'booklevel': afterbooklevel,
        'bookname': afterbookname,
        'smartboard': aftersmartboard,
        'purchaseurl': afterpurchaseurl,
        })

        var myBooksSecondControl = await db.collection('Person').doc(req.body.userid).collection('myBooks');
        var denemeDelete = await db.collection('Person').doc(req.body.userid).collection('myBooks').doc('deneme');
        if(denemeDelete.exists){
        denemeDelete.delete;
        }else{
        console.log('great455');
        }

        res.status(200).send({
                    paymentIntent: paymentIntent.client_secret,
                    ephemeralKey: ephemeralKey.secret,
                    customer: customerId,
                    success: true,

                })
    }

    res.status(200).send({
                paymentIntent: paymentIntent.client_secret,
                ephemeralKey: ephemeralKey.secret,
                customer: customerId,
                success: false,

            })




    } catch (error) {
        console.log(error)
        console.log(error.message)
        res.status(404).send({ success: false, error: error.message })
    }
});

这是firebase函数链接的状态参数

但这是我条带化事件的状态参数。

该函数可能无法从条带事件中获取状态参数,但我不确定我对条带没有太多经验。
前端代码如下。

Future<void> initPayment(
    {required String email,
      required String amount,
      required String userid,
      required String currency,
      required String bookid,
      required String bookimage,
      required String booklevel,
      required String bookname,
      required String smartboard,
      required String purchaseurl,
      required String client_secret,
      required BuildContext context}) async {
  try {
    // 1. Create a payment intent on the server
    final response = await http.post(
        Uri.parse(
            'myFunctionLink'),
        body: {
          'email': email,
          'amount': amount.toString(),
          'currency': currency,
          'bookid': bookid,
          'userid': userid,
          'bookimage': bookimage,
          'booklevel': booklevel,
          'bookname': bookname,
          'smartboard': smartboard,
          'purchaseurl': purchaseurl,
          'client_secret': client_secret,
          //userid & bookid ekle
        });

    final jsonResponse = jsonDecode(response.body);
    log(jsonResponse.toString());
    // 2. Initialize the payment sheet
    await Stripe.instance.initPaymentSheet(
        paymentSheetParameters: SetupPaymentSheetParameters(
          paymentIntentClientSecret: jsonResponse['paymentIntent'],
          merchantDisplayName: 'National English',
          customerId: jsonResponse['customer'],
          customerEphemeralKeySecret: jsonResponse['ephemeralKey'],
        ));
    await Stripe.instance.presentPaymentSheet();
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(
        content: Text('Payment is successful'),
      ),
    );
  } catch (errorr) {
    if (errorr is StripeException) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text('An error occured ${errorr.error.localizedMessage}'),
        ),
      );
    } else {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text('An error occured $errorr'),
        ),
      );
    }
  }
}

我找不到我该怎么办。

yrdbyhpb

yrdbyhpb1#

如果您可以看到PaymentIntent在您的Stripe Dashboard上成功创建,则意味着Stripe API调用成功(您可以console.log paymentIntent、emporialKey和customerId以确保它们正常运行)。
在调用Stripe API*(调用db.collection的一部分)之后,您需要进一步调试Firebase函数 *。

相关问题