openPaypal method
Future<void>
openPaypal({
- required BuildContext context,
- required String clientId,
- required String secretKey,
- required List<
Map< transactions,String, dynamic> > - bool sandboxMode = true,
- String? note,
- String paymentGatewayName = "PayPal",
- dynamic onShowToast()?,
- dynamic onPaymentSuccess()?,
- dynamic onPaymentFailure()?,
- dynamic onPaymentCancel()?,
Opens PayPal checkout screen with provided details
Parameters:
context
: BuildContext required for navigationclientId
: PayPal clientIdsecretKey
: PayPal secretKeysandboxMode
: Whether to use sandbox mode or livetransactions
: List of transactions to be paidnote
: Optional notepaymentGatewayName
: (Optional) Gateway name for default toast/logonShowToast
: (Optional) User can override default toastonPaymentSuccess
: Callback when payment succeedsonPaymentFailure
: Callback when payment failsonPaymentCancel
: Callback when payment is cancelled
Implementation
Future<void> openPaypal({
required BuildContext context,
required String clientId,
required String secretKey,
required List<Map<String, dynamic>> transactions,
bool sandboxMode = true,
String? note,
String paymentGatewayName = "PayPal",
Function()? onShowToast,
Function()? onPaymentSuccess,
Function()? onPaymentFailure,
Function()? onPaymentCancel,
}) async {
try {
Navigator.of(context, rootNavigator: true).push(
MaterialPageRoute(
builder: (ctx) => PaypalCheckoutView(
sandboxMode: sandboxMode,
clientId: clientId,
secretKey: secretKey,
transactions: transactions,
note: note ?? "Contact us for any questions on your order.",
onSuccess: (params) async {
final successResponse = IncodesPaymentResponse(
"Payment Successful",
true,
params['id']?.toString() ?? "",
);
if (onShowToast != null) {
onShowToast();
} else {
IncodesListener().onSuccess(
response: successResponse,
paymentGatewayName: paymentGatewayName,
isShowToast: true,
);
}
onPaymentSuccess?.call();
Navigator.of(ctx, rootNavigator: true).pop();
},
onError: (error) {
final failureResponse = IncodesPaymentResponse(
"Payment Failed: $error",
false,
"",
);
if (onShowToast != null) {
onShowToast();
} else {
IncodesListener().onFailure(
response: failureResponse,
paymentGatewayName: paymentGatewayName,
isShowToast: true,
);
}
onPaymentFailure?.call();
Navigator.of(ctx, rootNavigator: true).pop();
},
onCancel: () {
final cancelResponse = IncodesPaymentResponse(
"Payment Cancelled",
false,
"",
);
if (onShowToast != null) {
onShowToast();
} else {
IncodesListener().onFailure(
response: cancelResponse,
paymentGatewayName: paymentGatewayName,
isShowToast: true,
);
}
onPaymentCancel?.call();
Navigator.of(ctx, rootNavigator: true).pop();
},
),
),
);
} catch (e) {
log("PayPal Exception => $e");
}
}