【问题标题】:Is there a way to create monthly subscription via flutter and stripe?有没有办法通过颤振和条纹创建每月订阅?
【发布时间】:2022-03-31 06:58:31
【问题描述】:

我正在为我的实践创建一个应用程序,其中包含每月订阅和每次订阅。我已经将它与火力相结合。定期购买部分已经完成,但是我不知道如何处理flutter-stripe中的每月订阅。节点、C#、Go、python 等中的条带文档中已经有代码可用。这是链接:https://stripe.com/docs/billing/subscriptions/checkout/fixed-price;说真的,我不理解这些代码。这就是为什么我想知道无论如何都可以通过颤动条纹二人组进行定期付款。如果有,请告诉我。这是一次购买的代码:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:stripe_payment/stripe_payment.dart';
import 'package:cloud_functions/cloud_functions.dart';

class StripeService{
  final HttpsCallable intent = FirebaseFunctions.instance.httpsCallable('createPaymentIntent');
  User user;
  DocumentReference userDocReference;

  StripePayments(this.user) {
    user != null ? userDocReference = FirebaseFirestore.instance.collection("User").doc(user.uid)
    : user = FirebaseAuth.instance.currentUser;
    userDocReference = FirebaseFirestore.instance.collection("User").doc(user.uid);
  }

  startPaymentProcess(double amount) {
    StripePayment.paymentRequestWithCardForm(CardFormPaymentRequest()).then((paymentMethod) {
      amount *= 100.0; // multipliying with 100 to change $ to cents
      intent.call(<String, dynamic>{'amount': amount, 'currency': 'usd'}).then(
          (response) {
            _cnfrmPayment(response.data["client_secret"],
            paymentMethod); //function for confirmation for payment
      });
    });
  }

  _cnfrmPayment(String clientSecret, PaymentMethod paymentMethod, context) {
    StripePayment.confirmPaymentIntent(PaymentIntent(
      clientSecret: clientSecret,
      paymentMethodId: paymentMethod.id,
   )
  ).then((intentResult) {
      userDocReference.set({
        "status": intentResult.status,
        "paymentMethodId": intentResult.paymentMethodId,
        "paymentIntentId": intentResult.paymentIntentId
      });
      Scaffold.of(context).showSnackBar(
        SnackBar(
          content: Text("Payment done successfully")
        )
      );
    });
  }

  confirmDialog(String clientSecret, PaymentMethod paymentMethod, context) {
    var confirm = AlertDialog(
      title: Text("Confirm Payement"),
      content: Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text(
              "Make Payment",
              style: TextStyle(fontSize: 25),
            ),
            Text("Charge amount:\$100")
          ],
        ),
      ),
      actions: <Widget>[
        RaisedButton(
          child: Text('CANCEL'),
          onPressed: () {
            Navigator.of(context).pop();
            final snackBar = SnackBar(
              content: Text('Payment Cancelled'),
            );
            Scaffold.of(context).showSnackBar(snackBar);
          },
        ),
        RaisedButton(
          child: new Text('Confirm'),
          onPressed: () {
            Navigator.of(context).pop();
            _cnfrmPayment(
            clientSecret, paymentMethod); // function to confirm Payment
          },
        ),
      ],
    );
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return confirm;
        }
     );
  }
}

我从 https://medium.com/@hamza39460/stripe-payments-in-flutter-cb2f9cb053d1 中号帖子中复制的我的 firebase 函数。我已经在节点 8 中部署了这个功能,因为节点 >= 10 只在 firebase 的 blaze 计划上运行,而且我是一名学生。完整代码如下:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

const firestore = admin.firestore();
const settings = { timestampInSnapshots: true };
firestore.settings(settings)
const stripe = require('stripe')('MY_STRIPE_SECRET_KEY');
exports.createPaymentIntent = functions.https.onCall((data, context) => {
    return stripe.paymentIntents.create({
        amount: data.amount,
        currency: data.currency,
        payment_method_types: ['card'],
    });
});

【问题讨论】:

  • 订阅需要使用您的密钥通过后端调用创建,因此您可以在设置中使用 Firebase 进行调用。 firebase.google.com/products/extensions/… 有一个(非 Flutter)工作示例
  • @taintedzodiac 扩展程序要求您在 firebase 中制定计划

标签: firebase flutter stripe-payments e-commerce


【解决方案1】:

我相信有一个现有的包可以用来实现这种功能。

flutter_stripe:

Stripe Flutter SDK 可让您构建令人愉悦的支付方式 使用 Flutter 在您的原生 Android 和 iOS 应用程序中体验。我们 提供强大且可定制的 UI 屏幕和元素 开箱即用,用于收集用户的付款详细信息。

您还可以关注this blog,获取有关使用该软件包实施付款的有用指南。

【讨论】:

    猜你喜欢
    • 2019-03-31
    • 2020-07-05
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多