Shohijahon Hakimjonov
Shohijahon Hakimjonov

Reputation: 58

Woocommerce custom payment gateway is not executing form.submit

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.

Upvotes: 1

Views: 1200

Answers (2)

Юрий Столов
Юрий Столов

Reputation: 17

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;

Upvotes: 0

Dmitry Leiko
Dmitry Leiko

Reputation: 4412

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>';
}

Upvotes: 1

Related Questions