hamropay_flutter 0.0.2 copy "hamropay_flutter: ^0.0.2" to clipboard
hamropay_flutter: ^0.0.2 copied to clipboard

An un-official Flutter plugin for hamro-pay Payment Gateway, integrate with ease and without any hassle.

example/lib/main.dart

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:hamropay_flutter/hamropay_flutter.dart';
import 'package:hamropay_flutter/utils/constants/api-constants.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Hamropay Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primarySwatch: Colors.green),
      home: const HamroPayApp(title: 'Hamro-Pay Payment'),
    );
  }
}

class HamroPayApp extends StatefulWidget {
  final String title;

  const HamroPayApp({super.key, required this.title});

  @override
  State<HamroPayApp> createState() => _HamroPayAppState();
}

class _HamroPayAppState extends State<HamroPayApp> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: SizedBox(
        width: double.infinity,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset("assets/png/hamro-pay.png", height: 60),
            SizedBox(height: 10),
            HamroPayButton(
              title: "Pay with Hamro-pay bottomsheet",
              onPressed: () async {
                String merchantTxnId =
                    DateTime.now().millisecondsSinceEpoch.toString() +
                    "_" +
                    (1000 + Random().nextInt(8999)).toString();
                // Now you can get the response in the .then() or use await
                try {
                  final result = await HamroPayService.instance.pay(
                    context: context,
                    displayMode: HamroPayDisplayMode.bottomSheet,
                    hamroPayConfig: HamroPayConfig.dev(
                      clientId: "pass client ID",
                      clientSecret: "pass cleint secret",
                      clientApiKey: "pass client Api key",
                      transactionAmount:
                          0, // transaction amount must be in the long
                      phoneNumber: "pass mobile number",
                      merchantId: "pass merchant id",
                      merchantTransactionId: "pass merchantTxnId",
                      products: [
                        Product(
                          name: "Shirt",
                          imageUrl:
                              "https://veirdo.in/cdn/shop/files/b_0119493a-9927-4550-8323-baefe5f625c0.jpg?v=1724147309",
                          description: "Cotton Shirt",
                          price: 99.99,
                          quantity: 1,
                        ),
                        Product(
                          name: "yshirt ",
                          imageUrl:
                              "https://veirdo.in/cdn/shop/files/b_0119493a-9927-4550-8323-baefe5f625c0.jpg?v=1724147309",
                          description: "Cotton Shirt",
                          price: 99.99,
                          quantity: 1,
                        ),
                      ],
                    ),
                  );

                  // Handle the payment result here
                  if (result.error != null) {
                    // Payment failed or was cancelled
                    print("Payment Error: ${result.error}");
                    // Show error message to user
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(
                        content: Text("Payment failed: ${result.error}"),
                      ),
                    );
                  } else if (result.data != null) {
                    // Payment was successful
                    print("Payment Success: ${result.data}");
                    // Handle successful payment
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(
                        content: Text("Payment Status:${result.data?.status}"),
                      ),
                    );
                  }
                } catch (e) {
                  // Handle any exceptions
                  print("Payment Error: $e");
                  ScaffoldMessenger.of(
                    context,
                  ).showSnackBar(SnackBar(content: Text("Payment error: $e")));
                }
              },

              // ... rest of your button properties
              textStyle: Theme.of(context).textTheme.titleMedium?.copyWith(
                color: Theme.of(context).colorScheme.onPrimary,
              ),
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.red,
              ),
            ),
            SizedBox(height: 10),
            HamroPayButton(
              title: "Pay with Hamro-pay fullscreen",
              onPressed: () async {
                String merchantTxnId =
                    DateTime.now().millisecondsSinceEpoch.toString() +
                    "_" +
                    (1000 + Random().nextInt(8999)).toString();

                // Now you can get the response in the .then() or use await
                try {
                  final result = await HamroPayService.instance.pay(
                    context: context,
                    hamroPayConfig: HamroPayConfig.dev(
                      clientId: "pass client ID",
                      clientSecret: "pass cleint secret",
                      clientApiKey: "pass client Api key",
                      transactionAmount:
                      0, // transaction amount must be in the long
                      phoneNumber: "pass mobile number",
                      merchantId: "pass merchant id",
                      merchantTransactionId: "pass merchantTxnId",
                      products: [
                        Product(
                          name: "Shirt",
                          imageUrl:
                              "https://veirdo.in/cdn/shop/files/b_0119493a-9927-4550-8323-baefe5f625c0.jpg?v=1724147309",
                          description: "Cotton Shirt",
                          price: 99.99,
                          quantity: 1,
                        ),
                        Product(
                          name: "yshirt ",
                          imageUrl:
                              "https://veirdo.in/cdn/shop/files/b_0119493a-9927-4550-8323-baefe5f625c0.jpg?v=1724147309",
                          description: "Cotton Shirt",
                          price: 99.99,
                          quantity: 1,
                        ),
                      ],
                    ),
                  );

                  // Handle the payment result here
                  if (result.error != null) {
                    // Payment failed or was cancelled
                    print("Payment Error: ${result.error}");
                    // Show error message to user
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(
                        content: Text("Payment failed: ${result.error}"),
                      ),
                    );
                  } else if (result.data != null) {
                    // Payment was successful
                    print("Payment Success: ${result.data}");
                    // Handle successful payment
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(
                        content: Text("Payment Status:${result.data?.status}"),
                      ),
                    );
                  }
                } catch (e) {
                  // Handle any exceptions
                  print("Payment Error: $e");
                  ScaffoldMessenger.of(
                    context,
                  ).showSnackBar(SnackBar(content: Text("Payment error: $e")));
                }
              },

              // ... rest of your button properties
              textStyle: Theme.of(context).textTheme.titleMedium?.copyWith(
                color: Theme.of(context).colorScheme.onPrimary,
              ),
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.red,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
4
likes
135
points
59
downloads

Publisher

unverified uploader

Weekly Downloads

An un-official Flutter plugin for hamro-pay Payment Gateway, integrate with ease and without any hassle.

Homepage
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

crypto, dartz, dio, flutter, flutter_inappwebview, flutter_web_plugins, plugin_platform_interface, pretty_dio_logger, web, webview_flutter

More

Packages that depend on hamropay_flutter

Packages that implement hamropay_flutter