processPayment method
Process payment after card data has been collected via HPF
sessionToken - Session token obtained from getSessionToken()
cardholderName - Cardholder name (required)
email - Customer email (optional)
billingAddress - Billing address object (optional)
browserInfo - Browser information object (optional)
birthDay - Birth day in format YYYY-MM-DD (optional)
selectedLanguage - Selected language code (optional)
Returns PaymentProcessingResponse with result, redirect URL, etc.
Implementation
Future<PaymentProcessingResponse> processPayment({
required String sessionToken,
required String cardholderName,
String? email,
Map<String, dynamic>? billingAddress,
Map<String, dynamic>? browserInfo,
String? birthDay,
String? selectedLanguage,
}) async {
final uri = Uri.parse('$checkoutHost/api/v1/processing/purchase/card');
final requestBody = <String, dynamic>{
'name': cardholderName,
'with_hosted_fields': true,
};
if (email != null) requestBody['email'] = email;
if (billingAddress != null) requestBody['billing_address'] = billingAddress;
if (browserInfo != null) requestBody['browser_info'] = browserInfo;
if (birthDay != null) requestBody['birth_day'] = birthDay;
if (selectedLanguage != null) requestBody['selected_language'] = selectedLanguage;
final headers = <String, String>{
'Content-Type': 'application/json',
'Token': sessionToken,
};
try {
final response = await http.post(
uri,
headers: headers,
body: jsonEncode(requestBody),
);
if (response.statusCode == 200) {
final jsonResponse = jsonDecode(response.body) as Map<String, dynamic>;
final paymentResponse = PaymentProcessingResponse.fromJson(jsonResponse);
return paymentResponse;
} else {
final errorBody = jsonDecode(response.body) as Map<String, dynamic>?;
final errorMessage = errorBody?['error_message'] as String? ??
'Payment processing failed';
throw Exception(errorMessage);
}
} catch (e) {
// Re-throw with clean error message
if (e is Exception) {
rethrow;
}
throw Exception('Error processing payment: $e');
}
}