pay method
Future<HmaroPayPaymentResult>
pay({
- required BuildContext context,
- required HamroPayConfig hamroPayConfig,
- HamroPayDisplayMode? displayMode = HamroPayDisplayMode.page,
- BottomSheetConfig bottomSheetConfig = const BottomSheetConfig(),
Implementation
Future<HmaroPayPaymentResult> pay({
required BuildContext context,
required HamroPayConfig hamroPayConfig,
// HamroPayPageContent? pageContent,
HamroPayDisplayMode? displayMode = HamroPayDisplayMode.page,
BottomSheetConfig bottomSheetConfig = const BottomSheetConfig(),
}) async {
try {
// Create repository with the config's server URL
final ApiCallRepository repository = ApiCallRepository(serverUrl: hamroPayConfig.serverUrl);
final sessionId = await repository.createSessionId(
clientApiKey: hamroPayConfig.clientApiKey,
clientId: hamroPayConfig.clientId,
clientSecret: hamroPayConfig.clientSecret,
merchantId: hamroPayConfig.merchantId,
merchantTransactionId: hamroPayConfig.merchantTransactionId,
transactionAmount: hamroPayConfig.transactionAmount,
products: hamroPayConfig.products,
phoneNumber: hamroPayConfig.phoneNumber,
);
Widget payPage = HamroPayPage(
// pageContent: pageContent,
merchantTransactionId: hamroPayConfig.merchantTransactionId,
merchantId: hamroPayConfig.merchantId,
clientId: hamroPayConfig.clientId,
clientSecret: hamroPayConfig.clientSecret,
sessionId: sessionId,
transactionAmount: hamroPayConfig.transactionAmount,
clientApiKey: hamroPayConfig.clientApiKey,
serverUrl: hamroPayConfig.serverUrl, // Pass the server URL
);
dynamic result;
if (displayMode == HamroPayDisplayMode.bottomSheet) {
result = await showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: bottomSheetConfig.backgroundColor,
isDismissible: bottomSheetConfig.isDismissible,
enableDrag: bottomSheetConfig.enableDrag,
builder: (_) => DraggableScrollableSheet(
expand: false,
initialChildSize: bottomSheetConfig.initialChildSize,
builder: (_, controller) => Material(
borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
child: payPage,
),
),
);
}
/* if (displayMode == HamroPayDisplayMode.bottomSheet) {
result = await showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
isDismissible: false,
enableDrag: false,
builder: (_) => DraggableScrollableSheet(
expand: false,
initialChildSize: bottomSheetConfig.initialChildSize,
builder: (_, controller) => Material(
borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
child: payPage,
),
),
);
}*/
else {
result = await Navigator.push<dynamic>(
context,
MaterialPageRoute(builder: (context) => payPage),
);
}
return result;
} catch (e) {
return HmaroPayPaymentResult(error: 'Payment Failed or Cancelled!');
}
}