id = 'innovext_allpay_aio';
$this->icon = apply_filters('inno_woocommerce_allpay_icon', plugins_url('icon/allpay-logo.png', __FILE__));
$this->has_fields = false;
$this->method_title = '歐付寶全方位金流';
$this->init_form_fields();
$this->init_settings();
// Define user set variables
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
$this->instruction_succ = $this->get_option('instruction_succ');
$this->instruction_on_hold = $this->get_option('instruction_on_hold');
$this->expire_date = $this->get_option('ExpireDate');
$this->exclude_payments = $this->get_option('exclude_payments');
$this->order_prefix = $this->get_option('order_prefix');
$this->min_amount = $this->get_option('min_amount');
$this->reduce_stock = $this->get_option('reduce_stock');
$this->english = $this->get_option('english');
$this->query_allpay_trade_info = $this->get_option('query_allpay_trade_info');
$this->payment_method_label = $this->get_option('payment_method_label');
$this->mer_id = $this->get_option('MerchantID');
$this->hash_key = $this->get_option('hash_key');
$this->hash_iv = $this->get_option('hash_iv');
$this->testmode = $this->get_option('testmode');
$this->admin_mode = $this->get_option('admin_mode');
$this->debug = $this->get_option('debug');
$this->payment_type = 'aio';
$this->notify_url = WC()->api_request_url( 'WC_innovext_allpay_aio' );
// Test Gateway
$this->live_gateway = 'https://payment.allpay.com.tw/Cashier/AioCheckOut';
$this->test_gateway = 'http://payment-stage.allpay.com.tw/Cashier/AioCheckOut';
// debug log
if ( 'yes' == $this->debug ) {
$this->log = new WC_Logger();
}
if ( 'yes' == $this->testmode ) {
$this->title .= '|測試';
}
if ( 'yes' == $this->admin_mode ) {
$this->title .= '|管理員';
}
// Actions
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
//add_action( 'woocommerce_thankyou_'. $this->id, array( $this, 'thankyou_page' ) );
add_action( 'woocommerce_receipt_'. $this->id, array( $this, 'receipt_page' ) );
add_action( 'woocommerce_api_wc_'. $this->id, array( $this, 'check_allpay_response' ) );
add_action( 'valid_allpay_ipn_request', array( $this, 'successful_request' ) );
add_action( 'woocommerce_admin_order_data_after_billing_address', array( $this, 'allpay_admin_order_data_after_billing_address' ) , 10, 1 );
add_filter( 'woocommerce_thankyou_order_received_text', array( $this, 'thankyou_order_received_text' ), 10, 2 );
// add query order metabox
add_action( 'woocommerce_admin_order_data_after_billing_address', array( $this, 'add_allpay_query_trade_info_meta_box' ) , 10, 1 );
// Customer Emails
add_action('woocommerce_email_before_order_table', array($this, 'email_instructions'), 10, 2 );
}
/**
* Initialise Gateway Settings Form Fields
*
* @access public
* @return void
*/
function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => '啟用/關閉',
'type' => 'checkbox',
'label' => '啟動 歐付寶全方位金流',
'default' => 'yes'
),
'title' => array(
'title' => '標題',
'type' => 'text',
'description' => '客戶在結帳時所看到的標題',
'default' => '歐付寶全方位金流',
'desc_tip' => true,
),
'MerchantID' => array(
'title' => '商店代號',
'type' => 'text',
'description' => '請填入您的歐付寶商店代號,測試模式請填入2000132',
'default' => '',
),
'hash_key' => array(
'title' => 'Hash Key',
'type' => 'text',
'description' => '請填入您歐付寶廠商後台系統的Hash Key,測試模式請填入5294y06JbISpM5x9',
'default' => '',
),
'hash_iv' => array(
'title' => 'Hash IV',
'type' => 'text',
'description' => '請填入您歐付寶廠商後台系統的Hash IV,測試模式請填入v77hoKGq4kWxNNIS',
'default' => '',
),
'description' => array(
'title' => '客戶訊息',
'type' => 'textarea',
'description' => '在這裡輸入下訂前,客戶會看到的訊息',
'default' => '歐付寶全方位金流 - 儲值支付帳戶、歐付寶餘額、信用卡、WebATM線上匯款、ATM櫃員機匯款、超商代碼、超商條碼、支付寶付款、財付通付款。',
'desc_tip' => true,
),
'instruction_succ' => array(
'title' => '成功付款訂單指示',
'type' => 'textarea',
'description' => '在這裡輸入下訂後成功付款,客戶會看到的訊息',
'default' => '謝謝您,已經成功收到您的付款,我們會儘快進行出貨的動作。',
'desc_tip' => true,
),
'instruction_on_hold' => array(
'title' => '待付款訂單指示',
'type' => 'textarea',
'description' => '在這裡輸入下訂後尚未付款,客戶會看到的訊息,使用在銀行虛擬帳號、超商代碼、超商條碼',
'default' => '請注意,我們尚未收到您的付款。使用歐付寶ATM繳款(虛擬帳號)、超商代碼繳款的顧客,已將繳款訊息寄至您的信箱,若您忘記或移失繳款資訊,請再重新下訂。請依歐付寶所提供的資訊進行付款,若您成功付款,系統會自動接收已付款訊息,我們會儘快進行出貨的動作。',
'desc_tip' => true,
),
'ExpireDate' => array(
'title' => 'ATM繳費期限(天)',
'type' => 'number',
'placeholder' => 3,
'description' => 'ATM繳款的允許繳費有效天數,最長60天,最短1天,不填則預設為3天',
'default' => '',
'desc_tip' => true,
'custom_attributes' => array(
'min' => 1,
'max' => 60,
'step' => 1
),
'css' => 'width:60px;',
),
'exclude_payments' => array(
'title' => '排除付款方式',
'type' => 'multiselect',
'class' => 'chosen_select',
'css' => 'width: 450px;',
'default' => '',
'description' => '使用者在歐付寶付款介面不會看見該付款方式,可留白',
'desc_tip' => true,
'options' => self::get_main_payment_type_args(),
'custom_attributes' => array(
'data-placeholder' => '選擇排除付款方式'
),
),
'payment_method_label' => array(
'title' => '付款標題顯示方式',
'type' => 'select',
'description' => '選擇在訂單列表或電子郵件時付款方式標題如何顯示,主要付款方式的標題會顯示成,例:網路ATM,細項付款方式標題會顯示成,例:台新WebATM',
'default' => 'default',
'desc_tip' => true,
'options' => array(
'default' => '預設標題',
'main_label' => '主要付款標題',
'detailed_label' => '細項付款標題'
)
),
'order_prefix' => array(
'title' => '訂單編號前綴',
'type' => 'text',
'description' => '訂單編號的前綴,建議只使用英文,不建議使用數字,不可包含特殊符號,可留白。如果有設前綴的話,那訂單編號會像是"WC123"',
'desc_tip' => true,
'default' => 'WC',
),
'min_amount' => array(
'title' => '最低訂單金額',
'type' => 'number',
'placeholder' => wc_format_localized_price( 0 ),
'description' => '顧客訂單金額必需大於此金額才可使用歐付寶結帳,0 為不限制',
'default' => '0',
'desc_tip' => true,
'custom_attributes' => array(
'min' => 0,
'step' => 1
),
'css' => 'width:60px;',
),
'query_allpay_trade_info' => array(
'title' => '顯示歐付寶訂單資訊',
'type' => 'checkbox',
'label' => '啟用查詢訂單',
'default' => 'no',
'description' => '有別於在WooCommerce帳單資訊欄位顯示訂單狀態,這項功能可以讓您了解歐付寶所儲存訂單的當前資訊',
'desc_tip' => true,
),
'cron_frequency_min' => array(
'title' => '檢查過期訂單',
'type' => 'number',
'placeholder' => 0,
'description' => '檢查未付款訂單的頻率(分鐘),像是超商條碼、代碼付款、ATM付款等有付款期限的訂單,讓它們在到期又未付款的話,自動更改訂單狀態為"取消",0 為不啟用',
'default' => '360',
'desc_tip' => true,
'custom_attributes' => array(
'min' => 0,
'step' => 1
),
'css' => 'width:60px;',
'autoload' => false
),
'reduce_stock' => array(
'title' => '庫存扣除',
'type' => 'select',
'description' => '選擇',
'default' => 'default',
'desc_tip' => false,
'description' => '選擇要在下單後,在還沒收到款項前扣除庫存,或是在歐付寶收到款項之後再扣除',
'options' => array(
'default' => '付款前',
'after' => '付款後'
)
),
'english' => array(
'title' => '英文結帳',
'type' => 'checkbox',
'label' => '啟用英文結帳介面',
'default' => 'no',
'description' => '進入歐付寶結帳頁,顯示的語系會是英文,注意:若開啟此選項,歐付寶只會提供信用卡刷卡結帳',
),
'testmode' => array(
'title' => '歐付寶測試模式',
'type' => 'checkbox',
'label' => '啟用測試模式',
'default' => 'no',
'description' => '歐付寶測試模式請填入測試商店代號、Hash Key、Hash IV',
),
'admin_mode' => array(
'title' => '管理員模式',
'type' => 'checkbox',
'label' => '啟用管理員測試模式',
'default' => 'no',
'description' => '開啟這項選項,可以只讓管理員看到歐付寶結帳方式',
'desc_tip' => true,
),
'debug' => array(
'title' => '除錯紀錄',
'type' => 'checkbox',
'label' => '啟用除錯紀錄',
'default' => 'no',
'description' => sprintf( '紀錄除錯/回應訊息,檔案存放於%s', wc_get_log_file_path( 'allpay_aio' ) ),
)
);
}
/**
* Admin Panel Options
* - Options for bits like 'title' and availability on a country-by-country basis
*
* @access public
* @return void
*/
public function admin_options() {
echo '
現正開啟測試模式
'; echo '現正開啟管理員模式
'; echo '錯誤代碼 : '.$return_code.'
'; } } else { echo '發生錯誤,無法取得訂單狀態,若您已付款,請通知商店管理員。
'; } } /** * Thank you order received text * * @param string $message the original message * @param object $order WC Order * @return mixed */ function thankyou_order_received_text( $text, $order ) { $order_id = $order->id; $return_code = get_post_meta( $order_id, '_RtnCode', true ); if ( $order->payment_method != 'innovext_allpay_aio' ) { return $text; } if( $return_code ) { if( $return_code === '1' && $this->instruction_succ ) { return wpautop( wptexturize( $this->instruction_succ ) ); } elseif( ( $return_code === '2' || $return_code === '10100073' ) && $this->instruction_on_hold ) { return wpautop( wptexturize( $this->instruction_on_hold ) ); } else { return '錯誤代碼 : '.$return_code.'
'; } } else { return '發生錯誤,無法取得訂單狀態,若您已付款,請通知商店管理員。
'; } return $text; } /** * Get check mac value. Check mac value is the validation mechanism of allpay * to check the post value from/to allpay to prevent the value been falsified. * * @param array $args * @return string */ function get_check_mac_value( $args ){ ksort($args); $hash_key = $this->hash_key; $hash_iv = $this->hash_iv; $args_hash_key = array_merge( array( 'HashKey'=> $hash_key ), $args, array( 'HashIV' => $hash_iv ) ); $args_string = ''; foreach( $args_hash_key as $v => $k ){ $args_string .= $v .'='. $k .'&'; } $args_string = rtrim( $args_string, "&" ); $args_urlencode = urlencode( $args_string ); $args_to_lower = strtolower( $args_urlencode ); $check_mac_value = md5( $args_to_lower ); return $check_mac_value; } /** * Generate the allpay button link (POST method) * * @access public * @param string $order_id * @return string */ function generate_allpay_form( $order_id ) { global $woocommerce; $order = new WC_Order($order_id); $allpay_args = $this->get_allpay_args($order); if( 'yes' == $this->testmode ){ $allpay_gateway = $this->test_gateway; }else{ $allpay_gateway = $this->live_gateway; } $check_mac_value = $this->get_check_mac_value( $allpay_args ); wc_enqueue_js( ' $.blockUI({ message: "感謝您的訂購,接下來畫面將導向付款頁面", baseZ: 99999, overlayCSS: { background: "#fff", opacity: 0.6 }, css: { padding: "20px", zindex: "9999999", textAlign: "center", color: "#555", border: "3px solid #aaa", backgroundColor:"#fff", cursor: "wait", lineHeight: "24px", } }); jQuery("#allpay_payment_form").submit(); ' ); $output = ''; return $output; } /** * Output for the order received page. * * @access public * @return void */ function receipt_page( $order_id ) { global $woocommerce; echo '感謝您的訂購,接下來將導向付款頁面,請稍後
'; // Clear cart $woocommerce->cart->empty_cart(); $order = new WC_Order( $order_id ); echo $this->generate_allpay_form( $order_id ); } /** * Add content to the WC emails. * * @access public * @param WC_Order $order * @param bool $sent_to_admin * @return void */ function email_instructions( $order, $sent_to_admin ) { if ( $order->payment_method !== $this->id ) { return; } $order_id = $order->id; $payment_type = get_post_meta( $order_id, '_PaymentType', true ); if( $payment_type ){ $payment_type_label = self::parse_payment_type( $payment_type ); } if ( $order->status == 'processing' ) { if( $sent_to_admin ){ echo "已收到付款,歐付寶付款方式 - ".$payment_type_label; }else{ echo wpautop( wptexturize( $this->instruction_succ ) ). PHP_EOL; } } elseif( $order->status == 'on-hold' ) { if( $sent_to_admin ) { echo "尚未付款,歐付寶付款方式 - ".$payment_type_label; } else { echo wpautop( wptexturize( $this->instruction_on_hold ) ). PHP_EOL; // vaccount detail, cvs detail $this->print_order_meta( $order ); } } } /** * Print order meta for webatm, atm, cvs in email notification * * @access public * @return void */ function print_order_meta( $order ) { if( $this->email_return_code ) { $email_return_code = $this->email_return_code; } else { $email_return_code = ''; } if( '2' == $email_return_code ) { ?>| 付款方式 | email_payment_type_label ?> |
|---|---|
| 銀行代碼 | email_bank_code ?> |
| 繳費帳號 | email_vaccount ?> |
| 繳費期限 | email_expire_date ?> |
| 付款方式 | email_payment_type_label ?> |
|---|---|
| 繳費代碼 | email_payment_no ?> |
| 繳費期限 | email_expire_date ?> |
| 訂單編號 | '. $merchant_trade_no . ' |
|---|---|
| 付款方式 | '. $payment_type_label . ' |
| 付款狀態 | '; if( $return_code === '1' ){ echo '已付款'; }elseif( $return_code === '2' || $return_code === '10100073' ){ echo '尚未付款'; } echo ' |
| 繳費期限 | '. $expire_date . ' |
| '.$label.' | '.$v.' |
|---|---|
| 備註 | 交易狀態代碼 "1" 為已付款,"2" 或 "10100073" 為待付款,其餘為交易失敗,交易狀態代碼請由歐付寶廠商後台查尋。 |
| 訂單編號 | |
|---|---|
| 付款方式 | |
| 付款狀態 | 已收到付款 |
| 訂單編號 | |
|---|---|
| 付款方式 | |
| 付款狀態 | 尚未付款 |