Need help to make Forminator integrate with Paystack API
-
Hello,
I am trying to integrate Forminator to work with Paystack payment API. I want users to fill a form which has a calculate field called “Total”. Once they submit the form, they are immediately redirected to the payment page on Paystack where the total field value is displayed as the amount to be paid. Below is the code, However, the form submits successfully but it is not redirecting to the paystack payment page. I can’t figure out why it is not redirecting.
Please kindly help review the code to see if any mistake was made.
Thank
// Create a payment gateway for Forminator using Paystack API $form_id = 9087; function powi_create_paystack_gateway($submission_id, $form_id) { // Get the total amount from the Forminator form submission $payment_amount = $_POST['forminator_field_total']; // Use Forminator API's method to get all the fields in the form $form_fields = Forminator_API::get_form_fields($form_id, $submission_id); // Create an array of required fields to send to Paystack API $fields = array( "amount" => $payment_amount * 100, // Amount should be in kobo, so multiply by 100 "email" => $form_fields['email']['value'], // Email field from form "metadata" => array( "custom_fields" => array( "field_1" => $form_fields['name']['value'], // Custom fields from form "field_2" => $form_fields['phone']['value'] ) ), "reference" => "forminator_payment_" . $form_id . "_" . time() // Custom reference number ); // Use Paystack API to initialize payment $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://api.paystack.co/transaction/initialize", CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => json_encode($fields), CURLOPT_HTTPHEADER => array( "authorization: Bearer sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "content-type: application/json", "cache-control: no-cache" ), )); // Fetch the response from Paystack API $response = curl_exec($curl); curl_close($curl); // Decode the JSON response $response_array = json_decode($response, true); // Check if the Paystack API request was successful if ($response_array['status'] === true) { // Redirect the user to the Paystack payment page to complete the transaction wp_redirect($response_array['data']['authorization_url']); exit; } else { // Handle the error case here echo 'Paystack API request failed'; // You can display an error message or perform any other necessary actions } } // Use the Forminator API hook to trigger the payment gateway when the form is submitted add_action('forminator_custom_form_submit_response', 'powi_create_paystack_gateway', 10, 2);
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Need help to make Forminator integrate with Paystack API’ is closed to new replies.