首页 > 解决方案 > Woocommerce 自定义支付网关重定向回 woocommerce

问题描述

我有一个自定义网关,我是 woocommerce 自定义插件的新手,现在我遇到的问题是我可以重定向到我的支付网关,但是在我的支付网关 (HTML) 中,我如何在付款后重定向回来?我还不了解整个过程,我知道一旦选择了付款,我就可以重定向到我的自定义支付网关,但令人困惑的部分是从支付网关返回到 woocommerce 说它是否成功

    <?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->id           = 'Cloudcart';
            $this->method_title = __('Cloudcart', 'Cloudcart-chearaan-woo');
            $this->icon         = plugins_url( 'Cloudcart.png', __FILE__ );
            $this->has_fields   = false;
            $this->notify_url   = str_replace( 'https:', 'http:', add_query_arg( 'wc-api', 'woocommerce_Cloudcart', home_url( '/' ) ) );

            // Load the form fields.
            $this->init_form_fields();

            // Load the settings.
            $this->init_settings();

            // Define user set variables
            $this->Cloudcarturl              = $this->settings['Cloudcarturl'];
            $this->title                = $this->settings['title'];
            $this->description          = $this->settings['description'];
            $this->merchantid           = $this->settings['merchantid'];
            $this->hashKey              = $this->settings['hashKey'];
            $this->transactionDate      = date('Y-m-d H:i:s O');
            $this->woo_version          = $this->get_woo_version();

            // Actions
            add_action('init', array(&$this, 'successful_request'));
            add_action('woocommerce_api_woocommerce_Cloudcart', array( &$this, 'successful_request' ));
            add_action('woocommerce_receipt_Cloudcart', array(&$this, 'receipt_page'));
            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' ));
            }
        } 

        /**
         * Initialise Gateway Settings Form Fields
         */
        function init_form_fields() {

            $this->form_fields = array(
                'enabled' => array(
                                'title' => __( 'Enable/Disable:', 'Cloudcart-chearaan-woo' ), 
                                'type' => 'checkbox', 
                                'label' => __( 'Enable Cloudcart', 'Cloudcart-chearaan-woo' ), 
                                'default' => 'yes'
                            ), 
                'Cloudcarturl' => array(
                                'title' => __( 'Test/Production:', 'Cloudcart-chearaan-woo' ), 
                                'type' => 'checkbox', 
                                'label' => __( 'Test', 'Cloudcart-chearaan-woo' ), 
                                'default' => 'yes'
                            ), 
                'title' => array(
                                'title' => __( 'Title:', 'Cloudcart-chearaan-woo' ), 
                                'type' => 'text', 
                                'description' => __( 'The title which the user sees during checkout.', 'Cloudcart-chearaan-woo' ), 
                                'default' => __( 'Cloudcart Online Payment Gateway', 'Cloudcart-chearaan-woo' )
                            ),
                'description' => array(
                                'title' => __( 'Description:', 'Cloudcart-chearaan-woo' ), 
                                'type' => 'textarea', 
                                'description' => __( 'Description which the user sees during checkout.', 'Cloudcart-chearaan-woo' ), 
                                'default' => __('Pay securely through Cloudcart\'s Secure Servers.', 'Cloudcart-chearaan-woo')
                            ),
                'merchantid' => array(
                                'title' => __( 'Merchant ID:', 'Cloudcart-chearaan-woo' ), 
                                'type' => 'text', 
                                'description' => __( 'Please enter your Merchant ID as provided by Cloudcart.', 'Cloudcart-chearaan-woo' ), 
                                'default' => ''
                            ),
                'hashKey' => array(
                                'title' => __( 'Merchant hashKey:', 'Cloudcart-chearaan-woo' ), 
                                'type' => 'text', 
                                'description' => __( 'Please enter your Merchant hashKey as provided by Cloudcart.', 'Cloudcart-chearaan-woo' ), 
                                'default' => ''
                            )
                );

        }


        public function admin_options() {
            ?>
            <h3>Cloudcart</h3>
            <p><?php _e('Cloudcart works by sending the user to Cloudcart to enter their payment information.', 'Cloudcart-chearaan-woo'); ?></p>

            <table class="form-table">
            <?php
                // Generate the HTML For the settings form.
                $this->generate_settings_html();
            ?>
            </table><!--/.form-table-->
            <?php
        } // End admin_options()

        /**
         * There are no payment fields, but we want to show the description if set.
         **/
        function payment_fields() {
            if ($this->description) echo wpautop(wptexturize($this->description));
        }

        /**
         * Generate the button link
         **/
        public function generate_Cloudcart_form( $order_id ) {
            global $woocommerce;


            $order = new WC_Order( $order_id );

            if ($this->Cloudcarturl == "yes"){
                $Cloudcart_adr = "https://ckconsulting.ddns.net/API/CloudCart/";
            }else{
                $Cloudcart_adr = "https://ckconsulting.ddns.net/API/CloudCart/";
            }


            $sHash = strtoupper(hash('sha256', $this->hashKey."Continue".str_pad($this->merchantid, 10, '0', STR_PAD_LEFT).str_pad($order->id, 20, '0', STR_PAD_LEFT).str_pad(($order->order_total*100), 12, '0', STR_PAD_LEFT)));
            $Cloudcart_args = array(
                'secureHash'        => $sHash,
                'mid'               => str_pad($this->merchantid, 10, '0', STR_PAD_LEFT),
                'invno'             => str_pad($order->id, 20, '0', STR_PAD_LEFT),
                'amt'               => str_pad(($order->order_total*100), 12, '0', STR_PAD_LEFT),
                'desc'              => str_pad("Order No ".$order_id, 255, ' ', STR_PAD_RIGHT),
                'postURL'           => $this->notify_url,
                'phone'             => $order->billing_phone,
                'email'             => $order->billing_email,
                'param'             => 'WC|V1'
            );

            $Cloudcart_args_array = array();

            foreach ($Cloudcart_args as $key => $value) {
                $Cloudcart_args_array[] = '<input type="hidden" name="'.$key.'" value="'. $value .'" /><br>';
            }

            wc_enqueue_js('
                jQuery(function(){
                            jQuery("body").block(
                                { 
                                    message: "<img src=\"'.$woocommerce->plugin_url().'/images/uploading.gif\" alt=\"Redirecting…\" style=\"float:left; margin-right: 10px;\" />'.__('Thank you for your order. We are now redirecting you to Cloudcart to make payment.', 'Cloudcart-chearaan-woo').'", 
                                    overlayCSS: 
                                    { 
                                        background: "#fff", 
                                        opacity: 0.5 
                                    },
                                    css: { 
                                        padding:        18, 
                                        textAlign:      "center", 
                                        color:          "#555", 
                                        border:         "2px solid #aaa", 
                                        backgroundColor:"#fff", 
                                        cursor:         "wait",
                                        lineHeight:     "30px"
                                    } 
                                });
                            jQuery("#submit_Cloudcart_payment_form").click();
                        });
            ');

        return '<form action="'.$Cloudcart_adr.'" method="post">
                    ' . implode('', $Cloudcart_args_array) . '
                    <input type="submit" class="button-alt" id="submit_Cloudcart_payment_form" value="'.__('Pay via Cloudcart', 'Cloudcart-chearaan-woo').'" /> <a class="button cancel" href="'.$order->get_cancel_order_url().'">'.__('Cancel order &amp; restore cart', 'Cloudcart-chearaan-woo').'</a>
                </form>';           

        }

        /**
         * 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->woo_version >= 2.1){
                $redirect = $order->get_checkout_payment_url( true );           
            }else if( $this->woo_version < 2.1 ){
                $redirect = add_query_arg('order', $order->id, add_query_arg('key', $order->order_key, get_permalink(get_option('woocommerce_pay_page_id'))));
            }else{
                $redirect = add_query_arg('order', $order->id, add_query_arg('key', $order->order_key, get_permalink(get_option('woocommerce_pay_page_id'))));
            }

            return array(
                'result'    => 'success',
                'redirect'  => $redirect
            );

        }

        /**
         * receipt_page
         **/
        function receipt_page( $order ) {

            echo '<p>'.__('Please click the button below to pay with Cloudcart.', 'Cloudcart-chearaan-woo').'</p>';

            echo $this->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']) && $_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->payment_complete();

                    $order->add_order_note('Cloudcart Payment was SUCCESSFUL '.'<br>AuthCode is '  . $tranID);

                    wp_redirect( $this->get_return_url($order) ); exit;
                    //wp_redirect( $this->order->get_checkout_order_received_url() ); exit;

                }else{

                    $order->update_status('failed', sprintf(__('Cloudcart Payment Failed. Error Communicating with Bank.', 'Cloudcart-chearaan-woo') ) );

                    wp_redirect($order->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' );

标签: phpwordpresswoocommercepaymentgateway

解决方案


推荐阅读