Woocommerce支付网关插件集成

时间:2013-07-20 02:15:17

标签: integration payment-gateway checkout woocommerce

我一直在使用Woocommerce 1.6.6,并且有一个自定义插件可以在结账处接受电子支票信息,买家在结账时输入他们的银行名称,账号,路由号码。从那以后,我买了一个2.0的新主题,插件不适用于新主题,该网站空白。我需要知道如何更新插件以便它可以与Woocommerce 2.0一起使用。我联系了开发人员,他们说这次更新插件不是优先考虑的事情,并希望收取500美元以将插件更新为2.0。

    <?php
/*
Plugin Name: WooCommerce - e-Cheque Payments
Plugin URI: http://www.visser.com.au/woocommerce/plugins/echeque-payments/
Description: Allows WooCommerce store owners to accept electronic cheques for manual deposit into the store owner's bank account.
Version: 1.0.2
Author: Visser Labs
Author URI: http://www.visser.com.au/about/
License: GPL2
*/

load_plugin_textdomain( 'woo_cp', null, dirname( plugin_basename( __FILE__ ) ) . '/languages' );

include_once( 'includes/common.php' );

function woo_cp_init() {

    global $woo_cp;

    $woo_cp = array(
        'filename' => basename( __FILE__ ),
        'dirname' => basename( dirname( __FILE__ ) ),
        'abspath' => dirname( __FILE__ ),
        'relpath' => basename( dirname( __FILE__ ) ) . '/' . basename( __FILE__ )
    );

    $woo_cp['prefix'] = 'woo_cp';
    $woo_cp['name'] = __( 'e-Cheque Payments for WooCommerce', 'woo_cp' );
    $woo_cp['menu'] = __( 'e-Cheque Payments', 'woo_cp' );

}
add_action( 'init', 'woo_cp_init' );

function woo_cp_admin_init() {

    wp_enqueue_style( 'woo_cp_styles', plugins_url( '/templates/admin/woocommerce-cp_admin_sales.css', __FILE__ ) );

}
add_action( 'admin_init', 'woo_cp_admin_init' );

if( is_admin() ) {

    /* Start of: WordPress Administration */

    include_once( 'includes/install.php' );
    register_activation_hook( __FILE__, 'woo_cp_install' );

    function woo_cp_add_settings_link( $links, $file ) {

        static $this_plugin;
        if( !$this_plugin ) $this_plugin = plugin_basename( __FILE__ );
        if( $file == $this_plugin ) {
            $settings_link = '<a href="admin.php?page=woocommerce">' . __( 'Settings', 'woo_cp' ) . '</a>';
            array_unshift( $links, $settings_link );
        }
        return $links;

    }
    add_filter( 'plugin_action_links', 'woo_cp_add_settings_link', 10, 2 );

    function add_echeque_meta_box() {

        $post_type = 'shop_order';
        add_meta_box( 'woocommerce-product-echeque', __( 'e-Cheque Payment', 'woo_cp' ), 'woo_cp_meta_box', $post_type, 'normal', 'default' );

    }
    add_action( 'add_meta_boxes', 'add_echeque_meta_box' );

    function woo_cp_meta_box() {

        global $woo_cp, $post, $wpdb;

        wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' );
        $cheque_id = get_posts( array(
            'post_type' => 'cheque_payment',
            'post_parent' => $post->ID,
            'limit' => 1
        ) );
        if( $cheque_id ) {
            $cheque = new stdClass();
            $cheque->ID = $cheque_id[0]->ID;
            $cheque->account_holder = get_post_meta( $cheque->ID, '_account_holder', true );
            if( !$cheque->account_holder )
                $cheque->account_holder = '-';
            $cheque->bank_name = get_post_meta( $cheque->ID, '_bank_name', true );
            if( !$cheque->bank_name )
                $cheque->bank_name = '-';
            $cheque->routing_number = get_post_meta( $cheque->ID, '_routing_number', true );
            if( !$cheque->routing_number )
                $cheque->routing_number = '-';
            $cheque->account_number = get_post_meta( $cheque->ID, '_account_number', true );
            if( !$cheque->account_number )
                $cheque->account_number = '-';
            $cheque->check_number = get_post_meta( $cheque->ID, '_check_number', true );
            if( !$cheque->check_number )
                $cheque->check_number = '-';

        }

        require( $woo_cp['abspath'] . '/templates/admin/woocommerce-cp_admin_sales.php' );

    }

    /* End of: WordPress Administration */

}

