jio_payment_sdk 0.0.13 copy "jio_payment_sdk: ^0.0.13" to clipboard
jio_payment_sdk: ^0.0.13 copied to clipboard

A Flutter package to integrate Jio Payment Gateway.

example/lib/main.dart

import 'dart:io';
import 'package:flutter/material.dart';
import 'dart:math';
import 'package:jio_payment_sdk/jio_payment_sdk.dart';

void main() => runApp(const MyApp());

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: Homepage());
  }
}

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

  @override
  State<Homepage> createState() => _HomepageState();
}

class _HomepageState extends State<Homepage> implements PaymentCallback {
  String selectedEnv = 'uat';

  final TextEditingController inputPrice = TextEditingController();
  final TextEditingController merchantIdController = TextEditingController();
  final TextEditingController aggIdController = TextEditingController();
  final TextEditingController secretKeyController = TextEditingController();
  final TextEditingController mccCodeController = TextEditingController();

  String response = '';
  String? amountError;

  final Map<String, Map<String, String>> envDefaults = {
    'prod': {
      'merchantId': 'JP2000000000016',
      'aggId': 'JP2000000000001',
      'secretKey': 'bc400a4b14e04f1b85b63afc8f4d10b6',
    },
    'uat': {
      'merchantId': 'JP2000000000031',
      'aggId': '',
      'secretKey': 'abc',
    },
  };

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

  void _applyEnvDefaults() {
    final defaults = envDefaults[selectedEnv]!;
    merchantIdController.text = defaults['merchantId']!;
    aggIdController.text = defaults['aggId']!;
    secretKeyController.text = defaults['secretKey']!;
  }

  String merchantTxnNo() {
    final random = Random.secure();
    String txnNo = '';
    for (int i = 0; i < 16; i++) {
      txnNo += random.nextInt(10).toString();
    }
    return "TEST$txnNo";
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(title: const Text('Jio Payment SDK'), centerTitle: true),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            if (response.isNotEmpty) ...[
              Center(child: Text(response, style: const TextStyle(fontSize: 16))),
              const SizedBox(height: 16),
            ],

            // Environment dropdown
            const Text('Environment', style: TextStyle(fontWeight: FontWeight.bold)),
            const SizedBox(height: 8),
            DropdownButtonFormField<String>(
              initialValue: selectedEnv,
              decoration: const InputDecoration(border: OutlineInputBorder()),
              items: const [
                DropdownMenuItem(value: 'uat', child: Text('UAT')),
                DropdownMenuItem(value: 'prod', child: Text('PROD')),
              ],
              onChanged: (value) {
                if (value != null) {
                  setState(() {
                    selectedEnv = value;
                    _applyEnvDefaults();
                  });
                }
              },
            ),
            const SizedBox(height: 16),

            // Merchant ID
            const Text('Merchant ID', style: TextStyle(fontWeight: FontWeight.bold)),
            const SizedBox(height: 8),
            TextField(
              controller: merchantIdController,
              readOnly: true,
              decoration: const InputDecoration(border: OutlineInputBorder()),
            ),
            const SizedBox(height: 16),

            // Agg ID
            const Text('Aggregator ID', style: TextStyle(fontWeight: FontWeight.bold)),
            const SizedBox(height: 8),
            TextField(
              controller: aggIdController,
              readOnly: true,
              decoration: const InputDecoration(border: OutlineInputBorder()),
            ),
            const SizedBox(height: 16),

            // Secret Key
            const Text('Secret Key', style: TextStyle(fontWeight: FontWeight.bold)),
            const SizedBox(height: 8),
            TextField(
              controller: secretKeyController,
              readOnly: true,
              decoration: const InputDecoration(border: OutlineInputBorder()),
            ),
            const SizedBox(height: 16),

            // MCC Code - Android only
            if (Platform.isAndroid) ...[
              const Text('MCC Code', style: TextStyle(fontWeight: FontWeight.bold)),
              const SizedBox(height: 8),
              TextField(
                controller: mccCodeController,
                keyboardType: TextInputType.number,
                decoration: const InputDecoration(
                  border: OutlineInputBorder(),
                  hintText: 'Enter MCC Code',
                ),
              ),
              const SizedBox(height: 16),
            ],

            // Amount
            const Text('Amount', style: TextStyle(fontWeight: FontWeight.bold)),
            const SizedBox(height: 8),
            TextField(
              controller: inputPrice,
              keyboardType: TextInputType.number,
              decoration: InputDecoration(
                border: const OutlineInputBorder(),
                hintText: 'Enter amount',
                errorText: amountError,
              ),
              onChanged: (_) {
                if (amountError != null) {
                  setState(() {
                    amountError = null;
                  });
                }
              },
            ),
            const SizedBox(height: 24),

            // Pay button
            ElevatedButton(
              onPressed: () {
                if (inputPrice.text.isEmpty) {
                  setState(() {
                    amountError = 'Please enter an amount';
                  });
                  return;
                }
                final am = double.tryParse(inputPrice.text);
                if (am == null) {
                  setState(() {
                    amountError = 'Please enter a valid amount';
                  });
                  return;
                }

                setState(() {
                  response = '';
                });

                JioPaymentSdk.initializeJioPayments(
                  context,
                  callback: this,
                  amount: am,
                  env: selectedEnv == 'prod' ? JioPaymentEnv.prod : JioPaymentEnv.uat,
                  merchantId: merchantIdController.text,
                  aggId: aggIdController.text,
                  secretKey: secretKeyController.text,
                  email: 'test@gmail.in',
                  userName: 'Test User',
                  merchantName: 'Reliance',
                  merchantImage: "asset/Ajio logo.png",
                  merchantTrId: merchantTxnNo(),
                  isAssetMerchantImage: true,
                  mccCode: mccCodeController.text,
                  allowedPaymentTypes: ["NB", "UPI_QR", "UPI_INTENT", "UPI_VPA", "CARD"],
                  timeOut: 1000,
                );
              },
              style: ElevatedButton.styleFrom(padding: const EdgeInsets.symmetric(vertical: 14)),
              child: const Text("Pay", style: TextStyle(fontSize: 18)),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void onPaymentCompletedResponse(PaymentResult result) {
    setState(() {
      response = result.jsonData.toString();
    });
  }
}