iframe_cashpay_plugin 0.0.6 copy "iframe_cashpay_plugin: ^0.0.6" to clipboard
iframe_cashpay_plugin: ^0.0.6 copied to clipboard

outdated

A plugin to add payments iframe_cashpay to your Flutter application..

example/lib/main.dart

import 'dart:convert';
import 'dart:math';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:iframe_cashpay_plugin/iframe_cashpay_plugin.dart';
import 'beneficiary_list.dart';
import 'createorder.dart';

void main() {
  runApp(const PayMaterialApp());
}

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Pay for Flutter Demo',
      home: PaySampleApp(title: 'PaySampleApp'),
    );
  }
}

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

  @override
  State<PaySampleApp> createState() => PaySampleAppState();
}

class PaySampleAppState extends State<PaySampleApp> {
  List<BeneficiaryList> beneficiaryList = [];
  bool check1 = false;
  String desc = "";
  String result = "";

  //Token returned from Response login
  //Documentation https://documenter.getpostman.com/view/17550185/2s93XzwN9o
  String token = "**********************************************************";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        backgroundColor: Colors.white,
        body: ListView(children: [
          CheckboxListTile(
            title: const Text(
              'ساعة 5000ريال',
              textDirection: TextDirection.rtl,
            ),
            value: check1,
            onChanged: (bool? value) {
              onChackWristwatch(value!, 5000, "ساعة,");
            },
            secondary: const Icon(Icons.help),
          ),
          ElevatedButton(
              child: const Text('الدفع عبر كاش باي'),
              onPressed: () async {
                (beneficiaryList.isNotEmpty)
                    ? await createOrder(token)
                        .then((value) => value.ResultCode == 1
                            ? showModalBottomSheet<void>(
                                context: context,
                                isScrollControlled: true,
                                builder: (BuildContext context) {
                                  return Container(
                                      height:
                                          MediaQuery.of(context).size.height *
                                              0.7,
                                      decoration: const BoxDecoration(
                                        color: Colors.white,
                                        borderRadius: BorderRadius.only(
                                          topLeft: Radius.circular(25.0),
                                          topRight: Radius.circular(25.0),
                                        ),
                                      ),
                                      //BottomSheetIframeCashPay SDK to use iFrame CashPay
                                      child: IframeCashPay(
                                        iframeURL: value.iframeURL,
                                        onConfirmPayment: onConfirmPayment,
                                      ));
                                })
                            : null)
                    : null;
              }),
          Text(result)
        ]));
  }

//Await for iFrameCashPay to return message {NEEDTOCHECK or Confirmation}
  onConfirmPayment(message) {
    if (message.message == "Confirmation" || message.message == "NEEDTOCHECK") {
      //After Confirmatin from iFrame Returned message {NEEDTOCHECK or Confirmation}.
      //Here use CheckOrderStatus to check order status.
      //Documentation https://documenter.getpostman.com/view/17550185/2s93XzwN9o
      Navigator.pop(context);
    }
    setState(() {
      result = message.message;
    });
  }

//Documentation https://documenter.getpostman.com/view/17550185/2s93XzwN9o
  Future<CreateOrderResponse> createOrder(String token) async {
    CreateOrderResponse createOrderModel;
    var response = await http.post(
        Uri.parse('{{baseUrl}}/v1/CashPay/CreateOrder'),
        headers: <String, String>{
          'Content-Type': 'application/json; charset=UTF-8',
          'Authorization': token
        },
        body: jsonEncode(<String, Object>{
          "requestId": Random().nextInt(1000).toString(),
          "currencyID": 2,
          "payementTime": 5,
          "beneficiaryList": beneficiaryList.map((e) => e.toJson()).toList(),
          // "beneficiaryList": [
          //   {
          //     "identifier": "777777777",
          //     "identifierType": 1,
          //     "amount": 3000,
          //     "itemName": "ماوس"
          //   },
          //   {
          //     "identifier": "777777777",
          //     "identifierType": 1,
          //     "amount": 2000,
          //     "itemName": "ساعة"
          //   }
          // ],
          "des": desc
        }));
    if (response.statusCode == 200) {
      // If the server did return a 201 CREATED response,
      // then parse the JSON.
      createOrderModel =
          CreateOrderResponse.fromJson(jsonDecode(response.body));
      if (createOrderModel.ResultCode == 1) {
        return createOrderModel;
      } else {
        return const CreateOrderResponse(
            ResultCode: -1, iframeURL: "", orderID: "");
      }
    } else {
      return const CreateOrderResponse(
          ResultCode: -1, iframeURL: "", orderID: "");
    }
  }

  onChackWristwatch(bool value, double amount, String des) {
    check1 = value;
    if (value) {
      setState(() {
        desc += des;
        beneficiaryList.add(BeneficiaryList(
            amount: amount,
            identifier: "777777777",
            identifierType: 1,
            itemName: des));
      });
      if (kDebugMode) {
        print(
            'onChackWristwatch ${beneficiaryList.map((e) => e.toJson()).toList()}');
      }
    } else {
      setState(() {
        desc = desc.replaceAll(des, "");
        beneficiaryList
            .removeWhere((element) => element.identifier == "777777777");
      });
      if (kDebugMode) {
        print(
            'onChackWristwatch ${beneficiaryList.map((e) => e.toJson()).toList()}');
      }
    }
  }
}
5
likes
0
points
21
downloads

Publisher

unverified uploader

Weekly Downloads

A plugin to add payments iframe_cashpay to your Flutter application..

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, flutter_webview_plugin

More

Packages that depend on iframe_cashpay_plugin