fawry_sdk 2.0.1 fawry_sdk: ^2.0.1 copied to clipboard
The Fawry SDK Pub is designed to incorporate Fawry's native Android and iOS SDKs into Flutter projects.
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:fawry_sdk/fawry_sdk.dart';
import 'package:fawry_sdk/fawry_utils.dart';
import 'package:fawry_sdk/model/bill_item.dart';
import 'package:fawry_sdk/model/fawry_launch_model.dart';
import 'package:fawry_sdk/model/launch_customer_model.dart';
import 'package:fawry_sdk/model/launch_merchant_model.dart';
import 'package:fawry_sdk/model/payment_methods.dart';
import 'package:fawry_sdk/model/response.dart';
BillItem item = BillItem(
itemId: 'Item1',
description: 'Book',
quantity: 6,
price: 50,
);
List<BillItem> chargeItems = [item];
LaunchCustomerModel customerModel = LaunchCustomerModel(
customerProfileId: '533518',
customerName: 'John Doe',
customerEmail: 'john.doe@xyz.com',
customerMobile: '+201000000000',
);
LaunchMerchantModel merchantModel = LaunchMerchantModel(
merchantCode: "+/IAAY2notgLsdUB9VeTFg==",
merchantRefNum: FawryUtils.randomAlphaNumeric(10),
secureKey: '69826c87-963d-47b7-8beb-869f7461fd93',
);
FawryLaunchModel model = FawryLaunchModel(
allow3DPayment: true,
chargeItems: chargeItems,
launchCustomerModel: customerModel,
launchMerchantModel: merchantModel,
skipLogin: true,
skipReceipt: true,
payWithCardToken: false,
paymentMethods: PaymentMethods.ALL,
);
String baseUrl = "https://atfawry.fawrystaging.com/";
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Fawry SDK Flutter',
theme: ThemeData(
useMaterial3: true,
),
darkTheme: ThemeData.dark(
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late StreamSubscription? _fawryCallbackResultStream;
@override
void initState() {
super.initState();
initSDKCallback();
}
@override
void dispose() {
_fawryCallbackResultStream?.cancel();
super.dispose();
}
// Initialize the Fawry SDK callback
Future<void> initSDKCallback() async {
try {
_fawryCallbackResultStream =
FawrySDK.instance.callbackResultStream().listen((event) {
setState(() {
ResponseStatus response = ResponseStatus.fromJson(jsonDecode(event));
handleResponse(response);
});
});
} catch (ex) {
debugPrint(ex.toString());
}
}
// Handle the response from Fawry SDK
void handleResponse(ResponseStatus response) {
switch (response.status) {
case FawrySDK.RESPONSE_SUCCESS:
{
debugPrint('Message: ${response.message}');
debugPrint('Json Response: ${response.data}');
}
break;
case FawrySDK.RESPONSE_ERROR:
{
debugPrint('Error: ${response.message}');
}
break;
case FawrySDK.RESPONSE_PAYMENT_COMPLETED:
{
debugPrint(
'Payment Completed: ${response.message}, ${response.data}');
}
break;
}
}
// Get the current platform
String currentPlatform() {
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return 'Current platform --> Android';
case TargetPlatform.iOS:
return 'Current platform --> iOS';
default:
return 'Current platform --> Unknown';
}
}
// Initialize Fawry SDK with required parameters
Future<void> startPayment() async {
await FawrySDK.instance.startPayment(
launchModel: model,
baseURL: baseUrl,
lang: FawrySDK.LANGUAGE_ENGLISH,
);
}
Future<void> openCardsManager() async {
await FawrySDK.instance.openCardsManager(
launchModel: model,
baseURL: baseUrl,
lang: FawrySDK.LANGUAGE_ENGLISH,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('Fawry SDK Flutter example'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Center(
child: Text(currentPlatform()),
),
Container(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Column(
children: [
ElevatedButton(
onPressed: () async {
await startPayment();
},
child: const Text('Checkout / Pay'),
),
const SizedBox(height: 5.0),
ElevatedButton(
onPressed: () async {
await openCardsManager();
},
child: const Text('Manage Cards'),
),
],
),
),
],
),
);
}
}