diff --git a/changelog.txt b/changelog.txt index a40ca3e..b7d37d3 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,9 @@ == Changelog == -= 1.6.8 2025-03-20 = += 1.6.9 2025-XX-XX = +* Changed - Indentation and documentation update + += 1.6.8 2025-04-11 = * Added - Auto remove whitespace in brand id and secret key field on save * Added - Site health check for CHIP's API connection * Changed - Email fallback description and placeholder diff --git a/chip-for-woocommerce.php b/chip-for-woocommerce.php index d8febb7..fb9980e 100644 --- a/chip-for-woocommerce.php +++ b/chip-for-woocommerce.php @@ -125,7 +125,7 @@ public function add_gateways( $methods ) { public function setting_link( $links ) { $url_params = array( 'page' => 'wc-settings', - 'tab' => 'checkout', + 'tab' => 'checkout', ); if ( defined( 'DISABLE_CLONE_WC_GATEWAY_CHIP' ) ) { @@ -135,7 +135,7 @@ public function setting_link( $links ) { $url = add_query_arg( $url_params, admin_url( 'admin.php' ) ); $new_links = array( - 'settings' => sprintf( '%2$s', $url, esc_html__( 'Settings', 'chip-for-woocommerce' ) ) + 'settings' => sprintf( '%2$s', $url, esc_html__( 'Settings', 'chip-for-woocommerce' ) ), ); return array_merge( $new_links, $links ); @@ -151,17 +151,17 @@ public static function load() { public function block_support() { if ( class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) { - include plugin_dir_path( WC_CHIP_FILE ) . 'includes/blocks/class-wc-gateway-chip-blocks.php'; + include plugin_dir_path( WC_CHIP_FILE ) . 'includes/blocks/class-wc-gateway-chip-blocks-support.php'; include plugin_dir_path( WC_CHIP_FILE ) . 'includes/blocks/clone-wc-gateway-chip-blocks.php'; add_action( 'woocommerce_blocks_payment_method_type_registration', - function (Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry) { - $payment_method_registry->register( new WC_Gateway_Chip_Blocks_Support ); - $payment_method_registry->register( new WC_Gateway_Chip_2_Blocks_Support ); - $payment_method_registry->register( new WC_Gateway_Chip_3_Blocks_Support ); - $payment_method_registry->register( new WC_Gateway_Chip_4_Blocks_Support ); - $payment_method_registry->register( new WC_Gateway_Chip_5_Blocks_Support ); - $payment_method_registry->register( new WC_Gateway_Chip_6_Blocks_Support ); + function ( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) { + $payment_method_registry->register( new WC_Gateway_Chip_Blocks_Support() ); + $payment_method_registry->register( new WC_Gateway_Chip_2_Blocks_Support() ); + $payment_method_registry->register( new WC_Gateway_Chip_3_Blocks_Support() ); + $payment_method_registry->register( new WC_Gateway_Chip_4_Blocks_Support() ); + $payment_method_registry->register( new WC_Gateway_Chip_5_Blocks_Support() ); + $payment_method_registry->register( new WC_Gateway_Chip_6_Blocks_Support() ); } ); } diff --git a/includes/blocks/class-wc-gateway-chip-blocks-support.php b/includes/blocks/class-wc-gateway-chip-blocks-support.php new file mode 100644 index 0000000..d3a2ec4 --- /dev/null +++ b/includes/blocks/class-wc-gateway-chip-blocks-support.php @@ -0,0 +1,146 @@ +gateway = Chip_Woocommerce::get_chip_gateway_class( $this->name ); + $this->settings = $this->gateway->settings; + } + + /** + * Get the payment method type + * + * @return bool + */ + public function is_active() { + return $this->gateway->is_available(); + } + + /** + * Get payment method script handles + * + * @return array + */ + public function get_payment_method_script_handles() { + $script_path = 'assets/js/frontend/blocks_' . $this->name . '.js'; + $script_asset_path = plugin_dir_path( WC_CHIP_FILE ) . 'assets/js/frontend/blocks_' . $this->name . '.asset.php'; + $script_asset = file_exists( $script_asset_path ) + ? require $script_asset_path + : array( + 'dependencies' => array(), + 'version' => WC_CHIP_MODULE_VERSION, + ); + $script_url = WC_CHIP_URL . $script_path; + + wp_register_script( + "wc-{$this->name}-blocks", + $script_url, + $script_asset['dependencies'], + $script_asset['version'], + true + ); + + $localize_variable = array( + 'id' => $this->name, + 'fpx_b2c' => array( 'empty' => 'bank' ), + 'fpx_b2b1' => array( 'empty' => 'bank' ), + 'razer' => array( 'empty' => 'ewallet' ), + ); + + $whitelisted_payment_method = $this->gateway->get_payment_method_whitelist(); + $bypass_chip = $this->gateway->get_bypass_chip(); + + // Exclude razer_atome. + $razer_ewallet_list = array( 'razer_grabpay', 'razer_maybankqr', 'razer_shopeepay', 'razer_tng' ); + + if ( is_array( $whitelisted_payment_method ) && 'yes' === $bypass_chip ) { + if ( count( $whitelisted_payment_method ) == 1 ) { + if ( $whitelisted_payment_method[0] == 'fpx' ) { + $localize_variable['fpx_b2c'] = $this->gateway->list_fpx_banks(); + unset( $localize_variable['fpx_b2c'][''] ); + } elseif ( 'fpx_b2b1' === $whitelisted_payment_method[0] ) { + $localize_variable['fpx_b2b1'] = $this->gateway->list_fpx_b2b1_banks(); + unset( $localize_variable['fpx_b2b1'][''] ); + } else { + // Checker when whitelist one e-wallet only (razer). + if ( ( count( preg_grep( '/^razer_/', $whitelisted_payment_method ) ) > 0 ) ) { + $localize_variable['razer'] = $this->gateway->list_razer_ewallets(); + unset( $localize_variable['razer'][''] ); + } + } + } elseif ( count( array_diff( $whitelisted_payment_method, $razer_ewallet_list ) ) == 0 ) { + $localize_variable['razer'] = $this->gateway->list_razer_ewallets(); + unset( $localize_variable['razer'][''] ); + } + } + + wp_localize_script( "wc-{$this->name}-blocks", 'gateway_' . $this->name, $localize_variable ); + + return array( "wc-{$this->name}-blocks" ); + } + + /** + * Get the payment method data. + * This method is used to get the payment method data for the block checkout. + * + * @return array + */ + public function get_payment_method_data() { + $pm_whitelist = $this->get_setting( 'payment_method_whitelist' ); + $bypass_chip = $this->get_setting( 'bypass_chip' ); + $js_display = ''; + + if ( is_array( $pm_whitelist ) && count( $pm_whitelist ) == 1 && $pm_whitelist[0] == 'fpx' && $bypass_chip == 'yes' ) { + $js_display = 'fpx'; + } elseif ( is_array( $pm_whitelist ) && count( $pm_whitelist ) == 1 && $pm_whitelist[0] == 'fpx_b2b1' && $bypass_chip == 'yes' ) { + $js_display = 'fpx_b2b1'; + } elseif ( is_array( $pm_whitelist ) && $bypass_chip == 'yes' ) { + $razer_ewallet_list = array( 'razer_grabpay', 'razer_maybankqr', 'razer_shopeepay', 'razer_tng' ); + if ( count( array_diff( $pm_whitelist, $razer_ewallet_list ) ) == 0 ) { + $js_display = 'razer'; + } + } + + return array( + 'title' => $this->get_setting( 'title' ), + 'description' => $this->get_setting( 'description' ), + 'supports' => array_filter( $this->gateway->supports, array( $this->gateway, 'supports' ) ), + 'method_name' => $this->name, + 'saved_option' => $this->gateway->supports( 'tokenization' ), + 'save_option' => false, + 'js_display' => $js_display, + 'icon' => $this->gateway->icon, + ); + } +} diff --git a/includes/blocks/class-wc-gateway-chip-blocks.php b/includes/blocks/class-wc-gateway-chip-blocks.php deleted file mode 100644 index 4f3ad04..0000000 --- a/includes/blocks/class-wc-gateway-chip-blocks.php +++ /dev/null @@ -1,103 +0,0 @@ -gateway = Chip_Woocommerce::get_chip_gateway_class( $this->name ); - $this->settings = $this->gateway->settings; - } - - public function is_active() { - return $this->gateway->is_available(); - } - - public function get_payment_method_script_handles() { - $script_path = 'assets/js/frontend/blocks_' . $this->name . '.js'; - $script_asset_path = plugin_dir_path( WC_CHIP_FILE ) . 'assets/js/frontend/blocks_' . $this->name . '.asset.php'; - $script_asset = file_exists( $script_asset_path ) - ? require( $script_asset_path ) - : array( - 'dependencies' => array(), - 'version' => WC_CHIP_MODULE_VERSION - ); - $script_url = WC_CHIP_URL . $script_path; - - wp_register_script( - "wc-{$this->name}-blocks", - $script_url, - $script_asset['dependencies'], - $script_asset['version'], - true - ); - - $localize_variable = array( - 'id' => $this->name, - 'fpx_b2c' => [ 'empty' => 'bank' ], - 'fpx_b2b1' => [ 'empty' => 'bank' ], - 'razer' => [ 'empty' => 'ewallet' ], - ); - - $whitelisted_payment_method = $this->gateway->get_payment_method_whitelist(); - $bypass_chip = $this->gateway->get_bypass_chip(); - - // Exclude razer_atome - $razer_ewallet_list = [ 'razer_grabpay', 'razer_maybankqr', 'razer_shopeepay', 'razer_tng' ]; - - if ( is_array( $whitelisted_payment_method ) and $bypass_chip == 'yes' ) { - if ( count( $whitelisted_payment_method ) == 1 ) { - if ( $whitelisted_payment_method[0] == 'fpx' ) { - $localize_variable['fpx_b2c'] = $this->gateway->list_fpx_banks(); - unset( $localize_variable['fpx_b2c'][''] ); - } elseif ( $whitelisted_payment_method[0] == 'fpx_b2b1' ) { - $localize_variable['fpx_b2b1'] = $this->gateway->list_fpx_b2b1_banks(); - unset( $localize_variable['fpx_b2b1'][''] ); - } else { - // Checker when whitelist one e-wallet only (razer) - if ( ( count( preg_grep( "/^razer_/", $whitelisted_payment_method ) ) > 0 ) ) { - $localize_variable['razer'] = $this->gateway->list_razer_ewallets(); - unset( $localize_variable['razer'][''] ); - } - } - } elseif ( count( array_diff( $whitelisted_payment_method, $razer_ewallet_list ) ) == 0 ) { - $localize_variable['razer'] = $this->gateway->list_razer_ewallets(); - unset( $localize_variable['razer'][''] ); - } - } - - wp_localize_script( "wc-{$this->name}-blocks", 'gateway_' . $this->name, $localize_variable ); - - return [ "wc-{$this->name}-blocks" ]; - } - - public function get_payment_method_data() { - $pm_whitelist = $this->get_setting( 'payment_method_whitelist' ); - $bypass_chip = $this->get_setting( 'bypass_chip' ); - $js_display = ''; - - if ( is_array( $pm_whitelist ) and count( $pm_whitelist ) == 1 and $pm_whitelist[0] == 'fpx' and $bypass_chip == 'yes' ) { - $js_display = 'fpx'; - } elseif ( is_array( $pm_whitelist ) and count( $pm_whitelist ) == 1 and $pm_whitelist[0] == 'fpx_b2b1' and $bypass_chip == 'yes' ) { - $js_display = 'fpx_b2b1'; - } elseif ( is_array( $pm_whitelist ) and $bypass_chip == 'yes' ) { - $razer_ewallet_list = [ 'razer_grabpay', 'razer_maybankqr', 'razer_shopeepay', 'razer_tng' ]; - if ( count( array_diff( $pm_whitelist, $razer_ewallet_list ) ) == 0 ) { - $js_display = 'razer'; - } - } - - return [ - 'title' => $this->get_setting( 'title' ), - 'description' => $this->get_setting( 'description' ), - 'supports' => array_filter( $this->gateway->supports, [ $this->gateway, 'supports' ] ), - 'method_name' => $this->name, - 'saved_option' => $this->gateway->supports( 'tokenization' ), - 'save_option' => false, - 'js_display' => $js_display, - 'icon' => $this->gateway->icon, - ]; - } -} diff --git a/includes/blocks/clone-wc-gateway-chip-blocks.php b/includes/blocks/clone-wc-gateway-chip-blocks.php index 4d9114d..88813ec 100644 --- a/includes/blocks/clone-wc-gateway-chip-blocks.php +++ b/includes/blocks/clone-wc-gateway-chip-blocks.php @@ -16,4 +16,4 @@ class WC_Gateway_Chip_5_Blocks_Support extends WC_Gateway_Chip_Blocks_Support { class WC_Gateway_Chip_6_Blocks_Support extends WC_Gateway_Chip_Blocks_Support { protected $name = 'wc_gateway_chip_6'; -} \ No newline at end of file +} diff --git a/includes/class-wc-api-fpx.php b/includes/class-wc-api-fpx.php index 7d75e94..c0d384f 100644 --- a/includes/class-wc-api-fpx.php +++ b/includes/class-wc-api-fpx.php @@ -8,7 +8,7 @@ class Chip_Woocommerce_API_FPX { public function __construct( $logger, $debug ) { $this->logger = $logger; - $this->debug = $debug; + $this->debug = $debug; } public function get_fpx() { @@ -23,7 +23,7 @@ public function get_fpx_b2b1() { return $this->call( 'GET', '/fpx_b2b1?time=' . time() ); } - private function call( $method, $route, $params = [] ) { + private function call( $method, $route, $params = array() ) { if ( ! empty( $params ) ) { $params = json_encode( $params ); } @@ -32,9 +32,9 @@ private function call( $method, $route, $params = [] ) { $method, sprintf( '%s%s', WC_CHIP_FPX_ROOT_URL, $route ), $params, - [ - 'Content-type' => 'application/json' - ] + array( + 'Content-type' => 'application/json', + ) ); $this->log_info( sprintf( 'received response: %s', $response ) ); @@ -54,22 +54,27 @@ private function call( $method, $route, $params = [] ) { return $result; } - private function request( $method, $url, $params = [], $headers = [] ) { - $this->log_info( sprintf( - '%s `%s`\n%s\n%s', - $method, + private function request( $method, $url, $params = array(), $headers = array() ) { + $this->log_info( + sprintf( + '%s `%s`\n%s\n%s', + $method, + $url, + var_export( $params, true ), + var_export( $headers, true ) + ) + ); + + $wp_request = wp_remote_request( $url, - var_export( $params, true ), - var_export( $headers, true ) - ) ); - - $wp_request = wp_remote_request( $url, array( - 'method' => $method, - 'sslverify' => ! defined( 'WC_CHIP_SSLVERIFY_FALSE' ), - 'headers' => $headers, - 'body' => $params, - 'timeout' => 3 - ) ); + array( + 'method' => $method, + 'sslverify' => ! defined( 'WC_CHIP_SSLVERIFY_FALSE' ), + 'headers' => $headers, + 'body' => $params, + 'timeout' => 3, + ) + ); $response = wp_remote_retrieve_body( $wp_request ); diff --git a/includes/class-wc-api.php b/includes/class-wc-api.php index 58b62ea..9806c7c 100644 --- a/includes/class-wc-api.php +++ b/includes/class-wc-api.php @@ -11,14 +11,14 @@ class Chip_Woocommerce_API { public function __construct( $secret_key, $brand_id, $logger, $debug ) { $this->secret_key = $secret_key; - $this->brand_id = $brand_id; - $this->logger = $logger; - $this->debug = $debug; + $this->brand_id = $brand_id; + $this->logger = $logger; + $this->debug = $debug; } public function set_key( $secret_key, $brand_id ) { $this->secret_key = $secret_key; - $this->brand_id = $brand_id; + $this->brand_id = $brand_id; } public function create_payment( $params ) { @@ -52,7 +52,7 @@ public function delete_token( $purchase_id ) { return $this->call( 'POST', "/purchases/$purchase_id/delete_recurring_token/" ); } - public function capture_payment( $payment_id, $params = [] ) { + public function capture_payment( $payment_id, $params = array() ) { $this->log_info( "capture payment: {$payment_id}" ); return $this->call( 'POST', "/purchases/{$payment_id}/capture/", $params ); @@ -89,7 +89,7 @@ public function payment_recurring_methods( $currency, $language, $amount ) { } public function get_payment( $payment_id ) { - $this->log_info( sprintf( "get payment: %s", $payment_id ) ); + $this->log_info( sprintf( 'get payment: %s', $payment_id ) ); // time() is to force fresh instead cache $result = $this->call( 'GET', "/purchases/{$payment_id}/?time=" . time() ); @@ -99,11 +99,11 @@ public function get_payment( $payment_id ) { } public function refund_payment( $payment_id, $params ) { - $this->log_info( sprintf( "refunding payment: %s", $payment_id ) ); + $this->log_info( sprintf( 'refunding payment: %s', $payment_id ) ); $result = $this->call( 'POST', "/purchases/{$payment_id}/refund/", $params ); - $this->log_info( sprintf( "payment refund result: %s", var_export( $result, true ) ) ); + $this->log_info( sprintf( 'payment refund result: %s', var_export( $result, true ) ) ); return $result; } @@ -134,7 +134,7 @@ public function balance() { return $result; } - private function call( $method, $route, $params = [] ) { + private function call( $method, $route, $params = array() ) { $secret_key = $this->secret_key; if ( ! empty( $params ) ) { $params = json_encode( $params ); @@ -144,10 +144,10 @@ private function call( $method, $route, $params = [] ) { $method, sprintf( '%s/v1%s', WC_CHIP_ROOT_URL, $route ), $params, - [ - 'Content-type' => 'application/json', + array( + 'Content-type' => 'application/json', 'Authorization' => "Bearer {$secret_key}", - ] + ) ); $this->log_info( sprintf( 'received response: %s', $response ) ); @@ -167,22 +167,27 @@ private function call( $method, $route, $params = [] ) { return $result; } - private function request( $method, $url, $params = [], $headers = [] ) { - $this->log_info( sprintf( - '%s `%s`\n%s\n%s', - $method, + private function request( $method, $url, $params = array(), $headers = array() ) { + $this->log_info( + sprintf( + '%s `%s`\n%s\n%s', + $method, + $url, + var_export( $params, true ), + var_export( $headers, true ) + ) + ); + + $wp_request = wp_remote_request( $url, - var_export( $params, true ), - var_export( $headers, true ) - ) ); - - $wp_request = wp_remote_request( $url, array( - 'method' => $method, - 'sslverify' => ! defined( 'WC_CHIP_SSLVERIFY_FALSE' ), - 'headers' => $headers, - 'body' => $params, - 'timeout' => 10, // charge card require longer timeout - ) ); + array( + 'method' => $method, + 'sslverify' => ! defined( 'WC_CHIP_SSLVERIFY_FALSE' ), + 'headers' => $headers, + 'body' => $params, + 'timeout' => 10, // charge card require longer timeout + ) + ); $response = wp_remote_retrieve_body( $wp_request ); diff --git a/includes/class-wc-bulk-action.php b/includes/class-wc-bulk-action.php index 88d0e43..b139c94 100644 --- a/includes/class-wc-bulk-action.php +++ b/includes/class-wc-bulk-action.php @@ -45,7 +45,7 @@ public function handle_bulk_actions( $redirect_to, $action, $ids ) { foreach ( $ids as $id ) { $order = wc_get_order( $id ); if ( ! $order->is_paid() ) { - $gateway_id = $order->get_payment_method(); + $gateway_id = $order->get_payment_method(); $wc_gateway_chip = Chip_Woocommerce::get_chip_gateway_class( $gateway_id ); if ( $wc_gateway_chip and ( $purchase = $order->get_meta( '_' . $gateway_id . '_purchase', true ) ) ) { @@ -53,7 +53,7 @@ public function handle_bulk_actions( $redirect_to, $action, $ids ) { WC()->queue()->schedule_single( time(), 'wc_chip_check_order_status', array( $purchase['id'], $id, 8, $gateway_id ), "{$gateway_id}_bulk_requery" ); do_action( 'wc_chip_bulk_order_requery', $id ); - $changed++; + ++$changed; } } } @@ -62,10 +62,10 @@ public function handle_bulk_actions( $redirect_to, $action, $ids ) { if ( $changed ) { $redirect_to = add_query_arg( array( - 'post_type' => $this->list_table_type, + 'post_type' => $this->list_table_type, 'bulk_action' => $action, - 'changed' => $changed, - 'ids' => join( ',', $ids ), + 'changed' => $changed, + 'ids' => join( ',', $ids ), ), $redirect_to ); @@ -89,7 +89,7 @@ public function bulk_admin_notices() { return; } - $number = isset( $_REQUEST['changed'] ) ? absint( $_REQUEST['changed'] ) : 0; // WPCS: input var ok, CSRF ok. + $number = isset( $_REQUEST['changed'] ) ? absint( $_REQUEST['changed'] ) : 0; // WPCS: input var ok, CSRF ok. $bulk_action = wc_clean( wp_unslash( $_REQUEST['bulk_action'] ) ); // WPCS: input var ok, CSRF ok. if ( $bulk_action == 'chip_requery' ) { @@ -99,4 +99,4 @@ public function bulk_admin_notices() { } } -Chip_Woocommerce_Bulk_Action::get_instance(); \ No newline at end of file +Chip_Woocommerce_Bulk_Action::get_instance(); diff --git a/includes/class-wc-gateway-chip.php b/includes/class-wc-gateway-chip.php index 9529e6a..3513d9b 100644 --- a/includes/class-wc-gateway-chip.php +++ b/includes/class-wc-gateway-chip.php @@ -1,7 +1,11 @@ init_id(); @@ -55,34 +58,34 @@ public function __construct() { $this->init_supports(); $this->init_has_fields(); - $this->secret_key = $this->get_option( 'secret_key' ); - $this->brand_id = $this->get_option( 'brand_id' ); - $this->due_strict = $this->get_option( 'due_strict', 'yes' ); - $this->due_str_t = $this->get_option( 'due_strict_timing', 60 ); - $this->purchase_sr = $this->get_option( 'purchase_send_receipt', 'yes' ); - $this->purchase_tz = $this->get_option( 'purchase_time_zone', 'Asia/Kuala_Lumpur' ); - $this->update_clie = $this->get_option( 'update_client_information' ); - $this->system_url_ = $this->get_option( 'system_url_scheme', 'https' ); - $this->force_token = $this->get_option( 'force_tokenization' ); - $this->disable_rec = $this->get_option( 'disable_recurring_support' ); - $this->disable_cli = $this->get_option( 'disable_clients_api' ); - $this->payment_met = $this->get_option( 'payment_method_whitelist' ); - $this->disable_red = $this->get_option( 'disable_redirect' ); - $this->disable_cal = $this->get_option( 'disable_callback' ); - $this->enable_auto = $this->get_option( 'enable_auto_clear_cart' ); - $this->debug = $this->get_option( 'debug' ); - $this->public_key = $this->get_option( 'public_key' ); - $this->arecuring_p = $this->get_option( 'available_recurring_payment_method' ); - $this->a_payment_m = $this->get_payment_method_list(); - $this->description = $this->get_option( 'description' ); - $this->webhook_pub = $this->get_option( 'webhook_public_key' ); - $this->bypass_chip = $this->get_option( 'bypass_chip' ); - $this->add_charges = $this->get_option( 'enable_additional_charges' ); - $this->fix_charges = $this->get_option( 'fixed_charges', 100 ); - $this->per_charges = $this->get_option( 'percent_charges', 0 ); + $this->secret_key = $this->get_option( 'secret_key' ); + $this->brand_id = $this->get_option( 'brand_id' ); + $this->due_strict = $this->get_option( 'due_strict', 'yes' ); + $this->due_str_t = $this->get_option( 'due_strict_timing', 60 ); + $this->purchase_sr = $this->get_option( 'purchase_send_receipt', 'yes' ); + $this->purchase_tz = $this->get_option( 'purchase_time_zone', 'Asia/Kuala_Lumpur' ); + $this->update_clie = $this->get_option( 'update_client_information' ); + $this->system_url_ = $this->get_option( 'system_url_scheme', 'https' ); + $this->force_token = $this->get_option( 'force_tokenization' ); + $this->disable_rec = $this->get_option( 'disable_recurring_support' ); + $this->disable_cli = $this->get_option( 'disable_clients_api' ); + $this->payment_met = $this->get_option( 'payment_method_whitelist' ); + $this->disable_red = $this->get_option( 'disable_redirect' ); + $this->disable_cal = $this->get_option( 'disable_callback' ); + $this->enable_auto = $this->get_option( 'enable_auto_clear_cart' ); + $this->debug = $this->get_option( 'debug' ); + $this->public_key = $this->get_option( 'public_key' ); + $this->arecuring_p = $this->get_option( 'available_recurring_payment_method' ); + $this->a_payment_m = $this->get_payment_method_list(); + $this->description = $this->get_option( 'description' ); + $this->webhook_pub = $this->get_option( 'webhook_public_key' ); + $this->bypass_chip = $this->get_option( 'bypass_chip' ); + $this->add_charges = $this->get_option( 'enable_additional_charges' ); + $this->fix_charges = $this->get_option( 'fixed_charges', 100 ); + $this->per_charges = $this->get_option( 'percent_charges', 0 ); $this->cancel_order_flow = $this->get_option( 'cancel_order_flow' ); - $this->email_fallback = $this->get_option( 'email_fallback' ); - $this->enable_metabox = $this->get_option( 'enable_metabox' ); + $this->email_fallback = $this->get_option( 'email_fallback' ); + $this->enable_metabox = $this->get_option( 'enable_metabox' ); $this->init_form_fields(); $this->init_settings(); @@ -100,15 +103,21 @@ public function __construct() { $this->add_filters(); } + /** + * Initialize gateway ID. + */ protected function init_id() { - $this->id = strtolower( get_class( $this ) ); + $this->id = static::GATEWAY_ID; } + /** + * Initialize gateway icon. + */ protected function init_icon() { $logo = $this->get_option( 'display_logo', 'logo' ); $file_extension = 'png'; - $file_path = plugin_dir_path( WC_CHIP_FILE ) . 'assets/' . $logo . '.png'; + $file_path = plugin_dir_path( WC_CHIP_FILE ) . 'assets/' . $logo . '.png'; if ( ! file_exists( $file_path ) ) { $file_extension = 'svg'; } @@ -138,7 +147,7 @@ protected function init_currency_check() { } protected function init_supports() { - $supports = array( 'refunds', 'tokenization', 'subscriptions', 'subscription_cancellation', 'subscription_suspension', 'subscription_reactivation', 'subscription_amount_changes', 'subscription_date_changes', 'subscription_payment_method_change', 'subscription_payment_method_delayed_change', 'subscription_payment_method_change_customer', 'subscription_payment_method_change_admin', 'multiple_subscriptions', 'pre-orders' ); + $supports = array( 'refunds', 'tokenization', 'subscriptions', 'subscription_cancellation', 'subscription_suspension', 'subscription_reactivation', 'subscription_amount_changes', 'subscription_date_changes', 'subscription_payment_method_change', 'subscription_payment_method_delayed_change', 'subscription_payment_method_change_customer', 'subscription_payment_method_change_admin', 'multiple_subscriptions', 'pre-orders' ); $this->supports = array_merge( $this->supports, $supports ); } @@ -150,7 +159,7 @@ protected function init_one_time_gateway() { $one_time_gateway = false; if ( is_array( $this->payment_met ) and ! empty( $this->payment_met ) ) { - foreach ( [ 'visa', 'mastercard', 'maestro' ] as $card_network ) { + foreach ( array( 'visa', 'mastercard', 'maestro' ) as $card_network ) { if ( in_array( $card_network, $this->payment_met ) ) { $one_time_gateway = false; break; @@ -160,7 +169,7 @@ protected function init_one_time_gateway() { } if ( $one_time_gateway or $this->disable_rec == 'yes' ) { - $this->supports = [ 'products', 'refunds' ]; + $this->supports = array( 'products', 'refunds' ); } } @@ -196,7 +205,7 @@ public function add_filters() { public function get_icon() { $style = 'max-height: 25px; width: auto'; - if ( in_array( $this->get_option( 'display_logo', 'logo' ), [ 'paywithchip_all', 'paywithchip_fpx' ] ) ) { + if ( in_array( $this->get_option( 'display_logo', 'logo' ), array( 'paywithchip_all', 'paywithchip_fpx' ) ) ) { $style = ''; } @@ -257,9 +266,9 @@ public function handle_callback_token() { exit( $message ); } - $payment = json_decode( $content, true ); + $payment = json_decode( $content, true ); $payment_id = array_key_exists( 'id', $payment ) ? sanitize_key( $payment['id'] ) : ''; - } else if ( $payment_id ) { + } elseif ( $payment_id ) { $payment = $this->api()->get_payment( $payment_id ); } else { exit( __( 'Unexpected response', 'chip-for-woocommerce' ) ); @@ -338,7 +347,7 @@ public function handle_callback_order() { // $payment_id = WC()->session->get( 'chip_payment_id_' . $order_id ); - $payment = $order->get_meta( '_' . $this->id . '_purchase', true ); + $payment = $order->get_meta( '_' . $this->id . '_purchase', true ); $payment_id = $payment['id']; // if ( !$payment_id AND isset( $_SERVER['HTTP_X_SIGNATURE'] ) ) { @@ -351,9 +360,9 @@ public function handle_callback_order() { exit( $message ); } - $payment = json_decode( $content, true ); + $payment = json_decode( $content, true ); $payment_id = array_key_exists( 'id', $payment ) ? sanitize_key( $payment['id'] ) : ''; - } else if ( $payment_id ) { + } elseif ( $payment_id ) { $payment = $this->api()->get_payment( $payment_id ); } else { exit( __( 'Unexpected response', 'chip-for-woocommerce' ) ); @@ -380,25 +389,23 @@ public function handle_callback_order() { WC()->cart->empty_cart(); $this->log_order_info( 'payment processed', $order ); - } else { - if ( ! $order->is_paid() ) { - if ( ! empty( $payment['transaction_data']['attempts'] ) and ! empty( $payment_extra = $payment['transaction_data']['attempts'][0]['extra'] ) ) { - if ( isset( $payment_extra['payload'] ) and isset( $payment_extra['payload']['fpx_debitAuthCode'] ) ) { - $debit_auth_code = $payment_extra['payload']['fpx_debitAuthCode'][0]; - $fpx_txn_id = $payment_extra['payload']['fpx_fpxTxnId'][0]; - $fpx_seller_order_no = $payment_extra['payload']['fpx_sellerOrderNo'][0]; - - $order->add_order_note( - sprintf( __( 'FPX Debit Auth Code: %1$s. FPX Transaction ID: %2$s. FPX Seller Order Number: %3$s.', 'chip-for-woocommerce' ), $debit_auth_code, $fpx_txn_id, $fpx_seller_order_no ) - ); - } + } elseif ( ! $order->is_paid() ) { + if ( ! empty( $payment['transaction_data']['attempts'] ) and ! empty( $payment_extra = $payment['transaction_data']['attempts'][0]['extra'] ) ) { + if ( isset( $payment_extra['payload'] ) and isset( $payment_extra['payload']['fpx_debitAuthCode'] ) ) { + $debit_auth_code = $payment_extra['payload']['fpx_debitAuthCode'][0]; + $fpx_txn_id = $payment_extra['payload']['fpx_fpxTxnId'][0]; + $fpx_seller_order_no = $payment_extra['payload']['fpx_sellerOrderNo'][0]; + + $order->add_order_note( + sprintf( __( 'FPX Debit Auth Code: %1$s. FPX Transaction ID: %2$s. FPX Seller Order Number: %3$s.', 'chip-for-woocommerce' ), $debit_auth_code, $fpx_txn_id, $fpx_seller_order_no ) + ); } + } $order->update_status( 'wc-failed' ); $this->log_order_info( 'payment not successful', $order ); - } } $this->release_lock( $order_id ); @@ -422,284 +429,284 @@ public function handle_callback_order() { public function init_form_fields() { $this->form_fields['enabled'] = array( - 'title' => __( 'Enable/Disable', 'chip-for-woocommerce' ), - 'label' => sprintf( '%1$s %2$s', __( 'Enable', 'chip-for-woocommerce' ), $this->method_title ), - 'type' => 'checkbox', + 'title' => __( 'Enable/Disable', 'chip-for-woocommerce' ), + 'label' => sprintf( '%1$s %2$s', __( 'Enable', 'chip-for-woocommerce' ), $this->method_title ), + 'type' => 'checkbox', 'default' => 'no', ); $this->form_fields['title'] = array( - 'title' => __( 'Title', 'chip-for-woocommerce' ), - 'type' => 'text', + 'title' => __( 'Title', 'chip-for-woocommerce' ), + 'type' => 'text', 'description' => __( 'This controls the title which the user sees during checkout.', 'chip-for-woocommerce' ), - 'default' => sprintf( __( '%s', 'chip-for-woocommerce' ), $this->title ), + 'default' => sprintf( __( '%s', 'chip-for-woocommerce' ), $this->title ), ); $this->form_fields['method_title'] = array( - 'title' => __( 'Method Title', 'chip-for-woocommerce' ), - 'type' => 'text', + 'title' => __( 'Method Title', 'chip-for-woocommerce' ), + 'type' => 'text', 'description' => __( 'This controls the title in WooCommerce Admin.', 'chip-for-woocommerce' ), - 'default' => $this->method_title, + 'default' => $this->method_title, ); $this->form_fields['description'] = array( - 'title' => __( 'Description', 'chip-for-woocommerce' ), - 'type' => 'textarea', + 'title' => __( 'Description', 'chip-for-woocommerce' ), + 'type' => 'textarea', 'description' => __( 'This controls the description which the user sees during checkout.', 'chip-for-woocommerce' ), - 'default' => __( 'Pay with Online Banking (FPX)', 'chip-for-woocommerce' ), + 'default' => __( 'Pay with Online Banking (FPX)', 'chip-for-woocommerce' ), ); $this->form_fields['credentials'] = array( - 'title' => __( 'Credentials', 'chip-for-woocommerce' ), - 'type' => 'title', + 'title' => __( 'Credentials', 'chip-for-woocommerce' ), + 'type' => 'title', 'description' => __( 'Options to set Brand ID and Secret Key.', 'chip-for-woocommerce' ), ); $this->form_fields['brand_id'] = array( - 'title' => __( 'Brand ID', 'chip-for-woocommerce' ), - 'type' => 'text', - 'description' => __( 'Brand ID can be obtained from CHIP Collect Dashboard >> Developers >> Brands', 'chip-for-woocommerce' ), - 'sanitize_callback' => function($value) { - $value = trim($value); - $value = str_replace(' ', '', $value); - return $value; - } + 'title' => __( 'Brand ID', 'chip-for-woocommerce' ), + 'type' => 'text', + 'description' => __( 'Brand ID can be obtained from CHIP Collect Dashboard >> Developers >> Brands', 'chip-for-woocommerce' ), + 'sanitize_callback' => function ( $value ) { + $value = trim( $value ); + $value = str_replace( ' ', '', $value ); + return $value; + }, ); $this->form_fields['secret_key'] = array( - 'title' => __( 'Secret key', 'chip-for-woocommerce' ), - 'type' => 'text', - 'description' => __( 'Secret key can be obtained from CHIP Collect Dashboard >> Developers >> Keys', 'chip-for-woocommerce' ), - 'sanitize_callback' => function($value) { - $value = trim($value); - $value = str_replace(' ', '', $value); - return $value; - } + 'title' => __( 'Secret key', 'chip-for-woocommerce' ), + 'type' => 'text', + 'description' => __( 'Secret key can be obtained from CHIP Collect Dashboard >> Developers >> Keys', 'chip-for-woocommerce' ), + 'sanitize_callback' => function ( $value ) { + $value = trim( $value ); + $value = str_replace( ' ', '', $value ); + return $value; + }, ); $this->form_fields['miscellaneous'] = array( - 'title' => __( 'Miscellaneous', 'chip-for-woocommerce' ), - 'type' => 'title', + 'title' => __( 'Miscellaneous', 'chip-for-woocommerce' ), + 'type' => 'title', 'description' => __( 'Options to set display logo, due strict, send receipt, time zone, tokenization and payment method whitelist.', 'chip-for-woocommerce' ), ); $this->form_fields['display_logo'] = array( - 'title' => __( 'Display Logo', 'chip-for-woocommerce' ), - 'type' => 'select', - 'class' => 'wc-enhanced-select', - 'description' => sprintf( __( 'This controls which logo appeared on checkout page. Logo. FPX B2C. FPX B2B1. E-Wallet. Card.', 'chip-for-woocommerce' ), WC_CHIP_URL . 'assets/logo.png', WC_CHIP_URL . 'assets/fpx.png', WC_CHIP_URL . 'assets/fpx_b2b1.png', WC_CHIP_URL . 'assets/ewallet.png', WC_CHIP_URL . 'assets/card.png' ), - 'default' => 'logo', - 'options' => array( - 'logo' => 'CHIP Logo', - 'fpx' => 'FPX B2C', - 'fpx_b2b1' => 'FPX B2B1', - 'ewallet' => 'E-Wallet', - 'card' => 'Card', - 'fpx_only' => 'FPX Only', - 'ewallet_only' => 'E-Wallet Only', - 'card_only' => 'Card Only', - 'card_international' => 'Card with Maestro', - 'card_international_only' => 'Card with Maestro Only', - - 'paywithchip_all' => 'Pay with CHIP (All)', - 'paywithchip_fpx' => 'Pay with CHIP (FPX)', - - 'duitnow' => 'Duitnow QR', - 'duitnow_only' => 'Duitnow QR Only', - ), + 'title' => __( 'Display Logo', 'chip-for-woocommerce' ), + 'type' => 'select', + 'class' => 'wc-enhanced-select', + 'description' => sprintf( __( 'This controls which logo appeared on checkout page. Logo. FPX B2C. FPX B2B1. E-Wallet. Card.', 'chip-for-woocommerce' ), WC_CHIP_URL . 'assets/logo.png', WC_CHIP_URL . 'assets/fpx.png', WC_CHIP_URL . 'assets/fpx_b2b1.png', WC_CHIP_URL . 'assets/ewallet.png', WC_CHIP_URL . 'assets/card.png' ), + 'default' => 'fpx_only', + 'options' => array( + 'logo' => 'CHIP Logo', + 'fpx' => 'FPX B2C', + 'fpx_b2b1' => 'FPX B2B1', + 'ewallet' => 'E-Wallet', + 'card' => 'Card', + 'fpx_only' => 'FPX Only', + 'ewallet_only' => 'E-Wallet Only', + 'card_only' => 'Card Only', + 'card_international' => 'Card with Maestro', + 'card_international_only' => 'Card with Maestro Only', + + 'paywithchip_all' => 'Pay with CHIP (All)', + 'paywithchip_fpx' => 'Pay with CHIP (FPX)', + + 'duitnow' => 'Duitnow QR', + 'duitnow_only' => 'Duitnow QR Only', + ), ); $this->form_fields['due_strict'] = array( - 'title' => __( 'Due Strict', 'chip-for-woocommerce' ), - 'type' => 'checkbox', + 'title' => __( 'Due Strict', 'chip-for-woocommerce' ), + 'type' => 'checkbox', 'description' => __( 'Enforce due strict payment timeframe to block payment after due strict timing is passed.', 'chip-for-woocommerce' ), - 'default' => 'yes', + 'default' => 'yes', ); $this->form_fields['due_strict_timing'] = array( - 'title' => __( 'Due Strict Timing (minutes)', 'chip-for-woocommerce' ), - 'type' => 'number', + 'title' => __( 'Due Strict Timing (minutes)', 'chip-for-woocommerce' ), + 'type' => 'number', 'description' => sprintf( __( 'Due strict timing in minutes. Default to hold stock minutes: %1$s. This will only be enforced if Due Strict option is activated.', 'chip-for-woocommerce' ), get_option( 'woocommerce_hold_stock_minutes', '60' ) ), - 'default' => get_option( 'woocommerce_hold_stock_minutes', '60' ), + 'default' => get_option( 'woocommerce_hold_stock_minutes', '60' ), ); $this->form_fields['purchase_send_receipt'] = array( - 'title' => __( 'Purchase Send Receipt', 'chip-for-woocommerce' ), - 'type' => 'checkbox', + 'title' => __( 'Purchase Send Receipt', 'chip-for-woocommerce' ), + 'type' => 'checkbox', 'description' => __( 'Tick to ask CHIP to send a receipt to the customer upon successful payment. If enabled, CHIP will send a purchase receipt once payment is completed. WooCommerce will always sends order processing and completed notifications to the customer.', 'chip-for-woocommerce' ), - 'default' => 'no', + 'default' => 'no', ); $this->form_fields['purchase_time_zone'] = array( - 'title' => __( 'Purchase Time Zone', 'chip-for-woocommerce' ), - 'type' => 'select', - 'class' => 'wc-enhanced-select', + 'title' => __( 'Purchase Time Zone', 'chip-for-woocommerce' ), + 'type' => 'select', + 'class' => 'wc-enhanced-select', 'description' => __( 'Time zone setting for receipt page.', 'chip-for-woocommerce' ), - 'default' => 'Asia/Kuala_Lumpur', - 'options' => $this->get_timezone_list() + 'default' => 'Asia/Kuala_Lumpur', + 'options' => $this->get_timezone_list(), ); $this->form_fields['update_client_information'] = array( - 'title' => __( 'Update client information', 'chip-for-woocommerce' ), - 'type' => 'checkbox', + 'title' => __( 'Update client information', 'chip-for-woocommerce' ), + 'type' => 'checkbox', 'description' => __( 'Tick to update client information on purchase creation.', 'chip-for-woocommerce' ), - 'default' => 'yes', + 'default' => 'yes', ); $this->form_fields['system_url_scheme'] = array( - 'title' => __( 'System URL Scheme', 'chip-for-woocommerce' ), - 'type' => 'select', - 'class' => 'wc-enhanced-select', + 'title' => __( 'System URL Scheme', 'chip-for-woocommerce' ), + 'type' => 'select', + 'class' => 'wc-enhanced-select', 'description' => __( 'Choose https if you are facing issue with payment status update due to http to https redirection', 'chip-for-woocommerce' ), - 'default' => 'https', - 'options' => array( - 'default' => __( 'System Default', 'chip-for-woocommerce' ), - 'https' => __( 'HTTPS', 'chip-for-woocommerce' ), - ) + 'default' => 'https', + 'options' => array( + 'default' => __( 'System Default', 'chip-for-woocommerce' ), + 'https' => __( 'HTTPS', 'chip-for-woocommerce' ), + ), ); $this->form_fields['bypass_chip'] = array( - 'title' => __( 'Bypass CHIP payment page', 'chip-for-woocommerce' ), - 'type' => 'checkbox', + 'title' => __( 'Bypass CHIP payment page', 'chip-for-woocommerce' ), + 'type' => 'checkbox', 'description' => __( 'Tick to bypass CHIP payment page.', 'chip-for-woocommerce' ), - 'default' => 'yes', + 'default' => 'yes', ); $this->form_fields['disable_recurring_support'] = array( - 'title' => __( 'Disable card recurring support', 'chip-for-woocommerce' ), - 'type' => 'checkbox', + 'title' => __( 'Disable card recurring support', 'chip-for-woocommerce' ), + 'type' => 'checkbox', 'description' => __( 'Tick to disable card recurring support. This only applies to Visa, Mastercard and Maestro.', 'chip-for-woocommerce' ), - 'default' => 'no', + 'default' => 'no', ); $this->form_fields['disable_clients_api'] = array( - 'title' => __( 'Disable CHIP clients API', 'chip-for-woocommerce' ), - 'type' => 'checkbox', + 'title' => __( 'Disable CHIP clients API', 'chip-for-woocommerce' ), + 'type' => 'checkbox', 'description' => __( 'Tick to disable CHIP clients API integration.', 'chip-for-woocommerce' ), - 'default' => 'no', + 'default' => 'no', ); $this->form_fields['enable_auto_clear_cart'] = array( - 'title' => __( 'Enable auto clear Cart', 'chip-for-woocommerce' ), - 'type' => 'checkbox', - 'label' => __( 'Enable clear cart upon checkout', 'chip-for-woocommerce' ), + 'title' => __( 'Enable auto clear Cart', 'chip-for-woocommerce' ), + 'type' => 'checkbox', + 'label' => __( 'Enable clear cart upon checkout', 'chip-for-woocommerce' ), 'default' => 'no', ); $this->form_fields['force_tokenization'] = array( - 'title' => __( 'Force Tokenization', 'chip-for-woocommerce' ), - 'type' => 'checkbox', + 'title' => __( 'Force Tokenization', 'chip-for-woocommerce' ), + 'type' => 'checkbox', 'description' => __( 'Tick to force tokenization if possible. This only applies when Visa or Mastercard or Maestro payment method are available.', 'chip-for-woocommerce' ), - 'default' => 'no', - 'disabled' => empty( $this->arecuring_p ) + 'default' => 'no', + 'disabled' => empty( $this->arecuring_p ), ); $this->form_fields['payment_method_whitelist'] = array( - 'title' => __( 'Payment Method Whitelist', 'chip-for-woocommerce' ), - 'type' => 'multiselect', - 'class' => 'wc-enhanced-select', + 'title' => __( 'Payment Method Whitelist', 'chip-for-woocommerce' ), + 'type' => 'multiselect', + 'class' => 'wc-enhanced-select', 'description' => __( 'Choose payment method to enforce payment method whitelisting if possible.', 'chip-for-woocommerce' ), - 'default' => [ 'fpx' ], - 'options' => $this->a_payment_m, - 'disabled' => empty( $this->a_payment_m ) + 'default' => array( 'fpx' ), + 'options' => $this->a_payment_m, + 'disabled' => empty( $this->a_payment_m ), ); $this->form_fields['public_key'] = array( - 'title' => __( 'Public Key', 'chip-for-woocommerce' ), - 'type' => 'textarea', + 'title' => __( 'Public Key', 'chip-for-woocommerce' ), + 'type' => 'textarea', 'description' => __( 'Public key for validating callback will be auto-filled upon successful configuration.', 'chip-for-woocommerce' ), - 'disabled' => true, + 'disabled' => true, ); $this->form_fields['cancel_order_flow'] = array( - 'title' => __( 'Cancel Order Flow', 'chip-for-woocommerce' ), - 'type' => 'checkbox', + 'title' => __( 'Cancel Order Flow', 'chip-for-woocommerce' ), + 'type' => 'checkbox', 'description' => __( 'Tick to redirect customer to cancel order URL for unsuccessful payment.', 'chip-for-woocommerce' ), - 'default' => 'no', + 'default' => 'no', ); $this->form_fields['email_fallback'] = array( - 'title' => __( 'Email fallback', 'chip-for-woocommerce' ), - 'type' => 'email', - 'description' => __( 'Required if the merchant did not intend to request customer email address', 'chip-for-woocommerce' ), - 'placeholder' => 'merchant@gmail.com', + 'title' => __( 'Email fallback', 'chip-for-woocommerce' ), + 'type' => 'email', + 'description' => __( 'Required if the merchant did not intend to request customer email address', 'chip-for-woocommerce' ), + 'placeholder' => 'merchant@gmail.com', ); $this->form_fields['metabox'] = array( - 'title' => __( 'Metaboxes', 'chip-for-woocommerce' ), - 'type' => 'title', + 'title' => __( 'Metaboxes', 'chip-for-woocommerce' ), + 'type' => 'title', 'description' => sprintf( __( 'Option to set meta boxes', 'chip-for-woocommerce' ) ), ); $this->form_fields['enable_metabox'] = array( - 'title' => __( 'Enable account metabox', 'chip-for-woocommerce' ), - 'type' => 'checkbox', + 'title' => __( 'Enable account metabox', 'chip-for-woocommerce' ), + 'type' => 'checkbox', 'description' => __( 'Tick to enable account metabox in WordPress Dashboard. If you are using the same CHIP account for other payment method, you should enable only once.', 'chip-for-woocommerce' ), ); $this->form_fields['webhooks'] = array( - 'title' => __( 'Webhooks', 'chip-for-woocommerce' ), - 'type' => 'title', + 'title' => __( 'Webhooks', 'chip-for-woocommerce' ), + 'type' => 'title', 'description' => sprintf( __( 'Option to set public key. The supported event is %1$s', 'chip-for-woocommerce' ), 'Purchase Recurring Token Deleted' ), ); - $callback_url = preg_replace( "/^http:/i", "https:", add_query_arg( [ 'callback_flag' => 'yes' ], WC()->api_request_url( $this->id ) ) ); + $callback_url = preg_replace( '/^http:/i', 'https:', add_query_arg( array( 'callback_flag' => 'yes' ), WC()->api_request_url( $this->id ) ) ); $this->form_fields['webhook_public_key'] = array( - 'title' => __( 'Public Key', 'chip-for-woocommerce' ), - 'type' => 'textarea', + 'title' => __( 'Public Key', 'chip-for-woocommerce' ), + 'type' => 'textarea', 'description' => sprintf( __( 'This option to set public key that are generated through CHIP Dashboard >> Webhooks page. The callback url is: %s', 'chip-for-woocommerce' ), $callback_url ), ); $this->form_fields['additional_charges'] = array( - 'title' => __( 'Additional Charges', 'chip-for-woocommerce' ), - 'type' => 'title', + 'title' => __( 'Additional Charges', 'chip-for-woocommerce' ), + 'type' => 'title', 'description' => __( 'Options to add additional charges after checkout. This option doesn\'t apply to Woocommerce Pre-order fee.', 'chip-for-woocommerce' ), ); $this->form_fields['enable_additional_charges'] = array( - 'title' => __( 'Enable Additional Charges', 'chip-for-woocommerce' ), - 'type' => 'checkbox', + 'title' => __( 'Enable Additional Charges', 'chip-for-woocommerce' ), + 'type' => 'checkbox', 'description' => __( 'Tick to activate additional charges.', 'chip-for-woocommerce' ), ); $this->form_fields['fixed_charges'] = array( - 'title' => __( 'Fixed Charges (cents)', 'chip-for-woocommerce' ), - 'type' => 'number', + 'title' => __( 'Fixed Charges (cents)', 'chip-for-woocommerce' ), + 'type' => 'number', 'description' => __( 'Fixed charges in cents. Default to: 100. This will only be applied when additional charges are activated.', 'chip-for-woocommerce' ), - 'default' => '100', + 'default' => '100', ); $this->form_fields['percent_charges'] = array( - 'title' => __( 'Percentage Charges (%)', 'chip-for-woocommerce' ), - 'type' => 'number', + 'title' => __( 'Percentage Charges (%)', 'chip-for-woocommerce' ), + 'type' => 'number', 'description' => __( 'Percentage charges. Input 100 for 1%. Default to: 0. This will only be applied when additional charges are activated.', 'chip-for-woocommerce' ), - 'default' => '0', + 'default' => '0', ); $this->form_fields['troubleshooting'] = array( - 'title' => __( 'Troubleshooting', 'chip-for-woocommerce' ), - 'type' => 'title', + 'title' => __( 'Troubleshooting', 'chip-for-woocommerce' ), + 'type' => 'title', 'description' => __( 'Options to disable redirect, disable callback and turn on debugging.', 'chip-for-woocommerce' ), ); $this->form_fields['disable_redirect'] = array( - 'title' => __( 'Disable Redirect', 'chip-for-woocommerce' ), - 'type' => 'checkbox', + 'title' => __( 'Disable Redirect', 'chip-for-woocommerce' ), + 'type' => 'checkbox', 'description' => __( 'Disable redirect for troubleshooting purpose.', 'chip-for-woocommerce' ), ); $this->form_fields['disable_callback'] = array( - 'title' => __( 'Disable Callback', 'chip-for-woocommerce' ), - 'type' => 'checkbox', + 'title' => __( 'Disable Callback', 'chip-for-woocommerce' ), + 'type' => 'checkbox', 'description' => __( 'Disable callback for troubleshooting purpose.', 'chip-for-woocommerce' ), ); $this->form_fields['debug'] = array( - 'title' => __( 'Debug Log', 'chip-for-woocommerce' ), - 'type' => 'checkbox', - 'label' => __( 'Enable logging', 'chip-for-woocommerce' ), - 'default' => 'no', + 'title' => __( 'Debug Log', 'chip-for-woocommerce' ), + 'type' => 'checkbox', + 'label' => __( 'Enable logging', 'chip-for-woocommerce' ), + 'default' => 'no', 'description' => sprintf( __( 'Log events to %s', 'chip-for-woocommerce' ), esc_url( admin_url( 'admin.php?page=wc-status&tab=logs&source=chip-for-woocommerce' ) ) ), ); @@ -710,8 +717,7 @@ private function get_timezone_list() { $formatted_time_zones = array(); foreach ( $list_time_zones as $mtz ) { - $formatted_time_zones[ $mtz ] = str_replace( "_", " ", $mtz ); - ; + $formatted_time_zones[ $mtz ] = str_replace( '_', ' ', $mtz ); } return $formatted_time_zones; @@ -731,7 +737,7 @@ public function payment_fields() { parent::payment_fields(); // Check for razer - $pattern = "/^razer_/"; + $pattern = '/^razer_/'; $is_razer = false; // Check if payment_met empty @@ -744,26 +750,35 @@ public function payment_fields() { } if ( is_array( $this->payment_met ) and count( $this->payment_met ) == 1 and $this->payment_met[0] == 'fpx' and $this->bypass_chip == 'yes' ) { - woocommerce_form_field( 'chip_fpx_bank', array( - 'type' => 'select', - 'required' => true, - 'label' => __( 'Internet Banking', 'chip-for-woocommerce' ), - 'options' => $this->list_fpx_banks(), - ) ); + woocommerce_form_field( + 'chip_fpx_bank', + array( + 'type' => 'select', + 'required' => true, + 'label' => __( 'Internet Banking', 'chip-for-woocommerce' ), + 'options' => $this->list_fpx_banks(), + ) + ); } elseif ( is_array( $this->payment_met ) and count( $this->payment_met ) == 1 and $this->payment_met[0] == 'fpx_b2b1' and $this->bypass_chip == 'yes' ) { - woocommerce_form_field( 'chip_fpx_b2b1_bank', array( - 'type' => 'select', - 'required' => true, - 'label' => __( 'Corporate Internet Banking', 'chip-for-woocommerce' ), - 'options' => $this->list_fpx_b2b1_banks() - ) ); + woocommerce_form_field( + 'chip_fpx_b2b1_bank', + array( + 'type' => 'select', + 'required' => true, + 'label' => __( 'Corporate Internet Banking', 'chip-for-woocommerce' ), + 'options' => $this->list_fpx_b2b1_banks(), + ) + ); } elseif ( is_array( $this->payment_met ) and $is_razer and $this->bypass_chip == 'yes' ) { - woocommerce_form_field( 'chip_razer_ewallet', array( - 'type' => 'select', - 'required' => true, - 'label' => __( 'E-Wallet', 'chip-for-woocommerce' ), - 'options' => $this->list_razer_ewallets() - ) ); + woocommerce_form_field( + 'chip_razer_ewallet', + array( + 'type' => 'select', + 'required' => true, + 'label' => __( 'E-Wallet', 'chip-for-woocommerce' ), + 'options' => $this->list_razer_ewallets(), + ) + ); } elseif ( $this->id == 'wc_gateway_chip_5' ) { // do nothing } @@ -771,7 +786,7 @@ public function payment_fields() { if ( ! is_wc_endpoint_url( 'order-pay' ) and ! is_add_payment_method_page() and is_array( $this->payment_met ) and count( $this->payment_met ) >= 2 and $this->bypass_chip == 'yes' and ! isset( $_GET['change_payment_method'] ) ) { foreach ( $this->payment_met as $pm ) { - if ( in_array( $pm, [ 'visa', 'mastercard', 'maestro' ] ) ) { + if ( in_array( $pm, array( 'visa', 'mastercard', 'maestro' ) ) ) { wp_enqueue_script( "wc-{$this->id}-direct-post" ); $this->form(); break; @@ -780,6 +795,11 @@ public function payment_fields() { } } + /** + * Get the language code if WPML is installed. + * + * @return string + */ public function get_language() { if ( defined( 'ICL_LANGUAGE_CODE' ) ) { $ln = ICL_LANGUAGE_CODE; @@ -810,8 +830,16 @@ public function get_language() { return $ln; } + /** + * Validate checkout fields. If validation fails, an exception is thrown. + * This method is called from the checkout process. + * Only applicable when: + * - Payment method whitelist contains only one payment method. + * - Bypass CHIP payment page is enabled. + * + * @throws Exception When no payment method is selected. + */ public function validate_fields() { - // Check and throw error if payment method not selected if ( is_array( $this->payment_met ) and count( $this->payment_met ) == 1 and $this->bypass_chip == 'yes' ) { if ( $this->payment_met[0] == 'fpx' and isset( $_POST['chip_fpx_bank'] ) and strlen( $_POST['chip_fpx_bank'] ) == 0 ) { throw new Exception( __( 'Internet Banking is a required field.', 'chip-for-woocommerce' ) ); @@ -820,11 +848,11 @@ public function validate_fields() { } } - // Check for razer - $pattern = "/^razer_/"; + // Check for razer. + $pattern = '/^razer_/'; $is_razer = false; - // Check if payment_met empty + // Check if payment_met empty. if ( is_array( $this->payment_met ) ) { $output = preg_grep( $pattern, $this->payment_met ); @@ -834,22 +862,30 @@ public function validate_fields() { } if ( is_array( $this->payment_met ) and $this->bypass_chip == 'yes' and $is_razer and isset( $_POST['chip_razer_ewallet'] ) and strlen( $_POST['chip_razer_ewallet'] ) == 0 ) { - throw new Exception( __( "E-Wallet is a required field. $this->payment_met", 'chip-for-woocommerce' ) ); + throw new Exception( + wp_kses_post( __( 'E-Wallet is a required field.', 'chip-for-woocommerce' ) ) + ); } return true; } + /** + * Process the payment and return the result. + * + * @param int $order_id Order ID. + * @return array + */ public function process_payment( $order_id ) { do_action( 'wc_' . $this->id . '_before_process_payment', $order_id, $this ); - // Start of logic for subscription_payment_method_change_customer supports + // Start of logic for subscription_payment_method_change_customer supports. if ( isset( $_GET['change_payment_method'] ) and $_GET['change_payment_method'] == $order_id ) { return $this->process_payment_method_change( $order_id ); } - // End of logic for subscription_payment_method_change_customer supports + // End of logic for subscription_payment_method_change_customer supports. - $order = new WC_Order( $order_id ); + $order = new WC_Order( $order_id ); $user_id = $order->get_user_id(); $token_id = ''; @@ -870,69 +906,71 @@ public function process_payment( $order_id ) { $this->add_item_order_fee( $order ); } - $callback_url = add_query_arg( [ 'id' => $order_id ], WC()->api_request_url( $this->id ) ); + $callback_url = add_query_arg( array( 'id' => $order_id ), WC()->api_request_url( $this->id ) ); if ( defined( 'WC_CHIP_OLD_URL_SCHEME' ) and WC_CHIP_OLD_URL_SCHEME === true ) { $callback_url = home_url( '/?wc-api=' . get_class( $this ) . '&id=' . $order_id ); } - $params = [ + $params = array( 'success_callback' => $callback_url, 'success_redirect' => $callback_url, 'failure_redirect' => $callback_url, - 'cancel_redirect' => $callback_url, - 'force_recurring' => $this->force_token == 'yes', - 'send_receipt' => $this->purchase_sr == 'yes', - 'creator_agent' => 'WooCommerce: ' . WC_CHIP_MODULE_VERSION, - 'reference' => $order->get_id(), - 'platform' => 'woocommerce', - 'due' => $this->get_due_timestamp(), - 'purchase' => [ + 'cancel_redirect' => $callback_url, + 'force_recurring' => $this->force_token == 'yes', + 'send_receipt' => $this->purchase_sr == 'yes', + 'creator_agent' => 'WooCommerce: ' . WC_CHIP_MODULE_VERSION, + 'reference' => $order->get_id(), + 'platform' => 'woocommerce', + 'due' => $this->get_due_timestamp(), + 'purchase' => array( 'total_override' => round( $order->get_total() * 100 ), - 'due_strict' => $this->due_strict == 'yes', - 'timezone' => $this->purchase_tz, - 'currency' => $order->get_currency(), - 'language' => $this->get_language(), - 'products' => [], - ], - 'brand_id' => $this->brand_id, - 'client' => [ - 'email' => $order->get_billing_email(), - 'phone' => substr( $order->get_billing_phone(), 0, 32 ), - 'full_name' => $this->filter_customer_full_name( $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() ), - 'street_address' => substr( $order->get_billing_address_1() . ' ' . $order->get_billing_address_2(), 0, 128 ), - 'country' => substr( $order->get_billing_country(), 0, 2 ), - 'city' => substr( $order->get_billing_city(), 0, 128 ), - 'zip_code' => substr( $order->get_billing_postcode(), 0, 32 ), - 'state' => substr( $order->get_billing_state(), 0, 128 ), + 'due_strict' => $this->due_strict == 'yes', + 'timezone' => $this->purchase_tz, + 'currency' => $order->get_currency(), + 'language' => $this->get_language(), + 'products' => array(), + ), + 'brand_id' => $this->brand_id, + 'client' => array( + 'email' => $order->get_billing_email(), + 'phone' => substr( $order->get_billing_phone(), 0, 32 ), + 'full_name' => $this->filter_customer_full_name( $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() ), + 'street_address' => substr( $order->get_billing_address_1() . ' ' . $order->get_billing_address_2(), 0, 128 ), + 'country' => substr( $order->get_billing_country(), 0, 2 ), + 'city' => substr( $order->get_billing_city(), 0, 128 ), + 'zip_code' => substr( $order->get_billing_postcode(), 0, 32 ), + 'state' => substr( $order->get_billing_state(), 0, 128 ), 'shipping_street_address' => substr( $order->get_shipping_address_1() . ' ' . $order->get_shipping_address_2(), 0, 128 ), - 'shipping_country' => substr( $order->get_shipping_country(), 0, 2 ), - 'shipping_city' => substr( $order->get_shipping_city(), 0, 128 ), - 'shipping_zip_code' => substr( $order->get_shipping_postcode(), 0, 32 ), - 'shipping_state' => substr( $order->get_shipping_state(), 0, 128 ), - ], - ]; + 'shipping_country' => substr( $order->get_shipping_country(), 0, 2 ), + 'shipping_city' => substr( $order->get_shipping_city(), 0, 128 ), + 'shipping_zip_code' => substr( $order->get_shipping_postcode(), 0, 32 ), + 'shipping_state' => substr( $order->get_shipping_state(), 0, 128 ), + ), + ); $items = $order->get_items(); if ( is_countable( $items ) and count( $items ) > 100 ) { - $params['purchase']['products'] = [ [ - 'name' => 'Order #' . $order->get_id(), - 'price' => round( $order->get_total() * 100 ), - ] ]; + $params['purchase']['products'] = array( + array( + 'name' => 'Order #' . $order->get_id(), + 'price' => round( $order->get_total() * 100 ), + ), + ); } else { foreach ( $items as $item ) { - /** @var \WC_Order_Item_Product $item **/ + /** @var \WC_Order_Item_Product $item */ $price = round( $item->get_total() * 100 ); - $qty = $item->get_quantity(); + $qty = $item->get_quantity(); if ( $price < 0 ) { $price = 0; } $params['purchase']['products'][] = array( - 'name' => substr( $item->get_name(), 0, 256 ), - 'price' => round( $price / $qty ), - 'quantity' => $qty + 'name' => substr( $item->get_name(), 0, 256 ), + 'price' => round( $price / $qty ), + 'quantity' => $qty, ); } } @@ -943,10 +981,10 @@ public function process_payment( $order_id ) { if ( empty( $params['purchase']['products'] ) ) { $params['purchase']['products'] = array( - [ - 'name' => 'Product', + array( + 'name' => 'Product', 'price' => round( $order->get_total() * 100 ), - ] + ), ); } @@ -962,8 +1000,8 @@ public function process_payment( $order_id ) { if ( $user and $this->disable_cli != 'yes' ) { $params['client']['email'] = $user->user_email; - $client_with_params = $params['client']; - $old_client_records = true; + $client_with_params = $params['client']; + $old_client_records = true; unset( $params['client'] ); $params['client_id'] = get_user_meta( $order->get_user_id(), '_' . $this->id . '_client_id_' . substr( $this->secret_key, -8, -2 ), true ); @@ -981,7 +1019,7 @@ public function process_payment( $order_id ) { $client = $get_client['results'][0]; } else { $old_client_records = false; - $client = $chip->create_client( $client_with_params ); + $client = $chip->create_client( $client_with_params ); } update_user_meta( $order->get_user_id(), '_' . $this->id . '_client_id_' . substr( $this->secret_key, -8, -2 ), $client['id'] ); @@ -998,15 +1036,15 @@ public function process_payment( $order_id ) { $params['payment_method_whitelist'] = $this->payment_met; } - if ( isset( $_POST[ "wc-{$this->id}-new-payment-method" ] ) and in_array( $_POST[ "wc-{$this->id}-new-payment-method" ], [ 'true', 1 ] ) ) { + if ( isset( $_POST[ "wc-{$this->id}-new-payment-method" ] ) and in_array( $_POST[ "wc-{$this->id}-new-payment-method" ], array( 'true', 1 ) ) ) { $params['payment_method_whitelist'] = $this->get_payment_method_for_recurring(); - $params['force_recurring'] = true; + $params['force_recurring'] = true; } if ( function_exists( 'wcs_order_contains_subscription' ) ) { if ( $this->supports( 'tokenization' ) and wcs_order_contains_subscription( $order ) ) { $params['payment_method_whitelist'] = $this->get_payment_method_for_recurring(); - $params['force_recurring'] = true; + $params['force_recurring'] = true; if ( $params['purchase']['total_override'] == 0 ) { $params['skip_capture'] = true; @@ -1015,7 +1053,7 @@ public function process_payment( $order_id ) { } if ( $this->system_url_ == 'https' ) { - $params['success_callback'] = preg_replace( "/^http:/i", "https:", $params['success_callback'] ); + $params['success_callback'] = preg_replace( '/^http:/i', 'https:', $params['success_callback'] ); } if ( $this->disable_cal == 'yes' ) { @@ -1049,7 +1087,7 @@ public function process_payment( $order_id ) { WC_Pre_Orders_Order::mark_order_as_pre_ordered( $order ); return array( - 'result' => 'success', + 'result' => 'success', 'redirect' => $this->get_return_url( $order ), ); } @@ -1101,7 +1139,7 @@ public function process_payment( $order_id ) { $order->add_order_note( sprintf( __( 'Token ID: %1$s', 'chip-for-woocommerce' ), $token->get_token() ) ); $this->maybe_delete_payment_token( $charge_payment, $token_id ); - $get_payment = $chip->get_payment( $payment['id'] ); + $get_payment = $chip->get_payment( $payment['id'] ); $payment_requery_status = $get_payment['status']; } @@ -1122,7 +1160,7 @@ public function process_payment( $order_id ) { if ( is_array( $payment['payment_method_whitelist'] ) and ! empty( $payment['payment_method_whitelist'] ) ) { foreach ( $payment['payment_method_whitelist'] as $pm ) { - if ( ! in_array( $pm, [ 'visa', 'mastercard', 'maestro' ] ) ) { + if ( ! in_array( $pm, array( 'visa', 'mastercard', 'maestro' ) ) ) { $redirect_url = $payment['checkout_url']; break; } @@ -1134,7 +1172,7 @@ public function process_payment( $order_id ) { do_action( 'wc_' . $this->id . '_after_process_payment', $order_id, $this ); return array( - 'result' => 'success', + 'result' => 'success', 'redirect' => esc_url_raw( $this->bypass_chip( $redirect_url, $payment ) ), 'messages' => '
' . __( 'Redirecting to CHIP', 'chip-for-woocommerce' ) . '
', ); @@ -1149,7 +1187,7 @@ public function get_due_timestamp() { } public function can_refund_order( $order ) { - $has_api_creds = $this->enabled and $this->secret_key and $this->brand_id; + $has_api_creds = $this->enabled and $this->secret_key and $this->brand_id; $can_refund_order = $order and $order->get_transaction_id() and $has_api_creds; return apply_filters( 'wc_' . $this->id . '_can_refund_order', $can_refund_order, $order, $this ); @@ -1166,8 +1204,8 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { return new WP_Error( 'error', __( 'Refund failed.', 'chip-for-woocommerce' ) ); } - $chip = $this->api(); - $params = [ 'amount' => round( $amount * 100 ) ]; + $chip = $this->api(); + $params = array( 'amount' => round( $amount * 100 ) ); $result = $chip->refund_payment( $order->get_transaction_id(), $params ); @@ -1202,7 +1240,7 @@ public function process_admin_options() { parent::process_admin_options(); $post = $this->get_post_data(); - $brand_id = wc_clean( $post[ "woocommerce_{$this->id}_brand_id" ] ); + $brand_id = wc_clean( $post[ "woocommerce_{$this->id}_brand_id" ] ); $secret_key = wc_clean( $post[ "woocommerce_{$this->id}_secret_key" ] ); $chip = $this->api(); @@ -1265,50 +1303,50 @@ public function auto_charge( $total_amount, $renewal_order ) { return; } - $callback_url = add_query_arg( [ 'id' => $renewal_order_id ], WC()->api_request_url( $this->id ) ); + $callback_url = add_query_arg( array( 'id' => $renewal_order_id ), WC()->api_request_url( $this->id ) ); if ( defined( 'WC_CHIP_OLD_URL_SCHEME' ) and WC_CHIP_OLD_URL_SCHEME === true ) { $callback_url = home_url( '/?wc-api=' . get_class( $this ) . '&id=' . $renewal_order_id ); } - $params = [ + $params = array( 'success_callback' => $callback_url, - 'send_receipt' => $this->purchase_sr == 'yes', - 'creator_agent' => 'WooCommerce: ' . WC_CHIP_MODULE_VERSION, - 'reference' => $renewal_order_id, - 'platform' => 'woocommerce_subscriptions', - 'due' => $this->get_due_timestamp(), - 'brand_id' => $this->brand_id, - 'client_id' => get_user_meta( $renewal_order->get_user_id(), '_' . $this->id . '_client_id_' . substr( $this->secret_key, -8, -2 ), true ), - 'purchase' => [ - 'timezone' => $this->purchase_tz, - 'currency' => $renewal_order->get_currency(), - 'language' => $this->get_language(), - 'due_strict' => $this->due_strict == 'yes', + 'send_receipt' => $this->purchase_sr == 'yes', + 'creator_agent' => 'WooCommerce: ' . WC_CHIP_MODULE_VERSION, + 'reference' => $renewal_order_id, + 'platform' => 'woocommerce_subscriptions', + 'due' => $this->get_due_timestamp(), + 'brand_id' => $this->brand_id, + 'client_id' => get_user_meta( $renewal_order->get_user_id(), '_' . $this->id . '_client_id_' . substr( $this->secret_key, -8, -2 ), true ), + 'purchase' => array( + 'timezone' => $this->purchase_tz, + 'currency' => $renewal_order->get_currency(), + 'language' => $this->get_language(), + 'due_strict' => $this->due_strict == 'yes', 'total_override' => round( $total_amount * 100 ), - 'products' => [], - ], - ]; + 'products' => array(), + ), + ); $items = $renewal_order->get_items(); foreach ( $items as $item ) { $price = round( $item->get_total() * 100 ); - $qty = $item->get_quantity(); + $qty = $item->get_quantity(); if ( $price < 0 ) { $price = 0; } $params['purchase']['products'][] = array( - 'name' => substr( $item->get_name(), 0, 256 ), - 'price' => round( $price / $qty ), - 'quantity' => $qty + 'name' => substr( $item->get_name(), 0, 256 ), + 'price' => round( $price / $qty ), + 'quantity' => $qty, ); } $params = apply_filters( 'wc_' . $this->id . '_purchase_params', $params, $this ); - $chip = $this->api(); + $chip = $this->api(); $payment = $chip->create_payment( $params ); $renewal_order->update_meta_data( '_' . $this->id . '_purchase', $payment ); @@ -1316,7 +1354,7 @@ public function auto_charge( $total_amount, $renewal_order ) { do_action( 'wc_' . $this->id . '_chip_purchase', $payment, $renewal_order_id ); - $token = new WC_Payment_Token_CC; + $token = new WC_Payment_Token_CC(); foreach ( $tokens as $key => $t ) { if ( $t->get_gateway_id() == $this->id ) { $token = $t; @@ -1415,27 +1453,27 @@ public function add_payment_method() { $params = array( 'payment_method_whitelist' => $this->get_payment_method_for_recurring(), - 'creator_agent' => 'WooCommerce: ' . WC_CHIP_MODULE_VERSION, - 'platform' => 'woocommerce_subscriptions', - 'success_redirect' => $url, - 'failure_redirect' => $url, - 'force_recurring' => true, - 'reference' => get_current_user_id(), - 'brand_id' => $this->brand_id, - 'skip_capture' => true, - 'client' => [ - 'email' => wp_get_current_user()->user_email, - 'full_name' => $this->filter_customer_full_name( $customer->get_first_name() . ' ' . $customer->get_last_name() ) - ], - 'purchase' => [ + 'creator_agent' => 'WooCommerce: ' . WC_CHIP_MODULE_VERSION, + 'platform' => 'woocommerce_subscriptions', + 'success_redirect' => $url, + 'failure_redirect' => $url, + 'force_recurring' => true, + 'reference' => get_current_user_id(), + 'brand_id' => $this->brand_id, + 'skip_capture' => true, + 'client' => array( + 'email' => wp_get_current_user()->user_email, + 'full_name' => $this->filter_customer_full_name( $customer->get_first_name() . ' ' . $customer->get_last_name() ), + ), + 'purchase' => array( 'currency' => 'MYR', - 'products' => [ - [ - 'name' => 'Add payment method', - 'price' => 0 - ] - ] - ], + 'products' => array( + array( + 'name' => 'Add payment method', + 'price' => 0, + ), + ), + ), ); $chip = $this->api(); @@ -1474,7 +1512,7 @@ public function add_payment_method() { WC()->session->set( 'chip_preauthorize', $payment['id'] ); return array( - 'result' => 'redirect', + 'result' => 'redirect', 'redirect' => $payment['checkout_url'], ); } @@ -1541,8 +1579,8 @@ public function schedule_requery( $purchase_id, $order_id, $attempt = 1 ) { } public function payment_token_deleted( $token_id, $token ) { - $user_id = $token->get_user_id(); - $token_id = $token->get_id(); + $user_id = $token->get_user_id(); + $token_id = $token->get_id(); $payment_id = $token->get_token(); $chip_token_ids = get_user_meta( $user_id, '_' . $this->id . '_client_token_ids', true ); @@ -1571,7 +1609,7 @@ public function check_order_status( $purchase_id, $order_id, $attempt ) { try { $order = new WC_Order( $order_id ); - } catch (Exception $e) { + } catch ( Exception $e ) { $this->release_lock( $order_id ); return; } @@ -1622,27 +1660,27 @@ public function admin_notices() { public function list_fpx_banks() { $default_fpx = array( - '' => __( 'Choose your bank', 'chip-for-woocommerce' ), - 'ABB0233' => __( 'Affin Bank', 'chip-for-woocommerce' ), + '' => __( 'Choose your bank', 'chip-for-woocommerce' ), + 'ABB0233' => __( 'Affin Bank', 'chip-for-woocommerce' ), 'ABMB0212' => __( 'Alliance Bank (Personal)', 'chip-for-woocommerce' ), - 'AGRO01' => __( 'AGRONet', 'chip-for-woocommerce' ), + 'AGRO01' => __( 'AGRONet', 'chip-for-woocommerce' ), 'AMBB0209' => __( 'AmBank', 'chip-for-woocommerce' ), 'BIMB0340' => __( 'Bank Islam', 'chip-for-woocommerce' ), 'BMMB0341' => __( 'Bank Muamalat', 'chip-for-woocommerce' ), 'BKRM0602' => __( 'Bank Rakyat', 'chip-for-woocommerce' ), - 'BOCM01' => __( 'Bank Of China', 'chip-for-woocommerce' ), - 'BSN0601' => __( 'BSN', 'chip-for-woocommerce' ), + 'BOCM01' => __( 'Bank Of China', 'chip-for-woocommerce' ), + 'BSN0601' => __( 'BSN', 'chip-for-woocommerce' ), 'BCBB0235' => __( 'CIMB Bank', 'chip-for-woocommerce' ), - 'HLB0224' => __( 'Hong Leong Bank', 'chip-for-woocommerce' ), + 'HLB0224' => __( 'Hong Leong Bank', 'chip-for-woocommerce' ), 'HSBC0223' => __( 'HSBC Bank', 'chip-for-woocommerce' ), - 'KFH0346' => __( 'KFH', 'chip-for-woocommerce' ), - 'MBB0228' => __( 'Maybank2E', 'chip-for-woocommerce' ), + 'KFH0346' => __( 'KFH', 'chip-for-woocommerce' ), + 'MBB0228' => __( 'Maybank2E', 'chip-for-woocommerce' ), 'MB2U0227' => __( 'Maybank2u', 'chip-for-woocommerce' ), 'OCBC0229' => __( 'OCBC Bank', 'chip-for-woocommerce' ), - 'PBB0233' => __( 'Public Bank', 'chip-for-woocommerce' ), - 'RHB0218' => __( 'RHB Bank', 'chip-for-woocommerce' ), - 'SCB0216' => __( 'Standard Chartered', 'chip-for-woocommerce' ), - 'UOB0226' => __( 'UOB Bank', 'chip-for-woocommerce' ), + 'PBB0233' => __( 'Public Bank', 'chip-for-woocommerce' ), + 'RHB0218' => __( 'RHB Bank', 'chip-for-woocommerce' ), + 'SCB0216' => __( 'Standard Chartered', 'chip-for-woocommerce' ), + 'UOB0226' => __( 'UOB Bank', 'chip-for-woocommerce' ), ); if ( false === ( $fpx = get_transient( 'chip_fpx_b2c_banks' ) ) ) { @@ -1662,28 +1700,28 @@ public function list_fpx_banks() { public function list_fpx_b2b1_banks() { $default_fpx = array( - '' => __( 'Choose your bank', 'chip-for-woocommerce' ), - 'ABB0235' => __( 'AFFINMAX', 'chip-for-woocommerce' ), + '' => __( 'Choose your bank', 'chip-for-woocommerce' ), + 'ABB0235' => __( 'AFFINMAX', 'chip-for-woocommerce' ), 'ABMB0213' => __( 'Alliance Bank (Business)', 'chip-for-woocommerce' ), - 'AGRO02' => __( 'AGRONetBIZ', 'chip-for-woocommerce' ), + 'AGRO02' => __( 'AGRONetBIZ', 'chip-for-woocommerce' ), 'AMBB0208' => __( 'AmBank', 'chip-for-woocommerce' ), 'BIMB0340' => __( 'Bank Islam', 'chip-for-woocommerce' ), 'BMMB0342' => __( 'Bank Muamalat', 'chip-for-woocommerce' ), - 'BNP003' => __( 'BNP Paribas', 'chip-for-woocommerce' ), + 'BNP003' => __( 'BNP Paribas', 'chip-for-woocommerce' ), 'BCBB0235' => __( 'CIMB Bank', 'chip-for-woocommerce' ), - 'CIT0218' => __( 'Citibank Corporate Banking', 'chip-for-woocommerce' ), - 'DBB0199' => __( 'Deutsche Bank', 'chip-for-woocommerce' ), - 'HLB0224' => __( 'Hong Leong Bank', 'chip-for-woocommerce' ), + 'CIT0218' => __( 'Citibank Corporate Banking', 'chip-for-woocommerce' ), + 'DBB0199' => __( 'Deutsche Bank', 'chip-for-woocommerce' ), + 'HLB0224' => __( 'Hong Leong Bank', 'chip-for-woocommerce' ), 'HSBC0223' => __( 'HSBC Bank', 'chip-for-woocommerce' ), 'BKRM0602' => __( 'Bank Rakyat', 'chip-for-woocommerce' ), - 'KFH0346' => __( 'KFH', 'chip-for-woocommerce' ), - 'MBB0228' => __( 'Maybank2E', 'chip-for-woocommerce' ), + 'KFH0346' => __( 'KFH', 'chip-for-woocommerce' ), + 'MBB0228' => __( 'Maybank2E', 'chip-for-woocommerce' ), 'OCBC0229' => __( 'OCBC Bank', 'chip-for-woocommerce' ), - 'PBB0233' => __( 'Public Bank', 'chip-for-woocommerce' ), - 'PBB0234' => __( 'Public Bank PB enterprise', 'chip-for-woocommerce' ), - 'RHB0218' => __( 'RHB Bank', 'chip-for-woocommerce' ), - 'SCB0215' => __( 'Standard Chartered', 'chip-for-woocommerce' ), - 'UOB0228' => __( 'UOB Regional', 'chip-for-woocommerce' ), + 'PBB0233' => __( 'Public Bank', 'chip-for-woocommerce' ), + 'PBB0234' => __( 'Public Bank PB enterprise', 'chip-for-woocommerce' ), + 'RHB0218' => __( 'RHB Bank', 'chip-for-woocommerce' ), + 'SCB0215' => __( 'Standard Chartered', 'chip-for-woocommerce' ), + 'UOB0228' => __( 'UOB Regional', 'chip-for-woocommerce' ), ); if ( false === ( $fpx = get_transient( 'chip_fpx_b2b1_banks' ) ) ) { @@ -1715,9 +1753,9 @@ public function filter_non_available_fpx( &$default_fpx, $fpx ) { } public function list_razer_ewallets() { - $ewallet_list = [ + $ewallet_list = array( '' => __( 'Choose your e-wallet', 'chip-for-woocommerce' ), - ]; + ); if ( in_array( 'razer_atome', $this->payment_met ) ) { $ewallet_list['Atome'] = __( 'Atome', 'chip-for-woocommerce' ); @@ -1786,41 +1824,47 @@ public function bypass_chip( $url, $payment ) { public function process_payment_method_change( $order_id ) { if ( isset( $_POST[ "wc-{$this->id}-payment-token" ] ) and 'new' !== $_POST[ "wc-{$this->id}-payment-token" ] ) { return array( - 'result' => 'success', + 'result' => 'success', 'redirect' => wc_get_page_permalink( 'myaccount' ), ); } $customer = new WC_Customer( get_current_user_id() ); - $url = add_query_arg( [ 'id' => $order_id, 'process_payment_method_change' => 'yes' ], WC()->api_request_url( $this->id ) ); + $url = add_query_arg( + array( + 'id' => $order_id, + 'process_payment_method_change' => 'yes', + ), + WC()->api_request_url( $this->id ) + ); if ( defined( 'WC_CHIP_OLD_URL_SCHEME' ) and WC_CHIP_OLD_URL_SCHEME === true ) { $url = home_url( '/?wc-api=' . get_class( $this ) . '&id=' . $order_id ); } $params = array( 'payment_method_whitelist' => $this->get_payment_method_for_recurring(), - 'creator_agent' => 'WooCommerce: ' . WC_CHIP_MODULE_VERSION, - 'platform' => 'woocommerce_subscriptions', - 'success_redirect' => $url, - 'failure_redirect' => $url, - 'force_recurring' => true, - 'reference' => $order_id, - 'brand_id' => $this->brand_id, - 'skip_capture' => true, - 'client' => [ - 'email' => wp_get_current_user()->user_email, - 'full_name' => $this->filter_customer_full_name( $customer->get_first_name() . ' ' . $customer->get_last_name() ) - ], - 'purchase' => [ + 'creator_agent' => 'WooCommerce: ' . WC_CHIP_MODULE_VERSION, + 'platform' => 'woocommerce_subscriptions', + 'success_redirect' => $url, + 'failure_redirect' => $url, + 'force_recurring' => true, + 'reference' => $order_id, + 'brand_id' => $this->brand_id, + 'skip_capture' => true, + 'client' => array( + 'email' => wp_get_current_user()->user_email, + 'full_name' => $this->filter_customer_full_name( $customer->get_first_name() . ' ' . $customer->get_last_name() ), + ), + 'purchase' => array( 'currency' => 'MYR', - 'products' => [ - [ - 'name' => 'Add payment method', - 'price' => 0 - ] - ] - ], + 'products' => array( + array( + 'name' => 'Add payment method', + 'price' => 0, + ), + ), + ), ); $chip = $this->api(); @@ -1860,7 +1904,7 @@ public function process_payment_method_change( $order_id ) { if ( is_array( $payment['payment_method_whitelist'] ) and ! empty( $payment['payment_method_whitelist'] ) ) { foreach ( $payment['payment_method_whitelist'] as $pm ) { - if ( ! in_array( $pm, [ 'visa', 'mastercard', 'maestro' ] ) ) { + if ( ! in_array( $pm, array( 'visa', 'mastercard', 'maestro' ) ) ) { $redirect_url = $payment['checkout_url']; break; } @@ -1870,7 +1914,7 @@ public function process_payment_method_change( $order_id ) { } return array( - 'result' => 'success', + 'result' => 'success', 'redirect' => $redirect_url, ); } @@ -1903,7 +1947,7 @@ public function maybe_dont_update_payment_method( $update, $new_payment_method, public function handle_payment_method_change() { $subscription_id = intval( $_GET['id'] ); - $payment_id = WC()->session->get( 'chip_payment_method_change_' . $subscription_id ); + $payment_id = WC()->session->get( 'chip_payment_method_change_' . $subscription_id ); if ( ! wcs_is_subscription( $subscription_id ) ) { exit( __( 'Order is not subscription', 'chip-for-woocommerce' ) ); @@ -1920,9 +1964,9 @@ public function handle_payment_method_change() { exit( $message ); } - $payment = json_decode( $content, true ); + $payment = json_decode( $content, true ); $payment_id = array_key_exists( 'id', $payment ) ? sanitize_key( $payment['id'] ) : ''; - } else if ( $payment_id ) { + } elseif ( $payment_id ) { $payment = $this->api()->get_payment( $payment_id ); } else { exit( __( 'Unexpected response', 'chip-for-woocommerce' ) ); @@ -2030,7 +2074,7 @@ public function register_script() { true ); - wp_localize_script( "wc-{$this->id}-direct-post", 'gateway_option', [ 'id' => $this->id ] ); + wp_localize_script( "wc-{$this->id}-direct-post", 'gateway_option', array( 'id' => $this->id ) ); } public function form() { @@ -2044,7 +2088,7 @@ public function form() {

'; $default_fields = array( - 'card-name-field' => '

+ 'card-name-field' => '

', @@ -2087,31 +2131,43 @@ public function form() { } public function get_payment_method_list() { - return [ 'fpx' => 'FPX', 'fpx_b2b1' => 'FPX B2B1', 'mastercard' => 'Mastercard', 'maestro' => 'Maestro', 'visa' => 'Visa', 'razer_atome' => 'Razer Atome', 'razer_grabpay' => 'Razer Grabpay', 'razer_maybankqr' => 'Razer Maybankqr', 'razer_shopeepay' => 'Razer Shopeepay', 'razer_tng' => 'Razer Tng', 'duitnow_qr' => 'Duitnow QR' ]; + return array( + 'fpx' => 'FPX', + 'fpx_b2b1' => 'FPX B2B1', + 'mastercard' => 'Mastercard', + 'maestro' => 'Maestro', + 'visa' => 'Visa', + 'razer_atome' => 'Razer Atome', + 'razer_grabpay' => 'Razer Grabpay', + 'razer_maybankqr' => 'Razer Maybankqr', + 'razer_shopeepay' => 'Razer Shopeepay', + 'razer_tng' => 'Razer Tng', + 'duitnow_qr' => 'Duitnow QR', + ); } public function add_item_order_fee( &$order ) { foreach ( $order->get_items( 'fee' ) as $item_id => $item_value ) { - if ( in_array( $item_value->get_name( 'chip_view' ), [ 'Fixed Processing Fee', 'Variable Processing Fee' ] ) ) { + if ( in_array( $item_value->get_name( 'chip_view' ), array( 'Fixed Processing Fee', 'Variable Processing Fee' ) ) ) { $order->remove_item( $item_id ); } } // It cannot be baesd on the item id as in some cases the id is changed // if (!empty($item_id = $order->get_meta( '_chip_fixed_processing_fee', true))) { - // $item_id = absint( $item_id ); + // $item_id = absint( $item_id ); - // if ( $order->get_item( $item_id ) ) { - // $order->remove_item($item_id); - // } + // if ( $order->get_item( $item_id ) ) { + // $order->remove_item($item_id); + // } // } // if (!empty($item_id = $order->get_meta( '_chip_variable_processing_fee', true))) { - // $item_id = absint( $item_id ); + // $item_id = absint( $item_id ); - // if ( $order->get_item( $item_id ) ) { - // $order->remove_item($item_id); - // } + // if ( $order->get_item( $item_id ) ) { + // $order->remove_item($item_id); + // } // } do_action( 'wc_' . $this->id . '_before_add_item_order_fee', $order, $this ); @@ -2162,8 +2218,8 @@ public function get_payment_method_for_recurring() { if ( is_countable( $pmw = $this->get_payment_method_whitelist() ) and count( $pmw ) >= 1 ) { return $pmw; - } else if ( $this->supports( 'tokenization' ) ) { - return [ 'visa', 'mastercard' ]; // return the most generic card payment method + } elseif ( $this->supports( 'tokenization' ) ) { + return array( 'visa', 'mastercard' ); // return the most generic card payment method } return null; @@ -2208,7 +2264,7 @@ public function process_pre_order_payments( $order ) { return; } - $callback_url = add_query_arg( [ 'id' => $order->get_id() ], WC()->api_request_url( $this->id ) ); + $callback_url = add_query_arg( array( 'id' => $order->get_id() ), WC()->api_request_url( $this->id ) ); if ( defined( 'WC_CHIP_OLD_URL_SCHEME' ) and WC_CHIP_OLD_URL_SCHEME ) { $callback_url = home_url( '/?wc-api=' . get_class( $this ) . '&id=' . $order->get_id() ); } @@ -2218,48 +2274,48 @@ public function process_pre_order_payments( $order ) { } $total_pre_order_fee = WC_Pre_Orders_Product::get_pre_order_fee( $product ); - # TODO: Check if still require to minus total_pre_order_fee; + // TODO: Check if still require to minus total_pre_order_fee; $total = absint( $order->get_total() ) - absint( $total_pre_order_fee ); - $params = [ + $params = array( 'success_callback' => $callback_url, - 'send_receipt' => $this->purchase_sr == 'yes', - 'creator_agent' => 'WooCommerce: ' . WC_CHIP_MODULE_VERSION, - 'reference' => $order->get_id(), - 'platform' => 'woocommerce', - 'due' => $this->get_due_timestamp(), - 'brand_id' => $this->brand_id, - 'client_id' => get_user_meta( $order->get_user_id(), '_' . $this->id . '_client_id_' . substr( $this->secret_key, -8, -2 ), true ), - 'purchase' => [ - 'timezone' => $this->purchase_tz, - 'currency' => $order->get_currency(), - 'language' => $this->get_language(), - 'due_strict' => $this->due_strict == 'yes', + 'send_receipt' => $this->purchase_sr == 'yes', + 'creator_agent' => 'WooCommerce: ' . WC_CHIP_MODULE_VERSION, + 'reference' => $order->get_id(), + 'platform' => 'woocommerce', + 'due' => $this->get_due_timestamp(), + 'brand_id' => $this->brand_id, + 'client_id' => get_user_meta( $order->get_user_id(), '_' . $this->id . '_client_id_' . substr( $this->secret_key, -8, -2 ), true ), + 'purchase' => array( + 'timezone' => $this->purchase_tz, + 'currency' => $order->get_currency(), + 'language' => $this->get_language(), + 'due_strict' => $this->due_strict == 'yes', 'total_override' => round( $total * 100 ), - 'products' => [], - ], - ]; + 'products' => array(), + ), + ); $items = $order->get_items(); foreach ( $items as $item ) { $price = round( $item->get_total() * 100 ); - $qty = $item->get_quantity(); + $qty = $item->get_quantity(); if ( $price < 0 ) { $price = 0; } $params['purchase']['products'][] = array( - 'name' => substr( $item->get_name(), 0, 256 ), - 'price' => round( $price / $qty ), - 'quantity' => $qty + 'name' => substr( $item->get_name(), 0, 256 ), + 'price' => round( $price / $qty ), + 'quantity' => $qty, ); } $params = apply_filters( 'wc_' . $this->id . '_purchase_params', $params, $this ); - $chip = $this->api(); + $chip = $this->api(); $payment = $chip->create_payment( $params ); $order->add_order_note( @@ -2271,7 +2327,7 @@ public function process_pre_order_payments( $order ) { do_action( 'wc_' . $this->id . '_chip_purchase', $payment, $order->get_id() ); - $token = new WC_Payment_Token_CC; + $token = new WC_Payment_Token_CC(); foreach ( $tokens as $key => $t ) { if ( $t->get_gateway_id() == $this->id ) { $token = $t; @@ -2299,7 +2355,6 @@ public function process_pre_order_payments( $order ) { $order->add_order_note( sprintf( __( 'Token ID: %1$s', 'chip-for-woocommerce' ), $token->get_token() ) ); $this->release_lock( $order->get_id() ); - } public function register_metabox( $current_screen ) { @@ -2370,12 +2425,12 @@ public function load_metabox_info() { return; } - $this->chip_incoming_count = $turnover['incoming']['count']['all']; - $this->chip_incoming_fee = $turnover['incoming']['fee_sell'] / 100; + $this->chip_incoming_count = $turnover['incoming']['count']['all']; + $this->chip_incoming_fee = $turnover['incoming']['fee_sell'] / 100; $this->chip_incoming_turnover = $turnover['incoming']['turnover'] / 100; - $this->chip_outgoing_count = $turnover['outgoing']['count']['all']; - $this->chip_outgoing_fee = $turnover['outgoing']['fee_sell'] / 100; + $this->chip_outgoing_count = $turnover['outgoing']['count']['all']; + $this->chip_outgoing_fee = $turnover['outgoing']['fee_sell'] / 100; $this->chip_outgoing_turnover = $turnover['outgoing']['turnover'] / 100; $this->chip_company_balance = $balance['MYR']['available_balance'] / 100; @@ -2390,18 +2445,18 @@ public function meta_box_scripts() { $screen = get_current_screen(); // verify admin screen object if ( is_object( $screen ) ) { - if ( $screen->post_type == '' and in_array( $screen->id, [ 'dashboard' ] ) ) { + if ( $screen->post_type == '' and in_array( $screen->id, array( 'dashboard' ) ) ) { // enqueue script - wp_enqueue_script( $this->id . '_meta_box_script', WC_CHIP_URL . 'admin/meta-boxes/js/admin_' . $this->id . '.js', [ 'jquery' ] ); + wp_enqueue_script( $this->id . '_meta_box_script', WC_CHIP_URL . 'admin/meta-boxes/js/admin_' . $this->id . '.js', array( 'jquery' ) ); // localize script, create a custom js object wp_localize_script( $this->id . '_meta_box_script', $this->id . '_meta_box_obj', - [ - 'url' => admin_url( 'admin-ajax.php' ), + array( + 'url' => admin_url( 'admin-ajax.php' ), 'gateway_id' => $this->id, - ] + ) ); } } @@ -2419,15 +2474,15 @@ public function metabox_ajax_handler() { if ( array_key_exists( 'gateway_id', $_POST ) ) { if ( $_POST['gateway_id'] == $this->id ) { - $data = [ - 'balance' => number_format( $this->chip_company_balance, 2 ), - 'incoming_count' => number_format( $this->chip_incoming_count ), - 'incoming_fee' => number_format( $this->chip_incoming_fee, 2 ), + $data = array( + 'balance' => number_format( $this->chip_company_balance, 2 ), + 'incoming_count' => number_format( $this->chip_incoming_count ), + 'incoming_fee' => number_format( $this->chip_incoming_fee, 2 ), 'incoming_turnover' => number_format( $this->chip_incoming_turnover, 2 ), - 'outgoing_count' => number_format( $this->chip_outgoing_count ), - 'outgoing_fee' => number_format( $this->chip_outgoing_fee, 2 ), + 'outgoing_count' => number_format( $this->chip_outgoing_count ), + 'outgoing_fee' => number_format( $this->chip_outgoing_fee, 2 ), 'outgoing_turnover' => number_format( $this->chip_outgoing_turnover, 2 ), - ]; + ); echo json_encode( $data ); } } diff --git a/includes/class-wc-logger.php b/includes/class-wc-logger.php index a587279..d1e297e 100644 --- a/includes/class-wc-logger.php +++ b/includes/class-wc-logger.php @@ -10,4 +10,4 @@ public function __construct() { public function log( $message ) { $this->logger->notice( $message, array( 'source' => 'chip-for-woocommerce' ) ); } -} \ No newline at end of file +} diff --git a/includes/class-wc-migration.php b/includes/class-wc-migration.php index 6773487..f2a0ca5 100644 --- a/includes/class-wc-migration.php +++ b/includes/class-wc-migration.php @@ -46,16 +46,16 @@ private function v_127_to_130() { } $new_options = array( - 'enabled' => $gateway['enabled'], - 'title' => $gateway['label'], + 'enabled' => $gateway['enabled'], + 'title' => $gateway['label'], 'description' => $gateway['method_desc'], - 'brand_id' => $gateway['brand-id'], - 'secret_key' => $gateway['secret-key'], - 'debug' => $gateway['debug'], - 'public_key' => $gateway['public-key'], + 'brand_id' => $gateway['brand-id'], + 'secret_key' => $gateway['secret-key'], + 'debug' => $gateway['debug'], + 'public_key' => $gateway['public-key'], ); - $new_options['display_logo'] = 'logo'; + $new_options['display_logo'] = 'logo'; $new_options['disable_recurring_support'] = 'yes'; $new_options_2 = $new_options; @@ -70,9 +70,9 @@ private function v_127_to_130() { if ( ( $gateway_chip_card = get_option( 'woocommerce_chip-card_settings' ) ) ) { if ( ! ( get_option( 'woocommerce_wc_gateway_chip_2_settings' ) ) ) { $new_options_2['display_logo'] = 'card'; - $new_options_2['enabled'] = $gateway_chip_card['enabled']; - $new_options_2['title'] = $gateway_chip_card['label']; - $new_options_2['description'] = $gateway_chip_card['method_desc']; + $new_options_2['enabled'] = $gateway_chip_card['enabled']; + $new_options_2['title'] = $gateway_chip_card['label']; + $new_options_2['description'] = $gateway_chip_card['method_desc']; $new_options_2['payment_method_whitelist'] = array( 'visa', 'mastercard' ); @@ -86,9 +86,9 @@ private function v_127_to_130() { if ( ( $gateway_chip_b2b1 = get_option( 'woocommerce_chip-fpxb2b1_settings' ) ) ) { if ( ! ( get_option( 'woocommerce_wc_gateway_chip_3_settings' ) ) ) { $new_options_3['display_logo'] = 'fpx_b2b1'; - $new_options_3['enabled'] = $gateway_chip_b2b1['enabled']; - $new_options_3['title'] = $gateway_chip_b2b1['label']; - $new_options_3['description'] = $gateway_chip_b2b1['method_desc']; + $new_options_3['enabled'] = $gateway_chip_b2b1['enabled']; + $new_options_3['title'] = $gateway_chip_b2b1['label']; + $new_options_3['description'] = $gateway_chip_b2b1['method_desc']; $new_options_3['payment_method_whitelist'] = array( 'fpx_b2b1' ); @@ -108,4 +108,4 @@ private function v_127_to_130() { } } -Chip_Woocommerce_Migration::get_instance(); \ No newline at end of file +Chip_Woocommerce_Migration::get_instance(); diff --git a/includes/class-wc-queue.php b/includes/class-wc-queue.php index 8d6c487..62bf8dc 100644 --- a/includes/class-wc-queue.php +++ b/includes/class-wc-queue.php @@ -41,4 +41,4 @@ public function delete_payment_token( $purchase_id, $gateway_id ) { } } -Chip_Woocommerce_Queue::get_instance(); \ No newline at end of file +Chip_Woocommerce_Queue::get_instance(); diff --git a/includes/class-wc-receipt-link.php b/includes/class-wc-receipt-link.php index 5a8ceb4..f9fe115 100644 --- a/includes/class-wc-receipt-link.php +++ b/includes/class-wc-receipt-link.php @@ -22,7 +22,7 @@ public function add_actions() { } public function invoice_button( $order ) { - $gateway_id = $order->get_payment_method(); + $gateway_id = $order->get_payment_method(); $wc_gateway_chip = Chip_Woocommerce::get_chip_gateway_class( $gateway_id ); if ( $wc_gateway_chip and ( $purchase = $order->get_meta( '_' . $gateway_id . '_purchase', true ) ) ) { @@ -39,7 +39,7 @@ public function receipt_button( $order ) { return; } - $gateway_id = $order->get_payment_method(); + $gateway_id = $order->get_payment_method(); $wc_gateway_chip = Chip_Woocommerce::get_chip_gateway_class( $gateway_id ); if ( $wc_gateway_chip and ( $purchase = $order->get_meta( '_' . $gateway_id . '_purchase', true ) ) ) { @@ -55,7 +55,7 @@ public function receipt_button( $order ) { } public function view_button( $order ) { - $gateway_id = $order->get_payment_method(); + $gateway_id = $order->get_payment_method(); $wc_gateway_chip = Chip_Woocommerce::get_chip_gateway_class( $gateway_id ); if ( $wc_gateway_chip and ( $purchase = $order->get_meta( '_' . $gateway_id . '_purchase', true ) ) ) { @@ -69,4 +69,4 @@ public function view_button( $order ) { } } -Chip_Woocommerce_Receipt_Link::get_instance(); \ No newline at end of file +Chip_Woocommerce_Receipt_Link::get_instance(); diff --git a/includes/clone-wc-gateway-chip.php b/includes/clone-wc-gateway-chip.php index da7ed38..38a8d39 100644 --- a/includes/clone-wc-gateway-chip.php +++ b/includes/clone-wc-gateway-chip.php @@ -1,105 +1,54 @@ title = __( 'Corporate Online Banking (FPX)', 'chip-for-woocommerce' ); - } - - public function init_form_fields() { - parent::init_form_fields(); - $this->form_fields['payment_method_whitelist']['default'] = [ 'fpx_b2b1' ]; - $this->form_fields['description']['default'] = __( 'Pay with Corporate Online Banking (FPX)', 'chip-for-woocommerce' ); - } -} -class WC_Gateway_Chip_3 extends WC_Gateway_Chip { - const PREFERRED_TYPE = 'Card'; - - protected function init_title() { - $this->title = __( 'Visa / Mastercard', 'chip-for-woocommerce' ); - } - - public function init_form_fields() { - parent::init_form_fields(); - $this->form_fields['payment_method_whitelist']['default'] = [ 'maestro', 'visa', 'mastercard' ]; - $this->form_fields['description']['default'] = __( 'Pay with Visa / Mastercard', 'chip-for-woocommerce' ); - } -} -class WC_Gateway_Chip_4 extends WC_Gateway_Chip { - const PREFERRED_TYPE = 'E-Wallet'; - - protected function init_title() { - $this->title = __( 'Grabpay, TnG, Shopeepay, MB2QR', 'chip-for-woocommerce' ); - } - - public function init_form_fields() { - parent::init_form_fields(); - $this->form_fields['payment_method_whitelist']['default'] = [ 'razer_grabpay', 'razer_maybankqr', 'razer_shopeepay', 'razer_tng' ]; - $this->form_fields['description']['default'] = __( 'Pay with E-Wallet', 'chip-for-woocommerce' ); - } +/** + * Clone WooCommerce Chip Payment Gateway. + * + * This file is responsible for loading the Chip payment gateway classes + * and adding them to WooCommerce. + * + * @package WC_Gateway_Chip + */ + +// Exit if accessed directly. +if ( ! defined( 'ABSPATH' ) ) { + exit; } -class WC_Gateway_Chip_5 extends WC_Gateway_Chip { - const PREFERRED_TYPE = 'Atome'; +// Autoload Chip payment gateway classes. +// This function will automatically include the necessary class files. - public function __construct() { - parent::__construct(); - $this->payment_met = [ 'razer_atome' ]; - } - - protected function init_title() { - $this->title = __( 'Buy Now Pay Later', 'chip-for-woocommerce' ); - } - - protected function init_icon() { - $this->icon = apply_filters( 'wc_' . $this->id . '_load_icon', plugins_url( "assets/atome.svg", WC_CHIP_FILE ) ); - } +spl_autoload_register( + function ( $class_name ) { + $prefix = 'WC_Gateway_Chip_'; + $base_dir = plugin_dir_path( WC_CHIP_FILE ) . '/includes/gateways/'; - public function get_payment_method_list() { - return [ 'razer_atome' => 'Razer Atome' ]; - } + // Only handle your gateway classes. + if ( strpos( $class_name, $prefix ) !== 0 ) { + return; + } - protected function init_one_time_gateway() { - $this->supports = [ 'products', 'refunds' ]; - } + // Convert class to filename (e.g., WC_Gateway_Chip_FPX → class-wc-gateway-chip-fpx.php). + $file = $base_dir . 'class-' . strtolower( str_replace( '_', '-', $class_name ) ) . '.php'; - public function init_form_fields() { - parent::init_form_fields(); - $this->form_fields['payment_method_whitelist']['default'] = [ 'razer_atome' ]; - $this->form_fields['description']['default'] = __( 'Buy now pay later with Atome.
The bill will be split into three easy payments.
No hidden fees, 0% interest.

Terms & Conditions', 'chip-for-woocommerce' ); - unset( $this->form_fields['display_logo'] ); - unset( $this->form_fields['disable_recurring_support'] ); - unset( $this->form_fields['force_tokenization'] ); - unset( $this->form_fields['payment_method_whitelist'] ); - unset( $this->form_fields['bypass_chip'] ); - unset( $this->form_fields['webhooks'] ); - unset( $this->form_fields['webhook_public_key'] ); + if ( file_exists( $file ) ) { + require_once $file; + } } -} - -class WC_Gateway_Chip_6 extends WC_Gateway_Chip { - const PREFERRED_TYPE = 'Duitnow QR'; - - protected function init_title() { - $this->title = __( 'Duitnow QR', 'chip-for-woocommerce' ); - } - - public function init_form_fields() { - parent::init_form_fields(); - $this->form_fields['payment_method_whitelist']['default'] = [ 'duitnow_qr' ]; - $this->form_fields['description']['default'] = __( 'Pay with Duitnow QR', 'chip-for-woocommerce' ); - } -} +); add_filter( 'woocommerce_payment_gateways', 'chip_clone_wc_gateways' ); +/** + * Add Chip payment gateways to WooCommerce. + * + * @param array $methods Existing payment gateways. + * @return array Updated payment gateways. + */ function chip_clone_wc_gateways( $methods ) { - $methods[] = WC_Gateway_Chip_2::class; - $methods[] = WC_Gateway_Chip_3::class; - $methods[] = WC_Gateway_Chip_4::class; - $methods[] = WC_Gateway_Chip_5::class; - $methods[] = WC_Gateway_Chip_6::class; + $methods[] = WC_Gateway_Chip_Fpx_B2b1::class; // wc_gateway_chip_2 . + $methods[] = WC_Gateway_Chip_Card::class; // wc_gateway_chip_3 . + $methods[] = WC_Gateway_Chip_Ewallet::class; // wc_gateway_chip_4 . + $methods[] = WC_Gateway_Chip_Atome::class; // wc_gateway_chip_5 . + $methods[] = WC_Gateway_Chip_Duitnow_Qr::class; // wc_gateway_chip_6 . return $methods; -} \ No newline at end of file +} diff --git a/includes/gateways/class-wc-gateway-chip-atome.php b/includes/gateways/class-wc-gateway-chip-atome.php new file mode 100644 index 0000000..619771a --- /dev/null +++ b/includes/gateways/class-wc-gateway-chip-atome.php @@ -0,0 +1,75 @@ +payment_met = array( 'razer_atome' ); + } + + /** + * Override title. + */ + protected function init_title() { + $this->title = __( 'Buy Now Pay Later', 'chip-for-woocommerce' ); + } + + /** + * Override icon settings. + */ + protected function init_icon() { + $this->icon = apply_filters( 'wc_' . $this->id . '_load_icon', plugins_url( 'assets/atome.svg', WC_CHIP_FILE ) ); + } + + /** + * Set payment method whitelist to Razer Atome. + */ + public function get_payment_method_list() { + return array( 'razer_atome' => 'Razer Atome' ); + } + + /** + * Limit support to products and refunds. + * This to define the supported features of the payment gateway. + */ + protected function init_one_time_gateway() { + $this->supports = array( 'products', 'refunds' ); + } + + /** + * Set default payment method whitelist. + */ + public function init_form_fields() { + parent::init_form_fields(); + $this->form_fields['payment_method_whitelist']['default'] = array( 'razer_atome' ); + $this->form_fields['description']['default'] = __( 'Buy now pay later with Atome.
The bill will be split into three easy payments.
No hidden fees, 0% interest.

Terms & Conditions', 'chip-for-woocommerce' ); + unset( $this->form_fields['display_logo'] ); + unset( $this->form_fields['disable_recurring_support'] ); + unset( $this->form_fields['force_tokenization'] ); + unset( $this->form_fields['payment_method_whitelist'] ); + unset( $this->form_fields['bypass_chip'] ); + unset( $this->form_fields['webhooks'] ); + unset( $this->form_fields['webhook_public_key'] ); + } +} diff --git a/includes/gateways/class-wc-gateway-chip-card.php b/includes/gateways/class-wc-gateway-chip-card.php new file mode 100644 index 0000000..f4f680e --- /dev/null +++ b/includes/gateways/class-wc-gateway-chip-card.php @@ -0,0 +1,39 @@ +title = __( 'Visa / Mastercard', 'chip-for-woocommerce' ); + } + + /** + * Set default payment method whitelist. + */ + public function init_form_fields() { + parent::init_form_fields(); + $this->form_fields['payment_method_whitelist']['default'] = array( 'maestro', 'visa', 'mastercard' ); + $this->form_fields['description']['default'] = __( 'Pay with Visa / Mastercard', 'chip-for-woocommerce' ); + + // Set the default icon to card payment method. + $this->form_fields['display_logo']['default'] = 'card_only'; + } +} diff --git a/includes/gateways/class-wc-gateway-chip-duitnow-qr.php b/includes/gateways/class-wc-gateway-chip-duitnow-qr.php new file mode 100644 index 0000000..b9765ef --- /dev/null +++ b/includes/gateways/class-wc-gateway-chip-duitnow-qr.php @@ -0,0 +1,39 @@ +title = __( 'Duitnow QR', 'chip-for-woocommerce' ); + } + + /** + * Set default payment method whitelist. + */ + public function init_form_fields() { + parent::init_form_fields(); + $this->form_fields['payment_method_whitelist']['default'] = array( 'duitnow_qr' ); + $this->form_fields['description']['default'] = __( 'Pay with Duitnow QR', 'chip-for-woocommerce' ); + + // Set the default icon to ewallet payment method. + $this->form_fields['display_logo']['default'] = 'duitnow_only'; + } +} diff --git a/includes/gateways/class-wc-gateway-chip-ewallet.php b/includes/gateways/class-wc-gateway-chip-ewallet.php new file mode 100644 index 0000000..da333d2 --- /dev/null +++ b/includes/gateways/class-wc-gateway-chip-ewallet.php @@ -0,0 +1,39 @@ +title = __( 'Grabpay, TnG, Shopeepay, MB2QR', 'chip-for-woocommerce' ); + } + + /** + * Set default payment method whitelist. + */ + public function init_form_fields() { + parent::init_form_fields(); + $this->form_fields['payment_method_whitelist']['default'] = array( 'razer_grabpay', 'razer_maybankqr', 'razer_shopeepay', 'razer_tng' ); + $this->form_fields['description']['default'] = __( 'Pay with E-Wallet', 'chip-for-woocommerce' ); + + // Set the default icon to ewallet payment method. + $this->form_fields['display_logo']['default'] = 'ewallet_only'; + } +} diff --git a/includes/gateways/class-wc-gateway-chip-fpx-b2b1.php b/includes/gateways/class-wc-gateway-chip-fpx-b2b1.php new file mode 100644 index 0000000..eb16b3b --- /dev/null +++ b/includes/gateways/class-wc-gateway-chip-fpx-b2b1.php @@ -0,0 +1,36 @@ +title = __( 'Corporate Online Banking (FPX)', 'chip-for-woocommerce' ); + } + + /** + * Set default payment method whitelist. + */ + public function init_form_fields() { + parent::init_form_fields(); + $this->form_fields['payment_method_whitelist']['default'] = array( 'fpx_b2b1' ); + $this->form_fields['description']['default'] = __( 'Pay with Corporate Online Banking (FPX)', 'chip-for-woocommerce' ); + } +} diff --git a/uninstall.php b/uninstall.php index 95fe136..fff2e5e 100644 --- a/uninstall.php +++ b/uninstall.php @@ -7,4 +7,4 @@ delete_option( 'chip_woocommerce_payment_method' ); delete_option( 'woocommerce_chip-fpxb2b1_settings' ); delete_option( 'woocommerce_chip-card_settings' ); -delete_option( 'wc_chip_migrations' ); \ No newline at end of file +delete_option( 'wc_chip_migrations' );