easymerchantsdk 0.0.1
easymerchantsdk: ^0.0.1 copied to clipboard
EasyMerchantSDK
Easy Merchant Sdk Implementation. #
To implement the sdk in your flutter project, you have to add the below path in your pubspec.yaml file inside dependencies section:
dependencies:
easymerchantsdk:
git: git_url
ref: ref
Easy Merchant Sdk Implementation(Android Side). #
Follow the below steps to implement the easymerchant sdk in your flutter project
Change the minimum sdk version. #
In your android -> app -> build.gradle file, there is a defaultConfig section. Change the minSdk to 24 in the defaultConfig.
Add the dependencies. #
Add the below dependencies in your android -> app -> build.gradle file.
dependencies {
implementation 'com.app:paysdk:1.1.9'
implementation 'com.hbb20:ccp:2.7.3'
implementation 'com.github.androidmads:QRGenerator:1.0.1'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
}
Add the gradle urls. #
To add the gradle urls, open your android -> build.gradle file and add the below lines in it.
allprojects {
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
maven {
url = uri(your_github_url)
credentials {
username = username
password = password
}
}
}
configurations.all {
resolutionStrategy {
force 'org.jetbrains.kotlin:kotlin-stdlib:1.8.22' // Replace with your desired version
}
}
}
Changes in settings.gradle file. #
Now open your android -> settings.gradle and add the below code inside repositories block.
repositories {
...
maven { url 'https://jitpack.io' }
maven {
url = uri(your_github_url)
credentials {
username = username
password = password
}
}
}
Changes in MainActivity file #
Inside your andorid/app/src/main/kotlin/your_package_name There is MainActivity.kt file, replace all code except first line with below:
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import com.example.easymerchantsdk.EasymerchantsdkPlugin
class MainActivity: FlutterActivity(){
private val CHANNEL = "easymerchantsdk"
private var easymerchantsdkPlugin: EasymerchantsdkPlugin? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
easymerchantsdkPlugin = EasymerchantsdkPlugin()
easymerchantsdkPlugin?.setActivity(this)
}
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
// EasymerchantsdkPlugin.registerWith(flutterEngine.dartExecutor.binaryMessenger)
super.configureFlutterEngine(flutterEngine)
// GeneratedPluginRegistrant.registerWith(flutterEngine)
easymerchantsdkPlugin?.let {
EasymerchantsdkPlugin().setActivity(this)
}
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "billing") {
val amount = call.argument<String>("amount") ?: ""
val billinginfo = call.argument<String?>("billinginfo") ?: ""
easymerchantsdkPlugin?.onMethodCall(call, result)
} else if(call.method == "getPlatformVersion"){
result.success("Android ${android.os.Build.VERSION.RELEASE}")
} else {
result.notImplemented()
}
}
}
}
How to call the sdk. #
Now, to call the sdk, please check the below exmaple code of dart file.
import 'dart:convert';
import 'dart:io';
import 'package:easymerchantsdk/easymerchantsdk.dart';
import 'package:flutter/material.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';
final Easymerchantsdk easymerchant = Easymerchantsdk();
String amount = "";
final _scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
TextEditingController textEditingController = TextEditingController();
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
initPlatformState();
if (Platform.isIOS) {
initializeViewController();
}
});
}
Future<void> initPlatformState() async {
try {
final version =
await easymerchant.getPlatformVersion() ?? 'Unknown platform version';
setState(() {
_platformVersion = version;
});
} catch (e) {
showSnackbar('Failed to get platform version: $e');
}
}
Future<void> initializeViewController() async {
try {
await easymerchant.setViewController();
print('ViewController initialized successfully');
} catch (e) {
print('Failed to initialize ViewController: $e');
showSnackbar('Failed to initialize ViewController: $e');
}
}
Future<void> billingreq() async {
try {
Map<String, dynamic> additionalInfoRequest = {
"name": "Test User",
"email": "test@gmail.com",
"phone_number": "9465351125",
"country_code": "91",
"description": "Test"
};
Map<String, dynamic> billingInfo = {
"address": "Mohali, Punjab",
"country": "India",
"state": "Punjab",
"city": "Anandpur Sahib",
"postal_code": "140118",
"additional_info": additionalInfoRequest,
};
String jsonString = json.encode(billingInfo);
final result = await easymerchant.billing(
textEditingController.text.toString(),
jsonString,
"staging",
"mobilesdk1980IUuCzwWl",
"mobilesdk1980LVHnN0Oh"
);
setState(() {
amount = result ?? 'Unknown billing info';
});
print('Result is here: $result');
} catch (e) {
showSnackbar('Failed to get billing: $e');
}
}
Future<void> makePayment() async {
if (textEditingController.text.isEmpty) {
showSnackbar('Please enter an amount');
return;
}
try {
Map<String, dynamic> additionalInfoRequest = {
'name': 'Test User',
'email': 'test@gmail.com',
'phone_number': "9465351125",
'country_code': "91",
"description": "Test",
};
Map<String, dynamic> jsonRequest = {
'address': "Mohali, Punjab",
'country': 'India',
'state': "Punjab",
'city': "Anandpur Sahib",
"postal_code": "140118",
"additional_info": additionalInfoRequest,
};
String jsonRequestt = jsonEncode(jsonRequest);
final result = await easymerchant.makePayment(
textEditingController.text,
jsonRequestt,
"staging",
"mobilesdk1980IUuCzwWl",
"mobilesdk1980LVHnN0Oh"
);
if (result != null) {
showSnackbar(result);
setState(() {
amount = result;
});
print("Payment Response: $result");
} else {
setState(() {
amount = 'Payment failed';
});
showSnackbar("Payment failed");
}
} catch (e) {
showSnackbar('Payment error: $e');
}
}
void showSnackbar(String message) {
_scaffoldMessengerKey.currentState?.showSnackBar(SnackBar(content: Text(message)));
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: GestureDetector(
onTap: () {
FocusScope.of(context).unfocus();
},
child: Scaffold(
key: _scaffoldMessengerKey,
appBar: AppBar(
title: const Text('Easy Merchant SDK Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Running on: $_platformVersion\n'),
Padding(
padding: const EdgeInsets.all(25),
child: TextFormField(
keyboardType: TextInputType.number,
controller: textEditingController,
decoration: InputDecoration(
labelText: "Amount",
border: OutlineInputBorder(),
),
),
),
Text("Result: ${textEditingController.text.toString()}\n"),
if (Platform.isIOS)
ElevatedButton(
onPressed: () => billingreq(),
child: const Text('Pay Now (iOS)'),
),
if (Platform.isAndroid)
ElevatedButton(
onPressed: () async {
await makePayment();
},
child: const Text("Pay Now (Android)"),
),
],
),
),
),
),
);
}
}