S
S
Satellence2015-12-03 12:01:51
PHP
Satellence, 2015-12-03 12:01:51

Run a request before submitting a form?

Hello.
The site has a form for placing an order, which sends the user along with the POST form data to the payment system website for further selection of the payment method.
I ran into a problem, how to first create an order in your database before submitting the form.
I tried to cancel the default form action, make an ajax request, and upon receipt of an affirmative result, submit the form with JS. But in this case, the browser blocks pop-up windows.
What implementation ideas can you advise on this. At the same time, I would like to avoid the need for the user to double-click on the button.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Kovalsky, 2015-12-03
@dmitryKovalskiy

Step 1 - Create an order, display full information on it and offer to pay online.
Step 2 - send to the payment gateway to pay.
In 9 out of 10 payment gateways, the API requires an order number, which is part of the message for generating a signature and one of the request parameters. When a person clicks on the "Pay" button and you make the transition to the payment gateway - this data should ALREADY exist. Options? When you click on "Pay" to create an order, go to an empty page that contains only a form for accessing the payment gateway API, and to this script page, which immediately submits the form when the page is ready.

D
Dmitry Belyaev, 2015-12-03
@bingo347

Hidden iframe, the form target points to it, the form sends data to your server, your server in response sends a completed form for the payment system, in which target = "_top" on the DOMready event in the iframe, we submit the form It is
possible without an iframe, just a hidden form , send data via ajax to our server, fill in a hidden form on response and submit

E
Eugene, 2015-12-03
@n1ger

When a person selects an online form of payment, he presses a button that POST sends data to the handler, this handler saves the order to the database and we take the order number from the database.

$_sql = "INSERT INTO $table_orders SET
      user_id           = '$s_user_id',
      order_items       = '".addslashes(serialize($s_cart))."',
      sessionid         = '".session_id()."',
      order_sum         = '".$s_cart['sum']."',
      order_status      = 'new',
      user_name         = '$user_name',
      comment  = '$comment',
      payment_type      = '$payway',
      d_add             = NOW(),
      user_phone        = '$user_phone', $_sql_common";
        $order_id = exec_sql($_sql);

Connect the payment gateway, if not connected, generate the form according to the documentation of the payment gateway, where 'order_id' => $order_id,
and send the payment form to the gateway
$(document).ready(function () {
                    $('#form_payway').submit();
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question