fetchResponseUrl method
Future<void>
fetchResponseUrl(
)
Implementation
Future<void> fetchResponseUrl() async {
final environment = widget.ipgClient.environment;
final Map<String, dynamic> requestData = {
// Set by the developer
"logoUrl": widget.ipgClient.logoUrl,
"returnUrl": widget.ipgClient.returnUrl,
"webhookUrl": widget.ipgClient.webhookUrl,
"merchantKey": widget.ipgClient.merchantKey,
"paymentType": widget.paymentType,
// Hardcoded, developer is not allowed to set the values
"isMobilePayment": 1,
"integrationType": "Flutter SDK",
"integrationVersion": "2.0.4",
"statusReturnUrl": "${getEndpoint(environment)}/status-view",
// Generated internally, developer is not allowed to set the values
"packageName": await getPackageName(),
"checkValue": getCheckValue(
merchantKey: widget.ipgClient.merchantKey,
merchantToken: widget.ipgClient.merchantToken,
invoiceId: widget.invoiceId,
amount: widget.amount,
currencyCode: widget.currencyCode),
// Required params for all payments
"orderDescription": (widget.orderDescription != null && widget.orderDescription!.isNotEmpty)?
widget.orderDescription : 'Order from Payable Mobile Payment',
"invoiceId": widget.invoiceId,
"customerFirstName": widget.customerFirstName,
"customerLastName": widget.customerLastName,
"customerMobilePhone": widget.customerMobilePhone,
"customerEmail": widget.customerEmail,
"billingAddressStreet": widget.billingAddressStreet,
"billingAddressCity": widget.billingAddressCity,
"billingAddressCountry": widget.billingAddressCountry,
"billingAddressPostcodeZip": widget.billingAddressPostcodeZip,
"amount": widget.amount,
"currencyCode": widget.currencyCode,
};
// Required params for recurring payments only
if (widget.paymentType == 2) {
requestData.addAll({
"startDate": widget.startDate,
"endDate": widget.endDate,
"recurringAmount": widget.recurringAmount,
"interval": widget.interval,
"isRetry": widget.isRetry,
"retryAttempts": widget.retryAttempts,
"doFirstPayment": widget.doFirstPayment,
});
}
// Optional params
requestData.addAll({
if (widget.custom1 != null) "custom1": widget.custom1,
if (widget.custom2 != null) "custom2": widget.custom2,
if (widget.customerPhone != null) "customerPhone": widget.customerPhone,
if (widget.billingAddressStreet2 != null)
"billingAddressStreet2": widget.billingAddressStreet2,
if (widget.billingCompanyName != null)
"billingCompanyName": widget.billingCompanyName,
if (widget.billingAddressPostcodeZip != null)
"billingAddressPostcodeZip": widget.billingAddressPostcodeZip,
if (widget.billingAddressStateProvince != null)
"billingAddressStateProvince": widget.billingAddressStateProvince,
if (widget.shippingContactFirstName != null)
"shippingContactFirstName": widget.shippingContactFirstName,
if (widget.shippingContactLastName != null)
"shippingContactLastName": widget.shippingContactLastName,
if (widget.shippingContactMobilePhone != null)
"shippingContactMobilePhone": widget.shippingContactMobilePhone,
if (widget.shippingContactPhone != null)
"shippingContactPhone": widget.shippingContactPhone,
if (widget.shippingContactEmail != null)
"shippingContactEmail": widget.shippingContactEmail,
if (widget.shippingCompanyName != null)
"shippingCompanyName": widget.shippingCompanyName,
if (widget.shippingAddressStreet != null)
"shippingAddressStreet": widget.shippingAddressStreet,
if (widget.shippingAddressStreet2 != null)
"shippingAddressStreet2": widget.shippingAddressStreet2,
if (widget.shippingAddressCity != null)
"shippingAddressCity": widget.shippingAddressCity,
if (widget.shippingAddressStateProvince != null)
"shippingAddressStateProvince": widget.shippingAddressStateProvince,
if (widget.shippingAddressCountry != null)
"shippingAddressCountry": widget.shippingAddressCountry,
if (widget.shippingAddressPostcodeZip != null)
"shippingAddressPostcodeZip": widget.shippingAddressPostcodeZip,
});
// Prepare request
String endpoint = getEndpoint(environment);
String json = jsonEncode(requestData);
log('Environment: $endpoint');
log("Sending request: $json");
// Make request
final response = await http.post(
Uri.parse(endpoint),
headers: <String, String>{
'Content-Type': 'application/json',
},
body: json,
);
// Get response
if (response.statusCode == 200) {
log("Request success: ${response.body}");
final paymentData = PaymentData.fromJson(response.body);
if (widget.onPaymentStarted != null) {
widget.onPaymentStarted!(paymentData);
}
setState(() {
_responseUrl = paymentData.paymentPage;
controller
..setNavigationDelegate(NavigationDelegate(
onNavigationRequest: (NavigationRequest request) {
if (request.url.contains(widget.ipgClient.returnUrl)) {
if (widget.onPaymentCompleted != null) {
ReturnData data = getReturnData(request.url);
widget.onPaymentCompleted!(data);
return NavigationDecision.prevent;
}
}
return NavigationDecision.navigate;
}))
..loadRequest(Uri.parse(_responseUrl!));
});
} else {
log("Error response: ${response.body}");
if (widget.onPaymentError != null) {
widget.onPaymentError!(getErrorData(response.body));
}
setState(() {
_errorOccurred = true;
});
}
}