From 61a0c333c35331ded4d087e75f1356fbba10971d Mon Sep 17 00:00:00 2001 From: "allen.chang" Date: Mon, 24 Oct 2022 14:13:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0apple=20pay=20=E4=BB=98?= =?UTF-8?q?=E6=AC=BE=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/order/class-wooecpay-order.php | 1986 +++++++++-------- .../settings/class-wooecpay-setting-main.php | 1 + .../ecpay-ecommerce-for-woocommerce.php | 4 +- .../includes/config/logistic/settings.php | 8 + .../includes/config/main/settings.php | 6 +- .../logistic/class-wooecpay-logistic.php | 5 +- .../logistic/ecpay-logistic-cvs-711.php | 14 + .../logistic/ecpay-logistic-cvs-family.php | 14 + .../logistic/ecpay-logistic-cvs-hilife.php | 14 + .../logistic/ecpay-logistic-cvs-okmart.php | 14 + .../logistic/ecpay-logistic-response.php | 17 +- .../payment/class-wooecpay-gateway.php | 3 + .../services/payment/ecpay-gateway-base.php | 68 +- .../ecpay-gateway-credit-installment.php | 16 +- .../payment/ecpay-gateway-response.php | 314 +-- 15 files changed, 1399 insertions(+), 1085 deletions(-) diff --git a/ecpay-ecommerce-for-woocommerce/admin/order/class-wooecpay-order.php b/ecpay-ecommerce-for-woocommerce/admin/order/class-wooecpay-order.php index 769b396..08476e9 100644 --- a/ecpay-ecommerce-for-woocommerce/admin/order/class-wooecpay-order.php +++ b/ecpay-ecommerce-for-woocommerce/admin/order/class-wooecpay-order.php @@ -8,24 +8,31 @@ class Wooecpay_Order { public function __construct() { - if (is_admin()) { + if (is_admin()) { - // wp_enqueue_style('wooecpay_barcode_css', WOOECPAY_PLUGIN_URL . 'public/css/style.css'); - add_action('admin_enqueue_scripts' , array( $this, 'wooecpay_register_scripts' )); + // wp_enqueue_style('wooecpay_barcode_css', WOOECPAY_PLUGIN_URL . 'public/css/style.css'); + add_action('admin_enqueue_scripts' , array( $this, 'wooecpay_register_scripts' )); + + if ('yes' === get_option('wooecpay_enabled_payment', 'yes')) { - if ('yes' === get_option('wooecpay_ecpay_enabled_payment', 'yes')) { add_action( 'woocommerce_admin_billing_fields', array($this,'custom_order_meta'), 10, 1 ); add_action( 'woocommerce_admin_order_data_after_billing_address', array($this,'add_address_meta'), 10, 1 ); add_action( 'woocommerce_admin_order_data_after_order_details', array($this,'add_payment_info'), 10, 1 ); + add_action( 'woocommerce_admin_order_data_after_order_details', array($this,'check_order_status_cancel')); } - if ('yes' === get_option('wooecpay_ecpay_enabled_logistic', 'yes')) { + if ('yes' === get_option('wooecpay_enabled_logistic', 'yes')) { + add_action( 'woocommerce_admin_order_data_after_shipping_address', array($this,'logistic_button_display')); add_action( 'wp_ajax_send_logistic_order_action', array( $this, 'ajax_send_logistic_order_action' ) ); + + add_action( 'woocommerce_process_shop_order_meta', array( $this, 'order_update_sync_shipping_phone' ), 60 ); + } - if ('yes' === get_option('wooecpay_ecpay_enabled_invoice', 'yes')) { + if ('yes' === get_option('wooecpay_enabled_invoice', 'yes')) { + add_action( 'woocommerce_admin_order_data_after_billing_address', array($this,'add_invoice_meta'), 11, 1 ); // 手動開立 @@ -38,12 +45,12 @@ public function __construct() { if ('auto_cancel' === get_option('wooecpay_enabled_cancel_invoice_auto', 'auto_cancel')) { add_action('woocommerce_order_status_cancelled', array( $this, 'invoice_invalid' )); - add_action('woocommerce_order_status_refunded', array( $this, 'invoice_invalid' )); + add_action('woocommerce_order_status_refunded', array( $this, 'invoice_invalid' )); } } - } + } - if ('yes' === get_option('wooecpay_ecpay_enabled_invoice', 'yes')) { + if ('yes' === get_option('wooecpay_enabled_invoice', 'yes')) { // 自動開立 if ('auto_paid' === get_option('wooecpay_enabled_invoice_auto', 'auto_paid')) { @@ -131,6 +138,103 @@ public function add_payment_info($order) { } } + /** + * 無效訂單狀態更新 + * + * @return void + */ + public function check_order_status_cancel($order) { + + $query_trade_tag = get_post_meta( $order->get_id(), '_wooecpay_query_trade_tag', true ) ; + + if($query_trade_tag == 0){ + + // 判斷訂單狀態 + $order_status = $order->get_status(); + if( + $order_status == 'on-hold' || + $order_status == 'pending' + ){ + + // 判斷金流方式 + $payment_method = get_post_meta( $order->get_id(), '_payment_method', true ) ; + if( + $payment_method == 'Wooecpay_Gateway_Credit' || + $payment_method == 'Wooecpay_Gateway_Credit_Installment' || + $payment_method == 'Wooecpay_Gateway_Webatm' || + $payment_method == 'Wooecpay_Gateway_Atm' || + $payment_method == 'Wooecpay_Gateway_Cvs' || + $payment_method == 'Wooecpay_Gateway_Barcode' || + $payment_method == 'Wooecpay_Gateway_Applepay' + ){ + + // 判斷是否超過指定時間或自訂的保留時間 + + // 計算訂單建立時間是否超過指定時間 + if ( + $payment_method == 'Wooecpay_Gateway_Credit' || + $payment_method == 'Wooecpay_Gateway_Credit_Installment' + ) { + $offset = 60; // 信用卡 + } else { + $offset = 30; // 非信用卡 + } + + // 若使用者自訂的保留時間 > 綠界時間,則使用使用者設定的時間 + $hold_stock_minutes = empty(get_option('woocommerce_hold_stock_minutes')) ? 0 : get_option('woocommerce_hold_stock_minutes'); // 取得保留庫存時間 + + if ($hold_stock_minutes > $offset) { + $offset = $hold_stock_minutes; + } + + $date_created = $order->get_date_created()->getTimestamp(); // 訂單建立時間 + $dateCompare = strtotime('- '. $offset .' minute'); + + // 反查綠界訂單記錄API + if ($date_created <= $dateCompare) { + + $api_payment_query_trade_info = $this->get_ecpay_payment_api_query_trade_info(); + $merchant_trade_no = get_post_meta( $order->get_id(), '_wooecpay_payment_merchant_trade_no', true ) ; + + try { + + $factory = new Factory([ + 'hashKey' => $api_payment_query_trade_info['hashKey'], + 'hashIv' => $api_payment_query_trade_info['hashIv'], + ]); + + $postService = $factory->create('PostWithCmvVerifiedEncodedStrResponseService'); + + $input = [ + 'MerchantID' => $api_payment_query_trade_info['merchant_id'], + 'MerchantTradeNo' => $merchant_trade_no, + 'TimeStamp' => time(), + ]; + + $response = $postService->post($input, $api_payment_query_trade_info['action']); + // var_dump($response); + + // 逾期交易失敗 + if (isset($response['TradeStatus']) && $response['TradeStatus'] == 10200095) { + + // 更新訂單狀態/備註 + $order->add_order_note('逾期訂單自動取消'); + + $order->update_status('cancelled'); + $order->update_meta_data('_wooecpay_query_trade_tag', 1); + + $order->save(); + } + + } catch (RtnException $e) { + echo '(' . $e->getCode() . ')' . $e->getMessage() . PHP_EOL; + } + } + } + } + } + } + /** * 訂單發票資訊顯示 */ @@ -138,39 +242,39 @@ public function add_invoice_meta($order) { if ($order) { - $wooecpay_invoice_carruer_type = get_post_meta( $order->get_id(), '_wooecpay_invoice_carruer_type', true ) ; - $wooecpay_invoice_type = get_post_meta( $order->get_id(), '_wooecpay_invoice_type', true ) ; - $billing_company = get_post_meta( $order->get_id(), '_billing_company', true ) ; - $wooecpay_invoice_customer_identifier = get_post_meta( $order->get_id(), '_wooecpay_invoice_customer_identifier', true ) ; - $wooecpay_invoice_love_code = get_post_meta( $order->get_id(), '_wooecpay_invoice_love_code', true ) ; - $wooecpay_invoice_carruer_num = get_post_meta( $order->get_id(), '_wooecpay_invoice_carruer_num', true ) ; + $wooecpay_invoice_carruer_type = get_post_meta( $order->get_id(), '_wooecpay_invoice_carruer_type', true ) ; + $wooecpay_invoice_type = get_post_meta( $order->get_id(), '_wooecpay_invoice_type', true ) ; + $billing_company = get_post_meta( $order->get_id(), '_billing_company', true ) ; + $wooecpay_invoice_customer_identifier = get_post_meta( $order->get_id(), '_wooecpay_invoice_customer_identifier', true ) ; + $wooecpay_invoice_love_code = get_post_meta( $order->get_id(), '_wooecpay_invoice_love_code', true ) ; + $wooecpay_invoice_carruer_num = get_post_meta( $order->get_id(), '_wooecpay_invoice_carruer_num', true ) ; - $wooecpay_invoice_no = get_post_meta( $order->get_id(), '_wooecpay_invoice_no', true ) ; - $wooecpay_invoice_date = get_post_meta( $order->get_id(), '_wooecpay_invoice_date', true ) ; - $wooecpay_invoice_random_number = get_post_meta( $order->get_id(), '_wooecpay_invoice_random_number', true ) ; + $wooecpay_invoice_no = get_post_meta( $order->get_id(), '_wooecpay_invoice_no', true ) ; + $wooecpay_invoice_date = get_post_meta( $order->get_id(), '_wooecpay_invoice_date', true ) ; + $wooecpay_invoice_random_number = get_post_meta( $order->get_id(), '_wooecpay_invoice_random_number', true ) ; - $wooecpay_invoice_issue_type = get_post_meta( $order->get_id(), '_wooecpay_invoice_issue_type', true ) ; - $wooecpay_invoice_tsr = get_post_meta( $order->get_id(), '_wooecpay_invoice_tsr', true ) ; - $wooecpay_invoice_process = get_post_meta( $order->get_id(), '_wooecpay_invoice_process', true ) ; + $wooecpay_invoice_issue_type = get_post_meta( $order->get_id(), '_wooecpay_invoice_issue_type', true ) ; + $wooecpay_invoice_tsr = get_post_meta( $order->get_id(), '_wooecpay_invoice_tsr', true ) ; + $wooecpay_invoice_process = get_post_meta( $order->get_id(), '_wooecpay_invoice_process', true ) ; $order_status = $order->get_status(); // 開立發票按鈕顯示判斷 $invoice_create_button = false ; - if(empty($wooecpay_invoice_process) && - ( $order_status == 'processing' || $order_status == 'completed') - ){ - $invoice_create_button = true ; - } + if(empty($wooecpay_invoice_process) && + ( $order_status == 'processing' || $order_status == 'completed') + ){ + $invoice_create_button = true ; + } - // 作廢發票按鈕顯示判斷 - $invoice_invalid_button = false ; - if(!empty($wooecpay_invoice_process) && - ( $order_status == 'cancelled' || $order_status == 'refunded')) - { - $invoice_invalid_button = true ; - } + // 作廢發票按鈕顯示判斷 + $invoice_invalid_button = false ; + if(!empty($wooecpay_invoice_process) && + ( $order_status == 'cancelled' || $order_status == 'refunded')) + { + $invoice_invalid_button = true ; + } // 顯示 $invoiceType = [ @@ -207,8 +311,6 @@ public function add_invoice_meta($order) { default: break; } - - if(isset($invoiceCarruerType[$wooecpay_invoice_carruer_type])){ echo wp_kses_post('

開立類型:'. $invoiceCarruerType[$wooecpay_invoice_carruer_type] . '

') ; @@ -235,51 +337,57 @@ public function add_invoice_meta($order) { break; } - // 開立發票按鈕顯示判斷 - if($invoice_create_button){ - echo 'get_id().');\'>'; - } - - // 作廢發票按鈕顯示判斷 - if($invoice_invalid_button){ - echo 'get_id().');\'>'; - } + if($invoice_create_button){ + echo 'get_id().');\'>'; + } + // 作廢發票按鈕顯示判斷 + if($invoice_invalid_button){ + echo 'get_id().');\'>'; + } echo ''; } } + + /** + * 複寫聯絡電話至收件人電話 + */ + public function order_update_sync_shipping_phone($post_id){ + + $shipping_phone = get_post_meta($post_id, '_shipping_phone', true); + update_post_meta($post_id, 'wooecpay_shipping_phone', $shipping_phone); + } /** * 註冊JS */ - function wooecpay_register_scripts() { - + public function wooecpay_register_scripts() { + wp_register_script( 'wooecpay_main', WOOECPAY_PLUGIN_URL . 'public/js/wooecpay-main.js', array(), '1.0.0', true - ); + ); - // 載入js - wp_enqueue_script('wooecpay_main'); - } + // 載入js + wp_enqueue_script('wooecpay_main'); + } /** * 產生物流相關按鈕顯示 */ public function logistic_button_display($order) { - if ($order) { + if ($order) { - // 取得訂單資訊 - // $order_data = $order->get_data(); + // 取得訂單資訊 + // $order_data = $order->get_data(); - - // 取得物流方式 + // 取得物流方式 $shipping_method_id = $order->get_items('shipping') ; if($shipping_method_id) { @@ -289,181 +397,181 @@ public function logistic_button_display($order) { $order_status = $order->get_status(); - // 地圖按鈕顯示判斷 - if(true){ - - // 按鈕顯示旗標 - $map_button = false ; - - // 判斷物流方式是否允許變更門市 - if( - ( $order_status == 'on-hold' || $order_status == 'processing') && - ( - $shipping_method_id == 'Wooecpay_Logistic_CVS_711' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Family' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Hilife' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Okmart' - ) - ){ - - // 狀態判斷是否已經建立綠界物流單 AllPayLogisticsID - $ecpay_logistic_AllPayLogisticsID = get_post_meta( $order->get_id(), '_wooecpay_logistic_AllPayLogisticsID', true ); - - if(empty($ecpay_logistic_AllPayLogisticsID) ){ - $map_button = true ; - } - } - } - - // 物流訂單按鈕判斷 - if(true){ - - $logistic_order_button = true ; - - // 判斷是否為綠界物流 - if( - $shipping_method_id == 'Wooecpay_Logistic_CVS_711' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Family' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Hilife' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Okmart' || - $shipping_method_id == 'Wooecpay_Logistic_Home_Tcat' || - $shipping_method_id == 'Wooecpay_Logistic_Home_Ecan' - ){ - if( - $shipping_method_id == 'Wooecpay_Logistic_CVS_711' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Family' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Hilife' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Okmart' - ){ - - // 狀態判斷 _ecpay_logistic_cvs_store_id門市代號不存在 - $ecpay_logistic_cvs_store_id = get_post_meta( $order->get_id(), '_ecpay_logistic_cvs_store_id', true ); - - if(empty($ecpay_logistic_cvs_store_id)){ - $logistic_order_button = false ; - } - } - - } else { - - $logistic_order_button = false ; - } - - // 已經存在AllPayLogisticsID 關閉按鈕 - $AllPayLogisticsID = get_post_meta( $order->get_id(), '_wooecpay_logistic_AllPayLogisticsID', true ); - - if(!empty($AllPayLogisticsID)){ - $logistic_order_button = false ; - } + // 地圖按鈕顯示判斷 + if(true){ - if($order_status != 'on-hold' && $order_status != 'processing'){ - $logistic_order_button = false ; - } - } + // 按鈕顯示旗標 + $map_button = false ; - // 列印訂單按鈕判斷 - if(true){ + // 判斷物流方式是否允許變更門市 + if( + ( $order_status == 'on-hold' || $order_status == 'processing') && + ( + $shipping_method_id == 'Wooecpay_Logistic_CVS_711' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Family' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Hilife' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Okmart' + ) + ){ - $logistic_print_button = false ; + // 狀態判斷是否已經建立綠界物流單 AllPayLogisticsID + $ecpay_logistic_AllPayLogisticsID = get_post_meta( $order->get_id(), '_wooecpay_logistic_AllPayLogisticsID', true ); - // 已經存在AllPayLogisticsID 關閉按鈕 - $AllPayLogisticsID = get_post_meta( $order->get_id(), '_wooecpay_logistic_AllPayLogisticsID', true ); + if(empty($ecpay_logistic_AllPayLogisticsID) ){ + $map_button = true ; + } + } + } + + // 物流訂單按鈕判斷 + if(true){ + + $logistic_order_button = true ; + + // 判斷是否為綠界物流 + if( + $shipping_method_id == 'Wooecpay_Logistic_CVS_711' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Family' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Hilife' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Okmart' || + $shipping_method_id == 'Wooecpay_Logistic_Home_Tcat' || + $shipping_method_id == 'Wooecpay_Logistic_Home_Ecan' + ){ + + if( + $shipping_method_id == 'Wooecpay_Logistic_CVS_711' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Family' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Hilife' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Okmart' + ){ + + // 狀態判斷 _ecpay_logistic_cvs_store_id門市代號不存在 + $ecpay_logistic_cvs_store_id = get_post_meta( $order->get_id(), '_ecpay_logistic_cvs_store_id', true ); - if(!empty($AllPayLogisticsID)){ - $logistic_print_button = true ; + if(empty($ecpay_logistic_cvs_store_id)){ + $logistic_order_button = false ; } - } - - // 判斷是否為綠界物流 - if( - $shipping_method_id == 'Wooecpay_Logistic_CVS_711' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Family' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Hilife' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Okmart' || - $shipping_method_id == 'Wooecpay_Logistic_Home_Tcat' || - $shipping_method_id == 'Wooecpay_Logistic_Home_Ecan' - ){ - // 判斷是否為超商取貨 - if( - $shipping_method_id == 'Wooecpay_Logistic_CVS_711' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Family' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Hilife' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Okmart' - ){ - echo wp_kses_post('

超商編號:'. get_post_meta( $order->get_id(), '_ecpay_logistic_cvs_store_id', true ) . '

') ; - echo wp_kses_post('

超商名稱:'. get_post_meta( $order->get_id(), '_ecpay_logistic_cvs_store_name', true ) . '

') ; - - if ('yes' === get_option('wooecpay_keep_logistic_phone', 'yes')) { - echo wp_kses_post('

收件人電話:'. get_post_meta( $order->get_id(), 'wooecpay_shipping_phone', true ) . '

') ; - } - } - - echo '
'; - echo '

物流單資訊

' ; - - if(true){ - echo wp_kses_post('

廠商交易編號:'. get_post_meta( $order->get_id(), '_wooecpay_logistic_merchant_trade_no', true ) . '

') ; - echo wp_kses_post('

綠界物流編號:'. get_post_meta( $order->get_id(), '_wooecpay_logistic_AllPayLogisticsID', true ) . '

') ; - echo wp_kses_post('

寄貨編號:'. get_post_meta( $order->get_id(), '_wooecpay_logistic_CVSPaymentNo', true ) . '

') ; - echo wp_kses_post('

托運單號:'. get_post_meta( $order->get_id(), '_wooecpay_logistic_BookingNote', true ) . '

') ; - } - - // 產生地圖按鈕兒 - if($map_button){ - - // 組合地圖FORM + } + + } else { + $logistic_order_button = false ; + } + + // 已經存在AllPayLogisticsID 關閉按鈕 + $AllPayLogisticsID = get_post_meta( $order->get_id(), '_wooecpay_logistic_AllPayLogisticsID', true ); + + if(!empty($AllPayLogisticsID)){ + $logistic_order_button = false ; + } + + if($order_status != 'on-hold' && $order_status != 'processing'){ + $logistic_order_button = false ; + } + } + + // 列印訂單按鈕判斷 + if(true){ + + $logistic_print_button = false ; + + // 已經存在AllPayLogisticsID 關閉按鈕 + $AllPayLogisticsID = get_post_meta( $order->get_id(), '_wooecpay_logistic_AllPayLogisticsID', true ); + + if(!empty($AllPayLogisticsID)){ + $logistic_print_button = true ; + } + } + + // 判斷是否為綠界物流 + if( + $shipping_method_id == 'Wooecpay_Logistic_CVS_711' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Family' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Hilife' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Okmart' || + $shipping_method_id == 'Wooecpay_Logistic_Home_Tcat' || + $shipping_method_id == 'Wooecpay_Logistic_Home_Ecan' + ){ + + // 判斷是否為超商取貨 + if( + $shipping_method_id == 'Wooecpay_Logistic_CVS_711' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Family' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Hilife' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Okmart' + ){ + echo wp_kses_post('

超商編號:'. get_post_meta( $order->get_id(), '_ecpay_logistic_cvs_store_id', true ) . '

') ; + echo wp_kses_post('

超商名稱:'. get_post_meta( $order->get_id(), '_ecpay_logistic_cvs_store_name', true ) . '

') ; + + if ('yes' === get_option('wooecpay_keep_logistic_phone', 'yes')) { + echo wp_kses_post('

收件人電話:'. get_post_meta( $order->get_id(), 'wooecpay_shipping_phone', true ) . '

') ; + } + } + + echo '
'; + echo '

物流單資訊

' ; + + if(true){ + echo wp_kses_post('

廠商交易編號:'. get_post_meta( $order->get_id(), '_wooecpay_logistic_merchant_trade_no', true ) . '

') ; + echo wp_kses_post('

綠界物流編號:'. get_post_meta( $order->get_id(), '_wooecpay_logistic_AllPayLogisticsID', true ) . '

') ; + echo wp_kses_post('

寄貨編號:'. get_post_meta( $order->get_id(), '_wooecpay_logistic_CVSPaymentNo', true ) . '

') ; + echo wp_kses_post('

托運單號:'. get_post_meta( $order->get_id(), '_wooecpay_logistic_BookingNote', true ) . '

') ; + } + + // 產生地圖按鈕兒 + if($map_button){ + + // 組合地圖FORM $api_logistic_info = $this->get_ecpay_logistic_api_info('map'); $client_back_url = WC()->api_request_url('wooecpay_change_logistic_map_callback', true); $MerchantTradeNo = $this->get_merchant_trade_no($order->get_id(), get_option('wooecpay_logistic_order_prefix')); $LogisticsType = $this->get_logistics_sub_type($shipping_method_id) ; - try { - $factory = new Factory([ - 'hashKey' => $api_logistic_info['hashKey'], - 'hashIv' => $api_logistic_info['hashIv'], - 'hashMethod' => 'md5', - ]); - $autoSubmitFormService = $factory->create('AutoSubmitFormWithCmvService'); - - $inputMap = [ - 'MerchantID' => $api_logistic_info['merchant_id'], - 'MerchantTradeNo' => $MerchantTradeNo, - 'LogisticsType' => $LogisticsType['type'], - 'LogisticsSubType' => $LogisticsType['sub_type'], - 'IsCollection' => 'Y', - 'ServerReplyURL' => $client_back_url, - ]; - - $form_map = $autoSubmitFormService->generate($inputMap, $api_logistic_info['action'], 'ecpay_map'); - - $form_map = str_replace('', '', $form_map) ; - $form_map = str_replace('', '', $form_map) ; - $form_map = str_replace('', '', $form_map) ; - - echo ''; - echo $form_map ; - - - } catch (RtnException $e) { - echo wp_kses_post( '(' . $e->getCode() . ')' . $e->getMessage() ) . PHP_EOL; - } - - echo '  '; - } - - // 產生按鈕 - if($logistic_order_button){ - echo 'get_id().');\'>'; - } - - // 列印訂單按鈕判斷 - if($logistic_print_button){ - + try { + $factory = new Factory([ + 'hashKey' => $api_logistic_info['hashKey'], + 'hashIv' => $api_logistic_info['hashIv'], + 'hashMethod' => 'md5', + ]); + $autoSubmitFormService = $factory->create('AutoSubmitFormWithCmvService'); + + $inputMap = [ + 'MerchantID' => $api_logistic_info['merchant_id'], + 'MerchantTradeNo' => $MerchantTradeNo, + 'LogisticsType' => $LogisticsType['type'], + 'LogisticsSubType' => $LogisticsType['sub_type'], + 'IsCollection' => 'Y', + 'ServerReplyURL' => $client_back_url, + ]; + + $form_map = $autoSubmitFormService->generate($inputMap, $api_logistic_info['action'], 'ecpay_map'); + + $form_map = str_replace('', '', $form_map) ; + $form_map = str_replace('', '', $form_map) ; + $form_map = str_replace('', '', $form_map) ; + + echo ''; + echo $form_map ; + + } catch (RtnException $e) { + echo wp_kses_post( '(' . $e->getCode() . ')' . $e->getMessage() ) . PHP_EOL; + } + + echo '  '; + } + + // 產生按鈕 + if($logistic_order_button){ + echo 'get_id().');\'>'; + } + + // 列印訂單按鈕判斷 + if($logistic_print_button){ + $api_logistic_info = $this->get_ecpay_logistic_api_info('print', $shipping_method_id); $AllPayLogisticsID = get_post_meta( $order->get_id(), '_wooecpay_logistic_AllPayLogisticsID', true ) ; - $CVSPaymentNo = get_post_meta( $order->get_id(), '_wooecpay_logistic_CVSPaymentNo', true ) ; + $CVSPaymentNo = get_post_meta( $order->get_id(), '_wooecpay_logistic_CVSPaymentNo', true ) ; $CVSValidationNo = get_post_meta( $order->get_id(), '_wooecpay_logistic_CVSValidationNo', true ) ; // 組合送綠界物流列印參數 @@ -492,34 +600,35 @@ public function logistic_button_display($order) { break; } - try { - $factory = new Factory([ - 'hashKey' => $api_logistic_info['hashKey'], - 'hashIv' => $api_logistic_info['hashIv'], - 'hashMethod' => 'md5', - ]); - $autoSubmitFormService = $factory->create('AutoSubmitFormWithCmvService'); - - $form_print = $autoSubmitFormService->generate($inputPrint, $api_logistic_info['action'], '_Blank','ecpay_print'); - $form_print = str_replace('', '', $form_print) ; - $form_print = str_replace('', '', $form_print) ; - $form_print = str_replace('', '', $form_print) ; - - echo ''; - echo $form_print ; - - } catch (RtnException $e) { - echo wp_kses_post( '(' . $e->getCode() . ')' . $e->getMessage() ) . PHP_EOL; - } - - echo '  '; - } - - echo '
'; - } - - } - } + try { + + $factory = new Factory([ + 'hashKey' => $api_logistic_info['hashKey'], + 'hashIv' => $api_logistic_info['hashIv'], + 'hashMethod' => 'md5', + ]); + + $autoSubmitFormService = $factory->create('AutoSubmitFormWithCmvService'); + + $form_print = $autoSubmitFormService->generate($inputPrint, $api_logistic_info['action'], '_Blank','ecpay_print'); + $form_print = str_replace('', '', $form_print) ; + $form_print = str_replace('', '', $form_print) ; + $form_print = str_replace('', '', $form_print) ; + + echo ''; + echo $form_print ; + + } catch (RtnException $e) { + echo wp_kses_post( '(' . $e->getCode() . ')' . $e->getMessage() ) . PHP_EOL; + } + + echo '  '; + } + + echo '
'; + } + } + } } /** @@ -544,30 +653,28 @@ public function ajax_send_logistic_order_action() $shipping_method_id = $shipping_method_id->get_method_id() ; // 判斷是否為綠界物流 產生物流訂單 - if( - $shipping_method_id == 'Wooecpay_Logistic_CVS_711' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Family' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Hilife' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Okmart' || - $shipping_method_id == 'Wooecpay_Logistic_Home_Tcat' || - $shipping_method_id == 'Wooecpay_Logistic_Home_Ecan' - ){ - - $LogisticsType = $this->get_logistics_sub_type($shipping_method_id) ; - $api_logistic_info = $this->get_ecpay_logistic_api_info('create'); - $MerchantTradeNo = $this->get_merchant_trade_no($order->get_id(), get_option('wooecpay_logistic_order_prefix')); - - $sender_name = get_option('wooecpay_logistic_sender_name') ; - $sender_cellphone = get_option('wooecpay_logistic_sender_cellphone') ; - $sender_zipcode = get_option('wooecpay_logistic_sender_zipcode') ; - $sender_address = get_option('wooecpay_logistic_sender_address') ; - - $serverReplyURL = WC()->api_request_url('wooecpay_logistic_status_callback', true); - - - // 取得訂單資訊 - // $order_data = $order->get_data(); - + if( + $shipping_method_id == 'Wooecpay_Logistic_CVS_711' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Family' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Hilife' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Okmart' || + $shipping_method_id == 'Wooecpay_Logistic_Home_Tcat' || + $shipping_method_id == 'Wooecpay_Logistic_Home_Ecan' + ){ + + $LogisticsType = $this->get_logistics_sub_type($shipping_method_id) ; + $api_logistic_info = $this->get_ecpay_logistic_api_info('create'); + $MerchantTradeNo = $this->get_merchant_trade_no($order->get_id(), get_option('wooecpay_logistic_order_prefix')); + + $sender_name = get_option('wooecpay_logistic_sender_name') ; + $sender_cellphone = get_option('wooecpay_logistic_sender_cellphone') ; + $sender_zipcode = get_option('wooecpay_logistic_sender_zipcode') ; + $sender_address = get_option('wooecpay_logistic_sender_address') ; + + $serverReplyURL = WC()->api_request_url('wooecpay_logistic_status_callback', true); + + // 取得訂單資訊 + // $order_data = $order->get_data(); $CVSStoreID = $order->get_meta('_ecpay_logistic_cvs_store_id') ; if(!isset($CVSStoreID) || empty($CVSStoreID)){ @@ -578,74 +685,101 @@ public function ajax_send_logistic_order_action() } $payment_method = $order->get_payment_method() ; + if($payment_method == 'cod'){ $IsCollection = 'Y'; } else { $IsCollection = 'N'; } - $item_name = $this->get_item_name($order) ; - - if($LogisticsType['type'] == 'HOME'){ - - $inputLogisticOrder = [ - 'MerchantID' => $api_logistic_info['merchant_id'], - 'MerchantTradeNo' => $MerchantTradeNo, - 'MerchantTradeDate' => date('Y/m/d H:i:s'), - 'LogisticsType' => $LogisticsType['type'], - 'LogisticsSubType' => $LogisticsType['sub_type'], - 'GoodsAmount' => $order->get_total(), - 'GoodsName' => $item_name, - 'SenderName' => $sender_name, - 'SenderCellPhone' => $sender_cellphone, - 'SenderZipCode' => $sender_zipcode, - 'SenderAddress' => $sender_address, - 'ReceiverName' => $order->get_shipping_first_name() . $order->get_shipping_last_name(), - 'ReceiverCellPhone' => $order->get_billing_phone(), - 'ReceiverZipCode' => $order->get_shipping_postcode(), - 'ReceiverAddress' => $order->get_shipping_state().$order->get_shipping_city().$order->get_shipping_address_1().$order->get_shipping_address_2(), - 'Temperature' => '0001', - 'Distance' => '00', - 'Specification' => '0001', - 'ScheduledPickupTime' => '4', - 'ScheduledDeliveryTime' => '4', - 'ServerReplyURL' => $serverReplyURL, - ]; + // 綠界訂單顯示商品名稱判斷 + $item_name_default = '網路商品一批'; + if ('yes' === get_option('wooecpay_enabled_logistic_disp_item_name', 'yes')) { - } else if($LogisticsType['type'] == 'CVS'){ - - $inputLogisticOrder = [ - 'MerchantID' => $api_logistic_info['merchant_id'], - 'MerchantTradeNo' => $MerchantTradeNo, - 'MerchantTradeDate' => date('Y/m/d H:i:s'), - 'LogisticsType' => $LogisticsType['type'], - 'LogisticsSubType' => $LogisticsType['sub_type'], - 'GoodsAmount' => $order->get_total(), - 'GoodsName' => $item_name, - 'SenderName' => $sender_name, - 'SenderCellPhone' => $sender_cellphone, - 'ReceiverName' => $order->get_shipping_first_name() . $order->get_shipping_last_name(), - 'ReceiverCellPhone' => $order->get_billing_phone(), - 'ReceiverStoreID' => $CVSStoreID, - 'IsCollection' => $IsCollection, - 'ServerReplyURL' => $serverReplyURL, - ]; - - } + // 取出訂單品項 + $item_name = $this->get_item_name($order); - try { - $factory = new Factory([ - 'hashKey' => $api_logistic_info['hashKey'], - 'hashIv' => $api_logistic_info['hashIv'], - 'hashMethod' => 'md5', - ]); + // 判斷是否超過長度,如果超過長度改為預設文字 + if(strlen($item_name) > 50 ) { + + $item_name = $item_name_default; + + $order->add_order_note('商品名稱超過綠界物流可允許長度強制改為:'.$item_name); + $order->save(); + } - $postService = $factory->create('PostWithCmvEncodedStrResponseService'); - $response = $postService->post($inputLogisticOrder, $api_logistic_info['action']); + // 判斷特殊字元 + if(preg_match('/[\^\'\[\]`!@#%\\\&*+\"<>|_]/', $item_name)){ - if(isset($response['RtnCode']) && ( $response['RtnCode'] == 300 || $response['RtnCode'] == 2001 ) ){ + $item_name = $item_name_default; - // 更新訂單 + $order->add_order_note('商品名稱存在綠界物流不允許的特殊字元強制改為:'.$item_name); + $order->save(); + } + + } else { + $item_name = $item_name_default; + } + + if($LogisticsType['type'] == 'HOME'){ + + $inputLogisticOrder = [ + 'MerchantID' => $api_logistic_info['merchant_id'], + 'MerchantTradeNo' => $MerchantTradeNo, + 'MerchantTradeDate' => date('Y/m/d H:i:s'), + 'LogisticsType' => $LogisticsType['type'], + 'LogisticsSubType' => $LogisticsType['sub_type'], + 'GoodsAmount' => $order->get_total(), + 'GoodsName' => $item_name, + 'SenderName' => $sender_name, + 'SenderCellPhone' => $sender_cellphone, + 'SenderZipCode' => $sender_zipcode, + 'SenderAddress' => $sender_address, + 'ReceiverName' => $order->get_shipping_last_name() . $order->get_shipping_first_name(), + 'ReceiverCellPhone' => $order->get_billing_phone(), + 'ReceiverZipCode' => $order->get_shipping_postcode(), + 'ReceiverAddress' => $order->get_shipping_state().$order->get_shipping_city().$order->get_shipping_address_1().$order->get_shipping_address_2(), + 'Temperature' => '0001', + 'Distance' => '00', + 'Specification' => '0001', + 'ScheduledPickupTime' => '4', + 'ScheduledDeliveryTime' => '4', + 'ServerReplyURL' => $serverReplyURL, + ]; + + } else if($LogisticsType['type'] == 'CVS'){ + + $inputLogisticOrder = [ + 'MerchantID' => $api_logistic_info['merchant_id'], + 'MerchantTradeNo' => $MerchantTradeNo, + 'MerchantTradeDate' => date('Y/m/d H:i:s'), + 'LogisticsType' => $LogisticsType['type'], + 'LogisticsSubType' => $LogisticsType['sub_type'], + 'GoodsAmount' => $order->get_total(), + 'GoodsName' => $item_name, + 'SenderName' => $sender_name, + 'SenderCellPhone' => $sender_cellphone, + 'ReceiverName' => $order->get_shipping_last_name() . $order->get_shipping_first_name(), + 'ReceiverCellPhone' => $order->get_billing_phone(), + 'ReceiverStoreID' => $CVSStoreID, + 'IsCollection' => $IsCollection, + 'ServerReplyURL' => $serverReplyURL, + ]; + } + + try { + $factory = new Factory([ + 'hashKey' => $api_logistic_info['hashKey'], + 'hashIv' => $api_logistic_info['hashIv'], + 'hashMethod' => 'md5', + ]); + + $postService = $factory->create('PostWithCmvEncodedStrResponseService'); + $response = $postService->post($inputLogisticOrder, $api_logistic_info['action']); + + if(isset($response['RtnCode']) && ( $response['RtnCode'] == 300 || $response['RtnCode'] == 2001 ) ){ + + // 更新訂單 $order->update_meta_data( '_wooecpay_logistic_merchant_trade_no', $response['MerchantTradeNo'] ); $order->update_meta_data( '_wooecpay_logistic_RtnCode', $response['RtnCode'] ); $order->update_meta_data( '_wooecpay_logistic_RtnMsg', $response['RtnMsg'] ); @@ -663,25 +797,24 @@ public function ajax_send_logistic_order_action() 'msg' => '成功', ]; - } else { - - // add note - $order->add_order_note(print_r($response, true)); + } else { + // add note + $order->add_order_note(print_r($response, true)); $order->save(); - } + } - // var_dump($response); + // var_dump($response); } catch (RtnException $e) { - echo wp_kses_post( '(' . $e->getCode() . ')' . $e->getMessage() ) . PHP_EOL; + echo wp_kses_post( '(' . $e->getCode() . ')' . $e->getMessage() ) . PHP_EOL; } - } else { + } else { - $ajaxReturn = [ + $ajaxReturn = [ 'code' => '0002', 'msg' => '非綠界物流方式', ]; - } + } } else { @@ -689,7 +822,6 @@ public function ajax_send_logistic_order_action() 'code' => '0001', 'msg' => '查無訂單', ]; - } echo json_encode($ajaxReturn, true); @@ -734,6 +866,7 @@ public function invoice_create($order_id) 'hashKey' => $api_payment_info['hashKey'], 'hashIv' => $api_payment_info['hashIv'], ]); + $postService = $factory->create('PostWithAesJsonResponseService'); $items = [] ; @@ -752,17 +885,17 @@ public function invoice_create($order_id) // 物流費用 $shipping_fee = $order->get_shipping_total(); - if ($shipping_fee != 0) { - - $Items[] = [ - 'ItemName' => __('Shipping fee', 'ecpay-ecommerce-for-woocommerce'), - 'ItemCount' => 1, - 'ItemWord' => '批', - 'ItemPrice' => $shipping_fee, - 'ItemTaxType' => '1', - 'ItemAmount' => $shipping_fee, - ] ; - } + if ($shipping_fee != 0) { + + $Items[] = [ + 'ItemName' => __('Shipping fee', 'ecpay-ecommerce-for-woocommerce'), + 'ItemCount' => 1, + 'ItemWord' => '批', + 'ItemPrice' => $shipping_fee, + 'ItemTaxType' => '1', + 'ItemAmount' => $shipping_fee, + ] ; + } $country = $order->get_billing_country(); $countries = WC()->countries->get_countries(); @@ -772,83 +905,86 @@ public function invoice_create($order_id) $states = WC()->countries->get_states($country); $full_state = ($state && isset($states[$state])) ? $states[$state] : $state; - $data = [ - 'MerchantID' => $api_payment_info['merchant_id'], - 'RelateNumber' => $RelateNumber, - 'CustomerID' => '', - 'CustomerName' => $order->get_billing_last_name() . $order->get_billing_first_name(), - 'CustomerAddr' => $full_country . $full_state . $order->get_billing_city() . $order->get_billing_address_1() . $order->get_billing_address_2(), - 'CustomerPhone' => $order->get_billing_phone(), - 'CustomerEmail' => $order->get_billing_email(), - 'Print' => '0', - 'Donation' => '0', - 'LoveCode' => '', - 'CarrierType' => '', - 'CarrierNum' => '', - 'TaxType' => '1', - 'SalesAmount' => intval(round($order->get_total(), 0)), - 'Items' => $Items, - 'InvType' => '07' - ]; - - $wooecpay_invoice_type = get_post_meta( $order->get_id(), '_wooecpay_invoice_type', true ) ; - $wooecpay_invoice_carruer_type = get_post_meta( $order->get_id(), '_wooecpay_invoice_carruer_type', true ) ; - - switch ($wooecpay_invoice_type) { - - case 'p': - switch ($wooecpay_invoice_carruer_type ) { - - case '1': - $data['CarrierType'] = '1'; - break; - case '2': - $data['CarrierType'] = '2'; - $data['CarrierNum'] = get_post_meta( $order->get_id(), '_wooecpay_invoice_carruer_num', true ) ; - break; - case '3': - $data['CarrierType'] = '3'; - $data['CarrierNum'] = get_post_meta( $order->get_id(), '_wooecpay_invoice_carruer_num', true ) ; - break; - default: - $data['Print'] = '1'; - break; - } - - break; - - case 'c': - $data['Print'] = '1'; - $data['CustomerIdentifier'] = get_post_meta( $order->get_id(), '_wooecpay_invoice_customer_identifier', true ) ; - $company = $order->get_billing_company(); - if ($company) { - $data['CustomerName'] = $company; - } - break; - - case 'd': - $data['Donation'] = '1'; - $data['LoveCode'] = get_post_meta( $order->get_id(), '_wooecpay_invoice_love_code', true ) ; - break; - } + $data = [ + 'MerchantID' => $api_payment_info['merchant_id'], + 'RelateNumber' => $RelateNumber, + 'CustomerID' => '', + 'CustomerName' => $order->get_billing_last_name() . $order->get_billing_first_name(), + 'CustomerAddr' => $full_country . $full_state . $order->get_billing_city() . $order->get_billing_address_1() . $order->get_billing_address_2(), + 'CustomerPhone' => $order->get_billing_phone(), + 'CustomerEmail' => $order->get_billing_email(), + 'Print' => '0', + 'Donation' => '0', + 'LoveCode' => '', + 'CarrierType' => '', + 'CarrierNum' => '', + 'TaxType' => '1', + 'SalesAmount' => intval(round($order->get_total(), 0)), + 'Items' => $Items, + 'InvType' => '07' + ]; + + $wooecpay_invoice_type = get_post_meta( $order->get_id(), '_wooecpay_invoice_type', true ) ; + $wooecpay_invoice_carruer_type = get_post_meta( $order->get_id(), '_wooecpay_invoice_carruer_type', true ) ; + + switch ($wooecpay_invoice_type) { + + case 'p': + switch ($wooecpay_invoice_carruer_type ) { + + case '1': + $data['CarrierType'] = '1'; + break; + + case '2': + $data['CarrierType'] = '2'; + $data['CarrierNum'] = get_post_meta( $order->get_id(), '_wooecpay_invoice_carruer_num', true ) ; + break; + + case '3': + $data['CarrierType'] = '3'; + $data['CarrierNum'] = get_post_meta( $order->get_id(), '_wooecpay_invoice_carruer_num', true ) ; + break; + + default: + $data['Print'] = '1'; + break; + } + break; - $input = [ - 'MerchantID' => $api_payment_info['merchant_id'], - 'RqHeader' => [ - 'Timestamp' => time(), - 'Revision' => '3.0.0', - ], - 'Data' => $data, - ]; + case 'c': + $data['Print'] = '1'; + $data['CustomerIdentifier'] = get_post_meta( $order->get_id(), '_wooecpay_invoice_customer_identifier', true ) ; + $company = $order->get_billing_company(); + if ($company) { + $data['CustomerName'] = $company; + } + break; + + case 'd': + $data['Donation'] = '1'; + $data['LoveCode'] = get_post_meta( $order->get_id(), '_wooecpay_invoice_love_code', true ) ; + break; + + } + + $input = [ + 'MerchantID' => $api_payment_info['merchant_id'], + 'RqHeader' => [ + 'Timestamp' => time(), + 'Revision' => '3.0.0', + ], + 'Data' => $data, + ]; - $response = $postService->post($input, $api_payment_info['action']); + $response = $postService->post($input, $api_payment_info['action']); - if($response['TransCode'] == 1){ + if($response['TransCode'] == 1){ - if($response['Data']['RtnCode'] == 1){ + if($response['Data']['RtnCode'] == 1){ - // 更新訂單 + // 更新訂單 $order->update_meta_data( '_wooecpay_invoice_relate_number', $RelateNumber ); $order->update_meta_data( '_wooecpay_invoice_RtnCode', $response['Data']['RtnCode'] ); $order->update_meta_data( '_wooecpay_invoice_RtnMsg', $response['Data']['RtnMsg'] ); @@ -862,19 +998,19 @@ public function invoice_create($order_id) $order->add_order_note('發票開立成功:狀態:' . $response['Data']['RtnMsg'] . '('. $response['Data']['RtnCode'] . ')'); $order->save(); - } else { + } else { - $order->add_order_note('發票開立失敗:狀態:' . $response['Data']['RtnMsg'] . '('. $response['Data']['RtnCode'] . ')'); + $order->add_order_note('發票開立失敗:狀態:' . $response['Data']['RtnMsg'] . '('. $response['Data']['RtnCode'] . ')'); $order->save(); - } + } - } else { + } else { - $order->add_order_note('發票開立失敗:狀態:' . $response['TransMsg'] . '('. $response['TransCode'] . ')'); + $order->add_order_note('發票開立失敗:狀態:' . $response['TransMsg'] . '('. $response['TransCode'] . ')'); $order->save(); - } + } - $ajaxReturn = [ + $ajaxReturn = [ 'code' => '9999', 'msg' => '成功', ]; @@ -886,44 +1022,45 @@ public function invoice_create($order_id) } else { // 延遲開立 - - $api_payment_info = $this->get_ecpay_invoice_api_info('delay_issue'); - $RelateNumber = $this->get_relate_number($order->get_id(), get_option('wooecpay_invoice_prefix')); + $api_payment_info = $this->get_ecpay_invoice_api_info('delay_issue'); + $RelateNumber = $this->get_relate_number($order->get_id(), get_option('wooecpay_invoice_prefix')); try { - $factory = new Factory([ - 'hashKey' => $api_payment_info['hashKey'], - 'hashIv' => $api_payment_info['hashIv'], - ]); - $postService = $factory->create('PostWithAesJsonResponseService'); - $items = [] ; + $factory = new Factory([ + 'hashKey' => $api_payment_info['hashKey'], + 'hashIv' => $api_payment_info['hashIv'], + ]); - foreach ( $order->get_items() as $item_id => $item ) { + $postService = $factory->create('PostWithAesJsonResponseService'); - $Items[] = [ + $items = [] ; + + foreach ( $order->get_items() as $item_id => $item ) { + + $Items[] = [ 'ItemName' => mb_substr($item->get_name(), 0, 100), 'ItemCount' => $item->get_quantity(), 'ItemWord' => '批', 'ItemPrice' => round($item->get_total() / $item->get_quantity(), 4), - 'ItemTaxType' => '1', + 'ItemTaxType' => '1', 'ItemAmount' => round($item->get_total(), 2), - ] ; - } + ] ; + } - // 物流費用 - $shipping_fee = $order->get_shipping_total(); - if ($shipping_fee != 0) { + // 物流費用 + $shipping_fee = $order->get_shipping_total(); + if ($shipping_fee != 0) { - $Items[] = [ - 'ItemName' => __('Shipping fee', 'ecpay-ecommerce-for-woocommerce'), + $Items[] = [ + 'ItemName' => __('Shipping fee', 'ecpay-ecommerce-for-woocommerce'), 'ItemCount' => 1, 'ItemWord' => '批', - 'ItemPrice' => $shipping_fee, - 'ItemTaxType' => '1', - 'ItemAmount' => $shipping_fee, - ] ; - } + 'ItemPrice' => $shipping_fee, + 'ItemTaxType' => '1', + 'ItemAmount' => $shipping_fee, + ] ; + } $country = $order->get_billing_country(); $countries = WC()->countries->get_countries(); @@ -933,90 +1070,89 @@ public function invoice_create($order_id) $states = WC()->countries->get_states($country); $full_state = ($state && isset($states[$state])) ? $states[$state] : $state; - $data = [ - 'MerchantID' => $api_payment_info['merchant_id'], - 'RelateNumber' => $RelateNumber, - 'CustomerID' => '', - 'CustomerName' => $order->get_billing_last_name() . $order->get_billing_first_name(), - 'CustomerAddr' => $full_country . $full_state . $order->get_billing_city() . $order->get_billing_address_1() . $order->get_billing_address_2(), - 'CustomerPhone' => $order->get_billing_phone(), - 'CustomerEmail' => $order->get_billing_email(), - 'Print' => '0', - 'Donation' => '0', - 'LoveCode' => '', - 'CarrierType' => '', - 'CarrierNum' => '', - 'TaxType' => '1', - 'SalesAmount' => intval(round($order->get_total(), 0)), - 'Items' => $Items, - 'InvType' => '07', - - 'DelayFlag' => '1', - 'DelayDay' => $wooecpay_invoice_dalay_date, - 'Tsr' => $RelateNumber, - 'PayType' => '2', - 'PayAct' => 'ECPAY', - 'NotifyURL' => WC()->api_request_url('wooecpay_invoice_delay_issue_callback', true), - ]; + $data = [ + 'MerchantID' => $api_payment_info['merchant_id'], + 'RelateNumber' => $RelateNumber, + 'CustomerID' => '', + 'CustomerName' => $order->get_billing_last_name() . $order->get_billing_first_name(), + 'CustomerAddr' => $full_country . $full_state . $order->get_billing_city() . $order->get_billing_address_1() . $order->get_billing_address_2(), + 'CustomerPhone' => $order->get_billing_phone(), + 'CustomerEmail' => $order->get_billing_email(), + 'Print' => '0', + 'Donation' => '0', + 'LoveCode' => '', + 'CarrierType' => '', + 'CarrierNum' => '', + 'TaxType' => '1', + 'SalesAmount' => intval(round($order->get_total(), 0)), + 'Items' => $Items, + 'InvType' => '07', + + 'DelayFlag' => '1', + 'DelayDay' => $wooecpay_invoice_dalay_date, + 'Tsr' => $RelateNumber, + 'PayType' => '2', + 'PayAct' => 'ECPAY', + 'NotifyURL' => WC()->api_request_url('wooecpay_invoice_delay_issue_callback', true), + ]; - $wooecpay_invoice_type = get_post_meta( $order->get_id(), '_wooecpay_invoice_type', true ) ; - $wooecpay_invoice_carruer_type = get_post_meta( $order->get_id(), '_wooecpay_invoice_carruer_type', true ) ; - - switch ($wooecpay_invoice_type) { - - case 'p': - switch ($wooecpay_invoice_carruer_type ) { - - case '1': - $data['CarrierType'] = '1'; - break; - case '2': - $data['CarrierType'] = '2'; - $data['CarrierNum'] = get_post_meta( $order->get_id(), '_wooecpay_invoice_carruer_num', true ) ; - break; - case '3': - $data['CarrierType'] = '3'; - $data['CarrierNum'] = get_post_meta( $order->get_id(), '_wooecpay_invoice_carruer_num', true ) ; - break; - default: - $data['Print'] = '1'; - break; - } - - break; - - case 'c': - $data['Print'] = '1'; - $data['CustomerIdentifier'] = get_post_meta( $order->get_id(), '_wooecpay_invoice_customer_identifier', true ) ; - $company = $order->get_billing_company(); - if ($company) { - $data['CustomerName'] = $company; - } - break; - - case 'd': - $data['Donation'] = '1'; - $data['LoveCode'] = get_post_meta( $order->get_id(), '_wooecpay_invoice_love_code', true ) ; - break; - } + $wooecpay_invoice_type = get_post_meta( $order->get_id(), '_wooecpay_invoice_type', true ) ; + $wooecpay_invoice_carruer_type = get_post_meta( $order->get_id(), '_wooecpay_invoice_carruer_type', true ) ; + + switch ($wooecpay_invoice_type) { + + case 'p': + + switch ($wooecpay_invoice_carruer_type ) { + case '1': + $data['CarrierType'] = '1'; + break; + case '2': + $data['CarrierType'] = '2'; + $data['CarrierNum'] = get_post_meta( $order->get_id(), '_wooecpay_invoice_carruer_num', true ) ; + break; + case '3': + $data['CarrierType'] = '3'; + $data['CarrierNum'] = get_post_meta( $order->get_id(), '_wooecpay_invoice_carruer_num', true ) ; + break; + default: + $data['Print'] = '1'; + break; + } + break; - $input = [ - 'MerchantID' => $api_payment_info['merchant_id'], - 'RqHeader' => [ - 'Timestamp' => time(), - 'Revision' => '3.0.0', - ], - 'Data' => $data, - ]; + case 'c': + $data['Print'] = '1'; + $data['CustomerIdentifier'] = get_post_meta( $order->get_id(), '_wooecpay_invoice_customer_identifier', true ) ; + $company = $order->get_billing_company(); + if ($company) { + $data['CustomerName'] = $company; + } + break; + + case 'd': + $data['Donation'] = '1'; + $data['LoveCode'] = get_post_meta( $order->get_id(), '_wooecpay_invoice_love_code', true ) ; + break; + } - $response = $postService->post($input, $api_payment_info['action']); - + $input = [ + 'MerchantID' => $api_payment_info['merchant_id'], + 'RqHeader' => [ + 'Timestamp' => time(), + 'Revision' => '3.0.0', + ], + 'Data' => $data, + ]; + + $response = $postService->post($input, $api_payment_info['action']); + if($response['TransCode'] == 1){ - if($response['Data']['RtnCode'] == 1){ + if($response['Data']['RtnCode'] == 1){ - // 更新訂單 + // 更新訂單 $order->update_meta_data( '_wooecpay_invoice_relate_number', $RelateNumber ); $order->update_meta_data( '_wooecpay_invoice_RtnCode', $response['Data']['RtnCode'] ); $order->update_meta_data( '_wooecpay_invoice_RtnMsg', $response['Data']['RtnMsg'] ); @@ -1028,11 +1164,11 @@ public function invoice_create($order_id) $order->add_order_note('發票開立成功:狀態:' . $response['Data']['RtnMsg'] . '('. $response['Data']['RtnCode'] . ')'); $order->save(); - } else { + } else { - $order->add_order_note('發票開立失敗:狀態:' . $response['Data']['RtnMsg'] . '('. $response['Data']['RtnCode'] . ')'); + $order->add_order_note('發票開立失敗:狀態:' . $response['Data']['RtnMsg'] . '('. $response['Data']['RtnCode'] . ')'); $order->save(); - } + } } else { @@ -1040,13 +1176,13 @@ public function invoice_create($order_id) $order->save(); } - $ajaxReturn = [ + $ajaxReturn = [ 'code' => '9999', 'msg' => '成功', ]; } catch (RtnException $e) { - echo wp_kses_post( '(' . $e->getCode() . ')' . $e->getMessage() ) . PHP_EOL; + echo wp_kses_post( '(' . $e->getCode() . ')' . $e->getMessage() ) . PHP_EOL; } } } @@ -1086,33 +1222,36 @@ public function invoice_invalid($order_id) // 作廢發票 try { - $factory = new Factory([ - 'hashKey' => $api_payment_info['hashKey'], - 'hashIv' => $api_payment_info['hashIv'], - ]); - $postService = $factory->create('PostWithAesJsonResponseService'); - $data = [ - 'MerchantID' => $api_payment_info['merchant_id'], - 'InvoiceNo' => $wooecpay_invoice_no, - 'InvoiceDate' => $wooecpay_invoice_date, - 'Reason' => __('Invalid invoice', 'ecpay-ecommerce-for-woocommerce'), - ]; - $input = [ - 'MerchantID' => $api_payment_info['merchant_id'], - 'RqHeader' => [ - 'Timestamp' => time(), - 'Revision' => '3.0.0', - ], - 'Data' => $data, - ]; - - $response = $postService->post($input, $api_payment_info['action']); + $factory = new Factory([ + 'hashKey' => $api_payment_info['hashKey'], + 'hashIv' => $api_payment_info['hashIv'], + ]); - // var_dump($response); - if($response['Data']['RtnCode'] == 1 || $response['Data']['RtnCode'] == 5070453){ + $postService = $factory->create('PostWithAesJsonResponseService'); - // 更新訂單 + $data = [ + 'MerchantID' => $api_payment_info['merchant_id'], + 'InvoiceNo' => $wooecpay_invoice_no, + 'InvoiceDate' => $wooecpay_invoice_date, + 'Reason' => __('Invalid invoice', 'ecpay-ecommerce-for-woocommerce'), + ]; + + $input = [ + 'MerchantID' => $api_payment_info['merchant_id'], + 'RqHeader' => [ + 'Timestamp' => time(), + 'Revision' => '3.0.0', + ], + 'Data' => $data, + ]; + + $response = $postService->post($input, $api_payment_info['action']); + + // var_dump($response); + if($response['Data']['RtnCode'] == 1 || $response['Data']['RtnCode'] == 5070453){ + + // 更新訂單 $order->update_meta_data( '_wooecpay_invoice_relate_number', ''); $order->update_meta_data( '_wooecpay_invoice_RtnCode', ''); $order->update_meta_data( '_wooecpay_invoice_RtnMsg', '' ); @@ -1125,12 +1264,12 @@ public function invoice_invalid($order_id) $order->add_order_note('發票作廢成功: 發票號碼:' .$wooecpay_invoice_no . ' 狀態:' . $response['Data']['RtnMsg'] . '('. $response['Data']['RtnCode'] . ')'); $order->save(); + } else { - $order->add_order_note('發票作廢失敗:狀態:' . $response['Data']['RtnMsg'] . '('. $response['Data']['RtnCode'] . ')'); + $order->add_order_note('發票作廢失敗:狀態:' . $response['Data']['RtnMsg'] . '('. $response['Data']['RtnCode'] . ')'); $order->save(); - } - + } } catch (RtnException $e) { echo wp_kses_post( '(' . $e->getCode() . ')' . $e->getMessage() ) . PHP_EOL; @@ -1142,29 +1281,32 @@ public function invoice_invalid($order_id) $wooecpay_invoice_tsr = get_post_meta( $order->get_id(), '_wooecpay_invoice_tsr', true ) ; try { - $factory = new Factory([ - 'hashKey' => $api_payment_info['hashKey'], - 'hashIv' => $api_payment_info['hashIv'], - ]); - $postService = $factory->create('PostWithAesJsonResponseService'); - $data = [ - 'MerchantID' => $api_payment_info['merchant_id'], - 'Tsr' => $wooecpay_invoice_tsr, - ]; - $input = [ - 'MerchantID' => $api_payment_info['merchant_id'], - 'RqHeader' => [ - 'Timestamp' => time(), - 'Revision' => '3.0.0', - ], - 'Data' => $data, - ]; - $response = $postService->post($input, $api_payment_info['action']); - - if($response['Data']['RtnCode'] == 1){ + $factory = new Factory([ + 'hashKey' => $api_payment_info['hashKey'], + 'hashIv' => $api_payment_info['hashIv'], + ]); - // 更新訂單 + $postService = $factory->create('PostWithAesJsonResponseService'); + $data = [ + 'MerchantID' => $api_payment_info['merchant_id'], + 'Tsr' => $wooecpay_invoice_tsr, + ]; + + $input = [ + 'MerchantID' => $api_payment_info['merchant_id'], + 'RqHeader' => [ + 'Timestamp' => time(), + 'Revision' => '3.0.0', + ], + 'Data' => $data, + ]; + + $response = $postService->post($input, $api_payment_info['action']); + + if($response['Data']['RtnCode'] == 1){ + + // 更新訂單 $order->update_meta_data( '_wooecpay_invoice_relate_number', ''); $order->update_meta_data( '_wooecpay_invoice_RtnCode', ''); $order->update_meta_data( '_wooecpay_invoice_RtnMsg', '' ); @@ -1179,400 +1321,438 @@ public function invoice_invalid($order_id) $order->add_order_note('發票作廢成功: 交易單號:' .$wooecpay_invoice_tsr . ' 狀態:' . $response['Data']['RtnMsg'] . '('. $response['Data']['RtnCode'] . ')'); $order->save(); + } else { - $order->add_order_note('發票作廢失敗:狀態:' . $response['Data']['RtnMsg'] . '('. $response['Data']['RtnCode'] . ')'); + $order->add_order_note('發票作廢失敗:狀態:' . $response['Data']['RtnMsg'] . '('. $response['Data']['RtnCode'] . ')'); $order->save(); - } + } } catch (RtnException $e) { - echo wp_kses_post( '(' . $e->getCode() . ')' . $e->getMessage() ) . PHP_EOL; + echo wp_kses_post( '(' . $e->getCode() . ')' . $e->getMessage() ) . PHP_EOL; } - } } } } // logistic - // --------------------------------------------------- + // --------------------------------------------------- - protected function get_ecpay_logistic_api_info($action = '', $shipping_method_id = '' ) - { - $wooecpay_logistic_cvs_type = get_option('wooecpay_logistic_cvs_type'); + protected function get_ecpay_logistic_api_info($action = '', $shipping_method_id = '' ) + { + $wooecpay_logistic_cvs_type = get_option('wooecpay_logistic_cvs_type'); - $api_info = [ - 'merchant_id' => '', - 'hashKey' => '', - 'hashIv' => '', - 'action' => '', - ] ; + $api_info = [ + 'merchant_id' => '', + 'hashKey' => '', + 'hashIv' => '', + 'action' => '', + ] ; - if ('yes' === get_option('wooecpay_enabled_logistic_stage', 'yes')) { + if ('yes' === get_option('wooecpay_enabled_logistic_stage', 'yes')) { - if($wooecpay_logistic_cvs_type == 'C2C'){ + if($wooecpay_logistic_cvs_type == 'C2C'){ - $api_info = [ - 'merchant_id' => '2000933', - 'hashKey' => 'XBERn1YOvpM9nfZc', - 'hashIv' => 'h1ONHk4P4yqbl5LK', - ] ; + $api_info = [ + 'merchant_id' => '2000933', + 'hashKey' => 'XBERn1YOvpM9nfZc', + 'hashIv' => 'h1ONHk4P4yqbl5LK', + ] ; - } else if($wooecpay_logistic_cvs_type == 'B2C'){ + } else if($wooecpay_logistic_cvs_type == 'B2C'){ - $api_info = [ - 'merchant_id' => '2000132', - 'hashKey' => '5294y06JbISpM5x9', - 'hashIv' => 'v77hoKGq4kWxNNIS', - ] ; - } + $api_info = [ + 'merchant_id' => '2000132', + 'hashKey' => '5294y06JbISpM5x9', + 'hashIv' => 'v77hoKGq4kWxNNIS', + ] ; + } + + } else { + + $merchant_id = get_option('wooecpay_logistic_mid'); + $hash_key = get_option('wooecpay_logistic_hashkey'); + $hash_iv = get_option('wooecpay_logistic_hashiv'); + + $api_info = [ + 'merchant_id' => $merchant_id, + 'hashKey' => $hash_key, + 'hashIv' => $hash_iv, + ] ; + } - } else { - - $merchant_id = get_option('wooecpay_logistic_mid'); - $hash_key = get_option('wooecpay_logistic_hashkey'); - $hash_iv = get_option('wooecpay_logistic_hashiv'); - - $api_info = [ - 'merchant_id' => $merchant_id, - 'hashKey' => $hash_key, - 'hashIv' => $hash_iv, - ] ; - } + // URL位置判斷 + if ('yes' === get_option('wooecpay_enabled_logistic_stage', 'yes')) { + + switch ($action) { + + case 'map': + $api_info['action'] = 'https://logistics-stage.ecpay.com.tw/Express/map' ; + break; + + case 'create': + $api_info['action'] = 'https://logistics-stage.ecpay.com.tw/Express/Create' ; + break; + + case 'print': + + if($wooecpay_logistic_cvs_type == 'C2C'){ + + switch ($shipping_method_id) { + + case 'Wooecpay_Logistic_CVS_711': + $api_info['action'] = 'https://logistics-stage.ecpay.com.tw/Express/PrintUniMartC2COrderInfo' ; + break; + + case 'Wooecpay_Logistic_CVS_Family': + $api_info['action'] = 'https://logistics-stage.ecpay.com.tw/Express/PrintFAMIC2COrderInfo' ; + break; + + case 'Wooecpay_Logistic_CVS_Hilife': + $api_info['action'] = 'https://logistics-stage.ecpay.com.tw/Express/PrintHILIFEC2COrderInfo' ; + break; + + case 'Wooecpay_Logistic_CVS_Okmart': + $api_info['action'] = 'https://logistics-stage.ecpay.com.tw/Express/PrintOKMARTC2COrderInfo' ; + break; + + case 'Wooecpay_Logistic_Home_Tcat': + case 'Wooecpay_Logistic_Home_Ecan': + $api_info['action'] = 'https://logistics-stage.ecpay.com.tw/helper/printTradeDocument' ; + break; + + default: + $api_info['action'] = '' ; + break; + } + + } else if($wooecpay_logistic_cvs_type == 'B2C'){ + + switch ($shipping_method_id) { + + case 'Wooecpay_Logistic_CVS_711': + case 'Wooecpay_Logistic_CVS_Family': + case 'Wooecpay_Logistic_CVS_Hilife': + case 'Wooecpay_Logistic_Home_Tcat': + case 'Wooecpay_Logistic_Home_Ecan': + $api_info['action'] = 'https://logistics-stage.ecpay.com.tw/helper/printTradeDocument' ; + break; + default: + $api_info['action'] = '' ; + break; + } + } + + break; + + default: + break; + } + + } else { + + switch ($action) { + + case 'map': + $api_info['action'] = 'https://logistics.ecpay.com.tw/Express/map' ; + break; + + case 'create': + $api_info['action'] = 'https://logistics.ecpay.com.tw/Express/Create' ; + break; + + case 'print': + + if($wooecpay_logistic_cvs_type == 'C2C'){ + + switch ($shipping_method_id) { + + case 'Wooecpay_Logistic_CVS_711': + $api_info['action'] = 'https://logistics.ecpay.com.tw/Express/PrintUniMartC2COrderInfo' ; + break; + + case 'Wooecpay_Logistic_CVS_Family': + $api_info['action'] = 'https://logistics.ecpay.com.tw/Express/PrintFAMIC2COrderInfo' ; + break; + + case 'Wooecpay_Logistic_CVS_Hilife': + $api_info['action'] = 'https://logistics.ecpay.com.tw/Express/PrintHILIFEC2COrderInfo' ; + break; + + case 'Wooecpay_Logistic_CVS_Okmart': + $api_info['action'] = 'https://logistics.ecpay.com.tw/Express/PrintOKMARTC2COrderInfo' ; + break; + + case 'Wooecpay_Logistic_Home_Tcat': + case 'Wooecpay_Logistic_Home_Ecan': + $api_info['action'] = 'https://logistics.ecpay.com.tw/helper/printTradeDocument' ; + break; + + default: + $api_info['action'] = '' ; + break; + } + + } else if($wooecpay_logistic_cvs_type == 'B2C'){ + + switch ($shipping_method_id) { + + case 'Wooecpay_Logistic_CVS_711': + case 'Wooecpay_Logistic_CVS_Family': + case 'Wooecpay_Logistic_CVS_Hilife': + case 'Wooecpay_Logistic_Home_Tcat': + case 'Wooecpay_Logistic_Home_Ecan': + $api_info['action'] = 'https://logistics.ecpay.com.tw/helper/printTradeDocument' ; + break; + default: + $api_info['action'] = '' ; + break; + } + } + + break; + default: + break; + } + } - // URL位置判斷 - if ('yes' === get_option('wooecpay_enabled_logistic_stage', 'yes')) { - - switch ($action) { - - case 'map': - $api_info['action'] = 'https://logistics-stage.ecpay.com.tw/Express/map' ; - break; - - case 'create': - $api_info['action'] = 'https://logistics-stage.ecpay.com.tw/Express/Create' ; - break; - - case 'print': - - if($wooecpay_logistic_cvs_type == 'C2C'){ - - switch ($shipping_method_id) { - - case 'Wooecpay_Logistic_CVS_711': - $api_info['action'] = 'https://logistics-stage.ecpay.com.tw/Express/PrintUniMartC2COrderInfo' ; - break; - - case 'Wooecpay_Logistic_CVS_Family': - $api_info['action'] = 'https://logistics-stage.ecpay.com.tw/Express/PrintFAMIC2COrderInfo' ; - break; - - case 'Wooecpay_Logistic_CVS_Hilife': - $api_info['action'] = 'https://logistics-stage.ecpay.com.tw/Express/PrintHILIFEC2COrderInfo' ; - break; - - case 'Wooecpay_Logistic_CVS_Okmart': - $api_info['action'] = 'https://logistics-stage.ecpay.com.tw/Express/PrintOKMARTC2COrderInfo' ; - break; - - case 'Wooecpay_Logistic_Home_Tcat': - case 'Wooecpay_Logistic_Home_Ecan': - $api_info['action'] = 'https://logistics-stage.ecpay.com.tw/helper/printTradeDocument' ; - break; - - default: - $api_info['action'] = '' ; - break; - } - - } else if($wooecpay_logistic_cvs_type == 'B2C'){ - - switch ($shipping_method_id) { - - case 'Wooecpay_Logistic_CVS_711': - case 'Wooecpay_Logistic_CVS_Family': - case 'Wooecpay_Logistic_CVS_Hilife': - case 'Wooecpay_Logistic_Home_Tcat': - case 'Wooecpay_Logistic_Home_Ecan': - $api_info['action'] = 'https://logistics-stage.ecpay.com.tw/helper/printTradeDocument' ; - break; - default: - $api_info['action'] = '' ; - break; - } - } - - break; - - default: - break; - } + return $api_info; + } - } else { - - switch ($action) { - - case 'map': - $api_info['action'] = 'https://logistics.ecpay.com.tw/Express/map' ; - break; - - case 'create': - $api_info['action'] = 'https://logistics.ecpay.com.tw/Express/Create' ; - break; - - case 'print': - - if($wooecpay_logistic_cvs_type == 'C2C'){ - - switch ($shipping_method_id) { - - case 'Wooecpay_Logistic_CVS_711': - $api_info['action'] = 'https://logistics.ecpay.com.tw/Express/PrintUniMartC2COrderInfo' ; - break; - - case 'Wooecpay_Logistic_CVS_Family': - $api_info['action'] = 'https://logistics.ecpay.com.tw/Express/PrintFAMIC2COrderInfo' ; - break; - - case 'Wooecpay_Logistic_CVS_Hilife': - $api_info['action'] = 'https://logistics.ecpay.com.tw/Express/PrintHILIFEC2COrderInfo' ; - break; - - case 'Wooecpay_Logistic_CVS_Okmart': - $api_info['action'] = 'https://logistics.ecpay.com.tw/Express/PrintOKMARTC2COrderInfo' ; - break; - - case 'Wooecpay_Logistic_Home_Tcat': - case 'Wooecpay_Logistic_Home_Ecan': - $api_info['action'] = 'https://logistics.ecpay.com.tw/helper/printTradeDocument' ; - break; - - default: - $api_info['action'] = '' ; - break; - } - - } else if($wooecpay_logistic_cvs_type == 'B2C'){ - - switch ($shipping_method_id) { - - case 'Wooecpay_Logistic_CVS_711': - case 'Wooecpay_Logistic_CVS_Family': - case 'Wooecpay_Logistic_CVS_Hilife': - case 'Wooecpay_Logistic_Home_Tcat': - case 'Wooecpay_Logistic_Home_Ecan': - $api_info['action'] = 'https://logistics.ecpay.com.tw/helper/printTradeDocument' ; - break; - default: - $api_info['action'] = '' ; - break; - } - } - - break; - - default: - break; - } - } + protected function get_merchant_trade_no($order_id, $order_prefix = '') + { + $trade_no = $order_prefix . $order_id . 'SN' .(string) time() ; + return substr($trade_no, 0, 20); + } - return $api_info; - } + protected function get_logistics_sub_type($shipping_method_id) + { + $wooecpay_logistic_cvs_type = get_option('wooecpay_logistic_cvs_type'); - protected function get_merchant_trade_no($order_id, $order_prefix = '') - { - $trade_no = $order_prefix . $order_id . 'SN' .(string) time() ; - return substr($trade_no, 0, 20); - } + $logisticsType = [ + 'type' => '', + 'sub_type' => '', + ] ; - protected function get_logistics_sub_type($shipping_method_id) - { - $wooecpay_logistic_cvs_type = get_option('wooecpay_logistic_cvs_type'); + switch ($shipping_method_id) { - $logisticsType = [ - 'type' => '', - 'sub_type' => '', - ] ; - - switch ($shipping_method_id) { - case 'Wooecpay_Logistic_CVS_711': + case 'Wooecpay_Logistic_CVS_711': - $logisticsType['type'] = 'CVS' ; + $logisticsType['type'] = 'CVS' ; - if($wooecpay_logistic_cvs_type == 'C2C'){ - $logisticsType['sub_type'] = 'UNIMARTC2C' ; - } else if($wooecpay_logistic_cvs_type == 'B2C'){ - $logisticsType['sub_type'] = 'UNIMART' ; - } + if($wooecpay_logistic_cvs_type == 'C2C'){ + $logisticsType['sub_type'] = 'UNIMARTC2C' ; + } else if($wooecpay_logistic_cvs_type == 'B2C'){ + $logisticsType['sub_type'] = 'UNIMART' ; + } - break; - case 'Wooecpay_Logistic_CVS_Family': - - $logisticsType['type'] = 'CVS' ; + break; - if($wooecpay_logistic_cvs_type == 'C2C'){ - $logisticsType['sub_type'] = 'FAMIC2C' ; - } else if($wooecpay_logistic_cvs_type == 'B2C'){ - $logisticsType['sub_type'] = 'FAMI' ; - } + case 'Wooecpay_Logistic_CVS_Family': + + $logisticsType['type'] = 'CVS' ; + if($wooecpay_logistic_cvs_type == 'C2C'){ + $logisticsType['sub_type'] = 'FAMIC2C' ; + } else if($wooecpay_logistic_cvs_type == 'B2C'){ + $logisticsType['sub_type'] = 'FAMI' ; + } - break; - case 'Wooecpay_Logistic_CVS_Hilife': + break; - $logisticsType['type'] = 'CVS' ; + case 'Wooecpay_Logistic_CVS_Hilife': - if($wooecpay_logistic_cvs_type == 'C2C'){ - $logisticsType['sub_type'] = 'HILIFEC2C' ; - } else if($wooecpay_logistic_cvs_type == 'B2C'){ - $logisticsType['sub_type'] = 'HILIFE' ; - } + $logisticsType['type'] = 'CVS' ; - break; - case 'Wooecpay_Logistic_CVS_Okmart': + if($wooecpay_logistic_cvs_type == 'C2C'){ + $logisticsType['sub_type'] = 'HILIFEC2C' ; + } else if($wooecpay_logistic_cvs_type == 'B2C'){ + $logisticsType['sub_type'] = 'HILIFE' ; + } - $logisticsType['type'] = 'CVS' ; + break; - if($wooecpay_logistic_cvs_type == 'C2C'){ - $logisticsType['sub_type'] = 'OKMARTC2C' ; - } + case 'Wooecpay_Logistic_CVS_Okmart': - break; + $logisticsType['type'] = 'CVS' ; + if($wooecpay_logistic_cvs_type == 'C2C'){ + $logisticsType['sub_type'] = 'OKMARTC2C' ; + } - case 'Wooecpay_Logistic_Home_Tcat': - $logisticsType['type'] = 'HOME' ; - $logisticsType['sub_type'] = 'TCAT' ; - break; + break; - case 'Wooecpay_Logistic_Home_Ecan': - $logisticsType['type'] = 'HOME' ; - $logisticsType['sub_type'] = 'ECAN' ; - break; - } + case 'Wooecpay_Logistic_Home_Tcat': + $logisticsType['type'] = 'HOME' ; + $logisticsType['sub_type'] = 'TCAT' ; + break; - return $logisticsType; + case 'Wooecpay_Logistic_Home_Ecan': + $logisticsType['type'] = 'HOME' ; + $logisticsType['sub_type'] = 'ECAN' ; + break; } - protected function get_item_name($order) - { - $item_name = ''; + return $logisticsType; + } - if ( count($order->get_items()) ) { - foreach ($order->get_items() as $item) { - $item_name .= trim($item->get_name()) . ' ' ; - } - } + protected function get_item_name($order) + { + $item_name = ''; - return $item_name; + if ( count($order->get_items()) ) { + foreach ($order->get_items() as $item) { + $item_name .= trim($item->get_name()) . ' ' ; + } } - // invoice - // --------------------------------------------------- + return $item_name; + } + + // invoice + // --------------------------------------------------- protected function get_ecpay_invoice_api_info($action = '') - { - $api_info = [ - 'merchant_id' => '', - 'hashKey' => '', - 'hashIv' => '', - 'action' => '', - ] ; + { + $api_info = [ + 'merchant_id' => '', + 'hashKey' => '', + 'hashIv' => '', + 'action' => '', + ] ; + + if ('yes' === get_option('wooecpay_enabled_invoice_stage', 'yes')) { + + $api_info = [ + 'merchant_id' => '2000132', + 'hashKey' => 'ejCk326UnaZWKisg ', + 'hashIv' => 'q9jcZX8Ib9LM8wYk', + ] ; + + } else { + + $merchant_id = get_option('wooecpay_invoice_mid'); + $hash_key = get_option('wooecpay_invoice_hashkey'); + $hash_iv = get_option('wooecpay_invoice_hashiv'); + + $api_info = [ + 'merchant_id' => $merchant_id, + 'hashKey' => $hash_key, + 'hashIv' => $hash_iv, + ] ; + } - if ('yes' === get_option('wooecpay_enabled_invoice_stage', 'yes')) { + if ('yes' === get_option('wooecpay_enabled_invoice_stage', 'yes')) { - $api_info = [ - 'merchant_id' => '2000132', - 'hashKey' => 'ejCk326UnaZWKisg ', - 'hashIv' => 'q9jcZX8Ib9LM8wYk', - ] ; + switch ($action) { - } else { - - $merchant_id = get_option('wooecpay_invoice_mid'); - $hash_key = get_option('wooecpay_invoice_hashkey'); - $hash_iv = get_option('wooecpay_invoice_hashiv'); - - $api_info = [ - 'merchant_id' => $merchant_id, - 'hashKey' => $hash_key, - 'hashIv' => $hash_iv, - ] ; - } + case 'check_Love_code': + $api_info['action'] = 'https://einvoice-stage.ecpay.com.tw/B2CInvoice/CheckLoveCode' ; + break; - if ('yes' === get_option('wooecpay_enabled_invoice_stage', 'yes')) { + case 'check_barcode': + $api_info['action'] = 'https://einvoice-stage.ecpay.com.tw/B2CInvoice/CheckBarcode' ; + break; - switch ($action) { + case 'issue': + $api_info['action'] = 'https://einvoice-stage.ecpay.com.tw/B2CInvoice/Issue' ; + break; - case 'check_Love_code': - $api_info['action'] = 'https://einvoice-stage.ecpay.com.tw/B2CInvoice/CheckLoveCode' ; - break; + case 'delay_issue': + $api_info['action'] = 'https://einvoice-stage.ecpay.com.tw/B2CInvoice/DelayIssue' ; + break; - case 'check_barcode': - $api_info['action'] = 'https://einvoice-stage.ecpay.com.tw/B2CInvoice/CheckBarcode' ; - break; + case 'invalid': + $api_info['action'] = 'https://einvoice-stage.ecpay.com.tw/B2CInvoice/Invalid' ; + break; - case 'issue': - $api_info['action'] = 'https://einvoice-stage.ecpay.com.tw/B2CInvoice/Issue' ; - break; + case 'cancel_delay_issue': + $api_info['action'] = 'https://einvoice-stage.ecpay.com.tw/B2CInvoice/CancelDelayIssue' ; + break; - case 'delay_issue': - $api_info['action'] = 'https://einvoice-stage.ecpay.com.tw/B2CInvoice/DelayIssue' ; - break; + default: + break; + } - case 'invalid': - $api_info['action'] = 'https://einvoice-stage.ecpay.com.tw/B2CInvoice/Invalid' ; - break; + } else { - case 'cancel_delay_issue': - $api_info['action'] = 'https://einvoice-stage.ecpay.com.tw/B2CInvoice/CancelDelayIssue' ; - break; + switch ($action) { - default: - break; - } + case 'check_Love_code': + $api_info['action'] = 'https://einvoice.ecpay.com.tw/B2CInvoice/CheckLoveCode' ; + break; + + case 'check_barcode': + $api_info['action'] = 'https://einvoice.ecpay.com.tw/B2CInvoice/CheckBarcode' ; + break; - } else { + case 'issue': + $api_info['action'] = 'https://einvoice.ecpay.com.tw/B2CInvoice/Issue' ; + break; - switch ($action) { + case 'delay_issue': + $api_info['action'] = 'https://einvoice.ecpay.com.tw/B2CInvoice/DelayIssue' ; + break; + + case 'invalid': + $api_info['action'] = 'https://einvoice.ecpay.com.tw/B2CInvoice/Invalid' ; + break; - case 'check_Love_code': - $api_info['action'] = 'https://einvoice.ecpay.com.tw/B2CInvoice/CheckLoveCode' ; - break; - - case 'check_barcode': - $api_info['action'] = 'https://einvoice.ecpay.com.tw/B2CInvoice/CheckBarcode' ; - break; - - case 'issue': - $api_info['action'] = 'https://einvoice.ecpay.com.tw/B2CInvoice/Issue' ; - break; - - case 'delay_issue': - $api_info['action'] = 'https://einvoice.ecpay.com.tw/B2CInvoice/DelayIssue' ; - break; - - case 'invalid': - $api_info['action'] = 'https://einvoice.ecpay.com.tw/B2CInvoice/Invalid' ; - break; - - case 'cancel_delay_issue': - $api_info['action'] = 'https://einvoice.ecpay.com.tw/B2CInvoice/CancelDelayIssue' ; - break; - - default: - break; - } - } + case 'cancel_delay_issue': + $api_info['action'] = 'https://einvoice.ecpay.com.tw/B2CInvoice/CancelDelayIssue' ; + break; - return $api_info; + default: + break; + } } + return $api_info; + } + protected function get_relate_number($order_id, $order_prefix = '') - { - $relate_no = $order_prefix . $order_id . 'SN' .(string) time() ; - return substr($relate_no, 0, 20); - } + { + $relate_no = $order_prefix . $order_id . 'SN' .(string) time() ; + return substr($relate_no, 0, 20); + } + + // payment + // --------------------------------------------------- + protected function get_ecpay_payment_api_query_trade_info() + { + $api_payment_info = [ + 'merchant_id' => '', + 'hashKey' => '', + 'hashIv' => '', + 'action' => '', + ] ; + + if ('yes' === get_option('wooecpay_enabled_payment_stage', 'yes')) { + + $api_payment_info = [ + 'merchant_id' => '3002607', + 'hashKey' => 'pwFHCqoQZGmho4w6', + 'hashIv' => 'EkRm7iFT261dpevs', + 'action' => 'https://payment-stage.ecpay.com.tw/Cashier/QueryTradeInfo/V5', + ] ; + + } else { + + $merchant_id = get_option('wooecpay_payment_mid'); + $hash_key = get_option('wooecpay_payment_hashkey'); + $hash_iv = get_option('wooecpay_payment_hashiv'); + + $api_payment_info = [ + 'merchant_id' => $merchant_id, + 'hashKey' => $hash_key, + 'hashIv' => $hash_iv, + 'action' => 'https://payment.ecpay.com.tw/Cashier/QueryTradeInfo/V5', + ] ; + } + + return $api_payment_info; + } } diff --git a/ecpay-ecommerce-for-woocommerce/admin/settings/class-wooecpay-setting-main.php b/ecpay-ecommerce-for-woocommerce/admin/settings/class-wooecpay-setting-main.php index 820eb2c..132b976 100644 --- a/ecpay-ecommerce-for-woocommerce/admin/settings/class-wooecpay-setting-main.php +++ b/ecpay-ecommerce-for-woocommerce/admin/settings/class-wooecpay-setting-main.php @@ -52,6 +52,7 @@ public function output() { if ( $current_section == '' ) { wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=wooecpay_setting§ion=ecpay_main' ) ); + exit; } $settings = $this->get_settings( $current_section ); diff --git a/ecpay-ecommerce-for-woocommerce/ecpay-ecommerce-for-woocommerce.php b/ecpay-ecommerce-for-woocommerce/ecpay-ecommerce-for-woocommerce.php index 7099135..df0dfbe 100644 --- a/ecpay-ecommerce-for-woocommerce/ecpay-ecommerce-for-woocommerce.php +++ b/ecpay-ecommerce-for-woocommerce/ecpay-ecommerce-for-woocommerce.php @@ -6,7 +6,7 @@ * Plugin Name: ECPay Ecommerce for WooCommerce * Plugin URI: https://www.ecpay.com.tw * Description: Ecpay Plug for WooCommerce - * Version: 1.0.220728 + * Version: 1.0.221019 * Author: ECPay Green World FinTech Service Co., Ltd. * Author URI: https://www.ecpay.com.tw * License: GPLv2 @@ -20,7 +20,7 @@ defined( 'ABSPATH' ) or exit; // 相關常數定義 -define( 'WOOECPAY_VERSION', '1.0.220728' ); +define( 'WOOECPAY_VERSION', '1.0.220714' ); define( 'WOOECPAY_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); define( 'WOOECPAY_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); define( 'WOOECPAY_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); diff --git a/ecpay-ecommerce-for-woocommerce/includes/config/logistic/settings.php b/ecpay-ecommerce-for-woocommerce/includes/config/logistic/settings.php index 4be692b..c80ae50 100644 --- a/ecpay-ecommerce-for-woocommerce/includes/config/logistic/settings.php +++ b/ecpay-ecommerce-for-woocommerce/includes/config/logistic/settings.php @@ -12,6 +12,14 @@ 'desc' => __('Only letters and numbers allowed.', 'ecpay-ecommerce-for-woocommerce'), 'desc_tip' => true ], + [ + 'title' => __( 'Display order item name', 'ecpay-ecommerce-for-woocommerce' ), + 'id' => 'wooecpay_enabled_logistic_disp_item_name', + 'type' => 'checkbox', + 'default' => 'no', + 'desc' => __( 'Display order item name', 'ecpay-ecommerce-for-woocommerce' ), + 'desc_tip' => false + ], [ 'title' => __('CVS type', 'ecpay-ecommerce-for-woocommerce'), 'id' => 'wooecpay_logistic_cvs_type', diff --git a/ecpay-ecommerce-for-woocommerce/includes/config/main/settings.php b/ecpay-ecommerce-for-woocommerce/includes/config/main/settings.php index 8f8e9ca..f2c3e87 100644 --- a/ecpay-ecommerce-for-woocommerce/includes/config/main/settings.php +++ b/ecpay-ecommerce-for-woocommerce/includes/config/main/settings.php @@ -11,7 +11,7 @@ 'name' => __( 'Enable ECPay gateway method', 'ecpay-ecommerce-for-woocommerce'), 'type' => 'checkbox', 'desc' => __( 'Enable ECPay gateway method', 'ecpay-ecommerce-for-woocommerce'), - 'id' => 'wooecpay_ecpay_enabled_payment', + 'id' => 'wooecpay_enabled_payment', 'default' => 'no', 'desc_tip' => true, ], @@ -20,7 +20,7 @@ 'name' => __( 'Enable ECPay shipping method', 'ecpay-ecommerce-for-woocommerce'), 'type' => 'checkbox', 'desc' => __( 'Enable ECPay shipping method', 'ecpay-ecommerce-for-woocommerce'), - 'id' => 'wooecpay_ecpay_enabled_logistic', + 'id' => 'wooecpay_enabled_logistic', 'default' => 'no', 'desc_tip' => true, ], @@ -29,7 +29,7 @@ 'name' => __( 'Enable ECPay invoice method', 'ecpay-ecommerce-for-woocommerce'), 'type' => 'checkbox', 'desc' => __( 'Enable ECPay invoice method', 'ecpay-ecommerce-for-woocommerce'), - 'id' => 'wooecpay_ecpay_enabled_invoice', + 'id' => 'wooecpay_enabled_invoice', 'default' => 'no', 'desc_tip' => true, ], diff --git a/ecpay-ecommerce-for-woocommerce/includes/services/logistic/class-wooecpay-logistic.php b/ecpay-ecommerce-for-woocommerce/includes/services/logistic/class-wooecpay-logistic.php index 226d767..83c64aa 100644 --- a/ecpay-ecommerce-for-woocommerce/includes/services/logistic/class-wooecpay-logistic.php +++ b/ecpay-ecommerce-for-woocommerce/includes/services/logistic/class-wooecpay-logistic.php @@ -69,8 +69,8 @@ function gateway_disable_for_shipping_rate( $available_gateways ) { if ( ! is_admin() ) { - $chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); - $chosen_shipping = $chosen_methods[0]; + $chosen_shipping_tmp = wc_get_chosen_shipping_method_ids(); + $chosen_shipping = $chosen_shipping_tmp[0] ; if(!empty($chosen_shipping)){ @@ -89,6 +89,7 @@ function gateway_disable_for_shipping_rate( $available_gateways ) { $key == 'Wooecpay_Gateway_Atm' || $key == 'Wooecpay_Gateway_Cvs' || $key == 'Wooecpay_Gateway_Barcode' || + $key == 'Wooecpay_Gateway_Applepay' || $key == 'cod' ){ diff --git a/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-cvs-711.php b/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-cvs-711.php index eccd0c6..84e7b3e 100644 --- a/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-cvs-711.php +++ b/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-cvs-711.php @@ -24,4 +24,18 @@ public function __construct($instance_id = 0) parent::__construct(); } + + public function is_available($package) + { + $is_available = true; + + $total = WC()->cart->cart_contents_total ; + + // 金額超過2萬元時不顯示綠界超商物流 + if($total >= 20000){ + $is_available = false ; + } + + return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package, $this); + } } \ No newline at end of file diff --git a/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-cvs-family.php b/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-cvs-family.php index dc3c9c0..73eb00c 100644 --- a/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-cvs-family.php +++ b/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-cvs-family.php @@ -21,4 +21,18 @@ public function __construct($instance_id = 0) parent::__construct(); } + + public function is_available($package) + { + $is_available = true; + + $total = WC()->cart->cart_contents_total ; + + // 金額超過2萬元時不顯示綠界超商物流 + if($total >= 20000){ + $is_available = false ; + } + + return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package, $this); + } } \ No newline at end of file diff --git a/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-cvs-hilife.php b/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-cvs-hilife.php index 76aafde..4d8d5d5 100644 --- a/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-cvs-hilife.php +++ b/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-cvs-hilife.php @@ -21,4 +21,18 @@ public function __construct($instance_id = 0) parent::__construct(); } + + public function is_available($package) + { + $is_available = true; + + $total = WC()->cart->cart_contents_total ; + + // 金額超過2萬元時不顯示綠界超商物流 + if($total >= 20000){ + $is_available = false ; + } + + return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package, $this); + } } \ No newline at end of file diff --git a/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-cvs-okmart.php b/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-cvs-okmart.php index e3d6a77..cd1b21d 100644 --- a/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-cvs-okmart.php +++ b/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-cvs-okmart.php @@ -24,4 +24,18 @@ public function __construct($instance_id = 0) parent::__construct(); } + + public function is_available($package) + { + $is_available = true; + + $total = WC()->cart->cart_contents_total ; + + // 金額超過2萬元時不顯示綠界超商物流 + if($total >= 20000){ + $is_available = false ; + } + + return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package, $this); + } } \ No newline at end of file diff --git a/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-response.php b/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-response.php index da4af76..591cbf4 100644 --- a/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-response.php +++ b/ecpay-ecommerce-for-woocommerce/includes/services/logistic/ecpay-logistic-response.php @@ -103,7 +103,8 @@ public function map_response() // 紀錄訂單其他資訊 $order->update_meta_data( '_wooecpay_payment_order_prefix', get_option('wooecpay_payment_order_prefix') ); // 前綴 $order->update_meta_data( '_wooecpay_payment_merchant_trade_no', $merchant_trade_no ); //MerchantTradeNo - + $order->update_meta_data( '_wooecpay_query_trade_tag', 0); + $order->add_order_note(sprintf(__('Ecpay Payment Merchant Trade No %s', 'ecpay-ecommerce-for-woocommerce'), $merchant_trade_no)); $order->save(); @@ -124,7 +125,7 @@ public function map_response() 'MerchantTradeDate' => date('Y/m/d H:i:s'), 'PaymentType' => 'aio', 'TotalAmount' => (int) ceil($order->get_total()), - 'TradeDesc' => UrlService::ecpayUrlEncode(get_bloginfo('name')), + 'TradeDesc' => 'woocommerce_v2', 'ItemName' => $item_name, 'ChoosePayment' => $this->get_ChoosePayment($order->get_payment_method()), 'EncryptType' => 1, @@ -290,6 +291,8 @@ public function logistic_status_response() $order->add_order_note('物流貨態回傳:'.$RtnMsg.' ('.$RtnCode.')'); $order->save(); + + echo '1|OK'; } } } @@ -378,15 +381,15 @@ protected function add_type_info($input, $order) break; case 'ATM': - $input['ExpireDate'] = $this->get_option('_wooecpay_payment_expire_date', 3); + $input['ExpireDate'] = get_option('_wooecpay_payment_expire_date', 3); break; case 'BARCODE': - $input['StoreExpireDate'] = $this->get_option('_wooecpay_payment_expire_date', 3); + $input['StoreExpireDate'] = get_option('_wooecpay_payment_expire_date', 3); break; case 'CVS': - $input['StoreExpireDate'] = $this->get_option('_wooecpay_payment_expire_date', 86400); + $input['StoreExpireDate'] = get_option('_wooecpay_payment_expire_date', 86400); break; } @@ -416,6 +419,10 @@ protected function get_ChoosePayment($payment_method) case 'Wooecpay_Gateway_Barcode': $choose_payment = 'BARCODE' ; break; + + case 'Wooecpay_Gateway_Applepay': + $choose_payment = 'ApplePay' ; + break; } return $choose_payment ; diff --git a/ecpay-ecommerce-for-woocommerce/includes/services/payment/class-wooecpay-gateway.php b/ecpay-ecommerce-for-woocommerce/includes/services/payment/class-wooecpay-gateway.php index 8ff353f..ceee4ea 100644 --- a/ecpay-ecommerce-for-woocommerce/includes/services/payment/class-wooecpay-gateway.php +++ b/ecpay-ecommerce-for-woocommerce/includes/services/payment/class-wooecpay-gateway.php @@ -36,6 +36,8 @@ public function load_payment_gateway() { include WOOECPAY_PLUGIN_INCLUDE_DIR . '/services/payment/ecpay-gateway-webatm.php'; include WOOECPAY_PLUGIN_INCLUDE_DIR . '/services/payment/ecpay-gateway-barcode.php'; include WOOECPAY_PLUGIN_INCLUDE_DIR . '/services/payment/ecpay-gateway-cvs.php'; + include WOOECPAY_PLUGIN_INCLUDE_DIR . '/services/payment/ecpay-gateway-applepay.php'; + include WOOECPAY_PLUGIN_INCLUDE_DIR . '/services/payment/woocommerce-gateway-cod.php'; // 貨到付款相關程序 } @@ -47,6 +49,7 @@ public function add_method($methods) $methods[] = 'Wooecpay_Gateway_Atm'; $methods[] = 'Wooecpay_Gateway_Cvs'; $methods[] = 'Wooecpay_Gateway_Barcode'; + $methods[] = 'Wooecpay_Gateway_Applepay'; return $methods; } diff --git a/ecpay-ecommerce-for-woocommerce/includes/services/payment/ecpay-gateway-base.php b/ecpay-ecommerce-for-woocommerce/includes/services/payment/ecpay-gateway-base.php index b50b051..f48c7b6 100644 --- a/ecpay-ecommerce-for-woocommerce/includes/services/payment/ecpay-gateway-base.php +++ b/ecpay-ecommerce-for-woocommerce/includes/services/payment/ecpay-gateway-base.php @@ -45,41 +45,35 @@ public function receipt_page($order_id) // 執行地圖選擇 - // 取出商店代號 - $CVSStoreID = $order->get_meta('_ecpay_logistic_cvs_store_id') ; - - if(empty($CVSStoreID)){ - - // 不存在則走向地圖API - $api_logistic_info = $this->get_ecpay_logistic_api_info(); - $client_back_url = WC()->api_request_url('wooecpay_logistic_map_callback', true); - $MerchantTradeNo = $this->get_merchant_trade_no($order->get_id(), get_option('wooecpay_logistic_order_prefix')); - $LogisticsType = $this->get_logistics_sub_type($shipping_method_id) ; - - try { - $factory = new Factory([ - 'hashKey' => $api_logistic_info['hashKey'], - 'hashIv' => $api_logistic_info['hashIv'], - 'hashMethod' => 'md5', - ]); - $autoSubmitFormService = $factory->create('AutoSubmitFormWithCmvService'); - - $input = [ - 'MerchantID' => $api_logistic_info['merchant_id'], - 'MerchantTradeNo' => $MerchantTradeNo, - 'LogisticsType' => $LogisticsType['type'], - 'LogisticsSubType' => $LogisticsType['sub_type'], - 'IsCollection' => 'Y', - 'ServerReplyURL' => $client_back_url, - ]; - - $form_map = $autoSubmitFormService->generate($input, $api_logistic_info['action']); - - echo $form_map ; - - } catch (RtnException $e) { - echo wp_kses_post( '(' . $e->getCode() . ')' . $e->getMessage() ) . PHP_EOL; - } + // 不存在則走向地圖API + $api_logistic_info = $this->get_ecpay_logistic_api_info(); + $client_back_url = WC()->api_request_url('wooecpay_logistic_map_callback', true); + $MerchantTradeNo = $this->get_merchant_trade_no($order->get_id(), get_option('wooecpay_logistic_order_prefix')); + $LogisticsType = $this->get_logistics_sub_type($shipping_method_id) ; + + try { + $factory = new Factory([ + 'hashKey' => $api_logistic_info['hashKey'], + 'hashIv' => $api_logistic_info['hashIv'], + 'hashMethod' => 'md5', + ]); + $autoSubmitFormService = $factory->create('AutoSubmitFormWithCmvService'); + + $input = [ + 'MerchantID' => $api_logistic_info['merchant_id'], + 'MerchantTradeNo' => $MerchantTradeNo, + 'LogisticsType' => $LogisticsType['type'], + 'LogisticsSubType' => $LogisticsType['sub_type'], + 'IsCollection' => 'Y', + 'ServerReplyURL' => $client_back_url, + ]; + + $form_map = $autoSubmitFormService->generate($input, $api_logistic_info['action']); + + echo $form_map ; + + } catch (RtnException $e) { + echo wp_kses_post( '(' . $e->getCode() . ')' . $e->getMessage() ) . PHP_EOL; } @@ -103,6 +97,8 @@ public function receipt_page($order_id) // 紀錄訂單其他資訊 $order->update_meta_data( '_wooecpay_payment_order_prefix', get_option('wooecpay_payment_order_prefix') ); // 前綴 $order->update_meta_data( '_wooecpay_payment_merchant_trade_no', $merchant_trade_no ); //MerchantTradeNo + $order->update_meta_data( '_wooecpay_query_trade_tag', 0); + $order->add_order_note(sprintf(__('Ecpay Payment Merchant Trade No %s', 'ecpay-ecommerce-for-woocommerce'), $merchant_trade_no)); $order->save(); @@ -122,7 +118,7 @@ public function receipt_page($order_id) 'MerchantTradeDate' => date('Y/m/d H:i:s'), 'PaymentType' => 'aio', 'TotalAmount' => (int) ceil($order->get_total()), - 'TradeDesc' => UrlService::ecpayUrlEncode(get_bloginfo('name')), + 'TradeDesc' => 'woocommerce_v2', 'ItemName' => $item_name, 'ChoosePayment' => $this->payment_type, 'EncryptType' => 1, diff --git a/ecpay-ecommerce-for-woocommerce/includes/services/payment/ecpay-gateway-credit-installment.php b/ecpay-ecommerce-for-woocommerce/includes/services/payment/ecpay-gateway-credit-installment.php index e17a261..44311a8 100644 --- a/ecpay-ecommerce-for-woocommerce/includes/services/payment/ecpay-gateway-credit-installment.php +++ b/ecpay-ecommerce-for-woocommerce/includes/services/payment/ecpay-gateway-credit-installment.php @@ -53,11 +53,25 @@ public function payment_fields() { parent::payment_fields(); + $total = $this->get_order_total(); + echo '

' . _x('Number of periods', 'Checkout info', 'ecpay-ecommerce-for-woocommerce'); echo ''; } diff --git a/ecpay-ecommerce-for-woocommerce/includes/services/payment/ecpay-gateway-response.php b/ecpay-ecommerce-for-woocommerce/includes/services/payment/ecpay-gateway-response.php index 1f33842..37e8427 100644 --- a/ecpay-ecommerce-for-woocommerce/includes/services/payment/ecpay-gateway-response.php +++ b/ecpay-ecommerce-for-woocommerce/includes/services/payment/ecpay-gateway-response.php @@ -24,8 +24,6 @@ public function check_callback() $checkoutResponse = $factory->create(VerifiedArrayResponse::class); $info = $checkoutResponse->get($_POST); - - // error_log(print_r('respose', true), 3, 'path'); // 解析訂單編號 $order_id = $this->get_order_id($info) ; @@ -45,22 +43,41 @@ public function check_callback() // 付款完成 case 1: - $order->add_order_note(__('Payment completed', 'ecpay-ecommerce-for-woocommerce')); - $order->payment_complete(); + if(isset($info['SimulatePaid']) && $info['SimulatePaid'] == 0){ - $order->update_meta_data('_ecpay_card6no', $info['card6no']); - $order->update_meta_data('_ecpay_card4no', $info['card4no']); - $order->save_meta_data(); + // 判斷付款完成旗標,如果旗標不存或為0則執行 僅允許綠界一次作動 + $payment_complete_flag = get_post_meta( $order->get_id(), '_payment_complete_flag', true ); + + if(empty($payment_complete_flag)){ - // 產生物流訂單 - if ('yes' === get_option('wooecpay_enable_logistic_auto', 'yes')) { + $order->add_order_note(__('Payment completed', 'ecpay-ecommerce-for-woocommerce')); + $order->payment_complete(); - // 是否已經開立 - $wooecpay_logistic_AllPayLogisticsID = get_post_meta( $order->get_id(), '_wooecpay_logistic_AllPayLogisticsID', true ); + $order->update_meta_data('_ecpay_card6no', $info['card6no']); + $order->update_meta_data('_ecpay_card4no', $info['card4no']); + $order->save_meta_data(); - if(empty($wooecpay_logistic_AllPayLogisticsID)){ - $this->send_logistic_order_action($order_id); - } + // 產生物流訂單 + if ('yes' === get_option('wooecpay_enable_logistic_auto', 'yes')) { + + // 是否已經開立 + $wooecpay_logistic_AllPayLogisticsID = get_post_meta( $order->get_id(), '_wooecpay_logistic_AllPayLogisticsID', true ); + + if(empty($wooecpay_logistic_AllPayLogisticsID)){ + $this->send_logistic_order_action($order_id); + } + } + + // 異動付款完成旗標為1 + $order->update_meta_data('_payment_complete_flag', 1); + $order->save_meta_data(); + } + + } else { + + // 模擬付款 僅執行備註寫入 + $note = print_r($info, true); + $order->add_order_note('模擬付款/回傳參數:'. $note); } break; @@ -136,7 +153,6 @@ public function check_callback() } } - /** * 產生物流訂單 */ @@ -147,142 +163,174 @@ public function send_logistic_order_action($order_id) // 取得物流方式 $shipping_method_id = $order->get_items('shipping') ; - $shipping_method_id = reset($shipping_method_id); - $shipping_method_id = $shipping_method_id->get_method_id() ; - - // 判斷是否為綠界物流 產生物流訂單 - if( - $shipping_method_id == 'Wooecpay_Logistic_CVS_711' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Family' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Hilife' || - $shipping_method_id == 'Wooecpay_Logistic_CVS_Okmart' || - $shipping_method_id == 'Wooecpay_Logistic_Home_Tcat' || - $shipping_method_id == 'Wooecpay_Logistic_Home_Ecan' - ){ - $LogisticsType = $this->get_logistics_sub_type($shipping_method_id) ; - $api_logistic_info = $this->get_ecpay_logistic_api_info('create'); - $MerchantTradeNo = $this->get_merchant_trade_no($order->get_id(), get_option('wooecpay_logistic_order_prefix')); + if(!empty($shipping_method_id)){ - $sender_name = get_option('wooecpay_logistic_sender_name') ; - $sender_cellphone = get_option('wooecpay_logistic_sender_cellphone') ; - $sender_zipcode = get_option('wooecpay_logistic_sender_zipcode') ; - $sender_address = get_option('wooecpay_logistic_sender_address') ; + $shipping_method_id = reset($shipping_method_id); + $shipping_method_id = $shipping_method_id->get_method_id() ; - $serverReplyURL = WC()->api_request_url('wooecpay_logistic_status_callback', true); + // 判斷是否為綠界物流 產生物流訂單 + if( + $shipping_method_id == 'Wooecpay_Logistic_CVS_711' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Family' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Hilife' || + $shipping_method_id == 'Wooecpay_Logistic_CVS_Okmart' || + $shipping_method_id == 'Wooecpay_Logistic_Home_Tcat' || + $shipping_method_id == 'Wooecpay_Logistic_Home_Ecan' + ){ + $LogisticsType = $this->get_logistics_sub_type($shipping_method_id) ; + $api_logistic_info = $this->get_ecpay_logistic_api_info('create'); + $MerchantTradeNo = $this->get_merchant_trade_no($order->get_id(), get_option('wooecpay_logistic_order_prefix')); - // 取得訂單資訊 - // $order_data = $order->get_data(); + $sender_name = get_option('wooecpay_logistic_sender_name') ; + $sender_cellphone = get_option('wooecpay_logistic_sender_cellphone') ; + $sender_zipcode = get_option('wooecpay_logistic_sender_zipcode') ; + $sender_address = get_option('wooecpay_logistic_sender_address') ; - $CVSStoreID = $order->get_meta('_ecpay_logistic_cvs_store_id') ; + $serverReplyURL = WC()->api_request_url('wooecpay_logistic_status_callback', true); - if(!isset($CVSStoreID) || empty($CVSStoreID)){ - $ajaxReturn = [ - 'code' => '0003', - 'msg' => '查無超商資料', - ]; - } + // 取得訂單資訊 + // $order_data = $order->get_data(); - $payment_method = $order->get_payment_method() ; - if($payment_method == 'cod'){ - $IsCollection = 'Y'; - } else { - $IsCollection = 'N'; - } + $CVSStoreID = $order->get_meta('_ecpay_logistic_cvs_store_id') ; - $item_name = $this->get_item_name($order) ; - - if($LogisticsType['type'] == 'HOME'){ - - $inputLogisticOrder = [ - 'MerchantID' => $api_logistic_info['merchant_id'], - 'MerchantTradeNo' => $MerchantTradeNo, - 'MerchantTradeDate' => date('Y/m/d H:i:s'), - 'LogisticsType' => $LogisticsType['type'], - 'LogisticsSubType' => $LogisticsType['sub_type'], - 'GoodsAmount' => $order->get_total(), - 'GoodsName' => $item_name, - 'SenderName' => $sender_name, - 'SenderCellPhone' => $sender_cellphone, - 'SenderZipCode' => $sender_zipcode, - 'SenderAddress' => $sender_address, - 'ReceiverName' => $order->get_shipping_first_name() . $order->get_shipping_last_name(), - 'ReceiverCellPhone' => $order->get_billing_phone(), - 'ReceiverZipCode' => $order->get_shipping_postcode(), - 'ReceiverAddress' => $order->get_shipping_state().$order->get_shipping_city().$order->get_shipping_address_1().$order->get_shipping_address_2(), - 'Temperature' => '0001', - 'Distance' => '00', - 'Specification' => '0001', - 'ScheduledPickupTime' => '4', - 'ScheduledDeliveryTime' => '4', - 'ServerReplyURL' => $serverReplyURL, - ]; - - } else if($LogisticsType['type'] == 'CVS'){ - - $inputLogisticOrder = [ - 'MerchantID' => $api_logistic_info['merchant_id'], - 'MerchantTradeNo' => $MerchantTradeNo, - 'MerchantTradeDate' => date('Y/m/d H:i:s'), - 'LogisticsType' => $LogisticsType['type'], - 'LogisticsSubType' => $LogisticsType['sub_type'], - 'GoodsAmount' => $order->get_total(), - 'GoodsName' => $item_name, - 'SenderName' => $sender_name, - 'SenderCellPhone' => $sender_cellphone, - 'ReceiverName' => $order->get_shipping_first_name() . $order->get_shipping_last_name(), - 'ReceiverCellPhone' => $order->get_billing_phone(), - 'ReceiverStoreID' => $CVSStoreID, - 'IsCollection' => $IsCollection, - 'ServerReplyURL' => $serverReplyURL, - ]; - } - - try { - $factory = new Factory([ - 'hashKey' => $api_logistic_info['hashKey'], - 'hashIv' => $api_logistic_info['hashIv'], - 'hashMethod' => 'md5', - ]); - - $postService = $factory->create('PostWithCmvEncodedStrResponseService'); - $response = $postService->post($inputLogisticOrder, $api_logistic_info['action']); - - if( - isset($response['RtnCode']) && - ( $response['RtnCode'] == 300 || $response['RtnCode'] == 2001 ) - ){ - - // 更新訂單 - $order->update_meta_data( '_wooecpay_logistic_merchant_trade_no', $response['MerchantTradeNo'] ); - $order->update_meta_data( '_wooecpay_logistic_RtnCode', $response['RtnCode'] ); - $order->update_meta_data( '_wooecpay_logistic_RtnMsg', $response['RtnMsg'] ); - $order->update_meta_data( '_wooecpay_logistic_AllPayLogisticsID', $response['1|AllPayLogisticsID'] ); - $order->update_meta_data( '_wooecpay_logistic_LogisticsType', $response['LogisticsType'] ); - $order->update_meta_data( '_wooecpay_logistic_CVSPaymentNo', $response['CVSPaymentNo'] ); - $order->update_meta_data( '_wooecpay_logistic_CVSValidationNo', $response['CVSValidationNo'] ); - $order->update_meta_data( '_wooecpay_logistic_BookingNote', $response['BookingNote'] ); - - $order->add_order_note('建立物流訂單-物流廠商交易編號:'.$response['MerchantTradeNo']. ',狀態:' . $response['RtnMsg'] . '('. $response['RtnCode'] . ')'); - $order->save(); + if(!isset($CVSStoreID) || empty($CVSStoreID)){ $ajaxReturn = [ - 'code' => '9999', - 'msg' => '成功', + 'code' => '0003', + 'msg' => '查無超商資料', ]; + } + $payment_method = $order->get_payment_method() ; + if($payment_method == 'cod'){ + $IsCollection = 'Y'; } else { + $IsCollection = 'N'; + } + + // 綠界訂單顯示商品名稱判斷 + $item_name_default = '網路商品一批'; + + if ('yes' === get_option('wooecpay_enabled_logistic_disp_item_name', 'yes')) { + + // 取出訂單品項 + $item_name = $this->get_item_name($order); - // add note - $order->add_order_note(print_r($response, true)); - $order->save(); + // 判斷是否超過長度,如果超過長度改為預設文字 + if(strlen($item_name) > 50 ) { + + $item_name = $item_name_default; + + $order->add_order_note('商品名稱超過綠界物流可允許長度強制改為:'.$item_name); + $order->save(); + } + + // 判斷特殊字元 + if(preg_match('/[\^\'\[\]`!@#%\\\&*+\"<>|_]/', $item_name)){ + + $item_name = $item_name_default; + + $order->add_order_note('商品名稱存在綠界物流不允許的特殊字元強制改為:'.$item_name); + $order->save(); + } + + } else { + $item_name = $item_name_default; } - // var_dump($response); - } catch (RtnException $e) { - echo wp_kses_post( '(' . $e->getCode() . ')' . $e->getMessage() ) . PHP_EOL; + if($LogisticsType['type'] == 'HOME'){ + + $inputLogisticOrder = [ + 'MerchantID' => $api_logistic_info['merchant_id'], + 'MerchantTradeNo' => $MerchantTradeNo, + 'MerchantTradeDate' => date('Y/m/d H:i:s'), + 'LogisticsType' => $LogisticsType['type'], + 'LogisticsSubType' => $LogisticsType['sub_type'], + 'GoodsAmount' => $order->get_total(), + 'GoodsName' => $item_name, + 'SenderName' => $sender_name, + 'SenderCellPhone' => $sender_cellphone, + 'SenderZipCode' => $sender_zipcode, + 'SenderAddress' => $sender_address, + 'ReceiverName' => $order->get_shipping_first_name() . $order->get_shipping_last_name(), + 'ReceiverCellPhone' => $order->get_billing_phone(), + 'ReceiverZipCode' => $order->get_shipping_postcode(), + 'ReceiverAddress' => $order->get_shipping_state().$order->get_shipping_city().$order->get_shipping_address_1().$order->get_shipping_address_2(), + 'Temperature' => '0001', + 'Distance' => '00', + 'Specification' => '0001', + 'ScheduledPickupTime' => '4', + 'ScheduledDeliveryTime' => '4', + 'ServerReplyURL' => $serverReplyURL, + ]; + + } else if($LogisticsType['type'] == 'CVS'){ + + $inputLogisticOrder = [ + 'MerchantID' => $api_logistic_info['merchant_id'], + 'MerchantTradeNo' => $MerchantTradeNo, + 'MerchantTradeDate' => date('Y/m/d H:i:s'), + 'LogisticsType' => $LogisticsType['type'], + 'LogisticsSubType' => $LogisticsType['sub_type'], + 'GoodsAmount' => $order->get_total(), + 'GoodsName' => $item_name, + 'SenderName' => $sender_name, + 'SenderCellPhone' => $sender_cellphone, + 'ReceiverName' => $order->get_shipping_first_name() . $order->get_shipping_last_name(), + 'ReceiverCellPhone' => $order->get_billing_phone(), + 'ReceiverStoreID' => $CVSStoreID, + 'IsCollection' => $IsCollection, + 'ServerReplyURL' => $serverReplyURL, + ]; + } + + try { + $factory = new Factory([ + 'hashKey' => $api_logistic_info['hashKey'], + 'hashIv' => $api_logistic_info['hashIv'], + 'hashMethod' => 'md5', + ]); + + $postService = $factory->create('PostWithCmvEncodedStrResponseService'); + $response = $postService->post($inputLogisticOrder, $api_logistic_info['action']); + + if( + isset($response['RtnCode']) && + ( $response['RtnCode'] == 300 || $response['RtnCode'] == 2001 ) + ){ + + // 更新訂單 + $order->update_meta_data( '_wooecpay_logistic_merchant_trade_no', $response['MerchantTradeNo'] ); + $order->update_meta_data( '_wooecpay_logistic_RtnCode', $response['RtnCode'] ); + $order->update_meta_data( '_wooecpay_logistic_RtnMsg', $response['RtnMsg'] ); + $order->update_meta_data( '_wooecpay_logistic_AllPayLogisticsID', $response['1|AllPayLogisticsID'] ); + $order->update_meta_data( '_wooecpay_logistic_LogisticsType', $response['LogisticsType'] ); + $order->update_meta_data( '_wooecpay_logistic_CVSPaymentNo', $response['CVSPaymentNo'] ); + $order->update_meta_data( '_wooecpay_logistic_CVSValidationNo', $response['CVSValidationNo'] ); + $order->update_meta_data( '_wooecpay_logistic_BookingNote', $response['BookingNote'] ); + + $order->add_order_note('建立物流訂單-物流廠商交易編號:'.$response['MerchantTradeNo']. ',狀態:' . $response['RtnMsg'] . '('. $response['RtnCode'] . ')'); + $order->save(); + + $ajaxReturn = [ + 'code' => '9999', + 'msg' => '成功', + ]; + + } else { + + // add note + $order->add_order_note(print_r($response, true)); + $order->save(); + } + + // var_dump($response); + } catch (RtnException $e) { + echo wp_kses_post( '(' . $e->getCode() . ')' . $e->getMessage() ) . PHP_EOL; + } } } }