From aab4d0cf9c77d69688586781e821b78b6b73666e Mon Sep 17 00:00:00 2001 From: dimosKougiou Date: Thu, 18 Jul 2024 11:51:47 +0300 Subject: [PATCH] v1.0.7 --- README.md | 4 +- README.txt | 20 +- .../class-cardlink-payment-gateway-admin.php | 32 +- admin/css/cardlink-payment-gateway-admin.css | 2 +- cardlink-payment-gateway.php | 12 + .../class-wc-cardlink-payments-block.php | 90 +++++ ...s-cardlink-payment-gateway-woocommerce.php | 374 ++++++------------ includes/class-cardlink-payment-gateway.php | 3 + .../class-cardlink-payment-gateway-public.php | 70 +++- .../css/cardlink-payment-gateway-public.css | 1 + public/js/blocks/cardlink-block.min.asset.php | 1 + public/js/blocks/cardlink-block.min.js | 1 + public/js/cardlink-block.js | 110 ++++++ 13 files changed, 441 insertions(+), 279 deletions(-) create mode 100644 includes/blocks/class-wc-cardlink-payments-block.php create mode 100644 public/js/blocks/cardlink-block.min.asset.php create mode 100644 public/js/blocks/cardlink-block.min.js create mode 100644 public/js/cardlink-block.js diff --git a/README.md b/README.md index 8e45ffa..97230e5 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ - Contributors: cardlink - Tags: payments, payment-gateway - Requires at least: 5.8.3 -- Tested up to: 6.5.2 +- Tested up to: 6.5.5 - Requires PHP: 7.x - 8.x - License: GPLv2 or later - License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -11,6 +11,8 @@ ## Changelog +- **1.0.7** + - Added support for block-based checkout. - **1.0.6** - Support Alpha bonus transactions. - **1.0.5** diff --git a/README.txt b/README.txt index 7be3643..d2525fa 100644 --- a/README.txt +++ b/README.txt @@ -2,8 +2,8 @@ Contributors: cardlink Tags: payments, payment-gateway Requires at least: 5.8.3 -Tested up to: 6.5.2 -Stable tag: 6.5.2 +Tested up to: 6.5.5 +Stable tag: 6.5.5 Requires PHP: 7.4 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -48,3 +48,19 @@ Here are the detailed instructions to manually install a WordPress plugin by tra == Changelog == += 1.0.7 = +* Added support for block-based checkout. += 1.0.6 = +* Support Alpha bonus transactions. += 1.0.5 = +* Bugfix: Fix compatibility issues with Payment Plugins for Stripe WooCommerce and Payment Plugins for PayPal WooCommerce plugins += 1.0.4 = +* Bugfix: Check if WooCommerce is installed and activated before initialize plugin functionality += 1.0.3 = +* Styling updates on iframe popup window += 1.0.2 = +* Bugfix: Installments total number += 1.0.1 = +* Compatibility updates += 1.0.0 = +* Initial release \ No newline at end of file diff --git a/admin/class-cardlink-payment-gateway-admin.php b/admin/class-cardlink-payment-gateway-admin.php index 7508fc5..7d92d01 100644 --- a/admin/class-cardlink-payment-gateway-admin.php +++ b/admin/class-cardlink-payment-gateway-admin.php @@ -53,6 +53,8 @@ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; + add_filter( 'admin_body_class', [ $this, 'admin_body_class'] ); + } /** @@ -120,7 +122,7 @@ public function cardlink_message() { } else { $payment_method = $order->payment_method; } - if ( is_order_received_page() && ( 'cardlink_payment_gateway_woocommerce' == $payment_method || 'cardlink_payment_gateway_woocommerce_iris' == $payment_method ) ) { + if ( is_order_received_page() && ( 'cardlink_payment_gateway_woocommerce' == $payment_method ) ) { if ( method_exists( $order, 'get_meta' ) ) { $cardlink_message = $order->get_meta( '_cardlink_message', true ); } else { @@ -143,11 +145,33 @@ public function cardlink_message() { public function woocommerce_add_cardlink_gateway( $methods ) { $methods[] = 'Cardlink_Payment_Gateway_Woocommerce'; - //$methods[] = 'Cardlink_Payment_Gateway_Woocommerce_Iris'; - - // $methods[] = 'WC_cardlink_Gateway_masterpass'; return $methods; } + + public static function woocommerce_gateway_block_support() { + if ( class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) { + require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/blocks/class-wc-cardlink-payments-block.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_Cardlink_Blocks_Support() ); + } + ); + + } + } + + public function admin_body_class($classes) { + + $payment_gateway_id = 'cardlink_payment_gateway_woocommerce'; + $payment_gateways = WC_Payment_Gateways::instance(); + $payment_gateway = $payment_gateways->payment_gateways()[$payment_gateway_id]; + if ($payment_gateway->acquirer == '1') { + $classes .= ' acquirer-nexi-checkout'; + } + + return $classes; + } } diff --git a/admin/css/cardlink-payment-gateway-admin.css b/admin/css/cardlink-payment-gateway-admin.css index a3629cb..abd4a96 100644 --- a/admin/css/cardlink-payment-gateway-admin.css +++ b/admin/css/cardlink-payment-gateway-admin.css @@ -23,4 +23,4 @@ .installments_variation_row .button i { vertical-align: middle; -} \ No newline at end of file +} diff --git a/cardlink-payment-gateway.php b/cardlink-payment-gateway.php index 0f781e2..dd64bc6 100644 --- a/cardlink-payment-gateway.php +++ b/cardlink-payment-gateway.php @@ -99,3 +99,15 @@ function run_cardlink_payment_gateway() { if (in_array( $plugin_path, wp_get_active_and_valid_plugins() )) { run_cardlink_payment_gateway(); } + +function cardlink_payment_gateway_depedency_notice() { + if ( class_exists( 'WooCommerce' ) ) { + return; + } + ?> +
+

