flutter_payunit 0.0.12 copy "flutter_payunit: ^0.0.12" to clipboard
flutter_payunit: ^0.0.12 copied to clipboard

The new Pay Unit SDK Package facilitates the intergration of payments on your applications with a single button and on the go.

example/lib/main.dart

import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_payunit/flutter_payunit.dart';

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

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) => const MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
          body: PayButton(),
        ),
      );
}

class PayButton extends StatelessWidget {
  const PayButton({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.grey,
        appBar: AppBar(
          elevation: 1,
          backgroundColor: Colors.white,
          title: const Text(
            "Pay unit demo",
            style: TextStyle(color: Colors.black),
          ),
          centerTitle: true,
        ),
        body: Center(
          child: Padding(
            padding: const EdgeInsets.only(left: 30, right: 30),
            child: PayUnitButton(
              apiUsername: "xxxxxx-xxxxxxx-xxxxxx-xxxxxx",
              apiPassword: "xxxxxx-xxxxxxx-xxxxxx-xxxxxx",
              apiKey: 'live_xxxxxxxxxxxxxxxxxxxxxxxxxxx',
              mode: 'live', // test or live
              totalAmount: 1000,
              currency: 'XAF',
              paymentCountry: 'CM',
              actionAfterProccess: (transactionId, transactionStatus,
                  phoneNumber, transactionGateway) {
                if (transactionStatus == 'SUCCESS') {
                  // Do this if the transaction succeeds
                  debugPrint('The tranaction $transactionId is Successfull');
                } else {
                  // Do this if the transaction fails
                  debugPrint('The tranaction $transactionId Failed');
                }
              },
              onPaymentInitiated:
                  (transactionId, phoneNumber, transactionGateway) {
                // In case user closes the app before transaction status is gotten,
                // store transaction details locally and constantly check status anywhere from app
                // For more info on getting transaction status, check: https://developer.payunit.net/rest-api/get-payment-status
                // An example function to get transaction status is provided below
              },
              returnUrl:
                  'https://webhook.site/d457b2f3-dd71-4f04-9af5-e2fcf3be8f34',
              notifyUrl:
                  'https://webhook.site/d457b2f3-dd71-4f04-9af5-e2fcf3be8f34',
            ),
          ),
        ));
  }

  // USe this function to check transaction status in background when user closes app.
  Future<Map<String, dynamic>> getTransactionStatus({
    required String xApiKey,
    required String apiUserName,
    required String apiPassword,
    required String mode,
    required String transactionId,
  }) async {
    final dioClient = Dio(
      BaseOptions(
        baseUrl: "https://gateway.payunit.net",
        connectTimeout: const Duration(seconds: 15),
        contentType: 'application/json',
      ),
    );
    final token = base64.encode(utf8.encode('$apiUserName:$apiPassword'));
    final response = await dioClient.get(
      '/api/gateway/paymentstatus/$transactionId',
      options: Options(
        headers: {
          'Authorization': 'Basic $token',
          'x-api-key': xApiKey,
          'mode': mode,
        },
      ),
    );
    // final status = response.data['data']['transaction_status'];
    return response.data;
  }
}
2
likes
160
points
722
downloads

Publisher

unverified uploader

Weekly Downloads

The new Pay Unit SDK Package facilitates the intergration of payments on your applications with a single button and on the go.

Repository (GitLab)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

auto_size_text, dio, flutter, intl, logger, url_launcher

More

Packages that depend on flutter_payunit