payment_gateway_demo 0.0.10 copy "payment_gateway_demo: ^0.0.10" to clipboard
payment_gateway_demo: ^0.0.10 copied to clipboard

unlistedoutdated

A payment gateway demo by Konze.

example/lib/main.dart

import 'dart:convert';
import 'dart:math';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';
import 'package:payment_gateway_demo/cash_free_payment/cf_payment_order.dart';
import 'package:payment_gateway_demo/cash_free_payment/cf_payment_package.dart';
import 'package:payment_gateway_demo/cash_free_payment/cf_response.dart';
import 'package:payment_gateway_demo/stripe_payment/stripe_payment_package.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Stripe.publishableKey = "pk_test_lU68DpvxP7Mz1zTc9r0VGBML00HVnnQwiE";
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
      debugShowCheckedModeBanner: false,
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: const <Widget>[
            Text(
              'Payment Demo',
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          _performStripePayment();
          // _performCashFreePayment();
        },
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }

  _performStripePayment() {
    //int? paymentId = 123;
    String? paymentToken = "pk_test_lU68DpvxP7Mz1zTc9r0VGBML00HVnnQwiE";
    double? paymentAmount = 300;
    String? currency = "INR";
    String? productName = "8 Week Coaching";
    String? userEmail = "123@xyz.com";

    final Map<String, dynamic> paymentData = <String, dynamic>{};
    //paymentData['paymentId'] = paymentId;
    paymentData['paymentToken'] = paymentToken;
    paymentData['paymentAmount'] = paymentAmount;
    paymentData['currency'] = currency;
    paymentData['productName'] = productName;
    paymentData['userEmail'] = userEmail;

    var jsonData = jsonEncode(paymentData);

    Navigator.of(context).push(
      MaterialPageRoute(
        builder: (_context) {
          return StripePaymentPackagePage(
            jsonData,
            onCallBack: (value) {
              if (value is PaymentIntent) {
                PaymentIntent paymentIntent = value;
                debugPrint('value ==>> ${paymentIntent.amount}');

                if (paymentIntent.status == PaymentIntentsStatus.Succeeded) {
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(
                      content: Text(
                          'Success!: The payment was confirmed successfully, Thank You.'),
                    ),
                  );
                }
              } else {
                debugPrint('value ==>> $value');
                ScaffoldMessenger.of(context)
                    .showSnackBar(SnackBar(content: Text('Error: $value')));
              }
            },
          );
        },
      ),
    );
  }

  /// ================================
  _performCashFreePayment() async {
    CFPaymentPackagePage cfPaymentPackagePage = CFPaymentPackagePage();

    String getRandomNo() {
      var rng = Random();
      return 'order ${rng.nextInt(1000000)}';
    }

    String envType = "TEST";
    String requestId = getRandomNo();
    String orderAmount = "1";
    String? tokenData = "";
    String customerName = "Arjun";
    String orderNote = "Order Note";
    String orderCurrency = "INR";
    String appId = envType == "TEST"
        ? "1831dac3fd47d13be98b7fd11381"
        : "1848d0ce8441fb8ffa258bc98481";
    String customerPhone = "9012341234";
    String customerEmail = "sample@gmail.com";
    String notifyUrl = "https://test.gocashfree.com/notify";

    tokenData =
        await getToken(envType, appId, requestId, orderAmount, orderCurrency);

    CFPaymentOrder cfPaymentOrder = cfPaymentPackagePage.initPaymentData(
      requestId,
      orderAmount,
      tokenData,
      customerName,
      orderNote,
      orderCurrency,
      customerPhone,
      customerEmail,
      notifyUrl,
      appId,
      envType,
    );

    cfPaymentPackagePage.doPayment(
      context,
      cfPaymentOrder.toMap(),
      callback: (value) {
        if (value is CFResponse) {
          CFResponse response = value;
          //Todo: CFResponse will provide you the result like payment status, message and payment details.
        }
      },
    );
  }

  Future<String?> getToken(String envType, String appId, String requestId,
      String orderAmount, String orderCurrency) async {
    var dio = Dio();
    final response = await dio.post(
      envType == "TEST"
          ? 'https://test.cashfree.com/api/v2/cftoken/order'
          : 'https://api.cashfree.com/api/v2/cftoken/order',
      options: Options(
        headers: {
          'x-client-id': appId,
          'x-client-secret': envType == "TEST"
              ? '4c41ca2022d1fa588efa91b73af7bb3489421735'
              : '62f1476aee1c57c7bef6259e104f9a868b068ed6',
          'Content-Type': 'application/json'
        },
      ),
      data: jsonEncode(
        {
          'orderId': requestId,
          'orderAmount': orderAmount,
          'orderCurrency': orderCurrency,
        },
      ),
    );

    print("Token Gen Resp : " + response.data.toString());
    if (response.statusCode == 200) {
      print('Token : ' + response.data['cftoken']);
      return response.data['cftoken'];
    } else {
      print('Failed to generate token');
      return null;
    }
  }
}

/*class Token {
  final String? cfToken;

  Token({this.cfToken});

  factory Token.fromJson(Map<String, dynamic> json) {
    return Token(
      cfToken: json['cftoken'],
    );
  }
}

class Order {
  Order() {
    appId = stage == "TEST"
        ? "1831dac3fd47d13be98b7fd11381"
        : "1848d0ce8441fb8ffa258bc98481";
  }

  String stage = "TEST";
  String orderId = getRandomNo();
  String orderAmount = "1";
  String tokenData = "";
  String customerName = "Arjun";
  String orderNote = "Order Note";
  String orderCurrency = "INR";
  String appId = "";
  String customerPhone = "9012341234";
  String customerEmail = "sample@gmail.com";
  String notifyUrl = "https://test.gocashfree.com/notify";

  static String getRandomNo() {
    var rng = Random();
    return 'order ${rng.nextInt(1000000)}';
  }

  Map<String, dynamic> toMap() {
    return {
      "orderId": orderId,
      "orderAmount": orderAmount,
      "customerName": customerName,
      "orderNote": orderNote,
      "orderCurrency": orderCurrency,
      "appId": appId,
      "customerPhone": customerPhone,
      "customerEmail": customerEmail,
      "stage": stage,
      "tokenData": tokenData,
      "notifyUrl": notifyUrl
    };
  }

  String toString() {
    return " \norderId" +
        orderId +
        " \norderAmount " +
        orderAmount +
        " \ncustomerName " +
        customerName +
        " \norderNote " +
        orderNote +
        " \norderCurrency " +
        orderCurrency +
        " \nappId " +
        appId +
        " \ncustomerPhone " +
        customerPhone +
        " \ncustomerEmail " +
        customerEmail +
        " \nstage " +
        stage +
        " \nnotifyUrl " +
        notifyUrl +
        " \ntokenData " +
        tokenData;
  }
}*/
1
likes
0
points
45
downloads

Publisher

unverified uploader

Weekly Downloads

A payment gateway demo by Konze.

Homepage

License

unknown (license)

Dependencies

cashfree_pg, flutter, flutter_stripe, get

More

Packages that depend on payment_gateway_demo