• hi all,
    I am trying to make a Custom Gateway Plugin for my Merchant Account but I got stuck. This is my first time to create woocommerce payment plugin.
    I don’t know what my code is wrong. I can go third party payment website and received order in backend but status is ‘On-Hold’.
    But I can’t capturing the data which is the notification API send to me after the payment is done.

    Our data need to sent by api id, pw, item, notify_service, notify_url and return_url using json crul.
    notify_service for something to capture, what wing send to me
    notify_url for successful page
    return_url is the url for get back to your site when the user click the back button

    Here my code

    <?php
    /*
    Plugin Name: WooCommerce Wing Payment Gateway
    Plugin URI:
    Description: Wing Payment gateway for woocommerce
    Version: 1.0
    Author:
    Author URI:
    */
    
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    /**
     * Check if WooCommerce is active
     **/
    if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
    
        if (!class_exists('WC_PaymentWing')) {
            // Load the main class
            add_action('plugins_loaded', 'wc_paymentwing_init');
            // Add SimplePay as a WooCommerce gateway
            add_filter('woocommerce_payment_gateways', 'wc_paymentwing_add_gateway');
        }
    }
    function wc_paymentwing_init()
    {
        /**
         * Main class
         */
        class WC_PaymentWing extends WC_Payment_Gateway
        {
    
    		/**
             * Constructor for the gateway.
             */
            public function __construct() {
    			$this->id                 	= 'wc_paymentwing';
                $this->method_title       	= __('Wing Payment', 'wc_paymentwing');
                $this->method_description 	= __('Accept payments using Wing.', 'wc_paymentwing');
    			$this -> icon         		=  plugins_url( 'images/wing-logo.png' , __FILE__ );
                $this->has_fields         	= false;		
    
                // Logs
    			if ($this->debug=='yes') $this->log = $woocommerce->logger();
                // Load the settings
                $this->init_form_fields();
                // Initialise gateway settings
                $this->init_settings();
    
                // Settings
                $this->title               	= $this->get_option('title');
                $this->description         	= $this->get_option('description');
                $this->merchant        		= $this->get_option('merchant');
                $this->loginId     			= $this->get_option('loginId');
    			$this->password     		= $this->get_option('password');
    
    			$this->debug 				= $this->get_option( 'debug' );
    
    			// Logs
    			if ( 'yes' == $this->debug ) {
    				$this->log = new WC_Logger();
    			}
    
    			// Save the options to the database
    			if ( version_compare( WOOCOMMERCE_VERSION, '2.0.0', '>=' ) ) {
                    add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( &$this, 'process_admin_options' ) );
                } else {
                    add_action( 'woocommerce_update_options_payment_gateways', array( &$this, 'process_admin_options' ) );
                }
    
                add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page'));
    			//add_action('woocommerce_thankyou_',array(&$this, 'thankyou_page'));
    		}
    
    		/**
    		 * Output for the order received page.
    		 */
    		function receipt_page( $order ) {
    		echo '<p>' . __( 'Thank you for your order!', 'woocommerce' ) . '</p>';
    
    		}
    
    		/**
    		 * for capturing the data which is the notification my API send to you after the payment is done
    		 * such as transaction status, message, transaction ID, account & amount
    		 * it will be sent as json
    		 * something to capture what we send to you
    		 */
    		function after_processing() {
    
    			global $woocommerce;
    			@ob_clean();
    			$order = new WC_Order($order_id);
                //$transaction_key = $order->order_key;
    
    			$params = file_get_contents('php://input');
    			$json=json_decode($params,true);
    
    			if($json['tstatus'] == '200'){
    
    				$tid = $json['tid'];
    				$account = $json['account'];
    				$amount = $json['amount'];
    				$token = $json['token'];
    
    					// Payment Complete
    					$order->payment_complete();
    					// Empty cart
    					$woocommerce->cart->empty_cart();
    
    					$woocommerce->add_message(__('Wing - Transaction completed. Thank you for shopping with us.', 'wc_paymentwing'));
    
    			//echo "success";
    			} else echo "fail";
    		}
    
    		/**
             * Process the payment and return the result
             *
             * @access public
             * @param int $order_id
             * @return array
             */
            public function process_payment($order_id) {
    
                global $woocommerce;
    
                $order = new WC_Order($order_id);
    			$order_number = $order->get_order_number();
    			$item = "Payment for Order ID: $order_number on ". get_bloginfo('name');
    
    			$return_url =  esc_url( $order->get_cancel_order_url() )  ;				
    
    			$param = array(
    				'loginId'        =>	$this->loginId,
    				'password'       =>	$this->password,
    				'item'           =>	$item,
    				'amount'         => number_format($order->order_total, 2, '.', ''),
    				'merchant_name'  =>	$this->merchant,
    				'notify_service' => $this->after_processing(), // something to capture, what wing send to me
    				'notify_url'     =>	$order->get_checkout_order_received_url(), //notifiy_rul is successful page
    				'return_url'     =>	$return_url //return_url is the url for get back to your site when the user click the back button
    			);            
    
                if (is_null($param)) {
                    $woocommerce->add_error(__('Could not connect to payment gateway.', 'wc_paymentwing'));
                    return;
                }
    
    			// Save the payment token to the session for faster loading of the payment page
                $woocommerce->session->paymentToken = $param;
    
    			$url = "https://DOMAIN.COM/onlinepay/wing/authenticate";
    
    			$content = json_encode($param);
    			$curl = curl_init($url);
    			curl_setopt($curl, CURLOPT_HEADER, false);
    			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    			curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
    			curl_setopt($curl, CURLOPT_POST, true);
    			curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
    			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    			$response = curl_exec($curl);
    
    			if ($response === FALSE)
    				echo "cURL Error: " . curl_error($curl);
    			else
    			//header("Location: https://110.74.218.219/onlinepay/payment?token=$response");
    
    			$tokenURL = "https://DOMAIN.COM/onlinepay/payment?token=";
    
                // Redirect to the pay page
                return array(
                    'result'   => 'success',
    				'redirect'	=> $tokenURL . $response
                );		
    
            }
    
    		/**
             * Initialise Gateway Settings Form Fields
             */
            public function init_form_fields()
            {
    
                $this->form_fields = array(
                    'enabled' => array(
                        'title'   => __('Enable/Disable', 'wc_paymentwing'),
                        'type'    => 'checkbox',
                        'label'   => __('Enable Wing Payment', 'wc_paymentwing'),
                        'default' => '0'
                    ),
                    'title' => array(
                        'title'       => __('Title', 'wc_paymentwing'),
                        'type'        => 'text',
                        'description' => __('This controls the title which the user sees during checkout.', 'wc_paymentwing'),
                        'default'     => __('Wing Payment', 'wc_paymentwing'),
                        'desc_tip'    => true,
                    ),
                    'description' => array(
                        'title'       => __('Description', 'wc_paymentwing'),
                        'type'        => 'textarea',
                        'description' => __('This controls the description which the user sees during checkout.', 'wc_paymentwing'),
                        'default'     => __('Pay securely with Wing card.', 'wc_paymentwing')
                    ),
                    'merchant' => array(
                        'title'       => __('Merchant Name', 'wc_paymentwing'),
                        'type'        => 'text',
                        'description' => '',
    					'default'     => __('Company Name', 'wc_paymentwing')
                    ),
                    'loginId' => array(
                        'title'       => __('Login ID', 'wc_paymentwing'),
                        'type'        => 'text',
                        'description' => __('Support by Wing.', 'wc_paymentwing'),
                        'desc_tip'    => true,
                    ),
                    'password' => array(
                        'title'       => __('Password', 'wc_paymentwing'),
                        'type'        => 'text',
                        'description' => __('Support by Wing.', 'wc_paymentwing'),
                        'desc_tip'    => true,
                    ),
    				'debug' => array(
    				'title'       => __( 'Debug Log', 'wc_paymentwing' ),
    				'type'        => 'checkbox',
    				'label'       => __( 'Enable logging', 'wc_paymentwing' ),
    				'default'     => 'no',
    				'description' => sprintf( __( 'Log , inside <code>woocommerce/logs/wing-%s.txt</code>', 'wc_paymentwing' ), sanitize_file_name( wp_hash( 'wing' ) ) ),
    			)
                );
    		}
    
    	  /**
           * Admin Panel Options
          **/
          public function admin_options()
          {
             echo '<h3>'.__('Wing Payment Gateway', 'wc_paymentwing').'</h3>';
             echo '<p>'.__('Wing is most popular Cambodia payment gateway for online payment processing').'</p>';
             echo '<table class="form-table">';
             $this->generate_settings_html();
             echo '</table>';
    
          } 	
    
    	}
    }
    
    /**
     * Add Wing as a gateway to WooCommerce
     */
    function wc_paymentwing_add_gateway($methods) {
        $methods[] = 'WC_PaymentWing';
        return $methods;
    }
    ?>

    Hopefully, all can help me! Thanks.

    https://wordpress.org/plugins/woocommerce/

The topic ‘Redirect Third Party payment Gateway Error’ is closed to new replies.