Larry
Larry

Reputation: 461

Custom WooCommerce Gateway

I`m trying to develop a custom payment gateway for WooCommerce plugin, but I have a problem with the checkout page. What I want is to insert a form on the checkout final step page that is submitted automatically after 5 seconds.

My code is:

        ...
    add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page'));
    add_action('woocommerce_api_wc_' . $this->id, array($this, 'handle_callback'));
}

function handle_callback() {
    wp_die('handle_callback');
}

function receipt_page( $order ) 
{   
    echo "receipt page";
    $this->generate_submit_form_elements( $order );
}

The problem is that "receipt_page" action is not triggered. Thanks!

Upvotes: 1

Views: 978

Answers (1)

hamish
hamish

Reputation: 1182

oh, its because you forgot to set the has_fields flag to true.

//after setting the id and method_title, set the has_fields to true
          $this -> id = 'kiwipay';
          $this -> method_title = 'KiwiPay';

          $this->has_fields = true; // if you want credit card payment fields to show on the users checkout page

then in the process_payment function put this:

// Payload would look something like this.
$payload = array(
"amount" => $order.get_total(), 
"reference" => $order->get_order_number(),
"orderid" => $order->id,
"return_url" => $this->get_return_url($order)  //return to thank you page.
);

response = wp_remote_post( $environment_url, array(
                'method'    => 'POST',
                'body'      => http_build_query( $payload ),
                'timeout'   => 90,
                'sslverify' => false,
            ) );

// Retrieve the body's response if no errors found
$response_body = wp_remote_retrieve_body( $response );
$response_headers = wp_remote_retrieve_headers( $response );

//use this if you need to redirect the user to the payment page of the bank.
$querystring = http_build_query( $payload );

return array(
            'result'   => 'success',
            'redirect' => $environment_url . '?' . $querystring,
        );

Upvotes: 1

Related Questions