title: Woocommerce custom payment gateway redirect back to woocommerce
body: <p>I have a custom gateway, I am new to the custom plugins for woocommerce, now my problem I have is that I am able to redirect to my payment gateway but in my payment gateway (HTML) how do I redirect back after payment was made? I don't understand the whole process as yet, I know once payment is selected then I can redirect to my custom payment gateway but the confusing part is returning from payment gateway to woocommerce to say it was a success or not</p>
  <pre><code>    &lt;?php
  
  /** 
  * Plugin Name: Woocommerce CloudCart Payment Gateway
  * Plugin URI: http://cloudcart.online
  * Description: Payment Gateway to use CloudCart points.
  * Author: Cornelis Kuijpers
  * Author URI: http://kuijpersconsulting.co.za
  * Version: 1.0.1
  *
  */
  
  add_action('plugins_loaded', 'init_Cloudcart', 0);
  
  function init_Cloudcart() {
  
      if ( ! class_exists( 'WC_Payment_Gateway' ) ) return;
  
      class woocommerce_Cloudcart extends WC_Payment_Gateway {
  
          public function __construct() { 
              global $woocommerce;
  
              $this-&gt;id           = 'Cloudcart';
              $this-&gt;method_title = __('Cloudcart', 'Cloudcart-chearaan-woo');
              $this-&gt;icon         = plugins_url( 'Cloudcart.png', __FILE__ );
              $this-&gt;has_fields   = false;
              $this-&gt;notify_url   = str_replace( 'https:', 'http:', add_query_arg( 'wc-api', 'woocommerce_Cloudcart', home_url( '/' ) ) );
  
              // Load the form fields.
              $this-&gt;init_form_fields();
  
              // Load the settings.
              $this-&gt;init_settings();
  
              // Define user set variables
              $this-&gt;Cloudcarturl              = $this-&gt;settings['Cloudcarturl'];
              $this-&gt;title                = $this-&gt;settings['title'];
              $this-&gt;description          = $this-&gt;settings['description'];
              $this-&gt;merchantid           = $this-&gt;settings['merchantid'];
              $this-&gt;hashKey              = $this-&gt;settings['hashKey'];
              $this-&gt;transactionDate      = date('Y-m-d H:i:s O');
              $this-&gt;woo_version          = $this-&gt;get_woo_version();
  
              // Actions
              add_action('init', array(&amp;$this, 'successful_request'));
              add_action('woocommerce_api_woocommerce_Cloudcart', array( &amp;$this, 'successful_request' ));
              add_action('woocommerce_receipt_Cloudcart', array(&amp;$this, 'receipt_page'));
              if ( version_compare( WOOCOMMERCE_VERSION, '2.0.0', '&gt;=' ) ) {
                  add_action('woocommerce_update_options_payment_gateways_' . $this-&gt;id, array( &amp;$this, 'process_admin_options' ));
              } else {
                  add_action('woocommerce_update_options_payment_gateways', array( &amp;$this, 'process_admin_options' ));
              }
          } 
  
          /**
           * Initialise Gateway Settings Form Fields
           */
          function init_form_fields() {
  
              $this-&gt;form_fields = array(
                  'enabled' =&gt; array(
                                  'title' =&gt; __( 'Enable/Disable:', 'Cloudcart-chearaan-woo' ), 
                                  'type' =&gt; 'checkbox', 
                                  'label' =&gt; __( 'Enable Cloudcart', 'Cloudcart-chearaan-woo' ), 
                                  'default' =&gt; 'yes'
                              ), 
                  'Cloudcarturl' =&gt; array(
                                  'title' =&gt; __( 'Test/Production:', 'Cloudcart-chearaan-woo' ), 
                                  'type' =&gt; 'checkbox', 
                                  'label' =&gt; __( 'Test', 'Cloudcart-chearaan-woo' ), 
                                  'default' =&gt; 'yes'
                              ), 
                  'title' =&gt; array(
                                  'title' =&gt; __( 'Title:', 'Cloudcart-chearaan-woo' ), 
                                  'type' =&gt; 'text', 
                                  'description' =&gt; __( 'The title which the user sees during checkout.', 'Cloudcart-chearaan-woo' ), 
                                  'default' =&gt; __( 'Cloudcart Online Payment Gateway', 'Cloudcart-chearaan-woo' )
                              ),
                  'description' =&gt; array(
                                  'title' =&gt; __( 'Description:', 'Cloudcart-chearaan-woo' ), 
                                  'type' =&gt; 'textarea', 
                                  'description' =&gt; __( 'Description which the user sees during checkout.', 'Cloudcart-chearaan-woo' ), 
                                  'default' =&gt; __('Pay securely through Cloudcart\'s Secure Servers.', 'Cloudcart-chearaan-woo')
                              ),
                  'merchantid' =&gt; array(
                                  'title' =&gt; __( 'Merchant ID:', 'Cloudcart-chearaan-woo' ), 
                                  'type' =&gt; 'text', 
                                  'description' =&gt; __( 'Please enter your Merchant ID as provided by Cloudcart.', 'Cloudcart-chearaan-woo' ), 
                                  'default' =&gt; ''
                              ),
                  'hashKey' =&gt; array(
                                  'title' =&gt; __( 'Merchant hashKey:', 'Cloudcart-chearaan-woo' ), 
                                  'type' =&gt; 'text', 
                                  'description' =&gt; __( 'Please enter your Merchant hashKey as provided by Cloudcart.', 'Cloudcart-chearaan-woo' ), 
                                  'default' =&gt; ''
                              )
                  );
  
          }
  
  
          public function admin_options() {
              ?&gt;
              &lt;h3&gt;Cloudcart&lt;/h3&gt;
              &lt;p&gt;&lt;?php _e('Cloudcart works by sending the user to Cloudcart to enter their payment information.', 'Cloudcart-chearaan-woo'); ?&gt;&lt;/p&gt;
  
              &lt;table class=&quot;form-table&quot;&gt;
              &lt;?php
                  // Generate the HTML For the settings form.
                  $this-&gt;generate_settings_html();
              ?&gt;
              &lt;/table&gt;&lt;!--/.form-table--&gt;
              &lt;?php
          } // End admin_options()
  
          /**
           * There are no payment fields, but we want to show the description if set.
           **/
          function payment_fields() {
              if ($this-&gt;description) echo wpautop(wptexturize($this-&gt;description));
          }
  
          /**
           * Generate the button link
           **/
          public function generate_Cloudcart_form( $order_id ) {
              global $woocommerce;
  
  
              $order = new WC_Order( $order_id );
  
              if ($this-&gt;Cloudcarturl == &quot;yes&quot;){
                  $Cloudcart_adr = &quot;https://ckconsulting.ddns.net/API/CloudCart/&quot;;
              }else{
                  $Cloudcart_adr = &quot;https://ckconsulting.ddns.net/API/CloudCart/&quot;;
              }
  
  
              $sHash = strtoupper(hash('sha256', $this-&gt;hashKey.&quot;Continue&quot;.str_pad($this-&gt;merchantid, 10, '0', STR_PAD_LEFT).str_pad($order-&gt;id, 20, '0', STR_PAD_LEFT).str_pad(($order-&gt;order_total*100), 12, '0', STR_PAD_LEFT)));
              $Cloudcart_args = array(
                  'secureHash'        =&gt; $sHash,
                  'mid'               =&gt; str_pad($this-&gt;merchantid, 10, '0', STR_PAD_LEFT),
                  'invno'             =&gt; str_pad($order-&gt;id, 20, '0', STR_PAD_LEFT),
                  'amt'               =&gt; str_pad(($order-&gt;order_total*100), 12, '0', STR_PAD_LEFT),
                  'desc'              =&gt; str_pad(&quot;Order No &quot;.$order_id, 255, ' ', STR_PAD_RIGHT),
                  'postURL'           =&gt; $this-&gt;notify_url,
                  'phone'             =&gt; $order-&gt;billing_phone,
                  'email'             =&gt; $order-&gt;billing_email,
                  'param'             =&gt; 'WC|V1'
              );
  
              $Cloudcart_args_array = array();
  
              foreach ($Cloudcart_args as $key =&gt; $value) {
                  $Cloudcart_args_array[] = '&lt;input type=&quot;hidden&quot; name=&quot;'.$key.'&quot; value=&quot;'. $value .'&quot; /&gt;&lt;br&gt;';
              }
  
              wc_enqueue_js('
                  jQuery(function(){
                              jQuery(&quot;body&quot;).block(
                                  { 
                                      message: &quot;&lt;img src=\&quot;'.$woocommerce-&gt;plugin_url().'/images/uploading.gif\&quot; alt=\&quot;Redirecting…\&quot; style=\&quot;float:left; margin-right: 10px;\&quot; /&gt;'.__('Thank you for your order. We are now redirecting you to Cloudcart to make payment.', 'Cloudcart-chearaan-woo').'&quot;, 
                                      overlayCSS: 
                                      { 
                                          background: &quot;#fff&quot;, 
                                          opacity: 0.5 
                                      },
                                      css: { 
                                          padding:        18, 
                                          textAlign:      &quot;center&quot;, 
                                          color:          &quot;#555&quot;, 
                                          border:         &quot;2px solid #aaa&quot;, 
                                          backgroundColor:&quot;#fff&quot;, 
                                          cursor:         &quot;wait&quot;,
                                          lineHeight:     &quot;30px&quot;
                                      } 
                                  });
                              jQuery(&quot;#submit_Cloudcart_payment_form&quot;).click();
                          });
              ');
  
          return '&lt;form action=&quot;'.$Cloudcart_adr.'&quot; method=&quot;post&quot;&gt;
                      ' . implode('', $Cloudcart_args_array) . '
                      &lt;input type=&quot;submit&quot; class=&quot;button-alt&quot; id=&quot;submit_Cloudcart_payment_form&quot; value=&quot;'.__('Pay via Cloudcart', 'Cloudcart-chearaan-woo').'&quot; /&gt; &lt;a class=&quot;button cancel&quot; href=&quot;'.$order-&gt;get_cancel_order_url().'&quot;&gt;'.__('Cancel order &amp;amp; restore cart', 'Cloudcart-chearaan-woo').'&lt;/a&gt;
                  &lt;/form&gt;';           
  
          }
  
          /**
           * Process the payment and return the result
           **/
          function process_payment( $order_id ) {
  
              $order = new WC_Order( $order_id );
  
              echo get_permalink(get_option('woocommerce_pay_page_id'));
  
              if($this-&gt;woo_version &gt;= 2.1){
                  $redirect = $order-&gt;get_checkout_payment_url( true );           
              }else if( $this-&gt;woo_version &lt; 2.1 ){
                  $redirect = add_query_arg('order', $order-&gt;id, add_query_arg('key', $order-&gt;order_key, get_permalink(get_option('woocommerce_pay_page_id'))));
              }else{
                  $redirect = add_query_arg('order', $order-&gt;id, add_query_arg('key', $order-&gt;order_key, get_permalink(get_option('woocommerce_pay_page_id'))));
              }
  
              return array(
                  'result'    =&gt; 'success',
                  'redirect'  =&gt; $redirect
              );
  
          }
  
          /**
           * receipt_page
           **/
          function receipt_page( $order ) {
  
              echo '&lt;p&gt;'.__('Please click the button below to pay with Cloudcart.', 'Cloudcart-chearaan-woo').'&lt;/p&gt;';
  
              echo $this-&gt;generate_Cloudcart_form( $order );
  
          }
  
          /**
           * Server callback was valid, process callback (update order as passed/failed etc).
           **/
          function successful_request($Cloudcart_response) {
              global $woocommerce;
  
              echo 'Hello from the other side';
  
              if (isset($_GET['wc-api']) &amp;&amp; $_GET['wc-api'] == 'woocommerce_Cloudcart') {
                  /** need to trim from result **/
                  $Url_result = $_GET['result'];
                  $order = new WC_Order( (int) substr($Url_result,7,20) );
                  $tranID = (int)substr($Url_result,1,6);
  
                  if (substr($Url_result,0,1) == '0'){
                      $r_status = 0;
                  }else{
                  $r_status = 33;
                  }
  
                  /*
                  $order = new WC_Order( (int) $_POST['invno'] );
                  $r_status = (int) $_POST['result'];
  
                  */
  
                  if ($r_status == '0' ){
  
                      $order-&gt;payment_complete();
  
                      $order-&gt;add_order_note('Cloudcart Payment was SUCCESSFUL '.'&lt;br&gt;AuthCode is '  . $tranID);
  
                      wp_redirect( $this-&gt;get_return_url($order) ); exit;
                      //wp_redirect( $this-&gt;order-&gt;get_checkout_order_received_url() ); exit;
  
                  }else{
  
                      $order-&gt;update_status('failed', sprintf(__('Cloudcart Payment Failed. Error Communicating with Bank.', 'Cloudcart-chearaan-woo') ) );
  
                      wp_redirect($order-&gt;get_cancel_order_url()); exit;
  
                  }
              }               
          }
  
          function get_woo_version() {
  
              // If get_plugins() isn't available, require it
              if ( ! function_exists( 'get_plugins' ) )
                  require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  
              // Create the plugins folder and file variables
              $plugin_folder = get_plugins( '/woocommerce' );
              $plugin_file = 'woocommerce.php';
  
              // If the plugin version number is set, return it 
              if ( isset( $plugin_folder[$plugin_file]['Version'] ) ) {
                  return $plugin_folder[$plugin_file]['Version'];
  
              } else {
                  // Otherwise return null
                  return NULL;
              }
          }
      }
  }
  
  /**
   * Add the gateway to WooCommerce
   **/
  function add_Cloudcart( $methods ) {
      $methods[] = 'woocommerce_Cloudcart'; return $methods;
  }
  
  add_filter('woocommerce_payment_gateways', 'add_Cloudcart' );
  </code></pre>