Home > Mobile >  How to setup dynamic/variable price structure on flutter web where user can enter there own price by
How to setup dynamic/variable price structure on flutter web where user can enter there own price by

Time:11-03

I am creating a product on the stripe dashboard with a default price of $888 and it gives me a priceId.

enter image description here

then I am using firebase cloud functions and stripe extension to save the data of the product and its details, i am also creating a checkout session whenever user click on the "Add amount" button and then it redirected to the checkout page of stripe where it shows to pay $888 like this

but I want the user can first enter his desired amount on my flutter web UI and that amount user will he be paying on stripe checkout

this is the code that I use on my "add amount" button

  buyStuff(ProductDetials pd) async {
    setState(() {
      loadingPayment = true;
    });
    String userUid = FirebaseAuth.instance.currentUser!.uid;
    var docRef = await FirebaseFirestore.instance
        .collection('users')
        .doc(userUid)
        .collection('checkout_sessions')
        .add({
      'price': pd.priceId,
      'quantity': pd.quatity,
      'mode': 'payment',
      'success_url': 'https://success.com',
      'cancel_url': 'https://cancel.com',
    });

    docRef.snapshots().listen((ds) async {
      if (ds.exists) {
        //check any error
        var error;

        try {
          error = ds.get('error');
        } catch (e) {
          error = null;
        }

        if (error != null) {
          //show a dialog for error message
          print(error);
        } else {
          String url = ds.get('url');

          if (kIsWeb) {
            //open url in new tab

            launchUrl(Uri.parse(url));

This is my firestore db

This is my firestore db. I am using a textfield where I am taking the amount of the user and saving it on my texteditingcontroller. How can I change the price or create a new priceId. On stripe there is very less or few docs for flutter stripe.

update:- this is how solved it, on code i have added line_items

buyStuff(ProductDetials pd) async {
setState(() {
  loadingPayment = true;
});
String userUid = FirebaseAuth.instance.currentUser!.uid;
var docRef = await FirebaseFirestore.instance
    .collection('users')
    .doc(userUid)
    .collection('checkout_sessions')
    .add({
  'price': pd.priceId,
  'quantity': pd.quatity,
  'mode': 'payment',
  'line_items': [
    {
      'price_data': {
        'currency': 'usd',
        'product_data': {
          'name': 'Wallet',
        },
        'unit_amount': enterAmountController.text,
      },
      'quantity': 1,
    },
  ],
  'success_url': 'https://success.com',
  'cancel_url': 'https://cancel.com',
});

CodePudding user response:

With Checkout Session, you're able to use ad-hoc price with line_items.price_data to create dynamic amount when creating a Checkout Session. In this case, you don't have to create a price upfront. For example,

const session = await stripe.checkout.sessions.create({
  line_items: [
    {
      price_data: {
        currency: 'usd',
        product_data: {
          name: 'T-shirt',
        },
        unit_amount: 2000,
      },
      quantity: 1,
    },
  ],
  mode: 'payment',
  success_url: 'https://example.com/success',
  cancel_url: 'https://example.com/cancel',
});
  • Related