function init_echeque_gateway() {

    if( class_exists( 'WC_Payment_Gateway' ) ) {

        class woocommerce_echeque extends WC_Payment_Gateway {

            function __construct() {

                $this->id = 'echeque';
                $this->method_title = __( 'Electronic Cheque', 'woo_cp' );
                $this->has_fields = true;

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

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

                // Define user set variables
                $this->title = $this->settings['title'];
                $this->description = $this->settings['description'];
                $this->instructions = $this->settings['instructions'];

                /* display configuration options for this plugin */
                add_action( 'woocommerce_update_options_payment_gateways', array( &$this, 'process_admin_options' ) );
                add_action( 'woocommerce_thankyou_cod', array( &$this, 'thankyou' ) );

            }

            function admin_options() {

?>
    <h3><?php _e( 'e-Cheque','woo_cp'); ?></h3>
    <p><?php _e( 'Allows WooCommerce store owners to accept e-Cheques for manual deposit into the store owner\'s bank account.', 'woo_cp' ); ?></p>
    <table class="form-table">
<?php
                $this->generate_settings_html();
?>
    </table>
<?php
            }

            function init_form_fields() {

                $this->form_fields = array(
                    'enabled' => array(
                        'title' => __( 'Enable e-Cheques', 'woo_cp' ),
                        'label' => __( 'Enable e-Cheques', 'woo_cp' ),
                        'type' => 'checkbox',
                        'description' => '',
                        'default' => 'no'
                    ),
                    'title' => array(
                        'title' => __( 'Title', 'woo_cp' ), 
                        'type' => 'text', 
                        'description' => __( 'Payment method title that the customer will see on your website.', 'woo_cp' ), 
                        'default' => __( 'Cheque', 'woo_cp' )
                    ), 
                    'description' => array(
                        'title' => __( 'Description', 'woo_cp' ), 
                        'type' => 'textarea', 
                        'description' => __( 'Let the customer know that their order won\'t be shipping until you receive payment.', 'woo_cp' ), 
                        'default' => __( 'To pay via electronic cheque please fill the required details below. Your order wont be shipped until the funds have cleared in our account.', 'woo_cp' )
                    ), 
                    'instructions' => array(
                        'title' => __( 'Instructions', 'woo_cp' ), 
                        'type' => 'textarea', 
                        'description' => __( 'Instructions that will be added to the thank you page.', 'woo_cp' ), 
                        'default' => __( '', 'woo_cp' )
                    )
                );

            }

            /**
            * Payment fields for Cheque Payment Deluxe
            **/
            public function payment_fields() {

                global $woo_cp;

                $payment_settings = maybe_unserialize( get_option( 'woocommerce_echeque_settings' ) );

                if( $this->description ) echo wpautop( wptexturize( $this->description ) );
                require( $woo_cp['abspath'] . '/templates/store/woocommerce-cp_store_checkout.php' );

            }

            /**
             * Process the payment and return the result
             **/
            public function process_payment( $order_id ) {

                global $woo_cp, $woocommerce;

                $order = new WC_Order( $order_id );

                /* Mark as on-hold (we're awaiting approval) */
                $order->update_status( 'on-hold', __( 'Waiting to be processed', 'woocommerce' ) );

                $account_holder = $this->get_post( $woo_cp['prefix'] . '_account_holder' );
                $bank_name = $this->get_post( $woo_cp['prefix'] . '_bank_name' );
                $routing_number = $this->get_post( $woo_cp['prefix'] . '_routing_number' );
                $account_number = $this->get_post( $woo_cp['prefix'] . '_account_number' );
                $check_number = $this->get_post( $woo_cp['prefix'] . '_check_number' );

                $post_type = 'cheque_payment';
                $post_data = array(
                    'post_status' => 'publish',
                    'post_type' => $post_type,
                    'post_parent' => $order_id,
                    'post_author' => 1,
                    'post_name' => '',
                    'post_title' => __( 'e-Cheque for Order #' ) . $order_id,
                    'post_content' => '',
                    'comment_status' => 'closed'
                );
                $post_ID = wp_insert_post( $post_data );
                if( $post_ID ) {
                    update_post_meta( $post_ID, '_account_holder', $account_holder );
                    update_post_meta( $post_ID, '_bank_name', $bank_name );
                    update_post_meta( $post_ID, '_routing_number', $routing_number );
                    update_post_meta( $post_ID, '_account_number', $account_number );
                    update_post_meta( $post_ID, '_check_number', $check_number );
                }
                if( WP_DEBUG ) {
                    $order->add_order_note( __( 'Account Holder: ', 'woo_cp' ) . $account_holder );
                    $order->add_order_note( __( 'Bank Name: ', 'woo_cp' ) . $bank_name );
                    $order->add_order_note( __( 'Routing Number: ', 'woo_cp' ) . $routing_number );
                    $order->add_order_note( __( 'Account Number: ', 'woo_cp' ) . $account_number );
                    $order->add_order_note( __( 'Check Number: ', 'woo_cp' ) . $check_number );
                }

                /* Empty awaiting payment session */
                unset( $_SESSION['order_awaiting_payment'] );

                /* Remove cart */
                $woocommerce->cart->empty_cart();

                /* Return thankyou redirect */
                $checkout_redirect = apply_filters( 'woocommerce_get_checkout_redirect_page_id', woocommerce_get_page_id( 'thanks' ) );
                return array(
                    'result' => 'success',
                    'redirect' => add_query_arg( 'key', $order->order_key, add_query_arg( 'order', $order_id, get_permalink( $checkout_redirect ) ) )
                );

            }

            /**
             * Validate Frontend Fields
            **/
            function validate_fields() {

                global $woo_cp, $woocommerce;

                $error = array();

                $account_holder = $this->get_post( $woo_cp['prefix'] . '_account_holder' );
                if( !$account_holder )
                    $error[] = '<strong>' . __( 'Account Holder', 'woo_cp' ) . '</strong> ' . __( 'is a required field.', 'woocommerce' );
                $bank_name = $this->get_post( $woo_cp['prefix'] . '_bank_name' );
                if( !$bank_name )
                    $error[] = '<strong>' . __( 'Bank Name', 'woo_cp' ) . '</strong>' . __( 'is a required field.', 'woocommerce' );
                $routing_number = $this->get_post( $woo_cp['prefix'] . '_routing_number' );
                if( !$routing_number )
                    $error[] = '<strong>' . __( 'Routing Number', 'woo_cp' ) . '</strong> ' . __( 'is a required field.', 'woocommerce' );
                $account_number = $this->get_post( $woo_cp['prefix'] . '_account_number' );
                if( !$account_number )
                    $error[] = '<strong>' . __( 'Account Number', 'woo_cp' ) . '</strong> ' . __( 'is a required field.', 'woocommerce' );
                $check_number = $this->get_post( $woo_cp['prefix'] . '_check_number' );
                if( !$check_number )
                    $error[] = '<strong>' . __( 'Check Number', 'woo_cp' ) . '</strong> ' . __( 'is a required field.', 'woocommerce' );

                if( $error ) {
                    $size = count( $error );
                    for( $i = 0; $i < $size; $i++ )
                        $woocommerce->add_error( $error[$i] );
                } else {
                    return true;
                }

            }

            function thankyou() {

                if( $this->instructions != '' )
                    echo wpautop( $this->instructions );

            }

            private function get_post( $name ) {

                if( isset( $_POST[$name] ) )
                    return $_POST[$name];
                else
                    return NULL;

            }

            private function get_option( $name ) {

                return get_option( $this->option_prefix . $name );

            }

        }

        /**
         * Add the gateway to WooCommerce
         **/
        function woocommerce_echeque_add_gateway( $methods ) {

            $methods[] = 'woocommerce_echeque';
            return $methods;

        }
        add_filter( 'woocommerce_payment_gateways', 'woocommerce_echeque_add_gateway', 1 );

    }

}
add_action( 'plugins_loaded', 'init_echeque_gateway', 0 );
?>

0 个答案:

没有答案
相关问题