payhere_mobilesdk_flutter 1.0.3 payhere_mobilesdk_flutter: ^1.0.3 copied to clipboard
Make PayHere payments with your Flutter App!
import 'package:flutter/material.dart';
import 'package:payhere_mobilesdk_flutter/payhere_mobilesdk_flutter.dart';
void main() {
// MaterialApp is being initialized like this,
// instead of it being inside a separate class due
// to a context issue when showing AlertDialog(s) later
// in the app.
//
// See:
// "No MaterialLocalizations found - MyApp widgets require MaterialLocalizations to be
// provided by a Localizations widget ancestor"
// https://stackoverflow.com/questions/56275595/no-materiallocalizations-found-myapp-widgets-require-materiallocalizations-to
runApp(MaterialApp(home: App()));
}
class App extends StatefulWidget {
@override
_AppState createState() => _AppState();
}
class _AppState extends State<App> {
// String _platformVersion = 'Unknown';
@override
void initState() {
super.initState();
// initPlatformState();
}
void showAlert(BuildContext context, String title, String msg){
// set up the button
Widget okButton = TextButton(
child: Text("OK"),
onPressed: () { Navigator.pop(context); },
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text(title),
content: Text(msg),
actions: [
okButton,
],
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
void startOneTimePayment(BuildContext context) async{
Map paymentObject = {
"sandbox": true, // true if using Sandbox Merchant ID
"merchant_id": "1211149", // Replace your Merchant ID
"merchant_secret": "4uS83EVTOml4DxK0pruz0K8MPXVBpMuLE8m4bfpiWHBf",
"notify_url": "https://ent13zfovoz7d.x.pipedream.net/",
"order_id": "ItemNo12345",
"items": "Hello from Flutter!",
"amount": "50.00",
"currency": "LKR",
"first_name": "Saman",
"last_name": "Perera",
"email": "samanp@gmail.com",
"phone": "0771234567",
"address": "No.1, Galle Road",
"city": "Colombo",
"country": "Sri Lanka",
"delivery_address": "No. 46, Galle road, Kalutara South",
"delivery_city": "Kalutara",
"delivery_country": "Sri Lanka",
"custom_1": "",
"custom_2": ""
};
PayHere.startPayment(
paymentObject, (paymentId) {
print("One Time Payment Success. Payment Id: $paymentId");
showAlert(context, "Payment Success!", "Payment Id: $paymentId");
}, (error) {
print("One Time Payment Failed. Error: $error");
showAlert(context, "Payment Failed", "$error");
}, () {
print("One Time Payment Dismissed");
showAlert(context, "Payment Dismissed", "");
}
);
}
void startRecurringPayment(BuildContext context) async{
Map paymentObject = {
"sandbox": true, // true if using Sandbox Merchant ID
"merchant_id": "1211149", // Replace your Merchant ID
"merchant_secret": "4uS83EVTOml4DxK0pruz0K8MPXVBpMuLE8m4bfpiWHBf",
"notify_url": "https://ent13zfovoz7d.x.pipedream.net/",
"order_id": "ItemNo12345",
"items": "Hello from Flutter!",
"amount": "50.00",
"recurrence": "1 Month", // Recurring payment frequency
"duration": "1 Year", // Recurring payment duration
"currency": "LKR",
"first_name": "Saman",
"last_name": "Perera",
"email": "samanp@gmail.com",
"phone": "0771234567",
"address": "No.1, Galle Road",
"city": "Colombo",
"country": "Sri Lanka",
"delivery_address": "No. 46, Galle road, Kalutara South",
"delivery_city": "Kalutara",
"delivery_country": "Sri Lanka",
"custom_1": "",
"custom_2": ""
};
PayHere.startPayment(
paymentObject, (paymentId) {
print("Recurring Payment Success. Payment Id: $paymentId");
showAlert(context, "Payment Success!", "Payment Id: $paymentId");
}, (error) {
print("Recurring Payment Failed. Error: $error");
showAlert(context, "Payment Failed", "$error");
}, () {
print("Recurring Payment Dismissed");
showAlert(context, "Payment Dismissed", "");
}
);
}
void startTokenizationPayment(BuildContext context) async{
Map paymentObject = {
"sandbox": true, // true if using Sandbox Merchant ID
"preapprove": true, // Required
"merchant_id": "1211149", // Replace your Merchant ID
"merchant_secret": "4uS83EVTOml4DxK0pruz0K8MPXVBpMuLE8m4bfpiWHBf",
"notify_url": "https://ent13zfovoz7d.x.pipedream.net/",
"order_id": "ItemNo12345",
"items": "Hello from Flutter!",
"currency": "LKR",
"first_name": "Saman",
"last_name": "Perera",
"email": "samanp@gmail.com",
"phone": "0771234567",
"address": "No.1, Galle Road",
"city": "Colombo",
"country": "Sri Lanka",
};
PayHere.startPayment(
paymentObject, (paymentId) {
print("Tokenization Payment Success. Payment Id: $paymentId");
showAlert(context, "Payment Success!", "Payment Id: $paymentId");
}, (error) {
print("Tokenization Payment Failed. Error: $error");
showAlert(context, "Payment Failed", "$error");
}, () {
print("Tokenization Payment Dismissed");
showAlert(context, "Payment Dismissed", "");
}
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
onPressed: () { startOneTimePayment(context); },
child: Text('Start One Time Payment!')),
TextButton(
onPressed: () { startRecurringPayment(context); },
child: Text('Start Recurring Payment!')),
TextButton(
onPressed: () { startTokenizationPayment(context); },
child: Text('Start Tokenization Payment!')),
],
),
),
);
}
}