From fd892032fbef30231521c51d93f1304c7c4bc29a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20H=C3=B6gefjord?= Date: Tue, 1 Oct 2024 14:53:33 +0200 Subject: [PATCH] Version 1.8.1 (#103) Co-authored-by: anyakro Co-authored-by: Montazar --- .distignore | 33 +++- .github/workflows/build-and-deploy.yml | 37 +++++ .gitignore | 1 + classes/class-ledyer-confirmation.php | 4 +- classes/class-ledyer-lco-gateway.php | 2 +- classes/class-ledyer-main.php | 199 ++++++++++++++----------- composer.lock | 18 +++ includes/lco-functions.php | 3 + ledyer-checkout-for-woocommerce.php | 18 +-- readme.txt | 17 ++- 10 files changed, 220 insertions(+), 112 deletions(-) create mode 100644 .github/workflows/build-and-deploy.yml create mode 100644 composer.lock diff --git a/.distignore b/.distignore index e06f680..b7530d3 100644 --- a/.distignore +++ b/.distignore @@ -1,16 +1,37 @@ -/.git* + +/.git +/.github /.husky -/.vscode /node_modules -.distignore +/.vscode +/.wordpress-org +/tests .env +.editorconfig +.distignore .gitignore +.nvmrc commitlint.config.js composer.json -docker-compose.yml +composer-lock.json +Dockerfile Dockerfile-* -package-lock.json +docker-compose.phpunit.yml +docker-compose.yml +DOCKER_ENV +docker_tag +gulpfile.js +output.log package.json +package-lock.json +phpcs.ruleset.xml +phpunit-docker.xml +phpunit.xml +phpcs.xml +readme.md +README.md +readme.dev.txt README.md +renovate.json webpack.config.js -wpcron.txt +wpcron.txt \ No newline at end of file diff --git a/.github/workflows/build-and-deploy.yml b/.github/workflows/build-and-deploy.yml new file mode 100644 index 0000000..230a786 --- /dev/null +++ b/.github/workflows/build-and-deploy.yml @@ -0,0 +1,37 @@ +name: Build and deploy to WordPress.org +on: + release: + types: [published] +jobs: + tag: + name: New tag + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@main + + # Install all composer dependencies for the plugin. + - name: Install Composer dependencies + uses: php-actions/composer@v6 + with: + dev: no + php_version: 7.4 + + - name: Install PNPM + uses: pnpm/action-setup@v2 + with: + run_install: true + + - name: Build Assets + run: pnpm run build + + # Deploy the plugin to WordPress.org + - name: WordPress Plugin Deploy + id: deploy + uses: 10up/action-wordpress-plugin-deploy@stable + with: + generate-zip: true + env: + SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }} + SVN_USERNAME: ${{ secrets.SVN_USERNAME }} + SLUG: ledyer-checkout-for-woocommerce \ No newline at end of file diff --git a/.gitignore b/.gitignore index 6d06199..85ea342 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ wp-content/themes/twenty*/ # ignore node dependency directories node_modules/ +/vendor/ # ignore log files and databases *.log diff --git a/classes/class-ledyer-confirmation.php b/classes/class-ledyer-confirmation.php index 5b0cbfe..ec86397 100644 --- a/classes/class-ledyer-confirmation.php +++ b/classes/class-ledyer-confirmation.php @@ -32,8 +32,8 @@ public function actions() { * Confirm the order in Woo */ public function confirm_order() { - $ledyer_confirm = filter_input( INPUT_GET, 'lco_confirm', FILTER_SANITIZE_URL); - $order_key = filter_input( INPUT_GET, 'key', FILTER_SANITIZE_STRING); + $ledyer_confirm = filter_input( INPUT_GET, 'lco_confirm', FILTER_SANITIZE_URL ); + $order_key = filter_input( INPUT_GET, 'key', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); if ( empty( $ledyer_confirm ) || empty( $order_key ) ) { return; diff --git a/classes/class-ledyer-lco-gateway.php b/classes/class-ledyer-lco-gateway.php index 5680d1e..4a1785d 100644 --- a/classes/class-ledyer-lco-gateway.php +++ b/classes/class-ledyer-lco-gateway.php @@ -293,7 +293,7 @@ public function process_payment_handler( $order_id ) { $order->update_meta_data( '_wc_ledyer_session_id', $ledyer_order['id'] ); - $order->update_meta_data( '_transaction_id', $ledyer_order['orderId'] ); + $order->set_transaction_id( $ledyer_order['orderId'] ); $order->update_meta_data( '_ledyer_company_id', $ledyer_order['customer']['companyId'] ); diff --git a/classes/class-ledyer-main.php b/classes/class-ledyer-main.php index 74b942e..e26e376 100644 --- a/classes/class-ledyer-main.php +++ b/classes/class-ledyer-main.php @@ -25,7 +25,7 @@ class Ledyer_Checkout_For_WooCommerce { * * @var array */ - public $notices = []; + public $notices = array(); /** * Reference to credentials class. * @@ -51,21 +51,28 @@ class Ledyer_Checkout_For_WooCommerce { */ public $checkout; - const VERSION = '1.8.0'; - const SLUG = 'ledyer-checkout-for-woocommerce'; + const VERSION = '1.8.1'; + const SLUG = 'ledyer-checkout-for-woocommerce'; const SETTINGS = 'ledyer_checkout_for_woocommerce_settings'; public function actions() { - \add_action( 'plugins_loaded', [ $this, 'on_plugins_loaded' ] ); + \add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ) ); \add_action( 'admin_init', array( $this, 'on_admin_init' ) ); - add_action( 'rest_api_init', function () { - register_rest_route( 'ledyer/v1', '/notifications/', array( - 'methods' => 'POST', - 'callback' => [ $this, 'handle_notification' ], - 'permission_callback' => '__return_true' - ) ); - } ); + add_action( + 'rest_api_init', + function () { + register_rest_route( + 'ledyer/v1', + '/notifications/', + array( + 'methods' => 'POST', + 'callback' => array( $this, 'handle_notification' ), + 'permission_callback' => '__return_true', + ) + ); + } + ); add_action( 'woocommerce_checkout_fields', @@ -78,110 +85,122 @@ public function actions() { ); add_action( 'schedule_process_notification', array( $this, 'process_notification' ), 10, 1 ); - } /** * Handles notification callbacks + * * @param $request * * @return \WP_REST_Response */ public function handle_notification( $request ) { - $request_body = json_decode( $request->get_body()); - $response = new \WP_REST_Response(); + $request_body = json_decode( $request->get_body() ); + $response = new \WP_REST_Response(); - if (json_last_error() !== JSON_ERROR_NONE) { + if ( json_last_error() !== JSON_ERROR_NONE ) { Logger::log( 'Request body isn\'t valid JSON string.' ); $response->set_status( 400 ); return $response; } $ledyer_event_type = $request_body->{'eventType'}; - $ledyer_order_id = $request_body->{'orderId'}; + $ledyer_order_id = $request_body->{'orderId'}; - if ($ledyer_event_type === NULL || $ledyer_order_id === NULL) { + if ( $ledyer_event_type === null || $ledyer_order_id === null ) { Logger::log( 'Request body doesn\'t hold orderId and eventType data.' ); $response->set_status( 400 ); return $response; } - $scheduleId = as_schedule_single_action(time() + 120, 'schedule_process_notification', array($ledyer_order_id) ); - Logger::log( 'Enqueued notification: ' . $ledyer_event_type . ", schedule-id:" . $scheduleId ); + $scheduleId = as_schedule_single_action( time() + 120, 'schedule_process_notification', array( $ledyer_order_id ) ); + Logger::log( 'Enqueued notification: ' . $ledyer_event_type . ', schedule-id:' . $scheduleId ); $response->set_status( 200 ); return $response; } public function process_notification( $ledyer_order_id ) { - Logger::log( 'process notification: ' . $ledyer_order_id); - - $orders = wc_get_orders( - array( - 'meta_query' => array( - array( - 'key' => '_wc_ledyer_order_id', - 'value' => $ledyer_order_id, - 'compare' => '=', - ) - ), - 'date_created' => '>' . ( time() - MONTH_IN_SECONDS ) - ) - ); - - $order_id = isset($orders[0]) ? $orders[0]->get_id() : null; - $order = wc_get_order( $order_id ); - - if ( !is_object( $order ) ) { - Logger::log('Could not find woo order with ledyer id: ' . $ledyer_order_id ); + Logger::log( 'process notification: ' . $ledyer_order_id ); + + $orders = wc_get_orders( + array( + 'meta_query' => array( + array( + 'key' => '_wc_ledyer_order_id', + 'value' => $ledyer_order_id, + 'compare' => '=', + ), + ), + 'date_created' => '>' . ( time() - MONTH_IN_SECONDS ), + ) + ); + + $order_id = isset( $orders[0] ) ? $orders[0]->get_id() : null; + $order = wc_get_order( $order_id ); + + if ( ! is_object( $order ) ) { + Logger::log( 'Could not find woo order with ledyer id: ' . $ledyer_order_id ); return; } $ledyer_payment_status = ledyer()->api->get_payment_status( $ledyer_order_id ); - if ( is_wp_error($ledyer_payment_status) ) { + if ( is_wp_error( $ledyer_payment_status ) ) { \Ledyer\Logger::log( 'Could not get ledyer payment status ' . $ledyer_order_id ); return; } $ackOrder = false; - switch( $ledyer_payment_status['status']) { + switch ( $ledyer_payment_status['status'] ) { case \LedyerPaymentStatus::paymentPending: - if ( !$order->has_status( array( 'on-hold', 'processing', 'completed' ) ) ) { - $note = sprintf( __( 'New payment created in Ledyer with Payment ID %1$s. %2$s', - 'ledyer-checkout-for-woocommerce' ), $ledyer_order_id, $ledyer_payment_status['note'] ); - $order->update_status('on-hold', $note); + if ( ! $order->has_status( array( 'on-hold', 'processing', 'completed' ) ) ) { + $note = sprintf( + __( + 'New payment created in Ledyer with Payment ID %1$s. %2$s', + 'ledyer-checkout-for-woocommerce' + ), + $ledyer_order_id, + $ledyer_payment_status['note'] + ); + $order->update_status( 'on-hold', $note ); $ackOrder = true; } break; case \LedyerPaymentStatus::paymentConfirmed: - if ( !$order->has_status( array( 'processing', 'completed' ) ) ) { - $note = sprintf( __( 'New payment created in Ledyer with Payment ID %1$s. %2$s', - 'ledyer-checkout-for-woocommerce' ), $ledyer_order_id, $ledyer_payment_status['note'] ); - $order->add_order_note($note); - $order->payment_complete($ledyer_order_id); + if ( ! $order->has_status( array( 'processing', 'completed' ) ) ) { + $note = sprintf( + __( + 'New payment created in Ledyer with Payment ID %1$s. %2$s', + 'ledyer-checkout-for-woocommerce' + ), + $ledyer_order_id, + $ledyer_payment_status['note'] + ); + $order->add_order_note( $note ); + $order->payment_complete( $ledyer_order_id ); $ackOrder = true; } break; case \LedyerPaymentStatus::orderCaptured: - $order->update_status('completed'); + $order->update_status( 'completed' ); break; case \LedyerPaymentStatus::orderRefunded: - $order->update_status('refunded'); + $order->update_status( 'refunded' ); break; case \LedyerPaymentStatus::orderCancelled: - $order->update_status('cancelled'); + $order->update_status( 'cancelled' ); break; } - if ($ackOrder) { + if ( $ackOrder ) { $response = ledyer()->api->acknowledge_order( $ledyer_order_id ); - if( is_wp_error( $response ) ) { - \Ledyer\Logger::log( 'Couldn\'t acknowledge order ' . $ledyer_order_id ); + if ( is_wp_error( $response ) ) { + \Ledyer\Logger::log( 'Couldn\'t acknowledge order ' . $ledyer_order_id ); return; } $ledyer_update_order = ledyer()->api->update_order_reference( $ledyer_order_id, array( 'reference' => $order->get_order_number() ) ); if ( is_wp_error( $ledyer_update_order ) ) { - \Ledyer\Logger::log( 'Couldn\'t set merchant reference ' . $order->get_order_number() ); + \Ledyer\Logger::log( 'Couldn\'t set merchant reference ' . $order->get_order_number() ); return; } } @@ -213,10 +232,13 @@ public function on_plugins_loaded() { load_plugin_textdomain( 'ledyer-checkout-for-woocommerce', false, LCO_WC_PLUGIN_NAME . '/languages' ); add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateways' ) ); - add_filter( 'plugin_action_links_' . plugin_basename( LCO_WC_MAIN_FILE ), array( - $this, - 'plugin_action_links' - ) ); + add_filter( + 'plugin_action_links_' . plugin_basename( LCO_WC_MAIN_FILE ), + array( + $this, + 'plugin_action_links', + ) + ); } /** @@ -225,29 +247,29 @@ public function on_plugins_loaded() { public function include_files() { include_once LCO_WC_PLUGIN_PATH . '/includes/lco-functions.php'; include_once LCO_WC_PLUGIN_PATH . '/includes/lco-types.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-singleton.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-logger.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/admin/class-ledyer-meta-box.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/requests/class-ledyer-request.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/requests/order/class-ledyer-request-order.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/requests/order/session/class-ledyer-create-order.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/requests/order/session/class-ledyer-get-order.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/requests/order/session/class-ledyer-update-order.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/requests/order/management/class-ledyer-acknowledge-order.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/requests/order/management/class-ledyer-get-order.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-singleton.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-logger.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/admin/class-ledyer-meta-box.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/requests/class-ledyer-request.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/requests/order/class-ledyer-request-order.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/requests/order/session/class-ledyer-create-order.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/requests/order/session/class-ledyer-get-order.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/requests/order/session/class-ledyer-update-order.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/requests/order/management/class-ledyer-acknowledge-order.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/requests/order/management/class-ledyer-get-order.php'; include_once LCO_WC_PLUGIN_PATH . '/classes/requests/order/management/class-ledyer-get-payment-status.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/requests/order/management/class-ledyer-update-order-reference.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/requests/helpers/class-ledyer-cart.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/requests/helpers/class-ledyer-woocommerce-bridge.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-ajax.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-api.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-checkout.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-confirmation.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-credentials.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-fields.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-lco-gateway.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-merchant-urls.php'; - include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-templates.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/requests/order/management/class-ledyer-update-order-reference.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/requests/helpers/class-ledyer-cart.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/requests/helpers/class-ledyer-woocommerce-bridge.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-ajax.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-api.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-checkout.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-confirmation.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-credentials.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-fields.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-lco-gateway.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-merchant-urls.php'; + include_once LCO_WC_PLUGIN_PATH . '/classes/class-ledyer-templates.php'; } /** @@ -279,6 +301,7 @@ public function set_settings() { /** * Get LCO setting by name + * * @param string $key * * @return mixed @@ -335,23 +358,23 @@ public function on_admin_init() { */ public function modify_checkout_fields( $checkout_fields ) { - if( ! is_checkout() ) { + if ( ! is_checkout() ) { return $checkout_fields; } - if( ! isset( WC()->session ) || empty( WC()->session->get('chosen_payment_method') ) ) { + if ( ! isset( WC()->session ) || empty( WC()->session->get( 'chosen_payment_method' ) ) ) { return $checkout_fields; } - if( 'lco' === WC()->session->get('chosen_payment_method') ) { + if ( 'lco' === WC()->session->get( 'chosen_payment_method' ) ) { foreach ( $checkout_fields['billing'] as $key => $field ) { - if( false !== stripos( $key, 'first_name' ) || false !== stripos( $key, 'last_name' ) ) { + if ( false !== stripos( $key, 'first_name' ) || false !== stripos( $key, 'last_name' ) ) { $checkout_fields['billing'][ $key ]['required'] = false; } } foreach ( $checkout_fields['shipping'] as $key => $field ) { - if( false !== stripos( $key, 'first_name' ) || false !== stripos( $key, 'last_name' ) ) { + if ( false !== stripos( $key, 'first_name' ) || false !== stripos( $key, 'last_name' ) ) { $checkout_fields['shipping'][ $key ]['required'] = false; } } diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..ce49e8c --- /dev/null +++ b/composer.lock @@ -0,0 +1,18 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "ec2bc7f14e0adac7ccbe8b016e933a8d", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.6.0" +} diff --git a/includes/lco-functions.php b/includes/lco-functions.php index 327a5ab..edf72c3 100644 --- a/includes/lco-functions.php +++ b/includes/lco-functions.php @@ -199,6 +199,9 @@ function wc_ledyer_confirm_ledyer_order( $order_id ) { $payment_id = WC()->session->get( 'lco_wc_order_id' ); } + $ledyer_order = ledyer()->api->get_order_session( $payment_id ); + $order->set_transaction_id( $ledyer_order['orderId'] ); + if( null === $session_id ) { $session_id = WC()->session->get( 'lco_wc_session_id' ); } diff --git a/ledyer-checkout-for-woocommerce.php b/ledyer-checkout-for-woocommerce.php index 58c39f2..45a8b3d 100644 --- a/ledyer-checkout-for-woocommerce.php +++ b/ledyer-checkout-for-woocommerce.php @@ -5,7 +5,7 @@ * Description: Ledyer Checkout payment gateway for WooCommerce. * Author: Maksimer/Ledyer * Author URI: https://www.maksimer.com/ - * Version: 1.8.0 + * Version: 1.8.1 * Text Domain: ledyer-checkout-for-woocommerce * Domain Path: /languages * @@ -39,8 +39,8 @@ * Required minimums and constants */ \define( 'LCO_WC_VERSION', Ledyer_Checkout_For_WooCommerce::VERSION ); -\define( 'LCO_WC_MIN_PHP_VER', '5.6.0' ); -\define( 'LCO_WC_MIN_WC_VER', '3.9.0' ); +\define( 'LCO_WC_MIN_PHP_VER', '7.4.0' ); +\define( 'LCO_WC_MIN_WC_VER', '5.6.0' ); \define( 'LCO_WC_MAIN_FILE', __FILE__ ); \define( 'LCO_WC_PLUGIN_NAME', dirname( plugin_basename( LCO_WC_MAIN_FILE ) ) ); \define( 'LCO_WC_PLUGIN_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) ); @@ -48,12 +48,12 @@ // Declare HPOS compatibility. add_action( - 'before_woocommerce_init', - function () { - if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { - \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); - } - } + 'before_woocommerce_init', + function () { + if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { + \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); + } + } ); function ledyer() { diff --git a/readme.txt b/readme.txt index 71b908c..767aa61 100644 --- a/readme.txt +++ b/readme.txt @@ -2,11 +2,16 @@ Contributors: maksimer, ledyer Tags: woocommerce, ledyer, ecommerce, e-commerce, checkout Donate link: https://ledyer.com -Requires at least: 4.0 -Tested up to: 6.3 -Requires PHP: 7.0 -WC requires at least: 4.0.0 -WC tested up to: 8.2.0 -Stable tag: 1.8.0 +Requires at least: 5.0 +Tested up to: 6.6.2 +Requires PHP: 7.4 +WC requires at least: 5.6.0 +WC tested up to: 9.3.3 +Stable tag: 1.8.1 License: GPLv3 or later License URI: http://www.gnu.org/licenses/gpl-3.0.html + +== Changelog == += 2024.10.01 - version 1.8.1 = +* Fix - Update deprecated sanitization. +* Fix - Confirm that the current Ledyer payment id is set as transaction id in WooCommerce after purchase. \ No newline at end of file