flutterdartstripe-paymentsdart-frog

how do i implement stripe subscription on server side with dart using library like dart_frog?


i am working on a project in which i want to implement stripe subscription using dart_frog which is library provides rest functionality using dart.

user_service.dart

Future<void> userSubscription(
    UserSubscriptionModel userSubscriptionModel,
    String? userId,
  ) async {
    Logger().info(
      '<---------------------------stripe-subscription----------------------->',
    );
    // initilize stripe
    final stripe = Stripe(environment['STRIPE_PRIVATE_KEY']!);

    final customerGetRes = await stripe.customer.get(
      '$stripeBaseUrl/v1/customers/search',
      queryParameters: {'query': "name: 'harsh jani'"},
    );

    Logger().info('customer res : $customerGetRes');

    if (customerGetRes['id'] == null) {
      final customerPostRes = await stripe.customer.post(
        '$stripeBaseUrl/v1/customers',
        data: {'name': 'harsh jani', 'email': 'harsh1@yopmail.com'},
      );
      Logger().info('customer post res : $customerPostRes');
    }

    // await userCollection.update(
    //   where.eq('userId', userId),
    //   modify.set('brandSubscription', userSubscriptionModel.brandIds),
    //   upsert: true,
    // );

    final priceRes = await stripe.price.get('$stripeBaseUrl/v1/prices');

    Logger().info('price res : $priceRes');

    Logger().info(
      '---------------------------stripe-subscription------------------------',
    );
  }

user_controller.dart `

static Future<Response> userSubscription(RequestContext context) async {
    try {
      final request = context.request;
      final authenticator = context.read<Authenticator>();
      final userModuleServices = context.read<UserModuleService>();
      // Retrieve userId from the Authenticator service
      final userId = await authenticator.getUserId(context.request.headers);

      final brandIds = UserSubscriptionModel.fromJson(
        await request.json() as Map<String, dynamic>,
      );
      await userModuleServices.userSubscription(
        brandIds,
        userId.toString(),
      );
      return successResponse(
        ResponseKey.subscribtionSuccessHintAppMsg,
      );
    } catch (e) {
      Logger().info(e.toString());
      if (e is ExceptionMsg) {
        return Response.json(
          statusCode: e.statusCode,
          body: GeneralResponse(
            status: e.status,
            message: e.message,
          ),
        );
      } else {
        return internalServerError();
      }
    }
  }

i want to implement stripe subscription and also want to implement webhook for the subscription in dart.


Solution

  • Once you created the Customer and Price objects, you have two options to create a Subscription.

    Option 1. Directly create the Subscription (link). Then collect the payment method details, but you can't do that with the REST API. You'll need to use Stripe.js with the Payment Element as explained here.

    Option 2. Create a Checkout Session in Subscription mode (link), then redirect users to the Checkout page hosted by Stripe. This is explained in details here.