• Resolved dhoyin

    (@dhoyin)


    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);
    
    • This topic was modified 1 year, 9 months ago by dhoyin.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @dhoyin,

    Could you please check this custom snippet and see whether it helps with redirection?
    https://gist.github.com/wpmudev-sls/5af95872fe195ce45e50387ebfe03e91

    The above script adds multiple redirects based on submitted data, so you could remove the redirect for $main_redirect and only use the reference for $secondary_redirect.

    For example, remove this variable:

    private $main_redirect

    And also update the return of the calc_url function as:

    return $this->secondary_redirect;

    Please do note that it’ll still require custom coding and you’ll have to update the rest of your code inside the calc_url function based on the class.

    Could you please check and see whether the helps in moving forward?

    Looking forward to your response.

    Kind Regards,

    Nithin

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @dhoyin ,

    We haven’t heard from you for over a week now, so it looks like you don’t have more questions for us.

    Feel free to re-open this ticket if needed.

    Kind regards
    Kasia

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.