1
votes

thanks in advance. I am building a custom payment gateway for woocommerce. The thing i am struggling with is that the server of my payment gateway only accepts request when i submit a form. So i do form submit with redirect to payment gateway url. The problem is that woocommerce is not executing my form.submit in process_payment method.

So i tried using wp_remote_post, using curl but none of these work for me because i need to redirect to my payment gateway with data, as if in form.submit.

public function process_payment( $order_id ) {
        global $woocommerce;

        // Get this Order's information so that we know
        // who to charge and how much
        $customer_order = new WC_Order( $order_id );
  //Here i take some data and put it inside $a



    echo '<form name="customForm" action="https://gateway.com/web" method="post" id="customForm">
        <input type="hidden" name="token" id="token" value="<?php echo  $token;?>">
        <input type="hidden" name="key" id="key" value="<?php echo  $a->key;?>">
        <input type="hidden" name="callbackUrl" id="callbackUrl" value="<?php echo $a->callbackUrl;?>">
        <!-- callback url where alif sends information about status of transactions -->
        <input type="hidden" name="returnUrl" id="returnUrl" value="<?php echo $a->returnUrl;?>">
        <input type="hidden" name="amount" id="amount" value="<?php echo $a->amount;?>" required>
        <input type="hidden" name="orderId" id="orderId" value="<?php echo $a->orderid;?>">

        <input type="hidden" name="info" id="info" value="<?php echo $a->info;?>">

        <input type="hidden" name="email" id="email" value="<?php echo $a->email;?>">
        <input type="hidden" name="phone" id="phone" value="<?php echo $a->phone;?>">
    </form>';

    ?><script type="text/javascript">
        document.getElementById('customForm').submit();
    </script><?php
   }

I expected to be redirected to payment gateway url, but i dont get redirected and get invalid form message in woocommerce.

2
You probably need to build that form using JS. When the user click the "pay" button, you send data to the server using AJAX, then send back all those form values. Then you build the form using JS/jQuery, append it to <body> and submit it. - Chris G
So i created some fields and appended them using JS, and submited the form theAlifForm.appendChild(newInput1); theAlifForm.appendChild(newInput2); theAlifForm.appendChild(newInput3); theAlifForm.appendChild(newInput4); theAlifForm.appendChild(newInput5); theAlifForm.appendChild(newInput6); theAlifForm.appendChild(newInput7); console.log(theAlifForm); document.body.appendChild(theAlifForm); theAlifForm.submit(); </script> but all i get is invalid form and form just being shown in console and this in the end: {"result":"failure","messages":"","refresh":false,"reload":false} - Shohijahon Hakimjonov
The data inside the form i create myself inside php, so the only thing that i can send to gateway is the form with data and redirect. - Shohijahon Hakimjonov
Submitting the form should redirect to the payment website; does that happen or not? - Chris G
Could You present code by CURL? - Dell

2 Answers

1
votes

I think you need first create hook filter like this:

add_filter('woocommerce_receipt_' . $this->id, array(&$this, 'receipt_page'));

And then:

public function receipt_page($order_id) 
{
    // Get this Order's information so that we know
            // who to charge and how much
            $customer_order = new WC_Order($order_id);
            //Here i take some data and put it inside $a


            echo '<form name="customForm" action="https://gateway.com/web" method="post" id="customForm">
            <input type="hidden" name="token" id="token" value="<?php echo  $token;?>">
            <input type="hidden" name="key" id="key" value="<?php echo  $a->key;?>">
            <input type="hidden" name="callbackUrl" id="callbackUrl" value="<?php echo $a->callbackUrl;?>">
            <!-- callback url where alif sends information about status of transactions -->
            <input type="hidden" name="returnUrl" id="returnUrl" value="<?php echo $a->returnUrl;?>">
            <input type="hidden" name="amount" id="amount" value="<?php echo $a->amount;?>" required>
            <input type="hidden" name="orderId" id="orderId" value="<?php echo $a->orderid;?>">

            <input type="hidden" name="info" id="info" value="<?php echo $a->info;?>">

            <input type="hidden" name="email" id="email" value="<?php echo $a->email;?>">
            <input type="hidden" name="phone" id="phone" value="<?php echo $a->phone;?>">
            </form>';
}
0
votes

I think the issue with wrong syntax. Try using this syntax:

echo <<<HTML
<form name="customForm" action="https://gateway.com/web" method="post" id="customForm">
  <input type="hidden" name="token" id="token" value="{$token}">
  <input type="hidden" name="key" id="key" value="{$a->key}">
  <input type="hidden" name="callbackUrl" id="callbackUrl" value="{$a->callbackUrl}">
  <!-- callback url where alif sends information about status of transactions -->
  <input type="hidden" name="returnUrl" id="returnUrl" value="{$a->returnUrl}">
  <input type="hidden" name="amount" id="amount" value="{$a->amount}" required>
  <input type="hidden" name="orderId" id="orderId" value="{$a->orderid}">
  <input type="hidden" name="info" id="info" value="{$a->info}">
  <input type="hidden" name="email" id="email" value="{$a->email}">
  <input type="hidden" name="phone" id="phone" value="{$a->phone}">
</form>
<script type="text/javascript">
    document.getElementById('customForm').submit();
</script>
HTML;