+
+ settings = get_option('woocommerce_cardlink_payment_gateway_woocommerce_settings', []); + add_action( 'woocommerce_rest_checkout_process_payment_with_context', [ $this, 'add_payment_request_order_meta' ], 8, 2 ); + } + + public function is_active() { + return filter_var( $this->get_setting( 'enabled', false ), FILTER_VALIDATE_BOOLEAN ); + } + + public function get_payment_method_script_handles() { + $plugin_path = plugin_dir_url( dirname( dirname( __FILE__ ) ) ); + $script_asset_path = $plugin_path . '/public/js/cardlink-block.min.asset.php'; + $script_asset = file_exists( $script_asset_path ) ? require( $script_asset_path ) : array( + 'dependencies' => array(), + 'version' => CARDLINK_PAYMENT_GATEWAY_VERSION + ); + $script_url = $plugin_path . '/public/js/blocks/cardlink-block.min.js'; + + wp_register_script( 'wc-cardlink-payments-block', $script_url, $script_asset['dependencies'], $script_asset['version'], true ); + + if ( function_exists( 'wp_set_script_translations' ) ) { + wp_set_script_translations( 'wc-cardlink-payments-block', 'cardlink-payment-gateway', $plugin_path . '/languages/' ); + } + + return [ 'wc-cardlink-payments-block' ]; + } + + public function get_payment_method_data() { + + $installments_variation_data = $this->get_setting( 'installments_variation' ); + $installments_variation = null; + if ($installments_variation_data) { + $installments_split = explode( ',', $installments_variation_data ); + foreach ( $installments_split as $key => $value ) { + $installment = explode( ':', $value ); + if ( is_array( $installment ) && count( $installment ) != 2 ) { + // not valid rule for installments + continue; + } + $amount = (float) $installment[0]; + $max_installments = (int) $installment[1]; + $installments_variation[$amount] = $max_installments; + } + } + + return [ + 'title' => $this->get_setting( 'title' ), + 'description' => $this->get_setting( 'description' ), + 'supports' => ['products'], + 'tokenization' => $this->get_setting( 'tokenization' ) == 'yes', + 'installments' => abs( $this->get_setting( 'installments' ) ), + 'installment_variations' => $installments_variation, + ]; + } + + public function add_payment_request_order_meta( PaymentContext $context, PaymentResult &$result ) { + + $data = $context->payment_data; + + if ( $context->payment_method == 'cardlink_payment_gateway_woocommerce' ) { + $payment_method = $context->payment_method; + $maybe_store_card = $context->payment_data['wc-' . $payment_method . '-new-payment-method']; + $data[$payment_method . '-card-store'] = $maybe_store_card; + $data[$payment_method . '-card-doseis'] = $context->payment_data['installmentsvalue']; + if (array_key_exists('token', $data)) { + $token_id = $data['token']; + $token_class = new WC_Payment_Token_Data_Store; + $token = $token_class->get_token_by_id( $token_id ); + $data[$payment_method . '-card'] = $token->token; + } + $context->set_payment_data($data); + } + + } +} diff --git a/includes/class-cardlink-payment-gateway-woocommerce.php b/includes/class-cardlink-payment-gateway-woocommerce.php index 9d00192..47a670a 100644 --- a/includes/class-cardlink-payment-gateway-woocommerce.php +++ b/includes/class-cardlink-payment-gateway-woocommerce.php @@ -56,6 +56,17 @@ static function generic_add_meta( $orderid, $key, $value ) { } } + static function generic_get_meta( $order, $key ) { + + if ( method_exists( $order, 'get_meta' ) ) { + return $order->get_meta( $key ); + } + + $order_id = method_exists('get_id', $order) ? $order->get_id() : $order->id; + + return get_post_meta( $order_id, $key, true ); + } + /** * Verify a successful Payment! * */ @@ -244,6 +255,8 @@ static function check_response( $post_data, $enable_log, $shared_secret_key, $or return $order; } static function process_payment($order_id, $post_data, $method_id) { + error_log('process_payment'); + error_log(json_encode($post_data)); $order = new WC_Order( $order_id ); $doseis = isset( $post_data[ esc_attr( $method_id ) . '-card-doseis' ] ) ? intval( $post_data[ esc_attr( $method_id ) . '-card-doseis' ] ) : ''; if ( $doseis > 0 ) { @@ -374,6 +387,18 @@ public function __construct() { $this->icon = apply_filters( 'cardlink_icon', $plugin_root_path . '/public/img/cardlink.png' ); } + add_filter( 'woocommerce_payment_gateway_supports', [ $this, 'filter_payment_gateway_supports'], 10, 3 ); + add_filter( 'woocommerce_payment_gateway_add_payment_method_delay', function () { return 0;}, 20 ); + add_action( 'woocommerce_after_account_payment_methods', [ $this, 'add_payment_method_form_output'], 55 ); + } + + public function filter_payment_gateway_supports($supports, $feature, $payment_gateway) { + if ( $this->tokenization == 'yes' && $payment_gateway->id === $this->id ) { + if ($feature === 'add_payment_method' || $feature === 'tokenization') { + $supports = true; + } + } + return $supports; } public function admin_options() { @@ -554,6 +579,14 @@ function payment_fields() { } echo $this->get_payment_cards_html(); + echo $this->get_installments_html($amount); + } + + function get_installments_html($amount) { + if (is_account_page()) { + return; + } + ob_start(); $max_installments = $this->installments; $installments_variation = $this->installments_variation; @@ -593,14 +626,16 @@ function payment_fields() { echo $doseis_field; } + return ob_get_clean(); } - function get_payment_cards_html() { + if (is_account_page()) { + return; + } ob_start(); if ( is_user_logged_in() && $this->tokenization == 'yes' ) { - $counter = 0; $tokens = WC_Payment_Tokens::get_customer_tokens( get_current_user_id(), $this->id ); $token_class = new WC_Payment_Token_Data_Store; $html = ''; @@ -608,7 +643,7 @@ function get_payment_cards_html() { echo '
'; if ( ! empty( $tokens ) ) { foreach ( $tokens as $key => $tok ) { - if ( $counter == 0 ) { + if ( $tok->is_default() ) { $checked = ' checked'; } else { $checked = ''; @@ -629,7 +664,6 @@ function get_payment_cards_html() { '×' . ''; $html .= '
'; - $counter ++; } } if ( $html !== "" ) { @@ -675,17 +709,13 @@ function generate_cardlink_form( $order_id ) { $order = new WC_Order( $order_id ); - if ( method_exists( $order, 'get_meta' ) ) { - $installments = $order->get_meta( '_doseis' ); - if ( $installments == '' ) { - $installments = 1; - } - } else { - $installments = get_post_meta( $order_id, '_doseis', 1 ); - } + $installments = Cardlink_Payment_Gateway_Woocommerce_Helper::generic_get_meta( $order, '_doseis'); + $store_card = Cardlink_Payment_Gateway_Woocommerce_Helper::generic_get_meta( $order, '_cardlink_store_card' ); + $selected_card = Cardlink_Payment_Gateway_Woocommerce_Helper::generic_get_meta( $order, '_cardlink_card' ); - $store_card = get_post_meta( $order_id, '_cardlink_store_card', true ); - $selected_card = get_post_meta( $order_id, '_cardlink_card', true ); + if ( $installments == '' ) { + $installments = 1; + } $countries_obj = new WC_Countries(); $country = $order->get_billing_country(); @@ -699,7 +729,6 @@ function generate_cardlink_form( $order_id ) { 'timestamp' => current_time( 'mysql', 1 ) ) ); - $_SESSION['order_id'] = $order_id; WC()->session->set( 'order_id', $order_id ); @@ -916,92 +945,7 @@ function check_cardlink_response() { exit; } -} - -class Cardlink_Payment_Gateway_Woocommerce_Iris extends WC_Payment_Gateway { - public $id; - public $has_fields; - public $method_title; - public $method_description; - public $title; - public $description; - public $environment; - public $merchant_id; - public $acquirer; - public $shared_secret_key; - public $iris_customer_code; - public $popup; - public $enable_log; - public $api_request_url = 'Cardlink_Payment_Gateway_Woocommerce_Iris'; - public $table_name = 'cardlink_gateway_transactions'; - public function __construct() { - - $this->id = 'cardlink_payment_gateway_woocommerce_iris'; - $this->has_fields = true; - $this->method_title = __( 'Cardlink Payment Gateway Iris', 'cardlink-payment-gateway' ); - $this->method_description = __( 'Cardlink Payment Gateway allows you to accept payment through IRIS on your Woocommerce Powered Site.', 'cardlink-payment-gateway' ); - - // Load the form fields. - $this->init_form_fields(); - - // Load the settings - $this->init_settings(); - - // Define User set Variables - $this->title = sanitize_text_field( $this->get_option( 'title' ) ); - $this->description = sanitize_text_field( $this->get_option( 'description' ) ); - $this->iris_customer_code = sanitize_text_field( $this->get_option( 'iris_customer_code' ) ); - - add_action( 'woocommerce_receipt_' . $this->id, array( $this, 'receipt_page_iris' ) ); - add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( - $this, - 'process_admin_options' - ) ); - - } - - public function init_form_fields() { - $this->form_fields = array( - 'enabled' => array( - 'title' => __( 'Enable/Disable', 'cardlink-payment-gateway' ), - 'type' => 'checkbox', - 'label' => __( 'Enable Cardlink Payment Gateway', 'cardlink-payment-gateway' ), - 'description' => __( 'Enable or disable the gateway.', 'cardlink-payment-gateway' ), - 'desc_tip' => true, - 'default' => 'yes' - ), - 'title' => array( - 'title' => __( 'Title', 'cardlink-payment-gateway' ), - 'type' => 'text', - 'description' => __( 'This controls the title which the user sees during checkout.', 'cardlink-payment-gateway' ), - 'desc_tip' => true, - 'default' => __( 'Credit card via Cardlink', 'cardlink-payment-gateway' ) - ), - 'description' => array( - 'title' => __( 'Description', 'cardlink-payment-gateway' ), - 'type' => 'textarea', - 'description' => __( 'This controls the description which the user sees during checkout.', 'cardlink-payment-gateway' ), - 'desc_tip' => true, - 'default' => __( 'Pay Via Cardlink: Accepts Visa, Mastercard, Maestro, American Express, Diners, Discover.', 'cardlink-payment-gateway' ) - ), - 'iris_customer_code'=> array( - 'title' => __( 'IRIS customer code', 'cardlink-payment-gateway' ), - 'type' => 'text', - 'description' => __( 'Enter Your IRIS customer code', 'cardlink-payment-gateway' ), - 'default' => '', - 'desc_tip' => true - ) - ); - } - - public function receipt_page_iris( $order ) { - echo '

' . __( 'Thank you for your order. We are now redirecting you to make payment.', 'cardlink-payment-gateway' ) . '

'; - echo $this->generate_cardlink_form( $order ); - } - - public function generate_cardlink_form( $order_id ) { - - global $wpdb; + public function add_payment_method() { $locale = get_locale(); if ( $locale == 'el' ) { @@ -1010,182 +954,94 @@ public function generate_cardlink_form( $order_id ) { $lang = 'en'; } + $post_url = Cardlink_Payment_Gateway_Woocommerce_Helper::get_post_url( $this->environment, $this->acquirer ); $version = 2; $currency = 'EUR'; + $current_user_id = get_current_user_id(); + $customer = new WC_Customer( $current_user_id ); + + $form_data_array = [ + 'version' => $version, + 'mid' => $this->merchant_id, + 'lang' => $lang, + 'orderid' => $current_user_id . 'at' . date( 'Ymdhisu' ), + 'orderDesc' => '', + 'orderAmount' => 0, + 'currency' => $currency, + 'payerEmail' => $customer->get_billing_email(), + 'billCountry' => $customer->get_billing_country(), + 'billZip' => $customer->get_billing_postcode(), + 'billCity' => $customer->get_billing_city(), + 'billAddress' => $customer->get_billing_address_1(), + 'trType' => 8, + 'cssUrl' => $this->css_url, + 'confirmUrl' => get_rest_url() . 'wc-cardlink/v1/tokenizer?result=success', + 'cancelUrl' => get_rest_url() . 'wc-cardlink/v1/tokenizer?result=failure', + ]; - $payment_Gateway = Cardlink_Payment_Gateway_Woocommerce::instance(); - $this->environment = $payment_Gateway->environment; - $this->merchant_id = $payment_Gateway->merchant_id; - $this->acquirer = $payment_Gateway->acquirer; - $this->shared_secret_key = $payment_Gateway->shared_secret_key; - $this->enable_log = $payment_Gateway->enable_log; - $this->api_request_url = $payment_Gateway->api_request_url; - - $post_url = Cardlink_Payment_Gateway_Woocommerce_Helper::get_post_url( $this->environment, $this->acquirer ); + $form_secret = $this->shared_secret_key; + $form_data = iconv( 'utf-8', 'utf-8//IGNORE', implode( "", $form_data_array ) ) . $form_secret; + $form_data_array['digest'] = Cardlink_Payment_Gateway_Woocommerce_Helper::calculate_digest( $form_data ); + $encoded_string = base64_encode( json_encode( $form_data_array ) ); - $order = new WC_Order( $order_id ); + $url = add_query_arg( [ + 'url' => $post_url, + 'add_payment_method' => $encoded_string + ], wc_get_endpoint_url( 'payment-methods' )); - $countries_obj = new WC_Countries(); - $country = $order->get_billing_country(); - $country_states_array = $countries_obj->get_states(); - $state_code = $order->get_billing_state(); - $state = $country_states_array[ $country ][ $state_code ]; - - $wpdb->insert( $wpdb->prefix . $this->table_name, array( - 'trans_ticket' => $order_id, - 'merchantreference' => $order_id, - 'timestamp' => current_time( 'mysql', 1 ) - ) ); + return [ + 'result' => '', + 'redirect' => $url, + ]; - $_SESSION['order_id'] = $order_id; - WC()->session->set( 'order_id', $order_id ); + } - if ( $country != 'GR' ) { - $form_data_array = array( - 'version' => $version, - 'mid' => $this->merchant_id, - 'lang' => $lang, - 'orderid' => $order_id . 'at' . date( 'Ymdhisu' ), - 'orderDesc' => $this->get_rf_code( $order_id ), - 'orderAmount' => $order->get_total(), - 'currency' => $currency, - 'payerEmail' => $order->get_billing_email(), - 'payerPhone' => $order->get_billing_phone(), - 'billCountry' => $country, - 'billState' => $state_code, - 'billZip' => $order->get_billing_postcode(), - 'billCity' => $order->get_billing_city(), - 'billAddress' => $order->get_billing_address_1(), - 'payMethod' => 'IRIS', - 'confirmUrl' => get_site_url() . "/?wc-api=" . $this->api_request_url . "&result=success", - 'cancelUrl' => get_site_url() . "/?wc-api=" . $this->api_request_url . "&result=failure", - ); - } else { - $form_data_array = array( - 'version' => $version, - 'mid' => $this->merchant_id, - 'lang' => $lang, - 'orderid' => $order_id . 'at' . date( 'Ymdhisu' ), - 'orderDesc' => $this->get_rf_code( $order_id ), - 'orderAmount' => $order->get_total(), - 'currency' => $currency, - 'payerEmail' => $order->get_billing_email(), - 'payerPhone' => $order->get_billing_phone(), - 'billCountry' => $country, - 'billZip' => $order->get_billing_postcode(), - 'billCity' => $order->get_billing_city(), - 'billAddress' => $order->get_billing_address_1(), - 'payMethod' => 'IRIS', - 'confirmUrl' => get_site_url() . "/?wc-api=" . $this->api_request_url . "&result=success", - 'cancelUrl' => get_site_url() . "/?wc-api=" . $this->api_request_url . "&result=failure", - ); + public function add_payment_method_form_output() { + if (array_key_exists('tok_message', $_GET)) { + wc_add_notice( $_GET['tok_message'] ); + return; } - - $form_secret = $this->shared_secret_key; - $form_data = iconv( 'utf-8', 'utf-8//IGNORE', implode( "", $form_data_array ) ) . $form_secret; - $digest = Cardlink_Payment_Gateway_Woocommerce_Helper::calculate_digest( $form_data ); - - if ( $this->enable_log == 'yes' ) { - error_log( '---- Cardlink Transaction digest -----' ); - error_log( 'Data: ' ); - error_log( print_r( $form_data, true ) ); - error_log( 'Digest: ' ); - error_log( print_r( $digest, true ) ); - error_log( '---- End of Cardlink Transaction digest ----' ); + if (!array_key_exists('add_payment_method', $_GET)) { + return; } - $form_target = '_top'; - $html = '
'; + $post_url = $_GET['url']; + $post_data = json_decode(base64_decode($_GET['add_payment_method']), true); - foreach ( $form_data_array as $key => $value ) { + $html = ''; + + foreach ( $post_data as $key => $value ) { $html .= ''; } - $html .= ''; - $html .= ' -
- -
- '; + //$html .= ''; + $html .= ''; $html .= '
'; wc_enqueue_js( ' - $.blockUI({ - message: "' . esc_js( __( 'Thank you for your order. We are now redirecting you to make payment.', 'cardlink-payment-gateway' ) ) . '", - baseZ: 99999, - overlayCSS: { - background: "#fff", - opacity: 0.6 - }, - css: { - padding: "20px", - zindex: "9999999", - textAlign: "center", - color: "#555", - border: "3px solid #aaa", - backgroundColor:"#fff", - cursor: "wait", - lineHeight: "24px", - } - }); - ' ); - wc_enqueue_js( " - $('#payment_form').submit(); + $.blockUI({ + message: "' . esc_js( __( 'You are now redirecting to payment page.', 'cardlink-payment-gateway' ) ) . '", + baseZ: 99999, + overlayCSS: { + background: "#fff", + opacity: 0.6 + }, + css: { + padding: "20px", + zindex: "9999999", + textAlign: "center", + color: "#555", + border: "3px solid #aaa", + backgroundColor:"#fff", + cursor: "wait", + lineHeight: "24px", + } + }); + ' ); + wc_enqueue_js( " + $('#tokenizer_form').submit(); " ); - return $html; - } - - public function get_option( $key, $empty_value = null ) { - $option_value = parent::get_option( $key, $empty_value ); - - return $option_value; - } - - public function get_rf_code( $order_id ) { - $rf_payment_code = get_post_meta( $order_id, 'rf_payment_code', true ); - if ( $rf_payment_code !== '' ) { - return $rf_payment_code; - } - - $order = new WC_Order( $order_id ); - $order_total = $order->get_total(); - /* calculate payment check code */ - $paymentSum = 0; - if ( $order_total > 0 ) { - $ordertotal = str_replace( [ ',' ], '.', (string) $order_total ); - $ordertotal = number_format( $ordertotal, 2, '', '' ); - $ordertotal = strrev( $ordertotal ); - $factor = [ 1, 7, 3 ]; - $idx = 0; - for ( $i = 0; $i < strlen( $ordertotal ); $i ++ ) { - $idx = $idx <= 2 ? $idx : 0; - $paymentSum += $ordertotal[ $i ] * $factor[ $idx ]; - $idx ++; - } - } - $randomNumber = $this->generateRandomString( 13, $order_id ); - $paymentCode = $paymentSum ? ( $paymentSum % 8 ) : '8'; - $systemCode = '12'; - $tempCode = $this->iris_customer_code . $paymentCode . $systemCode . $randomNumber . '271500'; - $mod97 = bcmod( $tempCode, '97' ); - $cd = 98 - (int) $mod97; - $cd = str_pad( (string) $cd, 2, '0', STR_PAD_LEFT ); - $rf_payment_code = 'RF' . $cd . $this->iris_customer_code . $paymentCode . $systemCode . $randomNumber; - update_post_meta( $order_id, 'rf_payment_code', $rf_payment_code ); - return $rf_payment_code; - } - - public function generateRandomString( $length = 22, $order_id = 0 ) { - return str_pad( $order_id, $length, '0', STR_PAD_LEFT ); - } - - function process_payment( $order_id ) { - return Cardlink_Payment_Gateway_Woocommerce_Helper::process_payment($order_id, $_POST, $this->id ); + echo $html; } } - - - diff --git a/includes/class-cardlink-payment-gateway.php b/includes/class-cardlink-payment-gateway.php index e11b673..82b7b2a 100644 --- a/includes/class-cardlink-payment-gateway.php +++ b/includes/class-cardlink-payment-gateway.php @@ -159,6 +159,8 @@ private function define_admin_hooks() { $this->loader->add_action( 'plugins_loaded', $plugin_admin, 'load_payment_gateway' ); $this->loader->add_action( 'wp', $plugin_admin, 'cardlink_message' ); $this->loader->add_action( 'woocommerce_payment_gateways', $plugin_admin, 'woocommerce_add_cardlink_gateway' ); + $this->loader->add_action( 'woocommerce_blocks_loaded', $plugin_admin, 'woocommerce_gateway_block_support' ); + } /** @@ -175,6 +177,7 @@ private function define_public_hooks() { $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' ); $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' ); $this->loader->add_action( 'plugins_loaded', $plugin_public, 'delete_payment_token' ); + $this->loader->add_action( 'rest_api_init', $plugin_public, 'register_rest_routes' ); } diff --git a/public/class-cardlink-payment-gateway-public.php b/public/class-cardlink-payment-gateway-public.php index abe5c3e..f78435c 100644 --- a/public/class-cardlink-payment-gateway-public.php +++ b/public/class-cardlink-payment-gateway-public.php @@ -115,18 +115,6 @@ public function init_ajax_scripts() { $ajax_url_params = array(); - // You can remove this block if you don't use WPML -/* if ( function_exists( 'icl_object_id' ) ) { - global $sitepress; - - $current_lang = $sitepress->get_current_language(); - wp_localize_script( 'main', 'i18n', array( - 'lang' => $current_lang - ) ); - - $ajax_url_params['lang'] = $current_lang; - } */ - wp_localize_script($this->plugin_name, 'urls', array( 'home' => home_url(), 'theme' => get_template_directory(), @@ -137,4 +125,62 @@ public function init_ajax_scripts() { } + public function register_rest_routes() { + register_rest_route( + 'wc-cardlink/v1', + 'tokenizer', + array( + 'methods' => 'POST', + 'callback' => [ $this, 'handle_tokenized_transactions' ], + 'permission_callback' => '__return_true', + ) + ); + } + + public function handle_tokenized_transactions(WP_REST_Request $request) { + + $post_data = $request->get_params(); + $response_message = __( 'Payment method successfully added.', 'woocommerce' ); + $redirect_url = wc_get_endpoint_url( 'payment-methods', '', wc_get_page_permalink( 'myaccount' ) ); + error_log(json_encode($post_data)); + + $payment_gateway_instance = WC()->payment_gateways->payment_gateways()['cardlink_payment_gateway_woocommerce']; + $shared_secret_key = $payment_gateway_instance->shared_secret_key; + $method_id = $payment_gateway_instance->id; + + $digest = $post_data['digest']; + unset($post_data['result']); + unset($post_data['digest']); + + $form_data = ''; + foreach ($post_data as $k => $v) { + $form_data .= filter_var( $v, FILTER_SANITIZE_STRING ); + } + $form_data .= $shared_secret_key; + $computed_digest = Cardlink_Payment_Gateway_Woocommerce_Helper::calculate_digest( $form_data ); + + if ( $digest !== $computed_digest ){ + wp_redirect($redirect_url); + exit(); + } + + $order_id = explode("at", $post_data['orderid'], 2); + $current_user_id = $order_id[0]; + $extTokenExpYear = substr( $post_data['extTokenExp'], 0, 4 ); + $extTokenExpMonth = substr( $post_data['extTokenExp'], 4, 2 ); + + $token = new WC_Payment_Token_CC(); + $token->set_token( $post_data['extToken'] ); + $token->set_gateway_id( $method_id ); + $token->set_last4( $post_data['extTokenPanEnd'] ); + $token->set_expiry_year( $extTokenExpYear ); + $token->set_expiry_month( $extTokenExpMonth ); + $token->set_card_type( $post_data['payMethod'] ); + $token->set_user_id( $current_user_id ); + $token->save(); + + wp_redirect($redirect_url); + exit(); + } + } diff --git a/public/css/cardlink-payment-gateway-public.css b/public/css/cardlink-payment-gateway-public.css index e51527d..0485d51 100644 --- a/public/css/cardlink-payment-gateway-public.css +++ b/public/css/cardlink-payment-gateway-public.css @@ -70,6 +70,7 @@ background: #fff!important; } +form#add_payment_method ul.woocommerce-PaymentMethods li.payment_method_cardlink_payment_gateway_woocommerce label img, ul.wc_payment_methods li.wc_payment_method.payment_method_cardlink_payment_gateway_woocommerce label img { max-width: 100%; max-height: 35px!important; diff --git a/public/js/blocks/cardlink-block.min.asset.php b/public/js/blocks/cardlink-block.min.asset.php new file mode 100644 index 0000000..253ed7a --- /dev/null +++ b/public/js/blocks/cardlink-block.min.asset.php @@ -0,0 +1 @@ + array('react', 'wc-blocks-registry', 'wc-settings', 'wp-element', 'wp-html-entities', 'wp-i18n'), 'version' => 'd9c178bac3b3d7983579718462b98a67'); \ No newline at end of file diff --git a/public/js/blocks/cardlink-block.min.js b/public/js/blocks/cardlink-block.min.js new file mode 100644 index 0000000..3f9d8d1 --- /dev/null +++ b/public/js/blocks/cardlink-block.min.js @@ -0,0 +1 @@ +!function(e){var t={};function n(a){if(t[a])return t[a].exports;var o=t[a]={i:a,l:!1,exports:{}};return e[a].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,a){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:a})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var a=Object.create(null);if(n.r(a),Object.defineProperty(a,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(a,o,function(t){return e[t]}.bind(null,o));return a},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=6)}([function(e,t){e.exports=window.React},function(e,t){e.exports=window.wp.element},function(e,t){e.exports=window.wp.i18n},function(e,t){e.exports=window.wp.htmlEntities},function(e,t){e.exports=window.wc.wcBlocksRegistry},function(e,t){e.exports=window.wc.wcSettings},function(e,t,n){"use strict";n.r(t);var a,o,r=n(1),l=n(0),i=n.n(l),c=n(2),s=n(4),u=n(3),p=n(5);const m=Object(p.getSetting)("cardlink_payment_gateway_block_data",{}),d=Object(c.__)("Cardlink Payment Gateway","cardlink-payment-gateway-block"),f=Object(u.decodeEntities)(m.title)||d,y=e=>{const t=i.a.createElement("p",null,Object(u.decodeEntities)(m.description||""));let n=m.installments||1;const[a,o]=Object(l.useState)(""),{eventRegistration:r,emitResponse:s}=e,{onPaymentSetup:p}=r;if(Object(l.useEffect)(()=>{const e=p(async()=>({type:s.responseTypes.SUCCESS,meta:{paymentMethodData:{installmentsValue:a}}}));return()=>{e()}},[s.responseTypes.ERROR,s.responseTypes.SUCCESS,p,a]),m.installment_variations){const t=e.billing.cartTotal.value,a=e.billing.currency.minorUnit,o=t/Math.pow(10,a);let r=1;for(const[e,t]of Object.entries(m.installment_variations))o>=e&&(r=t);n=r}if(n<=1)return t;const d=[];for(let e=1;e<=n;e++)d.push(i.a.createElement("option",{key:e,value:e},1===e?Object(c.__)("Without installments","cardlink-payment-gateway"):e));const f=i.a.createElement("div",null,i.a.createElement("label",{htmlFor:"payment-installments"},Object(c.__)("Choose Installments","cardlink-payment-gateway")+": "),i.a.createElement("select",{id:"payment-installments",name:"installments",onChange:e=>{o(e.target.value)}},d));return i.a.createElement("div",null,t,f)},b={name:"cardlink_payment_gateway_woocommerce",label:Object(r.createElement)(e=>{const{PaymentMethodLabel:t}=e.components;return Object(r.createElement)(t,{text:f})},null),content:Object(r.createElement)(y,null),edit:Object(r.createElement)(y,null),canMakePayment:()=>!0,ariaLabel:f,supports:{features:m.supports,showSavedCards:null!==(a=m.tokenization)&&void 0!==a&&a,showSaveOption:null!==(o=m.tokenization)&&void 0!==o&&o}};Object(s.registerPaymentMethod)(b)}]); \ No newline at end of file diff --git a/public/js/cardlink-block.js b/public/js/cardlink-block.js new file mode 100644 index 0000000..9c0175a --- /dev/null +++ b/public/js/cardlink-block.js @@ -0,0 +1,110 @@ +import React from 'react'; +import { sprintf, __ } from '@wordpress/i18n'; +import { registerPaymentMethod } from '@woocommerce/blocks-registry'; +import { decodeEntities } from '@wordpress/html-entities'; +import { getSetting } from '@woocommerce/settings'; +import { useEffect, useState } from 'react'; + +const settings = getSetting('cardlink_payment_gateway_block_data', {}); + +const defaultLabel = __( + 'Cardlink Payment Gateway', + 'cardlink-payment-gateway-block' +); + +const label = decodeEntities(settings.title) || defaultLabel; + +const Content = (props) => { + + const description = React.createElement( 'p', null, decodeEntities( settings.description || '' ) ); + let installmentsNumber = settings.installments || 1; + + const [installmentsValue, setInstallmentsValue] = useState(''); + const { eventRegistration, emitResponse } = props; + const { onPaymentSetup } = eventRegistration; + useEffect( () => { + const unsubscribe = onPaymentSetup( async () => { + return { + type: emitResponse.responseTypes.SUCCESS, + meta: { + paymentMethodData: { + installmentsValue, + }, + }, + }; + } ); + return () => { + unsubscribe(); + }; + }, [ + emitResponse.responseTypes.ERROR, + emitResponse.responseTypes.SUCCESS, + onPaymentSetup, + installmentsValue + ] ); + + if (settings.installment_variations) { + const totals = props.billing.cartTotal.value; + const minorUnit = props.billing.currency.minorUnit; + const totalPrice = totals / Math.pow(10, minorUnit); + let maxInstallments = 1; + for (const [amount, installments] of Object.entries(settings.installment_variations)) { + if ( totalPrice >= amount ) { + maxInstallments = installments; + } + } + installmentsNumber = maxInstallments; + } + + if (installmentsNumber <= 1) { + return description; + } + + const installmentOptions = []; + for (let i = 1; i <= installmentsNumber; i++) { + installmentOptions.push( + React.createElement( + 'option', + { key: i, value: i }, + i === 1 ? __('Without installments', 'cardlink-payment-gateway') : i + ) + ); + } + + const handleInstallmentChange = (e) => { + setInstallmentsValue(e.target.value); + }; + const installments = React.createElement( + 'div', + null, + React.createElement('label', { htmlFor: 'payment-installments' }, `${__('Choose Installments', 'cardlink-payment-gateway')}: `), + React.createElement( + 'select', + { id: 'payment-installments', name: 'installments', onChange: handleInstallmentChange }, + installmentOptions + ) + ); + + return React.createElement( 'div', null, description, installments ); +}; + +const Label = (props) => { + const { PaymentMethodLabel } = props.components; + return ; +}; + +const Cardlink = { + name: "cardlink_payment_gateway_woocommerce", + label: