flutter_onepay 1.1.1 copy "flutter_onepay: ^1.1.1" to clipboard
flutter_onepay: ^1.1.1 copied to clipboard

PlatformAndroid

One stop payment solution by parish softwares

example/lib/main.dart

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_onepay/models/onepay_environment.dart';
import 'dart:async';

import 'package:flutter_onepay/parish_softwares_onepay.dart';

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

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

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

class _MyAppState extends State<MyApp> {
  final _apiKeyController = TextEditingController();
  final _secretKeyController = TextEditingController();
  final _transactionIdController = TextEditingController();

  String? _response = '';
  final _sdk = ParishSoftwaresOnepay();

  bool _isInitialized = false;

  @override
  void initState() {
    super.initState();
    _initializeSdkOnce();
  }

  Future<void> _initializeSdkOnce() async {
    if (!_isInitialized) {
      try {
        await _sdk.initialize(
          "efc6170a13f604fd6aecab45735b5f56f81a51f8",
          "65651889cc917c1fa5cdf26e811fedd565ce95de193b7e3218576faacc4922224c92c24f1c6a745e",
          OnePayEnvironment.TEST,
        );
        _isInitialized = true;
        print('SDK Initialized Successfully');
      } catch (e) {
        print('SDK Initialization Failed: $e');
      }
    }
  }

  @override
  void dispose() {
    _apiKeyController.dispose();
    _secretKeyController.dispose();
    _transactionIdController.dispose();
    super.dispose();
  }

  Future<void> _createOrder() async {
    try {
      final paymentResponse = await _sdk.createOrder({
        "amount": 500,
        "customerEmail": "emily.clark@gmail.com",
        "customerName": "Emily Clark",
        "customerPhone": "9417757511",
      });

      if (paymentResponse != null) {
        print('Order Response: ${paymentResponse.id}');
        final txnId = paymentResponse.id;

        setState(() {
          _response = paymentResponse.id;
          _transactionIdController.text = txnId;
        });
      } else {
        setState(() {
          _response = jsonEncode(paymentResponse);
        });
      }
    } catch (e) {
      print(e.toString());
    }
  }

  Future<void> _processPayment() async {
    final id = _transactionIdController.text.trim();
    if (id.isEmpty) {
      setState(() {
        _response = 'Transaction ID is required';
      });
      return;
    }
    try {
      final result = await _sdk.processPayment(id);
      setState(() {
        _response = result ?? 'Payment failed';
      });
    } catch (e) {
      print(e.toString());
    }
  }

  Future<void> _getTransactionDetails() async {
    try {
      final id = _transactionIdController.text.trim();
      if (id.isEmpty) {
        setState(() {
          _response = 'Transaction ID is required';
        });
        return;
      }

      final result = await _sdk.getTransactionDetails(id);
      setState(() {
        _response = jsonEncode(result);
      });
    } catch (e) {
      print("error caued ${e.toString}");
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('OnePay SDK Example')),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: SingleChildScrollView(
            child: Column(
              children: [
                TextField(
                  controller: _apiKeyController,
                  decoration: const InputDecoration(labelText: 'API Key'),
                ),
                TextField(
                  controller: _secretKeyController,
                  decoration: const InputDecoration(labelText: 'Secret Key'),
                ),
                const SizedBox(height: 16),
                ElevatedButton(
                  onPressed: _createOrder,
                  child: const Text('Create Order'),
                ),
                const SizedBox(height: 16),
                TextField(
                  controller: _transactionIdController,
                  decoration: const InputDecoration(
                    labelText: 'Transaction ID',
                  ),
                ),
                const SizedBox(height: 16),
                ElevatedButton(
                  onPressed: _processPayment,
                  child: const Text('Pay Now'),
                ),
                const SizedBox(height: 16),
                ElevatedButton(
                  onPressed: _getTransactionDetails,
                  child: const Text('Get Transaction Details'),
                ),
                const SizedBox(height: 24),
                Text(
                  'Response:\n$_response',
                  style: const TextStyle(fontSize: 14, color: Colors.blue),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
1
likes
120
points
44
downloads

Publisher

unverified uploader

Weekly Downloads

One stop payment solution by parish softwares

Homepage
Repository

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on flutter_onepay