samsungpay 0.0.5 samsungpay: ^0.0.5 copied to clipboard
you can call the native code in android to load samsung in-app payment gateway.
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:samsungpay/samsunf_pay_e.dart';
import 'package:samsungpay/samsungpay.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: const Home(),
),
);
}
}
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
String platformVersion = 'Unknown';
final TextEditingController productAmount = TextEditingController();
final TextEditingController taxAmount = TextEditingController();
final TextEditingController shippingAmount = TextEditingController();
final TextEditingController addedShippingAmount =
TextEditingController(text: '0');
final TextEditingController addedBillingAmount =
TextEditingController(text: '0');
final TextEditingController spayServiceID = TextEditingController();
final TextEditingController merchantID = TextEditingController();
final TextEditingController merchantName = TextEditingController();
final TextEditingController orderNumber = TextEditingController();
final List<String> currency = [
"USD",
"INR",
"KRW",
"EUR",
"AUD",
"JPY",
"CNY",
"GBP",
"SGD",
"RUB",
"BRL",
"HKD",
"THB",
"CAD",
"MYR",
"CHF",
"SEK",
"TWD",
"AED",
];
String dropdownValue = "AED";
@override
void initState() {
super.initState();
productAmount.text = '10';
taxAmount.text = '0';
shippingAmount.text = '0';
spayServiceID.text = "4e0aed1db4354e86a8b90f";
merchantID.text = "MPGS";
merchantName.text = "MPGS";
orderNumber.text = DateTime.now().millisecondsSinceEpoch.toString();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState(String spayServiceID) async {
try {
platformVersion =
(await Samsungpay.isAvailable(spayServiceID)).toString();
} on PlatformException catch (e) {
platformVersion = 'Failed to get platform version. $e';
}
// 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 Scaffold(
body: ListView(
children: [
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: productAmount,
keyboardType: TextInputType.number,
decoration: const InputDecoration(hintText: "Product Amount"),
),
),
),
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: taxAmount,
keyboardType: TextInputType.number,
decoration: const InputDecoration(hintText: "Tax Amount"),
),
),
),
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: shippingAmount,
keyboardType: TextInputType.number,
decoration: const InputDecoration(hintText: "Shipping Amount"),
),
),
),
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: addedShippingAmount,
keyboardType: TextInputType.number,
decoration:
const InputDecoration(hintText: "Added Shipping Amount"),
),
),
),
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: addedBillingAmount,
keyboardType: TextInputType.number,
decoration:
const InputDecoration(hintText: "Added Billing Amount"),
),
),
),
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: merchantID,
keyboardType: TextInputType.text,
decoration: const InputDecoration(hintText: "MerchantId"),
),
),
),
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: merchantName,
keyboardType: TextInputType.text,
decoration: const InputDecoration(hintText: "MerchantName"),
),
),
),
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: orderNumber,
keyboardType: TextInputType.text,
decoration: const InputDecoration(hintText: "OrderNumber"),
),
),
),
Center(
child: SizedBox(
width: 60,
child: DropdownButton<String>(
value: dropdownValue,
iconSize: 24,
elevation: 16,
style: const TextStyle(color: Colors.black87),
underline: Container(
height: 1,
color: Colors.deepPurpleAccent,
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: currency.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
),
),
const SizedBox(
height: 80,
),
if (!platformVersion.contains("SPAY_READY"))
Center(
child: SizedBox(
width: 220,
child: TextField(
controller: spayServiceID,
keyboardType: TextInputType.text,
decoration:
const InputDecoration(hintText: "SPAY_SERVICE_ID"),
),
),
),
Center(child: Text('Running on: $platformVersion\n')),
if (!platformVersion.contains("SPAY_READY"))
Center(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.black87,
minimumSize: const Size(220, 50),
),
onPressed: () {
if (spayServiceID.text.isNotEmpty) {
initPlatformState(spayServiceID.text);
} else {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text("Please enter service id")));
}
},
child: const Text(
"Samsung Pay Available",
style: TextStyle(
fontSize: 14,
),
),
),
),
if (platformVersion.contains("SPAY_READY"))
Center(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.black87,
minimumSize: const Size(220, 50),
),
onPressed: () async {
try {
String? data = (await Samsungpay.payNow(
double.parse(productAmount.text),
double.parse(taxAmount.text),
double.parse(shippingAmount.text),
double.parse(addedShippingAmount.text),
double.parse(addedBillingAmount.text),
dropdownValue,
merchantID.text,
merchantName.text,
orderNumber.text,
));
print("coming from android: $data");
} catch (e) {
platformVersion = 'payment failed $e';
e.toString();
setState(() {});
}
},
child: const Text(
"Samsung Pay",
style: TextStyle(
fontSize: 14,
),
),
),
),
Center(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.black87,
minimumSize: const Size(220, 50),
),
onPressed: () async {
try {
await Samsungpay.doActivateSamsungPay();
} on PlatformException catch (e) {
platformVersion = 'payment failed $e';
}
},
child: const Text(
"Open Samsung Pay",
style: TextStyle(
fontSize: 14,
),
),
),
),
],
),
);
}
}