medapay 0.1.3 icon indicating copy to clipboard operation
medapay: ^0.1.3 copied to clipboard

The MedaPay Flutter SDK provides convenient access to the MedaPay API from applications written in dart for Flutter.

example/lib/main.dart

import 'dart:convert';

import 'package:example/theme.dart';
import 'package:flutter/material.dart';
import 'package:medapay/entity.dart';
import 'package:medapay/medapay.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MedaPay SDK Demo',
      theme: ThemeData(
        primarySwatch: mtpalette,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      initialRoute: '/',
      routes: {
        '/': (context) => MyHomePage(title: 'MedaPay SDK Demo'),
        '/payment-canceled': (context) => PaymentCanceledScreen(),
        '/payment-completed': (context) => PaymentCompletedScreen(),
      },
    );
  }
}

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

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  MedaPayLauncher launcher = MedaPayLauncher(
      paymentTitle: 'Merchant Name',
      authToken: "<AUTH_TOKEN>",
      isSandbox: true);

  void onReferenceNumber(String referenceNumber) {
    print("Reference Number: " + referenceNumber);
  }

  void onSuccess(MedaPayBillInfo billInfo) {
    print("Success Data: " + billInfo.toString());
  }

  void onError(MedaPayError error) {
    print("Error: " + error.toString());
  }

  void onCancel() {
    print("Canceled by user");
  }

  void onBillInfo(MedaPayBillInfo billInfo) {
    print("Bill Info Data: " + billInfo.toString());
  }

  void _initPayment(context) {
    // launcher.initWithReferenceNumber(
    //     context: context,
    //     referenceNumber: '10000671',
    //     onSuccessRoute: "/payment-successful",
    //     onCanceledRoute: "/payment-canceled",
    //     onSuccess: onSuccess,
    //     onCancel: onCancel,
    //     onError: onError,
    //     onBillInfo: onBillInfo);
    launcher.init(
      context: context,
      payment: new MedaPayPayment(
          orderId: 'xx1235678',
          customerName: "Ayele Mekite",
          accountNumber: "251910000000",
          description:
              "Flutter SDK Demo Payment Flutter SDK Demo Payment Flutter SDK Demo Payment",
          amount: 0.50,
          callbackUrl: 'https://example.com/callback',
          cancelUrl: 'https://example.com/cancel'),
      onReferenceNumber: onReferenceNumber,
      onSuccess: onSuccess,
      onError: onError,
      onCancel: onCancel,
      onBillInfo: onBillInfo,
      paymentMethods: MedaPayPaymentMethods.NON_USSD_METHODS,
      preferedPaymentMethod: MedaPayPaymentMethods.AMOLE,
      completedRoute: "/payment-completed",
      cancelRoute: "/payment-canceled",
      errorRoute: "/payment-error",
    );
  }

  Widget centerLaunchPaymentButton(BuildContext context) {
    return Center(
        child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
          RaisedButton(
              onPressed: () {
                _initPayment(context);
              },
              color: mtpalette,
              child: const Text(
                'Launch Test Payment',
                style: TextStyle(
                    fontSize: 16,
                    color: Colors.white,
                    fontWeight: FontWeight.bold),
              )),
        ]));
  }

  Widget productListView(products) {
    return ListView.builder(
      itemCount: products.length,
      itemBuilder: (BuildContext ctx, int index) {
        return Card(
          child: ListTile(
            leading: Image.network(
              products[index]['image'],
              width: 50,
              height: 50,
              fit: BoxFit.cover,
            ),
            title: Text(products[index]['title']),
            subtitle: Text(products[index]['category']),
            // trailing: Text(products[index]['price']),
          ),
        );
      },
    );
  }

  Widget productGridView(products) {
    return GridView.count(
      crossAxisCount: 2,
      children: List.generate(
          products.length,
          (index) => Card(
                child: GridTile(
                  header: Text(products[index]['title']),
                  child: Image.network(products[index]['image']),
                  footer: Text(products[index][
                      'category']), // trailing: Text(products[index]['price']),
                ),
              )),
    );
  }

  Widget demoProducts(BuildContext context) {
    return FutureBuilder(
      builder: (context, snapshot) {
        var products = json.decode(snapshot.data.toString());

        return productGridView(products);
      },
      future: DefaultAssetBundle.of(context).loadString("assets/products.json"),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: centerLaunchPaymentButton(context),
      // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

class PaymentCanceledScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Payment Canceled"),
        ),
        body: Center(child: Text("Payment Canceled by User")));
  }
}

class PaymentCompletedScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final billInfo =
        ModalRoute.of(context).settings.arguments as MedaPayBillInfo;

    print("Success screen");
    print(billInfo);

    return Scaffold(
        appBar: AppBar(
          title: Text("Payment Successful"),
        ),
        body: Center(child: Text("Payment Successfully Completed!")));
  }
}
2
likes
60
pub points
0%
popularity

Publisher

unverified uploader

The MedaPay Flutter SDK provides convenient access to the MedaPay API from applications written in dart for Flutter.

Homepage

Documentation

API reference

License

Icon for licenses.BSD-3-Clause (LICENSE)

Dependencies

flutter, http, url_launcher, webview_flutter

More

Packages that depend on medapay