flutter_snapmint_sdk 0.0.4
flutter_snapmint_sdk: ^0.0.4 copied to clipboard
Snapmint Flutter SDK for Web, Android, iOS, Linux, macOS and Windows
example/lib/main.dart
import 'dart:convert';
import 'dart:math';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_snapmint_sdk/src/SnapmintButtonGoldWidget.dart';
import 'package:flutter_snapmint_sdk/src/SnapmintButtonWidget.dart';
import 'package:flutter_snapmint_sdk/flutter_snapmint_sdk.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
late Map<String, dynamic> _response;
final _flutterSnapmintSdkPlugin = FlutterSnapmintSdk();
@override
void initState() {
super.initState();
//initPlatformState();
}
Future<void> openSnapMintModule() async {
Map<String, dynamic> responseData;
try {
String jsonString = getJsonString();
String nativeData =
await _flutterSnapmintSdkPlugin.openSnapmintModule(jsonString) ??
{}.toString();
print("TEST! $nativeData");
responseData = jsonDecode(nativeData);
print("RESPONSE DATA! $responseData");
print(
"TEST! data is code => ${responseData["statusCode"]} message is => ${responseData["responseMsg"]}");
} on Exception catch (e) {
responseData = <String, dynamic>{
"error": "Exception occurred",
"message": e.toString(),
"statusCode": "ERROR"
};
print("TEST! Exception handled: $e");
}
if (!mounted) return;
setState(() {
_response = responseData;
});
// Show popup with response data
_showResponseDialog(responseData);
}
void _showResponseDialog(Map<String, dynamic> responseData) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(
responseData["statusCode"] == "SUCCESS" ? "Success Response" : "Error Response",
style: TextStyle(
color: responseData["statusCode"] == "SUCCESS" ? Colors.green : Colors.red,
fontWeight: FontWeight.bold,
),
),
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
"Status Code: ${responseData["statusCode"] ?? "N/A"}",
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
if (responseData["responseMsg"] != null) ...[
Text(
"Response Message:",
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 4),
Text(responseData["responseMsg"]),
const SizedBox(height: 8),
],
if (responseData["error"] != null) ...[
Text(
"Error:",
style: const TextStyle(fontWeight: FontWeight.bold, color: Colors.red),
),
const SizedBox(height: 4),
Text(responseData["error"]),
const SizedBox(height: 8),
],
if (responseData["message"] != null) ...[
Text(
"Message:",
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 4),
Text(responseData["message"]),
const SizedBox(height: 8),
],
const Text(
"Full Response Data:",
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 4),
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.grey[100],
borderRadius: BorderRadius.circular(4),
),
child: Text(
const JsonEncoder.withIndent(' ').convert(responseData),
style: const TextStyle(fontSize: 12, fontFamily: 'monospace'),
),
),
],
),
),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text("Close"),
),
],
);
},
);
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion = await _flutterSnapmintSdkPlugin.getPlatformVersion() ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
margin: const EdgeInsets.only(left: 20, right: 20),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
//Text('Running on: $_platformVersion\n'),
ElevatedButton(
onPressed: () {
openSnapMintModule();
},
child: const Text(
"Open SDK",
style: TextStyle(
fontSize: 14,
color: Colors.blue,
fontFamily: 'Roboto',
fontWeight: FontWeight.w400,
),
)),
const SizedBox(
height: 10,
),
SnapmintButtonWidget(
amount: 100.0.toString(),
jsonUrl:
'https://merchant-js.snapmint.com/assets/merchant/5842/snap_muscleblaze.json',
fontFamily: const TextStyle(fontFamily: 'Roboto'),
disablePopup: false,
),
const SizedBox(
height: 10,
),
SnapmintButtonGoldWidget(
amount: 8355,
goldAmount: 2997,
jsonUrl:
'https://merchant-js.snapmint.com/assets/merchant/5841/snap_healthkart.json',
fontFamilyStyle: const TextStyle(fontFamily: 'Roboto'),
disablePopup: false,
disableSecondLine: false,
)
],
),
),
],
)),
),
);
}
String getJsonString() {
final randomNumber = 100000 + Random().nextInt(900000);
Map<String, dynamic> finalData = {
"merchant_key": "wVxPNirP",
"merchant_token": "GvmnkbeH",
"merchant_id": 1990,
"merchant_confirmation_url":
"https://services-pp-customer.melorralabsinfra.com/api/snapmint/snapmint_order/order_success/",
"merchant_failure_url":
"https://services-pp-customer.melorralabsinfra.com/api/snapmint/snapmint_order/order_failure/",
"mobile": "7777788888",
"store_id": 1,
"order_id":
"MELORRA-$randomNumber", // Random can be DateTime
"order_value": 4000,
"udf1": 1.91,
"udf2": 7147,
"full_name": "test",
"email": "khem.raj@snapmint.com",
"billing_address_line1": "test",
"billing_zip": "560037",
"shipping_address_line1": "test",
"shipping_zip": "560037",
"deviceType": "android",
"products": [
{
"sku": "227285",
"name": "Bold Show Diamond Earrings",
"quantity": 1,
"unit_price": 4000,
"udf2": 3000,
"udf1": "1.910 g"
}
]
};
// Create the full JSON structure
Map<String, dynamic> jsonData = {
"finalData": finalData,
"base_url": "https://sandboxapi.snapmint.com/v1/public/s2s_online_checkout",
"suc_url":
"https://services-pp-customer.melorralabsinfra.com/api/snapmint/snapmint_order/order_success/",
"fail_url":
"https://services-pp-customer.melorralabsinfra.com/api/snapmint/snapmint_order/order_failure/",
};
// Convert the Map to a JSON string
String jsonString = jsonEncode(jsonData);
print(jsonString);
return jsonString; // Output the JSON string
}
}