From d0c706cc209ea9240b0a9f8988730a3d920a50ba Mon Sep 17 00:00:00 2001 From: dingo-d Date: Thu, 1 Mar 2018 22:21:01 +0100 Subject: [PATCH 1/2] Update to version 1.5 --- README.md | 13 +++++++++++- README.txt | 13 +++++++++++- admin/class-helpers.php | 36 ++++++++++++++++++++++++--------- admin/class-request.php | 8 ++------ includes/class-woo-solo-api.php | 6 +++++- woo-solo-api.php | 6 +++--- 6 files changed, 60 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index d967ca1..34662a1 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![GitHub tag](https://img.shields.io/github/tag/dingo-d/woo-solo-api.svg)](https://github.com/dingo-d/woo-solo-api) + # Woo Solo Api **Contributors**: dingo_bastard @@ -5,7 +7,7 @@ **Requires at least**: 4.4 **Requires PHP**: 5.6 **Tested up to**: 4.9.4 -**Stable tag**: 1.4 +**Stable tag**: 1.5 **WC requires at least**: 3.0.0 **WC tested up to**: 3.3.3 **License**: GPLv2 or later @@ -59,6 +61,11 @@ Be sure you have WooCommerce plugin installed first, otherwise you'll get an err ## Changelog +### 1.5 + +* Fixed the currency rate to pull it from the official hnb site +* Set the transient validity to 6 hours so every 6 hours it will expire and be created again + ### 1.4 * Added automatic conversion rate from Croatian National Bank (https://www.hnb.hr/temeljne-funkcije/monetarna-politika/tecajna-lista/tecajna-lista) @@ -91,6 +98,10 @@ Be sure you have WooCommerce plugin installed first, otherwise you'll get an err ## Additional notes +The 1.5 update fixed the currency rate. I parsed the results from https://www.hnb.hr/tecajn/htecajn.htm and created my own currency rate array. I also tested it with included taxes and it seems to be working ok. If for any reason the taxes are not being calculated or pulled in the solo api, there must be some third party plugin that is messing stuff up. Be mindful of that. + +---- + The 1.4 update fixed a lot of issues with the taxes, and tax rates. Although this has been fixed, the taxes probably wont't be picked up by the SOLO API if you set the option in the WooCommerce to include taxes in the price of the product. This is why you should separate the two. Also when creating a shipping tax class, be sure to create it as aseparate class, and leave the standard tax class for the product. So for instance the product will have a standard tax of 5%, and the shipping can have a separate class with tax rate of 25% (be sure to tick the shipping checkbox in this class). The including prices will be fixed in the next update. diff --git a/README.txt b/README.txt index 2159b38..a34e8ea 100755 --- a/README.txt +++ b/README.txt @@ -4,7 +4,7 @@ Tags: woocommerce, api, solo api, solo, api integration, shop, payment, woo Requires at least: 4.4 Requires PHP: 5.6 Tested up to: 4.9.4 -Stable tag: 1.4 +Stable tag: 1.5 WC requires at least: 3.0.0 WC tested up to: 3.3.3 License: GPLv2 or later @@ -65,12 +65,19 @@ Be sure you have WooCommerce plugin installed first, otherwise you'll get an err == Changelog == += 1.5 = + +* Fixed the currency rate to pull it from the official hnb site +* Set the transient validity to 6 hours so every 6 hours it will expire and be created again + = 1.4 = + * Added automatic conversion rate from Croatian National Bank (https://www.hnb.hr/temeljne-funkcije/monetarna-politika/tecajna-lista/tecajna-lista) * Add a fix for duplicated orders - you can now choose if you want to make SOLO API call on the checkout or when changing order status manually * Fix for taxes rounding error on shipping = 1.3 = + * Add additional debug methods * Code sniffer fixes * Add tax check - tax rate can be separate for shipping and for items, and are handled by WooCommerce @@ -95,6 +102,10 @@ Be sure you have WooCommerce plugin installed first, otherwise you'll get an err == Additional notes == +The 1.5 update fixed the currency rate. I parsed the results from https://www.hnb.hr/tecajn/htecajn.htm and created my own currency rate array. I also tested it with included taxes and it seems to be working ok. If for any reason the taxes are not being calculated or pulled in the solo api, there must be some third party plugin that is messing stuff up. Be mindful of that. + +---- + The 1.4 update fixed a lot of issues with the taxes, and tax rates. Although this has been fixed, the taxes probably wont't be picked up by the SOLO API if you set the option in the WooCommerce to include taxes in the price of the product. This is why you should separate the two. Also when creating a shipping tax class, be sure to create it as aseparate class, and leave the standard tax class for the product. So for instance the product will have a standard tax of 5%, and the shipping can have a separate class with tax rate of 25% (be sure to tick the shipping checkbox in this class). The including prices will be fixed in the next update. diff --git a/admin/class-helpers.php b/admin/class-helpers.php index 2dbc2d3..f34f353 100644 --- a/admin/class-helpers.php +++ b/admin/class-helpers.php @@ -3,6 +3,7 @@ * The plugin helpers class. * * @link https://madebydenis.com + * @since 1.5.0 Added cron job and change hnb API to an official one. * @since 1.3.0 * * @package Woo_Solo_Api\Admin @@ -22,10 +23,10 @@ class Helpers { /** * Returns the Croatian exchange rates * - * The link from which the exchange rates are pulled from - * http://hnbex.eu/api/v1/rates/daily/, is not an official API - * from HNB, but they are pulling the rates from the official - * site. + * @link https://www.hnb.hr/tecajn/htecajn.htm + * + * @since 1.5.0 Change link for the currency fetch. + * @since 1.3.0 * * @return array Exchange rates. */ @@ -34,17 +35,32 @@ public function get_exchange_rates() { $currency_rates = get_transient( 'exchange_rate_transient' ); // Get transient. if ( false === $currency_rates ) { // If no valid transient exists, run this. - $currency_api_url = 'http://hnbex.eu/api/v1/rates/daily/'; - $currency_remote = wp_remote_get( $currency_api_url ); + $url = 'https://www.hnb.hr/tecajn/htecajn.htm'; + + $headers = get_headers( $url ); + $status = substr( $headers[0], 9, 3 ); - // Is the API up? - if ( ! 200 === wp_remote_retrieve_response_code( $currency_remote ) ) { + // Is the link up? + if ( $status !== '200' ) { return false; } - $currency_rates = json_decode( wp_remote_retrieve_body( $currency_remote ), true ); + $contents = file_get_contents( $url ); + + $array = explode( "\n", $contents ); + unset( $array[0] ); + $array = array_values( $array ); + + $currency_rates = []; + + foreach ( $array as $arr_key => $arr_value ) { + $single_rate = array_values( array_filter( explode( ' ', $arr_value ) ) ); + $currency_name = preg_replace( '/[^a-zA-Z]+/', '', $single_rate[0] ); + + $currency_rates[ $currency_name ] = $single_rate[2]; + } - set_transient( 'exchange_rate_transient', $currency_rates, 1 * DAY_IN_SECONDS ); + set_transient( 'exchange_rate_transient', $currency_rates, 6 * HOUR_IN_SECONDS ); } // Are the results in an array? diff --git a/admin/class-request.php b/admin/class-request.php index 62fd291..29d4979 100644 --- a/admin/class-request.php +++ b/admin/class-request.php @@ -288,14 +288,10 @@ public function execute_solo_api_call( $order ) { $api_rates = $this->helper->get_exchange_rates(); $currency = $currency_helper[ $solo_api_currency ]; - $currency_rate = array_values( array_filter( array_map( function( $el ) use ( $currency ) { - if ( $el['currency_code'] === $currency ) { - return $el['median_rate']; - } - }, $api_rates ) ) ); + $currency_rate = $api_rates[ $currency ]; if ( ! empty( $currency_rate ) ) { - $num = (float) str_replace( ',', '.', $currency_rate[0] ); + $num = (float) str_replace( ',', '.', $currency_rate ); $post_url .= '&tecaj=' . str_replace( '.', ',', round( $num, 6 ) ); $customer_note .= "\n" . sprintf( '%1$s (1 %2$s = %3$s HRK)', diff --git a/includes/class-woo-solo-api.php b/includes/class-woo-solo-api.php index e200ed1..5f0695a 100755 --- a/includes/class-woo-solo-api.php +++ b/includes/class-woo-solo-api.php @@ -65,7 +65,7 @@ public function __construct() { if ( defined( 'SAWI_PLUGIN_VERSION' ) ) { $this->version = SAWI_PLUGIN_VERSION; } else { - $this->version = '1.4'; + $this->version = '1.5'; } if ( defined( 'SAWI_PLUGIN_NAME' ) ) { @@ -77,6 +77,7 @@ public function __construct() { $this->load_dependencies(); $this->set_locale(); $this->define_admin_hooks(); + } /** * Load the required dependencies for this plugin. @@ -116,9 +117,12 @@ private function set_locale() { private function define_admin_hooks() { $plugin_admin = new Admin\Admin( $this->get_plugin_name(), $this->get_version() ); $api_request = new Admin\Request(); + $api_helpers = new Admin\Helpers(); $this->loader->add_action( 'woocommerce_email_order_details', $api_request, 'solo_api_send_api_request', 15, 4 ); + $this->loader->add_action( 'init', $api_helpers, 'get_exchange_rates' ); + $this->loader->add_filter( 'post_mime_types', $plugin_admin, 'add_pdf_post_mime_type' ); $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' ); $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' ); diff --git a/woo-solo-api.php b/woo-solo-api.php index 4399cde..f0dc5ba 100755 --- a/woo-solo-api.php +++ b/woo-solo-api.php @@ -8,9 +8,9 @@ * @package Woo_Solo_Api * * Plugin Name: Woo Solo Api - * Plugin URI: https://madebydenis.com/woo-solo-api + * Plugin URI: https://wordpress.org/plugins/woo-solo-api/ * Description: This plugin provides integration of the SOLO API service with WooCommerce. - * Version: 1.4 + * Version: 1.5 * Author: Denis Žoljom * Author URI: https://madebydenis.com * License: GPL-2.0+ @@ -30,7 +30,7 @@ die; } -define( 'SAWI_PLUGIN_VERSION', '1.4' ); +define( 'SAWI_PLUGIN_VERSION', '1.5' ); define( 'SAWI_PLUGIN_NAME', 'woo-solo-api' ); // Include the autoloader so we can dynamically include the rest of the classes. From 281bb17d4a78a561def4ba0fa0a2a6ca95d98a03 Mon Sep 17 00:00:00 2001 From: dingo-d Date: Thu, 1 Mar 2018 22:21:56 +0100 Subject: [PATCH 2/2] Typo fix --- admin/class-helpers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/class-helpers.php b/admin/class-helpers.php index f34f353..e274d5a 100644 --- a/admin/class-helpers.php +++ b/admin/class-helpers.php @@ -3,7 +3,7 @@ * The plugin helpers class. * * @link https://madebydenis.com - * @since 1.5.0 Added cron job and change hnb API to an official one. + * @since 1.5.0 Change hnb API to an official one. * @since 1.3.0 * * @package Woo_Solo_Api\Admin