id = 'alipay';
$this->icon = apply_filters('woocommerce_alipay_icon', plugins_url('images/alipay.gif', __FILE__));
$this->has_fields = false;
// Load the form fields.
$this->init_form_fields();
// Load the settings.
$this->init_settings();
// Define user set variables
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->alipay_account = $this->get_option( 'alipay_account' );
$this->partnerID = $this->get_option( 'partnerID' );
$this->secure_key = $this->get_option( 'secure_key' );
$this->payment_method = $this->get_option( 'payment_method' );
$this->debug = $this->get_option( 'debug' );
$this->form_submission_method = $this->get_option( 'form_submission_method' ) == 'yes' ? true : false;
$this->availability = $this->get_option( 'availability' );
$this->countries = $this->get_option( 'countries' );
$this->secure_key = $this->get_option( 'secure_key' );
$this->notify_url = str_replace( 'https:', 'http:', add_query_arg( 'wc-api', 'WC_Alipay', home_url( '/' ) ) );//trailingslashit(home_url());
//Log
if ($this->debug=='yes') $this->log = $woocommerce->logger();
// Actions
add_action( 'woocommerce_api_wc_alipay', array(&$this, 'check_alipay_response') );
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_action('woocommerce_thankyou_alipay', array(&$this, 'thankyou_page'));
add_action('woocommerce_receipt_alipay', array(&$this, 'receipt_page'));
//Availability check
if( $this->availability == 'specific' ) {
add_filter('woocommerce_available_payment_gateways', array(&$this,'remove_alipay'));
}
}
/**
* Initialise Gateway Settings Form Fields
*
* @access public
* @return void
*/
function init_form_fields() {
global $woocommerce;
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'alipay' ),
'type' => 'checkbox',
'label' => __( 'Enable Alipay Payment', 'alipay' ),
'default' => 'no'
),
'title' => array(
'title' => __( 'Title', 'alipay' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'alipay' ),
'default' => __( 'Alipay', 'alipay' )
),
'description' => array(
'title' => __( 'Description', 'alipay' ),
'type' => 'textarea',
'default' => __('Pay via Alipay, if you don\'t have an Alipay account, you can also pay with your debit card or credit card', 'alipay')
),
'payment_method' => array(
'title' => __( 'Alipay Payment Gateway Type', 'alipay' ),
'type' => 'select',
'description' => __( 'Choose a payment method', 'alipay' ),
'options' => array(
'direct' => __('Direct Payment', 'alipay'),
'escrow' => __('Escrow Payment', 'alipay'),
'dualfun' => __('Dual(Direct Payment + Escrow payment)', 'alipay')
)
),
'partnerID' => array(
'title' => __('Partner ID', 'alipay'),
'type' => 'text',
'description' => __('Please enter the partner ID
If you don\'t have one, click here to get.', 'alipay'),
'css' => 'width:400px'
),
'secure_key' => array(
'title' => __('Security Key', 'alipay'),
'type' => 'text',
'description' =>__('Please enter the security key
If you don\'t have one, click here to get.', 'alipay'),
'css' => 'width:400px'
),
'alipay_account' => array(
'title' => __('Alipay Account', 'alipay'),
'type' => 'text',
'description' => __('Please enter your Alipay Email; this is needed in order to take payment.', 'alipay'),
'css' => 'width:200px'
),
'form_submission_method' => array(
'title' => __( 'Submission method', 'alipay' ),
'type' => 'checkbox',
'label' => __( 'Use form submission method.', 'alipay' ),
'description' => __( 'Enable this to post order data to Alipay via a form instead of using a redirect/querystring.', 'alipay' ),
'default' => 'no'
),
'availability' => array(
'title' => __( 'Method availability', 'alipay' ),
'type' => 'select',
'default' => 'all',
'class' => 'availability',
'options' => array(
'all' => __( 'All allowed countries', 'alipay' ),
'specific' => __( 'Specific Countries', 'alipay' ),
)
),
'countries' => array(
'title' => __( 'Specific Countries', 'alipay' ),
'type' => 'multiselect',
'class' => 'chosen_select',
'css' => 'width: 450px;',
'default' => '',
'options' => $woocommerce->countries->countries,
),
'debug' => array(
'title' => __( 'Debug Log', 'alipay' ),
'type' => 'checkbox',
'label' => __( 'Enable logging', 'alipay' ),
'default' => 'no',
'description' => __( 'Log Alipay events, such as trade status, inside woocommerce/logs/alipay.txt', 'alipay' ),
)
);
}
/**
* Admin Panel Options
* - Options for bits like 'title' and account etc.
*
* @since 1.0
*/
public function admin_options() {
?>
'.__('Thank you for your order, please click the button below to pay with Alipay.', 'alipay').'
'; echo $this->generate_alipay_form( $order ); } /** * Check for Alipay IPN Response * * @access public * @return void */ function check_alipay_response() { global $woocommerce; @ob_clean(); if ( isset($_POST['seller_id']) && $_POST['seller_id'] == $this->partnerID ){ if ($this->debug=='yes') $this->log->add( 'alipay','Received notification from Alipay, the order number is: '.$_POST['out_trade_no']); //Get order id $out_trade_no = $_POST['out_trade_no']; $array = explode('|', $out_trade_no); $order_id = $array[1]; if( !$order_id || !is_numeric($order_id ) ) wp_die("Invalid Order ID"); //Get alipay config $order = new WC_Order( $order_id ); $alipay_config = $this->get_alipay_config( $order ); //Verify alipay's notification require_once("lib/alipay_notify.class.php"); $alipayNotify = new AlipayNotify($alipay_config); //Log verification if ($this->debug=='yes') $log = true; $verify_result = $alipayNotify->verifyNotify( $log ); if ($this->debug=='yes'){ $verify_result = $verify_result? 'Valid' : 'Invalid'; if ($this->debug=='yes') $this->log->add( 'alipay','Verification result: '. $verify_result); } if($verify_result) { if($this->payment_method == 'direct') { if($_POST['trade_status'] == 'TRADE_FINISHED' || $_POST['trade_status'] == 'TRADE_SUCCESS') { $order->add_order_note( __('The order is completed', 'alipay') ); $order->payment_complete(); $this->successful_request( $_POST); } } else { if($_POST['trade_status'] == 'WAIT_BUYER_PAY') { $order->add_order_note( __('Order received, awaiting payment', 'alipay') ); $this->successful_request( $_POST); } else if($_POST['trade_status'] == 'WAIT_SELLER_SEND_GOODS') { $order->update_status('processing', __('Payment received, awaiting fulfilment', 'alipay')); $woocommerce->cart->empty_cart(); $this->successful_request( $_POST); } else if($_POST['trade_status'] == 'WAIT_BUYER_CONFIRM_GOODS') { $order->add_order_note( __('Your order has been shipped, awaiting buyer\'s confirmation', 'alipay') ); $this->successful_request( $_POST); } else if($_POST['trade_status'] == 'TRADE_FINISHED') { $order->payment_complete(); $order->add_order_note( __('The order is completed', 'alipay') ); $this->successful_request( $_POST); } else { header('HTTP/1.1 200 OK'); echo "success"; exit; } } } else { wp_die("fail"); } } else { wp_die( "Alipay Notification Request Failure" ); } } /** * Successful Payment! * * @access public * @param array $posted * @return void */ function successful_request( $posted ){ if ($this->debug=='yes') $this->log->add( 'alipay','Trade Status Received: ['. $posted['trade_status'] . '] For Order: ['. $_POST['out_trade_no'].']'); header('HTTP/1.1 200 OK'); echo "success"; exit; } } /** * Add the gateway to WooCommerce * * @access public * @param array $methods * @package WooCommerce/Classes/Payment * @return array */ function add_alipay_gateway( $methods ) { $methods[] = 'WC_Alipay'; return $methods; } add_filter('woocommerce_payment_gateways', 'add_alipay_gateway' ); } ?>