0

嗨,我打算为 OKpay 的付款方式开发一个 wordpress 插件,但是当我使用它时,我收到以下错误:- 致命错误:调用 /home/sixtydev/public_html/ 中未定义的方法 woocommerce_okpay::get_return_url() woo/wp-content/plugins/woocommerce-okpay/gateway-okpay.php 在线 193

这是我的代码

<?php
/**
 * OKPAY Payment Gateway
 * 
 * Provides a OKPAY Standard Payment Gateway.
 *
 * @class       woocommerce_okpay
 * @package     WooCommerce
 * @category    Payment Gateways
 * @author      WooThemes
 */
class woocommerce_okpay extends woocommerce_payment_gateway {

    public function __construct() { 
        global $woocommerce;

        $this->id           = 'okpay';
        $this->icon         = apply_filters('woocommerce_okpay_icon', $this->plugin_url() . '/okpay.png');
        $this->has_fields   = false;
        $this->liveurl      = 'https://www.okpay.com/process.html';
        $this->testurl      = 'https://www.okpay.com/process.html';

        // 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->email        = $this->settings['email'];
        $this->testmode     = 'no';//$this->settings['testmode'];
        $this->debug        = $this->settings['debug']; 
        $this->payfee   = ($this->settings['payfee'] == 'yes') ? 0 : 1;

        // Logs
        if ($this->debug=='yes') $this->log = $woocommerce->logger();

        // Actions
        add_action( 'init', array(&$this, 'check_ipn_response') );
        add_action('valid-okpay-standard-ipn-request', array(&$this, 'successful_request') );
        add_action('woocommerce_receipt_okpay', array(&$this, 'receipt_page'));
        add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));

        if ( !$this->is_valid_for_use() ) $this->enabled = false;
    } 


     /**
     * return plugin url
     * @return string
     */
    function plugin_url() {

            if (is_ssl()) :

                return str_replace('http://', 'https://', WP_PLUGIN_URL) . "/" . plugin_basename( dirname(dirname(__FILE__)));

            else :

                return WP_PLUGIN_URL . "/" . plugin_basename(dirname(__FILE__));

            endif;

    }

     /**
     * Check if this gateway is enabled and available in the user's country
     */
    function is_valid_for_use() {
        if (!in_array(get_option('woocommerce_currency'), array('AUD', 'BRL', 'CAD', 'MXN', 'NZD', 'HKD', 'SGD', 'USD', 'EUR', 'JPY', 'TRY', 'NOK', 'CZK', 'DKK', 'HUF', 'ILS', 'MYR', 'PHP', 'PLN', 'SEK', 'CHF', 'TWD', 'THB', 'GBP'))) return false;

        return true;
    }

    /**
     * Admin Panel Options 
     * - Options for bits like 'title' and availability on a country-by-country basis
     *
     * @since 1.0.0
     */
    public function admin_options() {

        ?>
        <h3><?php _e('OKPAY', 'woothemes'); ?></h3>
        <p><?php _e('OKPAY works by sending the user to OKPAY to enter their payment information.', 'woothemes'); ?></p>
        <table class="form-table">
        <?php
            if ( $this->is_valid_for_use() ) :

                // Generate the HTML For the settings form.
                $this->generate_settings_html();

            else :

                ?>
                    <div class="inline error"><p><strong><?php _e( 'Gateway Disabled', 'woothemes' ); ?></strong>: <?php _e( 'OKPAY does not support your store currency.', 'woothemes' ); ?></p></div>
                <?php

            endif;
        ?>
        </table><!--/.form-table-->
        <?php
    } // End admin_options()

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

        $this->form_fields = array(
            'enabled' => array(
                            'title' => __( 'Enable/Disable', 'woothemes' ), 
                            'type' => 'checkbox', 
                            'label' => __( 'Enable OKPAY', 'woothemes' ), 
                            'default' => 'yes'
                        ), 
            'title' => array(
                            'title' => __( 'Title', 'woothemes' ), 
                            'type' => 'text', 
                            'description' => __( 'This controls the title which the user sees during checkout.', 'woothemes' ), 
                            'default' => __( 'OKPAY', 'woothemes' )
                        ),
            'description' => array(
                            'title' => __( 'Description', 'woothemes' ), 
                            'type' => 'textarea', 
                            'description' => __( 'This controls the description which the user sees during checkout.', 'woothemes' ), 
                            'default' => __("Pay via OKPAY; you can pay with your credit card if you don't have a okpay account", 'woothemes')
                        ),
            'email' => array(
                            'title' => __( 'OKPAY Email', 'woothemes' ), 
                            'type' => 'text', 
                            'description' => __( 'Please enter your OKPAY email address; this is needed in order to take payment.', 'woothemes' ), 
                            'default' => ''
                        ),
            'payfee' => array(
                            'title' => __( 'OKPAY fee', 'woothemes' ),
                            'type' => 'checkbox',
                            'description' => __( 'Check if you pay the fee for orders or uncheck if client pay it.', 'woothemes' ),
                            'default' => 'yes'
                        ),
             /*
            'testmode' => array(
                            'title' => __( 'OKPAY sandbox', 'woothemes' ),
                            'type' => 'checkbox',
                            'label' => __( 'Enable OKPAY sandbox', 'woothemes' ),
                            'default' => 'yes'),  */
            'debug' => array(
                            'title' => __( 'Debug', 'woothemes' ), 
                            'type' => 'checkbox', 
                            'label' => __( 'Enable logging (<code>woocommerce/logs/okpay.txt</code>)', 'woothemes' ), 
                            'default' => 'no'
                        )
            );

    } // End init_form_fields()

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

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

        $order = &new woocommerce_order( $order_id );

        if ( $this->testmode == 'yes' ):
            $okpay_adr = $this->testurl . '?';
        else :
            $okpay_adr = $this->liveurl . '?';      
        endif;

        if ($this->debug=='yes') $this->log->add( 'okpay', 'Generating payment form for order #' . $order_id ." | ".$order->order_key. '. Notify URL: ' . $this->get_return_url( $order ));


        $okpay_args = 
            array(
                'ok_fees'               => $this->payfee,
                'ok_receiver'           => $this->email,

                'ok_currency'           => get_option('woocommerce_currency'),
                /*'ap_itemname'             => sprintf(__('Order #%s' , 'woothemes'), $order->id),
                'ap_quantity'           => 1,
                'ap_amount'             => number_format($order->order_total, 2, '.', ''),
                'ap_description'        => "Order from curiousuniversity",  */
                'ok_return_success'             => $this->get_return_url( $order ),
                'ok_return_fail'            => $order->get_cancel_order_url(),


                // IPN
                'ok_ipn'            => trailingslashit(home_url()).'?okpayListener=okpay_standard_IPN',

                // Address info
                'ok_payer_first_name'               => $order->billing_first_name,
                'ok_payer_last_name'                => $order->billing_last_name,
                'ok_payer_business_name'                => $order->billing_company,
                'ok_payer_street'       => $order->billing_address_1,
                //'ap_addressline2'     => $order->billing_address_2,
                'ok_payer_city'             => $order->billing_city,
                'ok_payer_state'        => $order->billing_state,
                'ok_payer_zip'      => $order->billing_postcode,
                'ok_payer_country'          => $order->billing_country,
                'ok_payer_email'        => $order->billing_email,
                'ok_payer_phone'        => $order->billing_phone,

                // Payment Info
                'ok_invoice'            => $order->order_key."|".$order_id,
                //'apc_2'               => $order_id,
                //'ap_discountamount'   => $order->get_order_discount()
        );


        if ($order->get_order_discount()>0) :

            // Don't pass items - paypal borks tax due to prices including tax. PayPal has no option for tax inclusive pricing sadly. Pass 1 item for the order items overall
            $okpay_args['ok_item_1_name']   = sprintf(__('Order #%s' , 'woothemes'), $order->id);
            $okpay_args['ok_item_1_quantity']       = 1;
            $okpay_args['ok_item_1_price']      = number_format($order->order_total - $order->order_shipping - $order->get_order_discount(), 2, '.', '');

            // Shipping Cost
            if ($order->order_shipping>0) :
                $okpay_args['ok_item_2_name'] = __('Shipping cost', 'woothemes');
                $okpay_args['ok_item_2_quantity']   = 1;
                $okpay_args['ok_item_2_price']  = number_format($order->order_shipping, 2);
            endif;

        else :

            // Tax
            //$okpay_args['ap_taxamount'] = $order->get_total_tax();

            // Cart Contents
            $item_loop = 0;
            if (sizeof($order->items)>0) : foreach ($order->items as $item) :
                if ($item['qty']) :

                    $item_loop++;

                    $item_name = $item['name'];

                    $item_meta = &new order_item_meta( $item['item_meta'] );
                    if ($meta = $item_meta->display( true, true )) :
                        $item_name .= ' ('.$meta.')';
                    endif;

                    $okpay_args['ok_item_'.$item_loop.'_name'] = $item_name;
                    $okpay_args['ok_item_'.$item_loop.'_quantity'] = $item['qty'];
                    $okpay_args['ok_item_'.$item_loop.'_price'] = number_format($item['cost'], 2, '.', '');

                endif;
            endforeach; endif;

            // Shipping Cost
            if ($order->order_shipping>0) :
                $item_loop++;
                $okpay_args['ok_item_'.$item_loop.'_name'] = __('Shipping cost', 'woothemes');
                $okpay_args['ok_item_'.$item_loop.'_quantity'] = '1';
                $okpay_args['ok_item_'.$item_loop.'_price']  = number_format($order->order_shipping, 2);
            endif;

        endif;

        $okpay_args_array = array();

        foreach ($okpay_args as $key => $value) {
            $okpay_args_array[] = '<input type="hidden" name="'.esc_attr( $key ).'" value="'.esc_attr( $value ).'" />';
        }

        $woocommerce->add_inline_js('
            jQuery("body").block({ 
                    message: "<img src=\"'.esc_url( $woocommerce->plugin_url() ).'/assets/images/ajax-loader.gif\" alt=\"Redirecting...\" style=\"float:left; margin-right: 10px;\" />'.__('Thank you for your order. We are now redirecting you to OKPAY to make payment.', 'woothemes').'",
                    overlayCSS: 
                    { 
                        background: "#fff", 
                        opacity: 0.6 
                    },
                    css: { 
                        padding:        20, 
                        textAlign:      "center", 
                        color:          "#555", 
                        border:         "3px solid #aaa", 
                        backgroundColor:"#fff", 
                        cursor:         "wait",
                        lineHeight:     "32px"
                    } 
                });
            jQuery("#submit_okpay_payment_form").click();
        ');

        return '<form action="'.esc_url( $okpay_adr ).'" method="post" id="okpay_payment_form">
                ' . implode('', $okpay_args_array) . '
                <input type="submit" class="button-alt" id="submit_okpay_payment_form" value="'.__('Pay via okpay', 'woothemes').'" /> <a class="button cancel" href="'.esc_url( $order->get_cancel_order_url() ).'">'.__('Cancel order &amp; restore cart', 'woothemes').'</a>
            </form>';

    }

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

        $order = &new woocommerce_order( $order_id );

        return array(
            'result'    => 'success',
            'redirect'  => add_query_arg('order', $order->id, add_query_arg('key', $order->order_key, get_permalink(get_option('woocommerce_pay_page_id'))))
        );

    }

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

        echo '<p>'.__('Thank you for your order, please click the button below to pay with okpay.', 'woothemes').'</p>';

        echo $this->generate_okpay_form( $order );

    }

    /**
     * Check okpay IPN validity
     **/
    function check_ipn_request_is_valid() {
        global $woocommerce;

        if ($this->debug=='yes') $this->log->add( 'okpay', 'Checking IPN response is valid...' );
        return true;
    }

    /**
     * Check for okpay IPN Response
     **/
    function check_ipn_response() {

        if (isset($_GET['okpayListener']) && $_GET['okpayListener'] == 'okpay_standard_IPN'):

            $_POST = stripslashes_deep($_POST);

            if ($this->check_ipn_request_is_valid()) :

                do_action("valid-okpay-standard-ipn-request", $_POST);

            endif;

        endif;

    }

    /**
     * Successful Payment!
     **/
    function successful_request( $posted ) {
        if ($this->debug=='yes') $this->log->add( 'okpay', print_r($posted, true));

        $order_info = explode("|", $posted['ok_invoice']);
        $invoice_id = $order_info['0'];
        $order_id = $order_info['1'];


        if ( !empty($invoice_id) && !empty($order_id) ) {

            $order = new woocommerce_order( (int) $order_id );
            if ($this->debug=='yes') $this->log->add( 'okpay', "Check order key " . $order->order_key ." = ".$invoice_id);
            if ($order->order_key!=$invoice_id) exit;

            // Sandbox fix
            //if ($posted['ap_test']==1 && $posted['ok_txn_status']=='Pending') $posted['ok_txn_status'] = 'completed';

            // We are here so lets check status and do actions
            if ($this->debug=='yes') $this->log->add( 'okpay', "Check status");
            switch (strtolower($posted['ok_txn_status'])) :
                case 'completed' :
                    if ($this->debug=='yes') $this->log->add( 'okpay', "Success");
                    // Check order not already completed
                    if ($order->status == 'completed') exit;

                    // Payment completed
                    $order->add_order_note( __('IPN payment completed', 'woothemes') );
                    $order->payment_complete();

                    // Store PP Details
                    update_post_meta( (int) $order_id, 'Payer okpay address', $posted['ok_payer_email']);
                    update_post_meta( (int) $order_id, 'Transaction ID', $posted['ok_txn_id']);
                    update_post_meta( (int) $order_id, 'Payer first name', $posted['ok_payer_first_name']);
                    update_post_meta( (int) $order_id, 'Payer last name', $posted['ok_payer_last_name']);
                    update_post_meta( (int) $order_id, 'Payment type', $posted['ok_txn_payment_type']);

                break;
                case 'pending' :

                    // Mark order as refunded
                    $order->update_status('pending', sprintf(__('Payment %s via IPN.', 'woothemes'), strtolower($posted['ok_txn_status']) ) );

                    $message = woocommerce_mail_template(
                        __('Order refunded/reversed', 'woothemes'),
                        sprintf(__('Order #%s has been marked as pending - OKPAY reason code: %s', 'woothemes'), $order->id, $posted['ok_txn_pending_reason'] )
                    );

                    // Send the mail
                    woocommerce_mail( get_option('woocommerce_new_order_email_recipient'), sprintf(__('Payment for order #%s pending', 'woothemes'), $order->id), $message );

                break;
                case 'reversed' :

                    // Mark order as refunded
                    $order->update_status('refunded', sprintf(__('Payment %s via IPN.', 'woothemes'), strtolower($posted['ok_txn_status']) ) );

                    $message = woocommerce_mail_template(
                        __('Order refunded/reversed', 'woothemes'),
                        sprintf(__('Order #%s has been marked as refunded - OKPAY reason code: %s', 'woothemes'), $order->id, $posted['ok_txn_reversal_reason'] )
                    );

                    // Send the mail
                    woocommerce_mail( get_option('woocommerce_new_order_email_recipient'), sprintf(__('Payment for order #%s refunded/reversed', 'woothemes'), $order->id), $message );

                break;
                default:
                    // Order failed
                    $order->update_status('failed', sprintf(__('Payment %s via IPN.', 'woothemes'), strtolower($posted['ok_txn_status']) ) );
                break;
            endswitch;

            exit;

        }

    }

}

/**
 * Add the gateway to WooCommerce
 **/
function add_okpay_gateway( $methods ) {
    $methods[] = 'woocommerce_okpay'; return $methods;
}

add_filter('woocommerce_payment_gateways', 'add_okpay_gateway' );
4

1 回答 1

1

错误是因为该函数不存在。

我假设您从 PayPal 复制了此代码,对吗?

编辑:

你应该为它构建一个插件。

http://wcdocs.woothemes.com/codex/extending/payment-gateway-api/

于 2012-08-02T19:31:28.300 回答