From 0db4885c6bd9b27c703e396df321cb71cbcab069 Mon Sep 17 00:00:00 2001 From: Ivan Pugach Date: Wed, 22 May 2024 02:06:35 +0300 Subject: [PATCH 1/6] Improve plugin code standards --- mondu-buy-now-pay-later.php | 6 +- src/Mondu/Admin/Option/Account.php | 94 +++++--- src/Mondu/Admin/Option/Helper.php | 54 ++++- src/Mondu/Admin/Order.php | 97 ++++++-- src/Mondu/Admin/Settings.php | 143 ++++++++--- .../Exceptions/CredentialsNotSetException.php | 11 +- src/Mondu/Exceptions/MonduException.php | 11 +- src/Mondu/Exceptions/ResponseException.php | 28 ++- src/Mondu/Mondu/Api.php | 22 +- src/Mondu/Mondu/Blocks/MonduBlocksSupport.php | 36 ++- .../Mondu/Controllers/OrdersController.php | 46 +++- .../Mondu/Controllers/WebhooksController.php | 162 ++++++++++--- src/Mondu/Mondu/GatewayDirectDebit.php | 20 +- src/Mondu/Mondu/GatewayFields.php | 15 +- src/Mondu/Mondu/GatewayInstallment.php | 20 +- .../Mondu/GatewayInstallmentByInvoice.php | 20 +- src/Mondu/Mondu/GatewayInvoice.php | 20 +- src/Mondu/Mondu/Models/SignatureVerifier.php | 48 +++- src/Mondu/Mondu/MonduGateway.php | 61 +++-- src/Mondu/Mondu/MonduRequestWrapper.php | 129 +++++----- src/Mondu/Mondu/Presenters/PaymentInfo.php | 169 ++++++++----- src/Mondu/Mondu/Support/Helper.php | 40 +++- src/Mondu/Mondu/Support/OrderData.php | 12 +- src/Mondu/Plugin.php | 226 ++++++++++++------ 24 files changed, 1080 insertions(+), 410 deletions(-) diff --git a/mondu-buy-now-pay-later.php b/mondu-buy-now-pay-later.php index 1e2e60ef..30ae471b 100644 --- a/mondu-buy-now-pay-later.php +++ b/mondu-buy-now-pay-later.php @@ -3,7 +3,7 @@ * Plugin Name: Mondu Buy Now Pay Later * Plugin URI: https://github.com/mondu-ai/bnpl-checkout-woocommerce/releases * Description: Mondu provides B2B E-commerce and B2B marketplaces with an online payment solution to buy now and pay later. - * Version: 2.2.1 + * Version: 2.2.2 * Author: Mondu * Author URI: https://mondu.ai * @@ -27,7 +27,7 @@ die( 'Direct access not allowed' ); } -define( 'MONDU_PLUGIN_VERSION', '2.2.1' ); +define( 'MONDU_PLUGIN_VERSION', '2.2.2' ); define( 'MONDU_PLUGIN_FILE', __FILE__ ); define( 'MONDU_PLUGIN_PATH', __DIR__ ); define( 'MONDU_PLUGIN_BASENAME', plugin_basename(MONDU_PLUGIN_FILE) ); @@ -46,7 +46,7 @@ function mondu_env( $name, $default_value ) { mondu_env( 'MONDU_WEBHOOKS_URL', get_home_url() ); require_once 'src/autoload.php'; -add_action('plugins_loaded', [ new \Mondu\Plugin(), 'init' ]); +add_action( 'plugins_loaded', [ new \Mondu\Plugin(), 'init' ] ); function mondu_activate() { } diff --git a/src/Mondu/Admin/Option/Account.php b/src/Mondu/Admin/Option/Account.php index f85925d5..ce0bcaeb 100644 --- a/src/Mondu/Admin/Option/Account.php +++ b/src/Mondu/Admin/Option/Account.php @@ -1,84 +1,118 @@ 'sandbox_or_production', - 'tip' => __('Mondu\'s environment to use.', 'mondu'), + 'tip' => __( 'Mondu\'s environment to use.', 'mondu' ), ] ); - add_settings_field('api_token', - __('API Token', 'mondu'), + add_settings_field( + 'api_token', + __( 'API Token', 'mondu' ), [ $this, 'field_api_token' ], 'mondu-settings-account', 'mondu_account_settings_general', [ 'label_for' => 'api_token', - 'tip' => __('API Token provided by Mondu.', 'mondu'), + 'tip' => __( 'API Token provided by Mondu.', 'mondu' ), ] ); - add_settings_field('send_line_items', - __('Send line items', 'mondu'), + add_settings_field( + 'send_line_items', + __( 'Send line items', 'mondu' ), [ $this, 'field_send_line_items' ], 'mondu-settings-account', 'mondu_account_settings_general', [ 'label_for' => 'send_line_items', - 'tip' => __('Send the line items when creating order and invoice.', 'mondu'), + 'tip' => __( 'Send the line items when creating order and invoice.', 'mondu' ), ] ); } - public function field_sandbox_or_production( $args = [] ) { - $this->selectField(Plugin::OPTION_NAME, 'sandbox_or_production', [ - 'sandbox' => __('Sandbox', 'mondu'), - 'production' => __('Production', 'mondu'), - ], $args['tip']); + /** + * Field for sandbox or production + * + * @param array $args arguments. + */ + public function field_sandbox_or_production( $args = [] ) { + $this->selectField( Plugin::OPTION_NAME, 'sandbox_or_production', [ + 'sandbox' => __( 'Sandbox', 'mondu' ), + 'production' => __( 'Production', 'mondu' ), + ], $args['tip'] ); } - public function field_api_token( $args = [] ) { - $this->textField(Plugin::OPTION_NAME, 'api_token', $args['tip']); + /** + * Field for API token + * + * @param array $args arguments. + */ + public function field_api_token( $args = [] ) { + $this->textField( Plugin::OPTION_NAME, 'api_token', $args['tip'] ); } - public function field_send_line_items( $args = [] ) { - $this->selectField(Plugin::OPTION_NAME, 'send_line_items', [ - 'yes' => __('Yes', 'mondu'), - 'order' => __('Send line items only for orders', 'mondu'), - 'no' => __('No', 'mondu'), - ], $args['tip']); + /** + * Field for send line items + * + * @param array $args arguments. + */ + public function field_send_line_items( $args = [] ) { + $this->selectField( Plugin::OPTION_NAME, 'send_line_items', [ + 'yes' => __( 'Yes', 'mondu' ), + 'order' => __( 'Send line items only for orders', 'mondu' ), + 'no' => __( 'No', 'mondu' ), + ], $args['tip'] ); } - public function render( $validation_error = null, $webhooks_error = null ) { - if ( !current_user_can('manage_options') ) { - wp_die(esc_html__('You do not have sufficient permissions to access this page.')); + /** + * Render the account options + * + * @param mixed $validation_error validation error. + * @param mixed $webhooks_error webhooks error. + */ + public function render( $validation_error = null, $webhooks_error = null ) { + if ( !current_user_can( 'manage_options' ) ) { + wp_die( esc_html__( 'You do not have sufficient permissions to access this page.' ) ); } - $credentials_validated = get_option('_mondu_credentials_validated'); - $webhooks_registered = get_option('_mondu_webhooks_registered'); + $credentials_validated = get_option( '_mondu_credentials_validated' ); + $webhooks_registered = get_option( '_mondu_webhooks_registered' ); include MONDU_VIEW_PATH . '/admin/options.php'; } diff --git a/src/Mondu/Admin/Option/Helper.php b/src/Mondu/Admin/Option/Helper.php index fb0c1284..6ad5e84b 100644 --- a/src/Mondu/Admin/Option/Helper.php +++ b/src/Mondu/Admin/Option/Helper.php @@ -1,5 +1,9 @@ global_settings = get_option(Plugin::OPTION_NAME); + $this->global_settings = get_option( Plugin::OPTION_NAME ); } + /** + * Text field rendering. + * + * @param $option_name + * @param $field_name + * @param $tip + * @return void + */ protected function textField( $option_name, $field_name, $tip = '' ) { $field_id = $field_name; - $field_value = isset($this->global_settings[ $field_name ]) ? $this->global_settings[ $field_name ] : ''; + $field_value = isset( $this->global_settings[ $field_name ] ) ? $this->global_settings[ $field_name ] : ''; $field_name = $option_name . '[' . $field_name . ']'; ?> - - + + global_settings[ $field_name ]) ? $this->global_settings[ $field_name ] : ''; + $field_value = isset( $this->global_settings[ $field_name ] ) ? $this->global_settings[ $field_name ] : ''; $field_name = $option_name . '[' . $field_name . ']'; ?> - - $label ) { ?> - + diff --git a/src/Mondu/Admin/Order.php b/src/Mondu/Admin/Order.php index 2981583e..d749be3c 100644 --- a/src/Mondu/Admin/Order.php +++ b/src/Mondu/Admin/Order.php @@ -1,5 +1,9 @@ mondu_request_wrapper = new MonduRequestWrapper(); } + /** + * Add payment info box. + * + * @return void + * @throws \Exception + */ public function add_payment_info_box() { $screen = class_exists( '\Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController' ) && wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order'; - add_meta_box('mondu_payment_info', - __('Mondu Order Information', 'mondu'), - function ($post_or_order_object) { + add_meta_box( + 'mondu_payment_info', + __( 'Mondu Order Information', 'mondu' ), + function ( $post_or_order_object ) { $order = ( $post_or_order_object instanceof \WP_Post ) ? wc_get_order( $post_or_order_object->ID ) : $post_or_order_object; if ( null === $order ) return; - if ( !in_array($order->get_payment_method(), Plugin::PAYMENT_METHODS, true) ) return; + if ( !in_array( $order->get_payment_method(), Plugin::PAYMENT_METHODS, true ) ) return; - $this->render_meta_box_content($order); + $this->render_meta_box_content( $order ); }, $screen, 'normal' ); } + /** + * Invoice buttons js. + * + * @return void + */ public function invoice_buttons_js() { require_once MONDU_VIEW_PATH . '/admin/js/invoice.php'; } + /** + * Render meta box content. + * + * @param WC_Order $order Order object. + * + * @return void + * @throws \Exception + */ public function render_meta_box_content( $order ) { - $payment_info = new PaymentInfo($order->get_id()); + $payment_info = new PaymentInfo( $order->get_id() ); $payment_info->get_mondu_section_html(); } + /** + * Cancel invoice. + * + * @return void + */ public function cancel_invoice() { $is_nonce_valid = check_ajax_referer( 'mondu-cancel-invoice', 'security', false ); if ( !$is_nonce_valid ) { - status_header(400); - exit(esc_html__('Bad Request.', 'mondu')); + status_header( 400 ); + exit(esc_html__( 'Bad Request.', 'mondu' ) ); } - $invoice_id = isset($_POST['invoice_id']) ? sanitize_text_field($_POST['invoice_id']) : ''; - $mondu_order_id = isset($_POST['mondu_order_id']) ? sanitize_text_field($_POST['mondu_order_id']) : ''; - $order_id = isset($_POST['order_id']) ? sanitize_text_field($_POST['order_id']) : ''; + $invoice_id = isset( $_POST['invoice_id'] ) ? sanitize_text_field( $_POST['invoice_id'] ) : ''; + $mondu_order_id = isset( $_POST['mondu_order_id'] ) ? sanitize_text_field( $_POST['mondu_order_id'] ) : ''; + $order_id = isset( $_POST['order_id'] ) ? sanitize_text_field( $_POST['order_id'] ) : ''; $order = new WC_Order($order_id); try { - $this->mondu_request_wrapper->cancel_invoice($mondu_order_id, $invoice_id); + $this->mondu_request_wrapper->cancel_invoice( $mondu_order_id, $invoice_id ); } catch ( ResponseException $e ) { wp_send_json([ 'error' => true, @@ -91,22 +130,27 @@ public function cancel_invoice() { } } + /** + * Create invoice. + * + * @return void + */ public function create_invoice() { $is_nonce_valid = check_ajax_referer( 'mondu-create-invoice', 'security', false ); if ( !$is_nonce_valid ) { - status_header(400); - exit(esc_html__('Bad Request.', 'mondu')); + status_header( 400 ); + exit( esc_html__( 'Bad Request.', 'mondu' ) ); } - $order_id = isset($_POST['order_id']) ? sanitize_text_field($_POST['order_id']) : ''; + $order_id = isset( $_POST['order_id'] ) ? sanitize_text_field( $_POST['order_id'] ) : ''; - $order = new WC_Order($order_id); + $order = new WC_Order( $order_id ); if ( null === $order ) { return; } try { - $this->mondu_request_wrapper->ship_order($order_id); + $this->mondu_request_wrapper->ship_order( $order_id ); } catch ( ResponseException $e ) { wp_send_json([ 'error' => true, @@ -120,28 +164,33 @@ public function create_invoice() { } } + /** + * Confirm order. + * + * @return void + */ public function confirm_order() { $is_nonce_valid = check_ajax_referer( 'mondu-confirm-order', 'security', false ); if ( !$is_nonce_valid ) { - status_header(400); - exit(esc_html__('Bad Request.', 'mondu')); + status_header( 400 ); + exit(esc_html__( 'Bad Request.', 'mondu' ) ); } - $order_id = isset($_POST['order_id']) ? sanitize_text_field($_POST['order_id']) : ''; + $order_id = isset( $_POST['order_id'] ) ? sanitize_text_field( $_POST['order_id'] ) : ''; - $order = new WC_Order($order_id); + $order = new WC_Order( $order_id ); if ( null === $order ) { return; } - $mondu_order_id = isset($_POST['order_uuid']) ? sanitize_text_field($_POST['order_uuid']) : ''; + $mondu_order_id = isset( $_POST['order_uuid'] ) ? sanitize_text_field( $_POST['order_uuid'] ) : ''; if ( !$mondu_order_id ) { return; } try { - $this->mondu_request_wrapper->confirm_order($order_id, $mondu_order_id); + $this->mondu_request_wrapper->confirm_order( $order_id, $mondu_order_id ); } catch ( ResponseException $e ) { wp_send_json([ 'error' => true, diff --git a/src/Mondu/Admin/Settings.php b/src/Mondu/Admin/Settings.php index ea3a4115..54b8531d 100644 --- a/src/Mondu/Admin/Settings.php +++ b/src/Mondu/Admin/Settings.php @@ -1,10 +1,14 @@ global_settings = get_option(Plugin::OPTION_NAME); + $this->global_settings = get_option( Plugin::OPTION_NAME ); $this->mondu_request_wrapper = new MonduRequestWrapper(); } + /** + * Initialize. + * + * @return void + */ public function init() { - add_action('admin_menu', [ $this, 'plugin_menu' ]); - add_action('admin_init', [ $this, 'register_options' ]); - add_action('admin_post_download_logs', [ $this, 'download_mondu_logs' ]); - add_filter('woocommerce_screen_ids', [ $this, 'set_wc_screen_ids' ]); + add_action( 'admin_menu', [ $this, 'plugin_menu' ] ); + add_action( 'admin_init', [ $this, 'register_options' ] ); + add_action( 'admin_post_download_logs', [ $this, 'download_mondu_logs' ] ); + add_filter( 'woocommerce_screen_ids', [ $this, 'set_wc_screen_ids' ] ); } + /** + * Plugin menu. + * + * @return void + */ public function plugin_menu() { $mondu_icon = 'data:image/svg+xml;base64,' . base64_encode( file_get_contents( 'https://checkout.mondu.ai/logo.svg' ) ); @@ -57,110 +84,148 @@ public function plugin_menu() { ); } + /** + * Register options. + * + * @return void + */ public function register_options() { $this->account_options = new Account(); $this->account_options->register(); } + /** + * Set WC screen ids. + * + * @param $screen + * @return mixed + */ public function set_wc_screen_ids( $screen ) { $screen[] = 'toplevel_page_mondu-settings-account'; return $screen; } + /** + * Render account options. + * + * @return void + */ public function render_account_options() { $validation_error = null; $webhooks_error = null; - if ( isset($_POST['validate-credentials']) && check_admin_referer('validate-credentials', 'validate-credentials') ) { + if ( isset( $_POST['validate-credentials'] ) && check_admin_referer( 'validate-credentials', 'validate-credentials' ) ) { try { if ( $this->missing_credentials() ) { - throw new CredentialsNotSetException(__('Missing Credentials', 'mondu')); + throw new CredentialsNotSetException( __( 'Missing Credentials', 'mondu' ) ); } $secret = $this->mondu_request_wrapper->webhook_secret(); - update_option('_mondu_webhook_secret', $secret); + update_option( '_mondu_webhook_secret', $secret ); - update_option('_mondu_credentials_validated', time()); + update_option( '_mondu_credentials_validated', time() ); } catch ( MonduException $e ) { - delete_option('_mondu_credentials_validated'); + delete_option( '_mondu_credentials_validated' ); $validation_error = $e->getMessage(); } - } elseif ( isset($_POST['register-webhooks']) && check_admin_referer('register-webhooks', 'register-webhooks') ) { + } elseif ( isset( $_POST['register-webhooks'] ) && check_admin_referer( 'register-webhooks', 'register-webhooks' ) ) { try { $this->register_webhooks_if_not_registered(); - update_option('_mondu_webhooks_registered', time()); + update_option( '_mondu_webhooks_registered', time() ); } catch ( MonduException $e ) { - delete_option('_mondu_webhooks_registered'); + delete_option( '_mondu_webhooks_registered' ); $webhooks_error = $e->getMessage(); } } elseif ( isset($_GET['settings-updated']) || $this->missing_credentials() ) { - delete_option('_mondu_credentials_validated'); - delete_option('_mondu_webhooks_registered'); + delete_option( '_mondu_credentials_validated' ); + delete_option( '_mondu_webhooks_registered' ); } - $this->account_options->render($validation_error, $webhooks_error); + $this->account_options->render( $validation_error, $webhooks_error ); } + /** + * Return missing credentials. + * + * @return bool + */ private function missing_credentials() { return ( - !isset($this->global_settings) || - !is_array($this->global_settings) || - !isset($this->global_settings['api_token']) || - '' === $this->global_settings['api_token'] + !isset( $this->global_settings ) || + !is_array( $this->global_settings ) || + !isset( $this->global_settings['api_token'] ) || + '' === $this->global_settings['api_token'] ); } + /** + * Register webhooks if not registered. + * + * @return void + * @throws ResponseException + */ private function register_webhooks_if_not_registered() { $webhooks = $this->mondu_request_wrapper->get_webhooks(); - $registered_topics = array_map(function( $webhook ) { + $registered_topics = array_map( function( $webhook ) { return $webhook['topic']; - }, $webhooks); + }, $webhooks ); $required_topics = [ 'order', 'invoice' ]; foreach ( $required_topics as $topic ) { - if ( !in_array($topic, $registered_topics, true) ) { + if ( !in_array( $topic, $registered_topics, true ) ) { $this->mondu_request_wrapper->register_webhook($topic); } } } + /** + * Download Mondu logs. + * + * @return void + */ public function download_mondu_logs() { $is_nonce_valid = check_ajax_referer( 'mondu-download-logs', 'security', false ); if ( !$is_nonce_valid ) { - status_header(400); - exit(esc_html__('Bad Request.', 'mondu')); + status_header( 400 ); + exit( esc_html__( 'Bad Request.', 'mondu' ) ); } - $date = isset( $_POST['date'] ) ? sanitize_text_field($_POST['date']) : null; + $date = isset( $_POST['date'] ) ? sanitize_text_field( $_POST['date'] ) : null; if ( null === $date ) { - status_header(400); - exit(esc_html__('Date is required.')); + status_header( 400 ); + exit( esc_html__( 'Date is required.' ) ); } - $file = $this->get_file($date); + $file = $this->get_file( $date ); if ( null === $file ) { - status_header(404); - exit(esc_html__('Log not found.')); + status_header( 404 ); + exit(esc_html__( 'Log not found.' ) ); } $filename = 'mondu-' . $date . '.log'; - header('Content-Type: text/plain'); - header('Content-Disposition: attachment; filename="' . $filename . '";'); + header( 'Content-Type: text/plain' ); + header( 'Content-Disposition: attachment; filename="' . $filename . '";' ); - readfile($file); + readfile( $file ); die(); } + /** + * Get file. + * + * @param $date + * @return false|string + */ private function get_file( $date ) { $base_dir = WP_CONTENT_DIR . '/uploads/wc-logs/'; - $dir = opendir($base_dir); + $dir = opendir( $base_dir ); if ( $dir ) { - while ( $file = readdir($dir) ) { - if ( str_starts_with($file, 'mondu-' . $date) && str_ends_with($file, '.log') ) { + while ( $file = readdir( $dir ) ) { + if ( str_starts_with( $file, 'mondu-' . $date ) && str_ends_with( $file, '.log' ) ) { return $base_dir . $file; } } diff --git a/src/Mondu/Exceptions/CredentialsNotSetException.php b/src/Mondu/Exceptions/CredentialsNotSetException.php index abb22dfd..04145c68 100644 --- a/src/Mondu/Exceptions/CredentialsNotSetException.php +++ b/src/Mondu/Exceptions/CredentialsNotSetException.php @@ -1,6 +1,15 @@ body = $body; - parent::__construct($message, $code, null); + parent::__construct( $message, $code ); } + /** + * Get body + * + * @return null + */ public function getBody() { return $this->body; } diff --git a/src/Mondu/Mondu/Api.php b/src/Mondu/Mondu/Api.php index bf416a89..57a30ae1 100644 --- a/src/Mondu/Mondu/Api.php +++ b/src/Mondu/Mondu/Api.php @@ -1,5 +1,9 @@ global_settings = get_option(Plugin::OPTION_NAME); } + /** + * Register + */ public function register() { register_setting('mondu', Plugin::OPTION_NAME); } diff --git a/src/Mondu/Mondu/Blocks/MonduBlocksSupport.php b/src/Mondu/Mondu/Blocks/MonduBlocksSupport.php index c1492d48..d31f89d6 100644 --- a/src/Mondu/Mondu/Blocks/MonduBlocksSupport.php +++ b/src/Mondu/Mondu/Blocks/MonduBlocksSupport.php @@ -1,5 +1,9 @@ gateways = [ - new GatewayInvoice(false), - new GatewayDirectDebit(false), - new GatewayInstallment(false), - new GatewayInstallmentByInvoice(false) + new GatewayInvoice( false ), + new GatewayDirectDebit( false ), + new GatewayInstallment( false ), + new GatewayInstallmentByInvoice( false ) ]; } + /** + * Get payment method script handles + * + * @return array + */ public function get_payment_method_script_handles() { wp_register_script( 'mondu-blocks-integration', @@ -48,13 +65,18 @@ public function get_payment_method_script_handles() { true ); - wp_set_script_translations( 'mondu-blocks-integration', 'mondu', MONDU_PLUGIN_PATH. '/languages'); + wp_set_script_translations( 'mondu-blocks-integration', 'mondu', MONDU_PLUGIN_PATH. '/languages' ); return [ 'mondu-blocks-integration' ]; } + /** + * Get payment method data + * + * @return array + */ public function get_payment_method_data() { - $gateways = array_reduce($this->gateways, function($carry, $item) { + $gateways = array_reduce($this->gateways, function( $carry, $item ) { $carry[$item->id] = [ 'title' => $item->get_title(), 'description' => $item->description, diff --git a/src/Mondu/Mondu/Controllers/OrdersController.php b/src/Mondu/Mondu/Controllers/OrdersController.php index 5d8671cc..9b0c5ac5 100644 --- a/src/Mondu/Mondu/Controllers/OrdersController.php +++ b/src/Mondu/Mondu/Controllers/OrdersController.php @@ -1,5 +1,9 @@ namespace = 'mondu/v1/orders'; $this->mondu_request_wrapper = new MonduRequestWrapper(); } - // Register our routes + /** + * Register routes + */ public function register_routes() { - register_rest_route($this->namespace, '/confirm', [ + register_rest_route( $this->namespace, '/confirm', [ [ 'methods' => 'GET', 'callback' => [ $this, 'confirm' ], 'permission_callback' => '__return_true', ], - ]); - register_rest_route($this->namespace, '/decline', [ + ] ); + register_rest_route( $this->namespace, '/decline', [ [ 'methods' => 'GET', 'callback' => [ $this, 'decline' ], 'permission_callback' => '__return_true', ], - ]); + ] ); } + /** + * Confirm order + * + * @param WP_REST_Request $request Request + */ public function confirm( WP_REST_Request $request ) { $params = $request->get_params(); $order_number = $params['external_reference_id']; $mondu_order_id = $params['order_uuid']; $return_url = urldecode( $params['return_url'] ); - $order = Helper::get_order_from_order_number_or_uuid( $order_number, $mondu_order_id ); + $order = Helper::get_order_from_order_number_or_uuid( $order_number, $mondu_order_id ); try { if ( !$order ) { @@ -55,20 +74,25 @@ public function confirm( WP_REST_Request $request ) { } if ( in_array( $order->get_status(), [ 'pending', 'failed' ] ) ) { - $order->update_status('wc-on-hold', __('On hold', 'woocommerce')); + $order->update_status( 'wc-on-hold', __('On hold', 'woocommerce' ) ); - $this->mondu_request_wrapper->confirm_order($order->get_id(), $mondu_order_id); + $this->mondu_request_wrapper->confirm_order( $order->get_id(), $mondu_order_id ); } } catch ( \Exception $e ) { - Helper::log([ + Helper::log( [ 'error_confirming_order' => $params, - ]); + ] ); } wp_safe_redirect( $return_url ); exit; } + /** + * Decline order + * + * @param WP_REST_Request $request Request + */ public function decline( WP_REST_Request $request ) { $params = $request->get_params(); $order_number = $params['external_reference_id']; diff --git a/src/Mondu/Mondu/Controllers/WebhooksController.php b/src/Mondu/Mondu/Controllers/WebhooksController.php index 35cbe5e9..535bbf81 100644 --- a/src/Mondu/Mondu/Controllers/WebhooksController.php +++ b/src/Mondu/Mondu/Controllers/WebhooksController.php @@ -1,102 +1,133 @@ namespace = 'mondu/v1/webhooks'; $this->mondu_request_wrapper = new MonduRequestWrapper(); } + /** + * Register routes + */ public function register_routes() { - register_rest_route($this->namespace, '/index', [ + register_rest_route( $this->namespace, '/index', [ [ 'methods' => 'POST', 'callback' => [ $this, 'index' ], 'permission_callback' => '__return_true', ], - ]); + ] ); } + /** + * Webhooks index + * + * @param WP_REST_Request $request + * + * @return WP_REST_Response + */ public function index( WP_REST_Request $request ) { $verifier = new SignatureVerifier(); $params = $request->get_json_params(); - $topic = isset($params['topic']) ? $params['topic'] : null; + $topic = isset( $params['topic'] ) ? $params['topic'] : null; $body = $request->get_body(); - $signature_payload = $request->get_header('X-MONDU-SIGNATURE'); - $signature = $verifier->create_hmac($body); + $signature_payload = $request->get_header( 'X-MONDU-SIGNATURE' ); + $signature = $verifier->create_hmac( $body ); - Helper::log([ + Helper::log( [ 'webhook_topic' => $topic, 'params' => $params, - ]); + ] ); try { if ( $signature !== $signature_payload ) { - throw new MonduException(__('Signature mismatch.', 'mondu')); + throw new MonduException( __( 'Signature mismatch.', 'mondu' ) ); } switch ( $topic ) { case 'order/pending': - $result = $this->handle_pending($params); + $result = $this->handle_pending( $params ); break; case 'order/authorized': - $result = $this->handle_authorized($params); + $result = $this->handle_authorized( $params ); break; case 'order/confirmed': - $result = $this->handle_confirmed($params); + $result = $this->handle_confirmed( $params ); break; case 'order/declined': - $result = $this->handle_declined($params); + $result = $this->handle_declined( $params ); break; case 'invoice/created': - $result = $this->handle_invoice_created($params); + $result = $this->handle_invoice_created( $params ); break; case 'invoice/payment': - $result = $this->handle_invoice_payment($params); + $result = $this->handle_invoice_payment( $params ); break; case 'invoice/canceled': - $result = $this->handle_invoice_canceled($params); + $result = $this->handle_invoice_canceled( $params ); break; default: - $result = $this->handle_not_found_topic($params); + $result = $this->handle_not_found_topic( $params ); break; } $res_body = $result[0]; $res_status = $result[1]; } catch ( MonduException $e ) { - $this->mondu_request_wrapper->log_plugin_event($e, 'webhooks', array_merge($params, [ 'signature' => $signature ])); + $this->mondu_request_wrapper->log_plugin_event( $e, 'webhooks', array_merge( $params, [ 'signature' => $signature ] ) ); $res_body = [ 'message' => $e->getMessage() ]; $res_status = 400; } catch ( \Exception $e ) { - $this->mondu_request_wrapper->log_plugin_event($e, 'webhooks', $params); - $res_body = [ 'message' => __('Something happened on our end.', 'mondu') ]; + $this->mondu_request_wrapper->log_plugin_event( $e, 'webhooks', $params ); + $res_body = [ 'message' => __( 'Something happened on our end.', 'mondu' ) ]; $res_status = 200; } - return new WP_REST_Response($res_body, $res_status); + return new WP_REST_Response( $res_body, $res_status ); } + /** + * Handle pending + * + * @param array $params + * + * @return array + * @throws MonduException + */ private function handle_pending( $params ) { $woocommerce_order_number = $params['external_reference_id']; $mondu_order_id = $params['order_uuid']; if ( !$woocommerce_order_number || !$mondu_order_id ) { - throw new MonduException(__('Required params missing.', 'mondu')); + throw new MonduException( __( 'Required params missing.', 'mondu' ) ); } $order = Helper::get_order_from_order_number_or_uuid( $woocommerce_order_number, $mondu_order_id ); @@ -110,12 +141,20 @@ private function handle_pending( $params ) { return $this->return_success(); } + /** + * Handle authorized + * + * @param array $params + * + * @return array + * @throws MonduException + */ private function handle_authorized( $params ) { $woocommerce_order_number = $params['external_reference_id']; $mondu_order_id = $params['order_uuid']; if ( !$woocommerce_order_number || !$mondu_order_id ) { - throw new MonduException(__('Required params missing.', 'mondu')); + throw new MonduException( __( 'Required params missing.', 'mondu' ) ); } $order = Helper::get_order_from_order_number_or_uuid( $woocommerce_order_number, $mondu_order_id ); @@ -129,12 +168,20 @@ private function handle_authorized( $params ) { return $this->return_success(); } + /** + * Handle confirmed + * + * @param array $params + * + * @return array + * @throws MonduException + */ private function handle_confirmed( $params ) { $woocommerce_order_number = $params['external_reference_id']; $mondu_order_id = $params['order_uuid']; if ( !$woocommerce_order_number || !$mondu_order_id ) { - throw new MonduException(__('Required params missing.', 'mondu')); + throw new MonduException( __( 'Required params missing.', 'mondu' ) ); } $order = Helper::get_order_from_order_number_or_uuid( $woocommerce_order_number, $mondu_order_id ); @@ -146,18 +193,26 @@ private function handle_confirmed( $params ) { $order->add_order_note( esc_html( sprintf( __( 'Mondu order is on confirmed state.', 'mondu' ) ) ), false ); if ( in_array( $order->get_status(), [ 'pending', 'on-hold' ] ) ) { - $order->update_status('wc-processing', __('Processing', 'woocommerce')); + $order->update_status( 'wc-processing', __( 'Processing', 'woocommerce' ) ); } return $this->return_success(); } + /** + * Handle declined + * + * @param array $params + * + * @return array + * @throws MonduException + */ private function handle_declined( $params ) { $woocommerce_order_number = $params['external_reference_id']; $mondu_order_id = $params['order_uuid']; if ( !$woocommerce_order_number || !$mondu_order_id ) { - throw new MonduException(__('Required params missing.', 'mondu')); + throw new MonduException( __( 'Required params missing.', 'mondu' ) ); } $order = Helper::get_order_from_order_number_or_uuid( $woocommerce_order_number, $mondu_order_id ); @@ -169,18 +224,26 @@ private function handle_declined( $params ) { $order->add_order_note( esc_html( sprintf( __( 'Mondu order is on declined state.', 'mondu' ) ) ), false ); if ( $order->get_status() == 'on-hold' ) { - $order->update_status('wc-failed', __('Failed', 'woocommerce')); + $order->update_status( 'wc-failed', __( 'Failed', 'woocommerce' ) ); } return $this->return_success(); } + /** + * Handle invoice created + * + * @param array $params + * + * @return array + * @throws MonduException + */ private function handle_invoice_created( $params ) { $woocommerce_order_number = $params['external_reference_id']; $mondu_order_id = $params['order_uuid']; if ( !$woocommerce_order_number ) { - throw new MonduException(__('Required params missing.', 'mondu')); + throw new MonduException( __( 'Required params missing.', 'mondu' ) ); } $order = Helper::get_order_from_order_number_or_uuid( $woocommerce_order_number, $mondu_order_id ); @@ -194,6 +257,14 @@ private function handle_invoice_created( $params ) { return $this->return_success(); } + /** + * Handle invoice payment + * + * @param array $params + * + * @return array + * @throws MonduException + */ private function handle_invoice_payment( $params ) { $woocommerce_order_number = $params['external_reference_id']; $mondu_order_id = $params['order_uuid']; @@ -213,12 +284,20 @@ private function handle_invoice_payment( $params ) { return $this->return_success(); } + /** + * Handle invoice canceled + * + * @param array $params + * + * @return array + * @throws MonduException + */ private function handle_invoice_canceled( $params ) { $woocommerce_order_number = $params['external_reference_id']; $mondu_order_id = $params['order_uuid']; if ( !$woocommerce_order_number ) { - throw new MonduException(__('Required params missing.', 'mondu')); + throw new MonduException( __( 'Required params missing.', 'mondu' ) ); } $order = Helper::get_order_from_order_number_or_uuid( $woocommerce_order_number, $mondu_order_id ); @@ -232,18 +311,35 @@ private function handle_invoice_canceled( $params ) { return $this->return_success(); } + /** + * Handle not found topic + * + * @param array $params + * + * @return array + */ private function handle_not_found_topic( $params ) { - Helper::log([ + Helper::log( [ 'not_found_topic' => $params, - ]); + ] ); return $this->return_success(); } + /** + * Return success + * + * @return array + */ private function return_success() { return [ [ 'message' => 'Ok' ], 200 ]; } + /** + * Return not found + * + * @return array + */ private function return_not_found() { return [ [ 'message' => __('Not Found', 'mondu') ], 404 ]; } diff --git a/src/Mondu/Mondu/GatewayDirectDebit.php b/src/Mondu/Mondu/GatewayDirectDebit.php index b31dc975..6a2a6dc0 100644 --- a/src/Mondu/Mondu/GatewayDirectDebit.php +++ b/src/Mondu/Mondu/GatewayDirectDebit.php @@ -1,19 +1,27 @@ id = Plugin::PAYMENT_METHODS['direct_debit']; - $this->title = __('Mondu SEPA Direct Debit', 'mondu'); + $this->title = __( 'Mondu SEPA Direct Debit', 'mondu' ); $this->description = $payment_instructions; $this->method_description = $payment_instructions; $this->has_fields = true; - parent::__construct($register_hooks); + parent::__construct( $register_hooks ); } } diff --git a/src/Mondu/Mondu/GatewayFields.php b/src/Mondu/Mondu/GatewayFields.php index 5e49eb04..c22ff88f 100644 --- a/src/Mondu/Mondu/GatewayFields.php +++ b/src/Mondu/Mondu/GatewayFields.php @@ -1,7 +1,16 @@ [ - 'title' => __('Enable/Disable', 'woocommerce'), + 'title' => __( 'Enable/Disable', 'woocommerce' ), 'type' => 'checkbox', - 'label' => /* translators: %s: Payment Method */ sprintf(__('Enable %s payment method', 'mondu'), $payment_method), + 'label' => /* translators: %s: Payment Method */ sprintf( __( 'Enable %s payment method', 'mondu' ), $payment_method ), 'default' => 'no', ], ]; diff --git a/src/Mondu/Mondu/GatewayInstallment.php b/src/Mondu/Mondu/GatewayInstallment.php index eda7d52b..f8a6245d 100644 --- a/src/Mondu/Mondu/GatewayInstallment.php +++ b/src/Mondu/Mondu/GatewayInstallment.php @@ -1,19 +1,27 @@ id = Plugin::PAYMENT_METHODS['installment']; - $this->title = __('Mondu Installments', 'mondu'); + $this->title = __( 'Mondu Installments', 'mondu' ); $this->description = $payment_instructions; $this->method_description = $payment_instructions; $this->has_fields = true; - parent::__construct($register_hooks); + parent::__construct( $register_hooks ); } } diff --git a/src/Mondu/Mondu/GatewayInstallmentByInvoice.php b/src/Mondu/Mondu/GatewayInstallmentByInvoice.php index 35e8f45f..0f0ab9ac 100644 --- a/src/Mondu/Mondu/GatewayInstallmentByInvoice.php +++ b/src/Mondu/Mondu/GatewayInstallmentByInvoice.php @@ -1,19 +1,27 @@ id = Plugin::PAYMENT_METHODS['installment_by_invoice']; - $this->title = __('Mondu Installments by Invoice', 'mondu'); + $this->title = __( 'Mondu Installments by Invoice', 'mondu' ); $this->description = $payment_instructions; $this->method_description = $payment_instructions; $this->has_fields = true; - parent::__construct($register_hooks); + parent::__construct( $register_hooks ); } } diff --git a/src/Mondu/Mondu/GatewayInvoice.php b/src/Mondu/Mondu/GatewayInvoice.php index a39ea9b6..cb4db787 100644 --- a/src/Mondu/Mondu/GatewayInvoice.php +++ b/src/Mondu/Mondu/GatewayInvoice.php @@ -1,19 +1,27 @@ id = Plugin::PAYMENT_METHODS['invoice']; - $this->title = __('Mondu Invoice', 'mondu'); + $this->title = __( 'Mondu Invoice', 'mondu' ); $this->description = $payment_instructions; $this->method_description = $payment_instructions; $this->has_fields = true; - parent::__construct($register_hooks); + parent::__construct( $register_hooks ); } } diff --git a/src/Mondu/Mondu/Models/SignatureVerifier.php b/src/Mondu/Mondu/Models/SignatureVerifier.php index ea87456f..5732674f 100644 --- a/src/Mondu/Mondu/Models/SignatureVerifier.php +++ b/src/Mondu/Mondu/Models/SignatureVerifier.php @@ -1,29 +1,67 @@ secret = get_option('_mondu_webhook_secret'); + $this->secret = get_option( '_mondu_webhook_secret' ); } + /** + * Get secret + * + * @return string + */ public function get_secret() { return $this->secret; } + /** + * Set secret + * + * @param string $secret Secret. + * + * @return $this + */ public function set_secret( $secret ) { $this->secret = $secret; return $this; } + /** + * Create HMAC + * + * @param string $payload Payload. + * + * @return string + */ public function create_hmac( $payload ) { - return hash_hmac('sha256', $payload, $this->secret); + return hash_hmac( 'sha256', $payload, $this->secret ); } - + /** + * Verify signature + * + * @param $signature + * + * @return bool + */ public function verify( $signature ) { return $this->secret === $signature; } diff --git a/src/Mondu/Mondu/MonduGateway.php b/src/Mondu/Mondu/MonduGateway.php index 1d5b46b9..61bd87de 100644 --- a/src/Mondu/Mondu/MonduGateway.php +++ b/src/Mondu/Mondu/MonduGateway.php @@ -1,21 +1,35 @@ global_settings = get_option(Plugin::OPTION_NAME); + /** + * MonduGateway constructor. + * + * @param bool $register_hooks + */ + public function __construct( $register_hooks = true ) { + $this->global_settings = get_option( Plugin::OPTION_NAME ); $this->init_form_fields(); $this->init_settings(); @@ -36,9 +55,9 @@ public function __construct($register_hooks = true) { $this->mondu_request_wrapper = new MonduRequestWrapper(); if ($register_hooks) { - add_action('woocommerce_update_options_payment_gateways_' . $this->id, [ $this, 'process_admin_options' ]); - add_action('woocommerce_thankyou_' . $this->id, [ $this, 'thankyou_page' ]); - add_action('woocommerce_email_before_order_table', [ $this, 'email_instructions' ], 10, 3); + add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, [ $this, 'process_admin_options' ] ); + add_action( 'woocommerce_thankyou_' . $this->id, [ $this, 'thankyou_page' ] ); + add_action( 'woocommerce_email_before_order_table', [ $this, 'email_instructions' ], 10, 3 ); } } @@ -46,7 +65,7 @@ public function __construct($register_hooks = true) { * Initialise Gateway Settings Form Fields */ public function init_form_fields() { - $this->form_fields = GatewayFields::fields($this->title); + $this->form_fields = GatewayFields::fields( $this->title ); } /** @@ -56,7 +75,7 @@ public function init_form_fields() { * @return array */ public static function add( array $methods ) { - array_unshift($methods, static::class); + array_unshift( $methods, static::class ); return $methods; } @@ -76,7 +95,7 @@ public function payment_fields() { */ public function thankyou_page() { if ( $this->description ) { - echo wp_kses_post(wpautop(wptexturize($this->description))); + echo wp_kses_post( wpautop( wptexturize( $this->description ) ) ); } } @@ -86,12 +105,12 @@ public function thankyou_page() { * @param WC_Order $order */ public function email_instructions( $order ) { - if ( !Plugin::order_has_mondu($order) ) { + if ( !Plugin::order_has_mondu( $order ) ) { return; } if ( $this->description && $this->id === $order->get_payment_method() ) { - echo wp_kses_post(wpautop(wptexturize($this->description))); + echo wp_kses_post( wpautop( wptexturize( $this->description ) ) ); } } @@ -108,7 +127,7 @@ public function get_icon() { * * @since 1.3.2 */ - return apply_filters('woocommerce_gateway_icon', $icon_html, $this->id); + return apply_filters( 'woocommerce_gateway_icon', $icon_html, $this->id ); } /** @@ -119,12 +138,12 @@ public function get_icon() { * @throws ResponseException */ public function process_payment( $order_id ) { - $order = wc_get_order($order_id); + $order = wc_get_order( $order_id ); $success_url = $this->get_return_url( $order ); $mondu_order = $this->mondu_request_wrapper->create_order( $order, $success_url ); if ( !$mondu_order ) { - wc_add_notice(__('Error placing an order. Please try again.', 'mondu'), 'error'); + wc_add_notice( __( 'Error placing an order. Please try again.', 'mondu' ), 'error' ); return; } @@ -140,10 +159,10 @@ public function process_payment( $order_id ) { * @return string */ private function is_enabled() { - if ( null === get_option('_mondu_credentials_validated') ) { + if ( null === get_option( '_mondu_credentials_validated' ) ) { $this->settings['enabled'] = 'no'; } - return !empty($this->settings['enabled']) && 'yes' === $this->settings['enabled'] ? 'yes' : 'no'; + return !empty( $this->settings['enabled'] ) && 'yes' === $this->settings['enabled'] ? 'yes' : 'no'; } } diff --git a/src/Mondu/Mondu/MonduRequestWrapper.php b/src/Mondu/Mondu/MonduRequestWrapper.php index 42abef76..e9c0a719 100644 --- a/src/Mondu/Mondu/MonduRequestWrapper.php +++ b/src/Mondu/Mondu/MonduRequestWrapper.php @@ -1,8 +1,11 @@ api = new Api(); } @@ -32,7 +48,7 @@ public function create_order( WC_Order $order, $success_url ) { $response = $this->wrap_with_mondu_log_event( 'create_order', [ $order_data ] ); $mondu_order = $response['order']; - $order->update_meta_data(Plugin::ORDER_ID_KEY, $mondu_order['uuid']); + $order->update_meta_data( Plugin::ORDER_ID_KEY, $mondu_order['uuid'] ); $order->save(); return $mondu_order; } @@ -45,14 +61,14 @@ public function create_order( WC_Order $order, $success_url ) { * @throws ResponseException */ public function get_order( $order_id ) { - $order = new WC_Order($order_id); - if ( !Plugin::order_has_mondu($order) ) { + $order = new WC_Order( $order_id ); + if ( !Plugin::order_has_mondu( $order ) ) { return; } - $mondu_order_id = $order->get_meta(Plugin::ORDER_ID_KEY); + $mondu_order_id = $order->get_meta( Plugin::ORDER_ID_KEY ); $response = $this->wrap_with_mondu_log_event( 'get_order', [ $mondu_order_id ] ); - return isset($response['order']) ? $response['order'] : null; + return isset( $response['order'] ) ? $response['order'] : null; } /** @@ -64,12 +80,12 @@ public function get_order( $order_id ) { * @throws ResponseException */ public function adjust_order( $order_id, $data_to_update ) { - $order = new WC_Order($order_id); - if ( !Plugin::order_has_mondu($order) ) { + $order = new WC_Order( $order_id ); + if ( !Plugin::order_has_mondu( $order ) ) { return; } - $mondu_order_id = $order->get_meta(Plugin::ORDER_ID_KEY); + $mondu_order_id = $order->get_meta( Plugin::ORDER_ID_KEY ); $response = $this->wrap_with_mondu_log_event( 'adjust_order', [ $mondu_order_id, $data_to_update ] ); return $response['order']; } @@ -82,12 +98,12 @@ public function adjust_order( $order_id, $data_to_update ) { * @throws ResponseException */ public function cancel_order( $order_id ) { - $order = new WC_Order($order_id); - if ( !Plugin::order_has_mondu($order) ) { + $order = new WC_Order( $order_id ); + if ( !Plugin::order_has_mondu( $order ) ) { return; } - $mondu_order_id = $order->get_meta(Plugin::ORDER_ID_KEY); + $mondu_order_id = $order->get_meta( Plugin::ORDER_ID_KEY ); $response = $this->wrap_with_mondu_log_event( 'cancel_order', [ $mondu_order_id ] ); return $response['order']; } @@ -100,16 +116,16 @@ public function cancel_order( $order_id ) { * @throws ResponseException */ public function ship_order( $order_id ) { - $order = new WC_Order($order_id); - if ( !Plugin::order_has_mondu($order) ) { + $order = new WC_Order( $order_id ); + if ( !Plugin::order_has_mondu( $order ) ) { return; } - $mondu_order_id = $order->get_meta(Plugin::ORDER_ID_KEY); - $invoice_data = OrderData::invoice_data_from_wc_order($order); + $mondu_order_id = $order->get_meta( Plugin::ORDER_ID_KEY ); + $invoice_data = OrderData::invoice_data_from_wc_order( $order ); $response = $this->wrap_with_mondu_log_event( 'ship_order', [ $mondu_order_id, $invoice_data ] ); $invoice = $response['invoice']; - $order->update_meta_data(Plugin::INVOICE_ID_KEY, $invoice['uuid']); + $order->update_meta_data( Plugin::INVOICE_ID_KEY, $invoice['uuid'] ); $order->save(); return $invoice; } @@ -122,12 +138,12 @@ public function ship_order( $order_id ) { * @throws ResponseException */ public function get_invoices( $order_id ) { - $order = new WC_Order($order_id); - if ( !Plugin::order_has_mondu($order) ) { + $order = new WC_Order( $order_id ); + if ( !Plugin::order_has_mondu( $order ) ) { return; } - $mondu_order_id = $order->get_meta(Plugin::ORDER_ID_KEY); + $mondu_order_id = $order->get_meta( Plugin::ORDER_ID_KEY ); $response = $this->wrap_with_mondu_log_event( 'get_invoices', [ $mondu_order_id ] ); return $response['invoices']; } @@ -140,13 +156,13 @@ public function get_invoices( $order_id ) { * @throws ResponseException */ public function get_invoice( $order_id ) { - $order = new WC_Order($order_id); - if ( !Plugin::order_has_mondu($order) ) { + $order = new WC_Order( $order_id ); + if ( !Plugin::order_has_mondu( $order ) ) { return; } - $mondu_order_id = $order->get_meta(Plugin::ORDER_ID_KEY); - $mondu_invoice_id = $order->get_meta(Plugin::INVOICE_ID_KEY); + $mondu_order_id = $order->get_meta( Plugin::ORDER_ID_KEY ); + $mondu_invoice_id = $order->get_meta( Plugin::INVOICE_ID_KEY ); $response = $this->wrap_with_mondu_log_event( 'get_invoice', [ $mondu_order_id, $mondu_invoice_id ] ); return $response['invoice']; } @@ -158,7 +174,7 @@ public function get_invoice( $order_id ) { * @return array */ public function get_merchant_payment_methods() { - $merchant_payment_methods = get_transient('mondu_merchant_payment_methods'); + $merchant_payment_methods = get_transient( 'mondu_merchant_payment_methods' ); if ( false === $merchant_payment_methods ) { try { $response = $this->wrap_with_mondu_log_event( 'get_payment_methods' ); @@ -168,30 +184,31 @@ public function get_merchant_payment_methods() { } # return only an array with the identifier (invoice, direct_debit, installment, etc) - $merchant_payment_methods = array_map(function( $payment_method ) { + $merchant_payment_methods = array_map( function( $payment_method ) { return $payment_method['identifier']; - }, $response['payment_methods']); - set_transient('mondu_merchant_payment_methods', $merchant_payment_methods, 1 * 60); + }, $response['payment_methods'] ); + set_transient( 'mondu_merchant_payment_methods', $merchant_payment_methods, 1 * 60 ); return $merchant_payment_methods; } catch ( \Exception $e ) { - $merchant_payment_methods = array_keys(Plugin::PAYMENT_METHODS); - set_transient('mondu_merchant_payment_methods', $merchant_payment_methods, 10 * 60); + $merchant_payment_methods = array_keys( Plugin::PAYMENT_METHODS ); + set_transient( 'mondu_merchant_payment_methods', $merchant_payment_methods, 10 * 60 ); return $merchant_payment_methods; } } return $merchant_payment_methods; } - /** - * Confirm Order - * - * @param $order_id - * @return void|WC_Order - * @throws ResponseException - */ + /** + * Confirm Order + * + * @param $order_id + * @param $mondu_order_id + * @return void|WC_Order + * @throws ResponseException + */ public function confirm_order( $order_id, $mondu_order_id ) { - $order = new WC_Order($order_id); - if ( !Plugin::order_has_mondu($order) ) { + $order = new WC_Order( $order_id ); + if ( !Plugin::order_has_mondu( $order ) ) { return; } @@ -207,13 +224,13 @@ public function confirm_order( $order_id, $mondu_order_id ) { * @throws ResponseException */ public function update_order_if_changed_some_fields( $order ) { - if ( !Plugin::order_has_mondu($order) ) { + if ( !Plugin::order_has_mondu( $order ) ) { return; } - if ( array_intersect([ 'total', 'discount_total', 'discount_tax', 'cart_tax', 'total_tax', 'shipping_tax', 'shipping_total' ], array_keys($order->get_changes())) ) { - $data_to_update = OrderData::order_data_from_wc_order_with_amount($order); - $this->adjust_order($order->get_id(), $data_to_update); + if ( array_intersect( [ 'total', 'discount_total', 'discount_tax', 'cart_tax', 'total_tax', 'shipping_tax', 'shipping_total' ], array_keys( $order->get_changes() ) ) ) { + $data_to_update = OrderData::order_data_from_wc_order_with_amount( $order ); + $this->adjust_order( $order->get_id(), $data_to_update ); } } @@ -227,8 +244,8 @@ public function update_order_if_changed_some_fields( $order ) { * @throws ResponseException */ public function order_status_changed( $order_id, $from_status, $to_status ) { - $order = new WC_Order($order_id); - if ( !Plugin::order_has_mondu($order) ) { + $order = new WC_Order( $order_id ); + if ( !Plugin::order_has_mondu( $order ) ) { return; } @@ -239,10 +256,10 @@ public function order_status_changed( $order_id, $from_status, $to_status ) { ]); if ( 'cancelled' === $to_status ) { - $this->cancel_order($order_id); + $this->cancel_order( $order_id ); } if ( 'completed' === $to_status ) { - $this->ship_order($order_id); + $this->ship_order( $order_id ); } } @@ -255,12 +272,12 @@ public function order_status_changed( $order_id, $from_status, $to_status ) { * @throws ResponseException */ public function order_refunded( $order_id, $refund_id ) { - $order = new WC_Order($order_id); - if ( !Plugin::order_has_mondu($order) ) { + $order = new WC_Order( $order_id ); + if ( !Plugin::order_has_mondu( $order ) ) { return; } - $mondu_invoice_id = $order->get_meta(Plugin::INVOICE_ID_KEY); + $mondu_invoice_id = $order->get_meta( Plugin::INVOICE_ID_KEY ); if ( !$mondu_invoice_id ) { Helper::log([ 'skipping_credit_note_creation' => [ @@ -271,8 +288,8 @@ public function order_refunded( $order_id, $refund_id ) { return; } - $refund = new WC_Order_Refund($refund_id); - $credit_note = OrderData::create_credit_note($refund); + $refund = new WC_Order_Refund( $refund_id ); + $credit_note = OrderData::create_credit_note( $refund ); $this->wrap_with_mondu_log_event( 'create_credit_note', [ $mondu_invoice_id, $credit_note ] ); } @@ -305,7 +322,7 @@ public function register_webhook( $topic ) { $response = $this->wrap_with_mondu_log_event( 'register_webhook', [ $params ]); - return isset($response['webhooks']) ? $response['webhooks'] : null; + return isset( $response['webhooks'] ) ? $response['webhooks'] : null; } /** @@ -347,13 +364,13 @@ public function log_plugin_event( \Exception $exception, $event, $body = null ) 'version' => MONDU_PLUGIN_VERSION, 'language_version' => 'PHP ' . phpversion(), 'shop_version' => $wp_version, - 'origin_event' => strtoupper($event), + 'origin_event' => strtoupper( $event ), 'response_body' => $body, 'response_status' => (string) $exception->getCode(), 'error_message' => $exception->getMessage(), 'error_trace' => $exception->getTraceAsString(), ]; - $this->api->log_plugin_event($params); + $this->api->log_plugin_event( $params ); } /** diff --git a/src/Mondu/Mondu/Presenters/PaymentInfo.php b/src/Mondu/Mondu/Presenters/PaymentInfo.php index f92ccdba..ea78276b 100644 --- a/src/Mondu/Mondu/Presenters/PaymentInfo.php +++ b/src/Mondu/Mondu/Presenters/PaymentInfo.php @@ -1,5 +1,9 @@ order = new WC_Order($order_id); $this->mondu_request_wrapper = new MonduRequestWrapper(); @@ -45,20 +59,35 @@ public function __construct( $order_id ) { $this->invoices_data = $this->get_invoices(); } + /** + * Get Order Data + * + * @return array + */ public function get_order_data() { return $this->order_data; } + /** + * Get Invoices Data + * + * @return array + */ public function get_invoices_data() { return $this->invoices_data; } + /** + * Get WCPDF Shop Name + * + * @return string + */ public function get_wcpdf_shop_name() { $wcpdf = \WPO_WCPDF::instance(); return $wcpdf->documents->documents['\WPO\WC\PDF_Invoices\Documents\Invoice']->get_shop_name() !== null ? $wcpdf->documents->documents['\WPO\WC\PDF_Invoices\Documents\Invoice']->get_shop_name() : - get_bloginfo('name'); + get_bloginfo( 'name' ); } /** @@ -77,30 +106,30 @@ public function get_mondu_section_html() { ?>

- : - + : +

- : - + : +

order_data['state'], [ 'confirmed', 'partially_shipped' ], true) ) { + if ( in_array( $this->order_data['state'], [ 'confirmed', 'partially_shipped' ], true ) ) { ?> $this->order->get_id(), - 'security' => wp_create_nonce('mondu-create-invoice'), + 'security' => wp_create_nonce( 'mondu-create-invoice' ), ]; ?> - order_data['state'], [ 'authorized' ], true) && + if ( in_array( $this->order_data['state'], [ 'authorized' ], true ) && $this->order->get_status() === 'on-hold' ) { ?> @@ -108,11 +137,11 @@ public function get_mondu_section_html() { $mondu_data = [ 'order_id' => $this->order->get_id(), 'order_uuid' => $order_data['uuid'], - 'security' => wp_create_nonce('mondu-confirm-order'), + 'security' => wp_create_nonce( 'mondu-confirm-order' ), ]; ?> -

- +

order->get_payment_method(), Plugin::PAYMENT_METHODS, true) ) { + if ( !in_array( $this->order->get_payment_method(), Plugin::PAYMENT_METHODS, true ) ) { return null; } - if ( !isset($this->order_data['bank_account']) ) { + if ( !isset( $this->order_data['bank_account'] ) ) { return null; } @@ -160,44 +189,44 @@ public function get_mondu_payment_html() {

- : - + : +

- : - + : +

- : - + : +

- : - + : +

- : - + : +

- : - + : +

- : - + : +

@@ -205,40 +234,50 @@ public function get_mondu_payment_html() { } } + /** + * Get Mondu Net Term + * + * @return int|null + */ public function get_mondu_net_term() { - if ( !in_array($this->order->get_payment_method(), Plugin::PAYMENT_METHODS, true) ) { + if ( !in_array( $this->order->get_payment_method(), Plugin::PAYMENT_METHODS, true ) ) { return null; } - if ( $this->order_data && isset($this->order_data['authorized_net_term']) ) { + if ( $this->order_data && isset( $this->order_data['authorized_net_term'] ) ) { return $this->order_data['authorized_net_term']; } return null; } + /** + * Get Mondu Invoices HTML + * + * @return void + */ public function get_mondu_invoices_html() { foreach ( $this->invoices_data as $invoice ) { ?>

- : - + : +

- : - + : +

- : - + : +

- : - + : +

get_mondu_credit_note_html($invoice); + $this->get_mondu_credit_note_html( $invoice ); if ( 'canceled' !== $invoice['state'] ) { ?> $this->order->get_id(), 'invoice_id' => $invoice['uuid'], 'mondu_order_id' => $this->order_data['uuid'], - 'security' => wp_create_nonce('mondu-cancel-invoice'), + 'security' => wp_create_nonce( 'mondu-cancel-invoice' ), ]; ?> - -

:

+

:

  • - #' . esc_html($note['external_reference_id']) . '', esc_html( $note['gross_amount_cents'] / 100 ), esc_html($invoice['order']['currency']), esc_html__('Tax', 'mondu'), esc_html( $note['tax_cents'] / 100 ), esc_html($invoice['order']['currency']), $note['notes'] ? '- ' . esc_html( $note['notes'] ) : '' ); ?> + #' . esc_html( $note['external_reference_id'] ) . '', esc_html( $note['gross_amount_cents'] / 100 ), esc_html( $invoice['order']['currency'] ), esc_html__( 'Tax', 'mondu' ), esc_html( $note['tax_cents'] / 100 ), esc_html( $invoice['order']['currency'] ), $note['notes'] ? '- ' . esc_html( $note['notes'] ) : '' ); ?>
  • order->get_payment_method(), Plugin::PAYMENT_METHODS, true) ) { + if ( !in_array( $this->order->get_payment_method(), Plugin::PAYMENT_METHODS, true ) ) { return null; } $file = MONDU_VIEW_PATH . '/pdf/mondu-invoice-section.php'; - if ( !file_exists($file) ) { + if ( !file_exists( $file ) ) { return null; } // These variables are used in the file that is included $wcpdf_shop_name = $this->get_wcpdf_shop_name(); - $payment_method = $this->order->get_payment_method(); - $bank_account = $this->order_data['bank_account']; - $invoice_number = Helper::get_invoice_number( $this->order ); - $net_terms = $this->get_mondu_net_term(); - $mondu_uk_buyer = $bank_account['account_number'] && $bank_account['sort_code']; + $payment_method = $this->order->get_payment_method(); + $bank_account = $this->order_data['bank_account']; + $invoice_number = Helper::get_invoice_number( $this->order ); + $net_terms = $this->get_mondu_net_term(); + $mondu_uk_buyer = $bank_account['account_number'] && $bank_account['sort_code']; include $file; } + /** + * Get Invoices + * + * @return array + */ private function get_invoices() { try { - return $this->mondu_request_wrapper->get_invoices($this->order->get_id()); + return $this->mondu_request_wrapper->get_invoices( $this->order->get_id() ); } catch ( ResponseException $e ) { return []; } } + /** + * Get Order + * + * @return array + */ private function get_order() { try { - return $this->mondu_request_wrapper->get_order($this->order->get_id()); + return $this->mondu_request_wrapper->get_order( $this->order->get_id() ); } catch ( ResponseException $e ) { return []; } diff --git a/src/Mondu/Mondu/Support/Helper.php b/src/Mondu/Mondu/Support/Helper.php index 27f924fb..8a09d989 100644 --- a/src/Mondu/Mondu/Support/Helper.php +++ b/src/Mondu/Mondu/Support/Helper.php @@ -1,5 +1,9 @@ $mondu_order_uuid ]); - if ( !empty($orders) ) { + if ( !empty( $orders ) ) { return $orders[0]; } @@ -305,6 +320,8 @@ private static function get_order_hpos($order_number, $mondu_order_uuid) { } /** + * Get order from order number + * * @param $order_number * @param $mondu_order_uuid * @return bool|WC_Order @@ -337,11 +354,18 @@ public static function is_production() { ) { return true; } + return false; } + /** + * Log + * + * @param array $message + * @param string $level + */ public static function log( array $message, $level = 'DEBUG' ) { $logger = wc_get_logger(); - $logger->log( $level, wc_print_r($message, true), [ 'source' => 'mondu' ] ); + $logger->log( $level, wc_print_r( $message, true ), [ 'source' => 'mondu' ] ); } } diff --git a/src/Mondu/Mondu/Support/OrderData.php b/src/Mondu/Mondu/Support/OrderData.php index 5b071712..61e06142 100644 --- a/src/Mondu/Mondu/Support/OrderData.php +++ b/src/Mondu/Mondu/Support/OrderData.php @@ -1,12 +1,20 @@ 'mondu_invoice', 'direct_debit' => 'mondu_direct_debit', 'installment' => 'mondu_installment', 'installment_by_invoice' => 'mondu_installment_by_invoice', ]; - const AVAILABLE_COUNTRIES = [ 'DE', 'AT', 'NL', 'FR', 'BE', 'GB' ]; - protected $global_settings; + /** + * Available Countries + */ + const AVAILABLE_COUNTRIES = [ 'DE', 'AT', 'NL', 'FR', 'BE', 'GB' ]; + + /** + * Global Settings + * + * @var mixed + */ + protected $global_settings; /** * Mondu Request Wrapper @@ -41,13 +76,16 @@ class Plugin { private $mondu_request_wrapper; public function __construct() { - $this->global_settings = get_option(self::OPTION_NAME); + $this->global_settings = get_option( self::OPTION_NAME ); $this->mondu_request_wrapper = new MonduRequestWrapper(); } + /** + * Initialize the plugin + */ public function init() { - if ( !class_exists('WooCommerce') ) { + if ( !class_exists( 'WooCommerce' ) ) { # This file is required to deactivate the plugin. # WordPress is not fully loaded when we are activating the plugin. include_once ABSPATH . '/wp-admin/includes/plugin.php'; @@ -72,19 +110,19 @@ public function init() { /* * Load translations */ - add_action('init', [ $this, 'load_textdomain' ]); + add_action( 'init', [ $this, 'load_textdomain' ] ); - add_filter('mondu_order_locale', [ $this, 'get_mondu_order_locale' ], 1); + add_filter( 'mondu_order_locale', [ $this, 'get_mondu_order_locale' ], 1 ); /* * Adds the mondu gateway to the list of gateways * (And remove it again if we're not in Germany) */ - add_filter('woocommerce_payment_gateways', [ GatewayInvoice::class, 'add' ]); - add_filter('woocommerce_payment_gateways', [ GatewayDirectDebit::class, 'add' ]); - add_filter('woocommerce_payment_gateways', [ GatewayInstallment::class, 'add' ]); - add_filter('woocommerce_payment_gateways', [ GatewayInstallmentByInvoice::class, 'add' ]); - add_filter('woocommerce_available_payment_gateways', [ $this, 'remove_gateway_if_country_unavailable' ]); + add_filter( 'woocommerce_payment_gateways', [ GatewayInvoice::class, 'add' ] ); + add_filter( 'woocommerce_payment_gateways', [ GatewayDirectDebit::class, 'add' ] ); + add_filter( 'woocommerce_payment_gateways', [ GatewayInstallment::class, 'add' ] ); + add_filter( 'woocommerce_payment_gateways', [ GatewayInstallmentByInvoice::class, 'add' ] ); + add_filter( 'woocommerce_available_payment_gateways', [ $this, 'remove_gateway_if_country_unavailable' ] ); /* * Show action links on the plugin screen. @@ -98,9 +136,9 @@ public function init() { /* * These deal with order and status changes */ - add_action('woocommerce_order_status_changed', [ $this->mondu_request_wrapper, 'order_status_changed' ], 10, 3); - add_action('woocommerce_before_order_object_save', [ $this->mondu_request_wrapper, 'update_order_if_changed_some_fields' ], 10, 1); - add_action('woocommerce_order_refunded', [ $this->mondu_request_wrapper, 'order_refunded' ], 10, 2); + add_action( 'woocommerce_order_status_changed', [ $this->mondu_request_wrapper, 'order_status_changed' ], 10, 3 ); + add_action( 'woocommerce_before_order_object_save', [ $this->mondu_request_wrapper, 'update_order_if_changed_some_fields' ] ); + add_action( 'woocommerce_order_refunded', [ $this->mondu_request_wrapper, 'order_refunded' ], 10, 2 ); add_action( 'woocommerce_blocks_loaded', function() { if ( class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) { add_action( @@ -122,24 +160,24 @@ function( PaymentMethodRegistry $payment_method_registry ) { /* * Validates required fields */ - add_action('woocommerce_after_checkout_validation', [ $this, 'validate_required_fields' ], 10, 2); + add_action( 'woocommerce_after_checkout_validation', [ $this, 'validate_required_fields' ], 10, 2 ); /* * Does not allow to change address */ - add_action('woocommerce_admin_order_data_after_billing_address', [ $this, 'change_address_warning' ], 10, 1); + add_action( 'woocommerce_admin_order_data_after_billing_address', [ $this, 'change_address_warning' ] ); /* * These methods add the Mondu invoice's info to a WCPDF Invoice */ if ( class_exists('WPO_WCPDF') ) { - add_action('wpo_wcpdf_after_order_details', [ $this, 'wcpdf_add_mondu_payment_info_to_pdf' ], 10, 2); - add_action('wpo_wcpdf_after_order_data', [ $this, 'wcpdf_add_status_to_invoice_when_order_is_canceled' ], 10, 2); - add_action('wpo_wcpdf_after_order_data', [ $this, 'wcpdf_add_paid_to_invoice_when_invoice_is_paid' ], 10, 2); - add_action('wpo_wcpdf_after_order_data', [ $this, 'wcpdf_add_status_to_invoice_when_invoice_is_canceled' ], 10, 2); - add_action('wpo_wcpdf_meta_box_after_document_data', [ $this, 'wcpdf_add_paid_to_invoice_admin_when_invoice_is_paid' ], 10, 2); - add_action('wpo_wcpdf_meta_box_after_document_data', [ $this, 'wcpdf_add_status_to_invoice_admin_when_invoice_is_canceled' ], 10, 2); - add_action('wpo_wcpdf_reload_text_domains', [ $this, 'wcpdf_add_mondu_payment_language_switch' ], 10, 1); + add_action( 'wpo_wcpdf_after_order_details', [ $this, 'wcpdf_add_mondu_payment_info_to_pdf' ], 10, 2 ); + add_action( 'wpo_wcpdf_after_order_data', [ $this, 'wcpdf_add_status_to_invoice_when_order_is_canceled' ], 10, 2 ); + add_action( 'wpo_wcpdf_after_order_data', [ $this, 'wcpdf_add_paid_to_invoice_when_invoice_is_paid' ], 10, 2 ); + add_action( 'wpo_wcpdf_after_order_data', [ $this, 'wcpdf_add_status_to_invoice_when_invoice_is_canceled' ], 10, 2 ); + add_action( 'wpo_wcpdf_meta_box_after_document_data', [ $this, 'wcpdf_add_paid_to_invoice_admin_when_invoice_is_paid' ], 10, 2 ); + add_action( 'wpo_wcpdf_meta_box_after_document_data', [ $this, 'wcpdf_add_status_to_invoice_admin_when_invoice_is_canceled' ], 10, 2 ); + add_action( 'wpo_wcpdf_reload_text_domains', [ $this, 'wcpdf_add_mondu_payment_language_switch' ] ); } if ( class_exists('BM') ) { @@ -147,11 +185,20 @@ function( PaymentMethodRegistry $payment_method_registry ) { } } + /** + * Load the plugin text domain for translation. + */ public function load_textdomain() { - $plugin_rel_path = dirname(plugin_basename(__FILE__)) . '/../../languages/'; - load_plugin_textdomain('mondu', false, $plugin_rel_path); + $plugin_rel_path = dirname( plugin_basename( __FILE__ ) ) . '/../../languages/'; + load_plugin_textdomain( 'mondu', false, $plugin_rel_path ); } + /** + * Check if the order has Mondu data + * + * @param WC_Order $order + * @return bool + */ public static function order_has_mondu( WC_Order $order ) { if ( !in_array($order->get_payment_method(), self::PAYMENT_METHODS, true) ) { return false; @@ -160,6 +207,12 @@ public static function order_has_mondu( WC_Order $order ) { return true; } + /** + * Change address warning + * + * @param WC_Order $order + * @return bool + */ public function change_address_warning( WC_Order $order ) { if ( !$this->order_has_mondu($order) ) { return; @@ -176,9 +229,15 @@ public function change_address_warning( WC_Order $order ) { jQuery('a.edit_address').remove(); }); "); - echo '

    ' . esc_html__('Since this order will be paid via Mondu you will not be able to change the addresses.', 'mondu') . '

    '; + echo '

    ' . esc_html__( 'Since this order will be paid via Mondu you will not be able to change the addresses.', 'mondu' ) . '

    '; } + /** + * Remove gateway if country unavailable + * + * @param array $available_gateways + * @return array + */ public function remove_gateway_if_country_unavailable( $available_gateways ) { if ( is_admin() || !is_checkout() ) { return $available_gateways; @@ -188,11 +247,11 @@ public function remove_gateway_if_country_unavailable( $available_gateways ) { foreach ( self::PAYMENT_METHODS as $payment_method => $woo_payment_method ) { $customer = $this->get_wc_customer(); - if ( !$this->is_country_available($customer->get_billing_country()) - || !in_array($payment_method, $mondu_payments, true) + if ( !$this->is_country_available( $customer->get_billing_country() ) + || !in_array( $payment_method, $mondu_payments, true ) ) { - if ( isset($available_gateways[ self::PAYMENT_METHODS[ $payment_method ] ]) ) { - unset($available_gateways[ self::PAYMENT_METHODS[ $payment_method ] ]); + if ( isset( $available_gateways[ self::PAYMENT_METHODS[ $payment_method ] ] ) ) { + unset( $available_gateways[ self::PAYMENT_METHODS[ $payment_method ] ] ); } } } @@ -209,10 +268,10 @@ public function remove_gateway_if_country_unavailable( $available_gateways ) { */ public static function add_action_links( $links ) { $action_links = [ - 'settings' => '' . esc_html__('Settings', 'woocommerce') . '', + 'settings' => '' . esc_html__( 'Settings', 'woocommerce' ) . '', ]; - return array_merge($action_links, $links); + return array_merge( $action_links, $links ); } /** @@ -229,12 +288,12 @@ public static function add_row_meta( $links, $file ) { } $row_meta = [ - 'docs' => '' . esc_html__('Docs', 'mondu') . '', - 'intro' => '' . esc_html__('Mondu introduction', 'mondu') . '', - 'faq' => '' . esc_html__('FAQ', 'mondu') . '', + 'docs' => '' . esc_html__( 'Docs', 'mondu' ) . '', + 'intro' => '' . esc_html__( 'Mondu introduction', 'mondu' ) . '', + 'faq' => '' . esc_html__( 'FAQ', 'mondu' ) . '', ]; - return array_merge($links, $row_meta); + return array_merge( $links, $row_meta ); } /** @@ -272,8 +331,8 @@ public function wcpdf_mondu_template_type( $template_type ) { * * @since 1.3.2 */ - $allowed_templates = apply_filters('mondu_wcpdf_template_type', [ 'invoice' ]); - if ( in_array($template_type, $allowed_templates, true) ) { + $allowed_templates = apply_filters( 'mondu_wcpdf_template_type', [ 'invoice' ] ); + if ( in_array( $template_type, $allowed_templates, true ) ) { return true; } @@ -288,12 +347,12 @@ public function wcpdf_mondu_template_type( $template_type ) { * @throws Exception */ public function wcpdf_add_mondu_payment_info_to_pdf( $template_type, $order ) { - if ( !$this->wcpdf_mondu_template_type($template_type) || !$this->order_has_mondu($order) ) { + if ( !$this->wcpdf_mondu_template_type( $template_type ) || !$this->order_has_mondu( $order ) ) { return; } - $payment_info = new PaymentInfo($order->get_id()); - echo esc_html($payment_info->get_mondu_wcpdf_section_html(true)); + $payment_info = new PaymentInfo( $order->get_id() ); + echo esc_html( $payment_info->get_mondu_wcpdf_section_html() ); } /** @@ -304,18 +363,18 @@ public function wcpdf_add_mondu_payment_info_to_pdf( $template_type, $order ) { * @throws Exception */ public function wcpdf_add_status_to_invoice_when_order_is_canceled( $template_type, $order ) { - if ( !$this->wcpdf_mondu_template_type($template_type) || !$this->order_has_mondu($order) ) { + if ( !$this->wcpdf_mondu_template_type( $template_type ) || !$this->order_has_mondu( $order ) ) { return; } - $payment_info = new PaymentInfo($order->get_id()); + $payment_info = new PaymentInfo( $order->get_id() ); $order_data = $payment_info->get_order_data(); if ( 'cancelled' === $order->get_status() || 'canceled' === $order_data['state'] ) { ?> - : - + : + wcpdf_mondu_template_type($template_type) || !$this->order_has_mondu($order) ) { + if ( !$this->wcpdf_mondu_template_type( $template_type ) || !$this->order_has_mondu( $order ) ) { return; } - $payment_info = new PaymentInfo($order->get_id()); + $payment_info = new PaymentInfo( $order->get_id() ); $invoice_data = $payment_info->get_invoices_data(); if ( $invoice_data && $invoice_data[0]['paid_out'] ) { ?> - : - + : + wcpdf_mondu_template_type($template_type) || !$this->order_has_mondu($order) ) { + if ( !$this->wcpdf_mondu_template_type( $template_type ) || !$this->order_has_mondu( $order ) ) { return; } - $payment_info = new PaymentInfo($order->get_id()); + $payment_info = new PaymentInfo( $order->get_id() ); $invoice_data = $payment_info->get_invoices_data(); if ( $invoice_data && 'canceled' === $invoice_data[0]['state'] ) { ?> - : - + : + get_type() !== 'invoice' || !$this->order_has_mondu($order) ) { + if ( $document->get_type() !== 'invoice' || !$this->order_has_mondu( $order ) ) { return; } - $payment_info = new PaymentInfo($order->get_id()); + $payment_info = new PaymentInfo( $order->get_id() ); $invoice_data = $payment_info->get_invoices_data(); if ( $invoice_data && $invoice_data[0]['paid_out'] ) { ?>

    - : - + : +

    get_type() !== 'invoice' || !$this->order_has_mondu($order) ) { + if ( $document->get_type() !== 'invoice' || !$this->order_has_mondu( $order ) ) { return; } - $payment_info = new PaymentInfo($order->get_id()); + $payment_info = new PaymentInfo( $order->get_id() ); $invoice_data = $payment_info->get_invoices_data(); if ( $invoice_data && 'canceled' === $invoice_data[0]['state'] ) { ?>

    - : - + : +

    load_textdomain(); } + /** + * WooCommerce notice + */ public function woocommerce_notice() { $class = 'notice notice-error'; - $message = __('Mondu requires WooCommerce to be activated.', 'mondu'); + $message = __( 'Mondu requires WooCommerce to be activated.', 'mondu' ); - printf('

    %2$s

    ', esc_attr($class), esc_html($message)); + printf( '

    %2$s

    ', esc_attr( $class ), esc_html( $message ) ); } + /** + * Check if the country is available + * + * @param $country + * @return bool + */ private function is_country_available( $country ) { - return in_array($country, self::AVAILABLE_COUNTRIES, true); + return in_array( $country, self::AVAILABLE_COUNTRIES, true ); } + /** + * Get the WC Customer object + * + * @return WC_Customer + * @throws Exception + */ private function get_wc_customer() { - return isset(WC()->customer) ? WC()->customer : new WC_Customer(get_current_user_id()); + return isset( WC()->customer ) ? WC()->customer : new WC_Customer( get_current_user_id() ); } + /** + * Get the Mondu order locale + * + * @return mixed + */ public function get_mondu_order_locale() { /** * WPML current language * * @since 1.3.2 */ - return apply_filters('wpml_current_language', get_locale()); + return apply_filters( 'wpml_current_language', get_locale() ); } } From c2837c654564c2e1b718c51da88c409bb9f7b65d Mon Sep 17 00:00:00 2001 From: Tigran Date: Sun, 26 May 2024 20:22:05 +0400 Subject: [PATCH 2/6] [PT-509] changed pbs rules set and reformatted whole repository for *.php files. --- mondu-buy-now-pay-later.php | 2 +- pbs-rules-set.xml | 6 +- src/Mondu/Admin/Option/Account.php | 64 +++--- src/Mondu/Admin/Option/Helper.php | 52 ++--- src/Mondu/Admin/Order.php | 158 ++++++++------- src/Mondu/Admin/Settings.php | 122 ++++++------ src/Mondu/Exceptions/ResponseException.php | 26 +-- src/Mondu/Mondu/Api.php | 22 +- src/Mondu/Mondu/Blocks/MonduBlocksSupport.php | 49 +++-- .../Mondu/Controllers/OrdersController.php | 38 ++-- .../Mondu/Controllers/WebhooksController.php | 188 +++++++++--------- src/Mondu/Mondu/Models/SignatureVerifier.php | 60 +++--- src/Mondu/Mondu/MonduGateway.php | 32 +-- src/Mondu/Mondu/MonduRequestWrapper.php | 36 ++-- src/Mondu/Mondu/Presenters/PaymentInfo.php | 132 ++++++------ src/Mondu/Mondu/Support/Helper.php | 92 ++++----- src/Mondu/Mondu/Support/OrderData.php | 8 +- src/Mondu/Plugin.php | 168 ++++++++-------- uninstall.php | 2 +- 19 files changed, 632 insertions(+), 625 deletions(-) diff --git a/mondu-buy-now-pay-later.php b/mondu-buy-now-pay-later.php index 30ae471b..6d8ee69f 100644 --- a/mondu-buy-now-pay-later.php +++ b/mondu-buy-now-pay-later.php @@ -63,7 +63,7 @@ function mondu_deactivate() { register_deactivation_hook( MONDU_PLUGIN_FILE, 'mondu_deactivate' ); // Here because this needs to happen before plugins_loaded hook -add_action('before_woocommerce_init', function() { +add_action('before_woocommerce_init', function () { if ( class_exists( FeaturesUtil::class ) ) { FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__ ); FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__ ); diff --git a/pbs-rules-set.xml b/pbs-rules-set.xml index 237dbe72..60f441fd 100644 --- a/pbs-rules-set.xml +++ b/pbs-rules-set.xml @@ -25,12 +25,14 @@ + + + - @@ -46,6 +48,8 @@ + + tests/ diff --git a/src/Mondu/Admin/Option/Account.php b/src/Mondu/Admin/Option/Account.php index ce0bcaeb..f8b59dbe 100644 --- a/src/Mondu/Admin/Option/Account.php +++ b/src/Mondu/Admin/Option/Account.php @@ -9,7 +9,7 @@ use Mondu\Plugin; -if ( ! defined( 'ABSPATH' ) ) { +if ( !defined( 'ABSPATH' ) ) { die( 'Direct access not allowed' ); } @@ -17,10 +17,10 @@ * Class Account */ class Account extends Helper { - /** - * Register the settings - */ - public function register() { + /** + * Register the settings + */ + public function register() { register_setting('mondu', Plugin::OPTION_NAME); /** @@ -44,7 +44,7 @@ public function register() { ] ); add_settings_field( - 'api_token', + 'api_token', __( 'API Token', 'mondu' ), [ $this, 'field_api_token' ], 'mondu-settings-account', @@ -55,7 +55,7 @@ public function register() { ] ); add_settings_field( - 'send_line_items', + 'send_line_items', __( 'Send line items', 'mondu' ), [ $this, 'field_send_line_items' ], 'mondu-settings-account', @@ -67,33 +67,33 @@ public function register() { ); } - /** - * Field for sandbox or production - * - * @param array $args arguments. - */ - public function field_sandbox_or_production( $args = [] ) { + /** + * Field for sandbox or production + * + * @param array $args arguments. + */ + public function field_sandbox_or_production( $args = [] ) { $this->selectField( Plugin::OPTION_NAME, 'sandbox_or_production', [ 'sandbox' => __( 'Sandbox', 'mondu' ), 'production' => __( 'Production', 'mondu' ), ], $args['tip'] ); } - /** - * Field for API token - * - * @param array $args arguments. - */ - public function field_api_token( $args = [] ) { + /** + * Field for API token + * + * @param array $args arguments. + */ + public function field_api_token( $args = [] ) { $this->textField( Plugin::OPTION_NAME, 'api_token', $args['tip'] ); } - /** - * Field for send line items - * - * @param array $args arguments. - */ - public function field_send_line_items( $args = [] ) { + /** + * Field for send line items + * + * @param array $args arguments. + */ + public function field_send_line_items( $args = [] ) { $this->selectField( Plugin::OPTION_NAME, 'send_line_items', [ 'yes' => __( 'Yes', 'mondu' ), 'order' => __( 'Send line items only for orders', 'mondu' ), @@ -101,13 +101,13 @@ public function field_send_line_items( $args = [] ) { ], $args['tip'] ); } - /** - * Render the account options - * - * @param mixed $validation_error validation error. - * @param mixed $webhooks_error webhooks error. - */ - public function render( $validation_error = null, $webhooks_error = null ) { + /** + * Render the account options + * + * @param mixed $validation_error validation error. + * @param mixed $webhooks_error webhooks error. + */ + public function render( $validation_error = null, $webhooks_error = null ) { if ( !current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.' ) ); } diff --git a/src/Mondu/Admin/Option/Helper.php b/src/Mondu/Admin/Option/Helper.php index 6ad5e84b..31dea3c7 100644 --- a/src/Mondu/Admin/Option/Helper.php +++ b/src/Mondu/Admin/Option/Helper.php @@ -18,28 +18,28 @@ * @package Mondu */ abstract class Helper { - /** - * The global settings. - * - * @var false|mixed|null - */ - protected $global_settings; + /** + * The global settings. + * + * @var false|mixed|null + */ + protected $global_settings; - /** - * Helper constructor. - */ + /** + * Helper constructor. + */ public function __construct() { $this->global_settings = get_option( Plugin::OPTION_NAME ); } - /** - * Text field rendering. - * - * @param $option_name - * @param $field_name - * @param $tip - * @return void - */ + /** + * Text field rendering. + * + * @param $option_name + * @param $field_name + * @param $tip + * @return void + */ protected function textField( $option_name, $field_name, $tip = '' ) { $field_id = $field_name; $field_value = isset( $this->global_settings[ $field_name ] ) ? $this->global_settings[ $field_name ] : ''; @@ -51,15 +51,15 @@ protected function textField( $option_name, $field_name, $tip = '' ) { global_settings[ $field_name ] ) ? $this->global_settings[ $field_name ] : ''; diff --git a/src/Mondu/Admin/Order.php b/src/Mondu/Admin/Order.php index d749be3c..6791f7e2 100644 --- a/src/Mondu/Admin/Order.php +++ b/src/Mondu/Admin/Order.php @@ -31,11 +31,11 @@ class Order { */ private $mondu_request_wrapper; - /** - * Order constructor. - * - * @return void - */ + /** + * Order constructor. + * + * @return void + */ public function init() { add_action('add_meta_boxes', [ $this, 'add_payment_info_box' ]); add_action('admin_footer', [ $this, 'invoice_buttons_js' ]); @@ -43,30 +43,34 @@ public function init() { add_action('wp_ajax_cancel_invoice', [ $this, 'cancel_invoice' ]); add_action('wp_ajax_create_invoice', [ $this, 'create_invoice' ]); - add_action('wp_ajax_confirm_order', [ $this, 'confirm_order' ]); + add_action('wp_ajax_confirm_order', [ $this, 'confirm_order' ]); $this->mondu_request_wrapper = new MonduRequestWrapper(); } - /** - * Add payment info box. - * - * @return void - * @throws \Exception - */ + /** + * Add payment info box. + * + * @return void + * @throws \Exception + */ public function add_payment_info_box() { $screen = class_exists( '\Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController' ) && wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order'; add_meta_box( - 'mondu_payment_info', + 'mondu_payment_info', __( 'Mondu Order Information', 'mondu' ), function ( $post_or_order_object ) { $order = ( $post_or_order_object instanceof \WP_Post ) ? wc_get_order( $post_or_order_object->ID ) : $post_or_order_object; - if ( null === $order ) return; - if ( !in_array( $order->get_payment_method(), Plugin::PAYMENT_METHODS, true ) ) return; + if ( null === $order ) { +return; + } + if ( !in_array( $order->get_payment_method(), Plugin::PAYMENT_METHODS, true ) ) { +return; + } $this->render_meta_box_content( $order ); }, @@ -75,33 +79,33 @@ function ( $post_or_order_object ) { ); } - /** - * Invoice buttons js. - * - * @return void - */ + /** + * Invoice buttons js. + * + * @return void + */ public function invoice_buttons_js() { require_once MONDU_VIEW_PATH . '/admin/js/invoice.php'; } - /** - * Render meta box content. - * - * @param WC_Order $order Order object. - * - * @return void - * @throws \Exception - */ + /** + * Render meta box content. + * + * @param WC_Order $order Order object. + * + * @return void + * @throws \Exception + */ public function render_meta_box_content( $order ) { $payment_info = new PaymentInfo( $order->get_id() ); $payment_info->get_mondu_section_html(); } - /** - * Cancel invoice. - * - * @return void - */ + /** + * Cancel invoice. + * + * @return void + */ public function cancel_invoice() { $is_nonce_valid = check_ajax_referer( 'mondu-cancel-invoice', 'security', false ); if ( !$is_nonce_valid ) { @@ -130,11 +134,11 @@ public function cancel_invoice() { } } - /** - * Create invoice. - * - * @return void - */ + /** + * Create invoice. + * + * @return void + */ public function create_invoice() { $is_nonce_valid = check_ajax_referer( 'mondu-create-invoice', 'security', false ); if ( !$is_nonce_valid ) { @@ -164,43 +168,43 @@ public function create_invoice() { } } - /** - * Confirm order. - * - * @return void - */ - public function confirm_order() { - $is_nonce_valid = check_ajax_referer( 'mondu-confirm-order', 'security', false ); - if ( !$is_nonce_valid ) { - status_header( 400 ); - exit(esc_html__( 'Bad Request.', 'mondu' ) ); - } - - $order_id = isset( $_POST['order_id'] ) ? sanitize_text_field( $_POST['order_id'] ) : ''; - - $order = new WC_Order( $order_id ); - if ( null === $order ) { - return; - } - - $mondu_order_id = isset( $_POST['order_uuid'] ) ? sanitize_text_field( $_POST['order_uuid'] ) : ''; - - if ( !$mondu_order_id ) { - return; - } - - try { - $this->mondu_request_wrapper->confirm_order( $order_id, $mondu_order_id ); - } catch ( ResponseException $e ) { - wp_send_json([ - 'error' => true, - 'message' => $e->getMessage(), - ]); - } catch ( MonduException $e ) { - wp_send_json([ - 'error' => true, - 'message' => $e->getMessage(), - ]); - } - } + /** + * Confirm order. + * + * @return void + */ + public function confirm_order() { + $is_nonce_valid = check_ajax_referer( 'mondu-confirm-order', 'security', false ); + if ( !$is_nonce_valid ) { + status_header( 400 ); + exit(esc_html__( 'Bad Request.', 'mondu' ) ); + } + + $order_id = isset( $_POST['order_id'] ) ? sanitize_text_field( $_POST['order_id'] ) : ''; + + $order = new WC_Order( $order_id ); + if ( null === $order ) { + return; + } + + $mondu_order_id = isset( $_POST['order_uuid'] ) ? sanitize_text_field( $_POST['order_uuid'] ) : ''; + + if ( !$mondu_order_id ) { + return; + } + + try { + $this->mondu_request_wrapper->confirm_order( $order_id, $mondu_order_id ); + } catch ( ResponseException $e ) { + wp_send_json([ + 'error' => true, + 'message' => $e->getMessage(), + ]); + } catch ( MonduException $e ) { + wp_send_json([ + 'error' => true, + 'message' => $e->getMessage(), + ]); + } + } } diff --git a/src/Mondu/Admin/Settings.php b/src/Mondu/Admin/Settings.php index 54b8531d..19c047a0 100644 --- a/src/Mondu/Admin/Settings.php +++ b/src/Mondu/Admin/Settings.php @@ -23,11 +23,11 @@ * @package Mondu */ class Settings { - /** - * Global settings. - * - * @var false|mixed|null - */ + /** + * Global settings. + * + * @var false|mixed|null + */ private $global_settings; /** @@ -44,20 +44,20 @@ class Settings { */ private $account_options; - /** - * Settings constructor. - */ + /** + * Settings constructor. + */ public function __construct() { $this->global_settings = get_option( Plugin::OPTION_NAME ); $this->mondu_request_wrapper = new MonduRequestWrapper(); } - /** - * Initialize. - * - * @return void - */ + /** + * Initialize. + * + * @return void + */ public function init() { add_action( 'admin_menu', [ $this, 'plugin_menu' ] ); add_action( 'admin_init', [ $this, 'register_options' ] ); @@ -65,11 +65,11 @@ public function init() { add_filter( 'woocommerce_screen_ids', [ $this, 'set_wc_screen_ids' ] ); } - /** - * Plugin menu. - * - * @return void - */ + /** + * Plugin menu. + * + * @return void + */ public function plugin_menu() { $mondu_icon = 'data:image/svg+xml;base64,' . base64_encode( file_get_contents( 'https://checkout.mondu.ai/logo.svg' ) ); @@ -84,32 +84,32 @@ public function plugin_menu() { ); } - /** - * Register options. - * - * @return void - */ + /** + * Register options. + * + * @return void + */ public function register_options() { $this->account_options = new Account(); $this->account_options->register(); } - /** - * Set WC screen ids. - * - * @param $screen - * @return mixed - */ + /** + * Set WC screen ids. + * + * @param $screen + * @return mixed + */ public function set_wc_screen_ids( $screen ) { $screen[] = 'toplevel_page_mondu-settings-account'; return $screen; } - /** - * Render account options. - * - * @return void - */ + /** + * Render account options. + * + * @return void + */ public function render_account_options() { $validation_error = null; $webhooks_error = null; @@ -145,29 +145,29 @@ public function render_account_options() { $this->account_options->render( $validation_error, $webhooks_error ); } - /** - * Return missing credentials. - * - * @return bool - */ + /** + * Return missing credentials. + * + * @return bool + */ private function missing_credentials() { return ( - !isset( $this->global_settings ) || - !is_array( $this->global_settings ) || - !isset( $this->global_settings['api_token'] ) || - '' === $this->global_settings['api_token'] + !isset( $this->global_settings ) || + !is_array( $this->global_settings ) || + !isset( $this->global_settings['api_token'] ) || + '' === $this->global_settings['api_token'] ); } - /** - * Register webhooks if not registered. - * - * @return void - * @throws ResponseException - */ + /** + * Register webhooks if not registered. + * + * @return void + * @throws ResponseException + */ private function register_webhooks_if_not_registered() { $webhooks = $this->mondu_request_wrapper->get_webhooks(); - $registered_topics = array_map( function( $webhook ) { + $registered_topics = array_map( function ( $webhook ) { return $webhook['topic']; }, $webhooks ); @@ -179,11 +179,11 @@ private function register_webhooks_if_not_registered() { } } - /** - * Download Mondu logs. - * - * @return void - */ + /** + * Download Mondu logs. + * + * @return void + */ public function download_mondu_logs() { $is_nonce_valid = check_ajax_referer( 'mondu-download-logs', 'security', false ); if ( !$is_nonce_valid ) { @@ -214,12 +214,12 @@ public function download_mondu_logs() { die(); } - /** - * Get file. - * - * @param $date - * @return false|string - */ + /** + * Get file. + * + * @param $date + * @return false|string + */ private function get_file( $date ) { $base_dir = WP_CONTENT_DIR . '/uploads/wc-logs/'; $dir = opendir( $base_dir ); diff --git a/src/Mondu/Exceptions/ResponseException.php b/src/Mondu/Exceptions/ResponseException.php index 20719a67..6a552302 100644 --- a/src/Mondu/Exceptions/ResponseException.php +++ b/src/Mondu/Exceptions/ResponseException.php @@ -13,25 +13,25 @@ */ class ResponseException extends MonduException { /** @var null $body */ - private $body = null; + private $body = null; - /** - * ResponseException constructor. - * - * @param string $message - * @param int $code - * @param null $body - */ + /** + * ResponseException constructor. + * + * @param string $message + * @param int $code + * @param null $body + */ public function __construct( $message = '', $code = 0, $body = null ) { $this->body = $body; parent::__construct( $message, $code ); } - /** - * Get body - * - * @return null - */ + /** + * Get body + * + * @return null + */ public function getBody() { return $this->body; } diff --git a/src/Mondu/Mondu/Api.php b/src/Mondu/Mondu/Api.php index 57a30ae1..66ff2812 100644 --- a/src/Mondu/Mondu/Api.php +++ b/src/Mondu/Mondu/Api.php @@ -17,23 +17,23 @@ * @package Mondu */ class Api { - /** - * Global settings - * - * @var array - */ + /** + * Global settings + * + * @var array + */ private $global_settings; - /** - * Api constructor. - */ + /** + * Api constructor. + */ public function __construct() { $this->global_settings = get_option(Plugin::OPTION_NAME); } - /** - * Register - */ + /** + * Register + */ public function register() { register_setting('mondu', Plugin::OPTION_NAME); } diff --git a/src/Mondu/Mondu/Blocks/MonduBlocksSupport.php b/src/Mondu/Mondu/Blocks/MonduBlocksSupport.php index d31f89d6..b387a088 100644 --- a/src/Mondu/Mondu/Blocks/MonduBlocksSupport.php +++ b/src/Mondu/Mondu/Blocks/MonduBlocksSupport.php @@ -32,24 +32,23 @@ final class MonduBlocksSupport extends AbstractPaymentMethodType { */ protected $gateways; - /** - * Initialize - */ - public function initialize() - { + /** + * Initialize + */ + public function initialize() { $this->gateways = [ new GatewayInvoice( false ), new GatewayDirectDebit( false ), new GatewayInstallment( false ), - new GatewayInstallmentByInvoice( false ) + new GatewayInstallmentByInvoice( false ), ]; } - /** - * Get payment method script handles - * - * @return array - */ + /** + * Get payment method script handles + * + * @return array + */ public function get_payment_method_script_handles() { wp_register_script( 'mondu-blocks-integration', @@ -65,30 +64,30 @@ public function get_payment_method_script_handles() { true ); - wp_set_script_translations( 'mondu-blocks-integration', 'mondu', MONDU_PLUGIN_PATH. '/languages' ); + wp_set_script_translations( 'mondu-blocks-integration', 'mondu', MONDU_PLUGIN_PATH . '/languages' ); return [ 'mondu-blocks-integration' ]; } - /** - * Get payment method data - * - * @return array - */ + /** + * Get payment method data + * + * @return array + */ public function get_payment_method_data() { - $gateways = array_reduce($this->gateways, function( $carry, $item ) { - $carry[$item->id] = [ - 'title' => $item->get_title(), + $gateways = array_reduce($this->gateways, function ( $carry, $item ) { + $carry[ $item->id ] = [ + 'title' => $item->get_title(), 'description' => $item->description, - 'supports' => $item->supports, - 'enabled' => $item->enabled + 'supports' => $item->supports, + 'enabled' => $item->enabled, ]; return $carry; }, []); return [ - 'gateways' => $gateways, - 'available_countries' => Plugin::AVAILABLE_COUNTRIES + 'gateways' => $gateways, + 'available_countries' => Plugin::AVAILABLE_COUNTRIES, ]; } -} \ No newline at end of file +} diff --git a/src/Mondu/Mondu/Controllers/OrdersController.php b/src/Mondu/Mondu/Controllers/OrdersController.php index 9b0c5ac5..17c60c05 100644 --- a/src/Mondu/Mondu/Controllers/OrdersController.php +++ b/src/Mondu/Mondu/Controllers/OrdersController.php @@ -24,17 +24,17 @@ class OrdersController extends WP_REST_Controller { */ private $mondu_request_wrapper; - /** - * Constructor - */ + /** + * Constructor + */ public function __construct() { $this->namespace = 'mondu/v1/orders'; $this->mondu_request_wrapper = new MonduRequestWrapper(); } /** - * Register routes - */ + * Register routes + */ public function register_routes() { register_rest_route( $this->namespace, '/confirm', [ [ @@ -52,11 +52,11 @@ public function register_routes() { ] ); } - /** - * Confirm order - * - * @param WP_REST_Request $request Request - */ + /** + * Confirm order + * + * @param WP_REST_Request $request Request + */ public function confirm( WP_REST_Request $request ) { $params = $request->get_params(); $order_number = $params['external_reference_id']; @@ -88,16 +88,16 @@ public function confirm( WP_REST_Request $request ) { exit; } - /** - * Decline order - * - * @param WP_REST_Request $request Request - */ + /** + * Decline order + * + * @param WP_REST_Request $request Request + */ public function decline( WP_REST_Request $request ) { - $params = $request->get_params(); - $order_number = $params['external_reference_id']; - $return_url = urldecode( $params['return_url'] ); - $order = Helper::get_order_from_order_number( $order_number ); + $params = $request->get_params(); + $order_number = $params['external_reference_id']; + $return_url = urldecode( $params['return_url'] ); + $order = Helper::get_order_from_order_number( $order_number ); $order->add_order_note( esc_html( sprintf( __( 'Order was declined by Mondu.', 'mondu' ) ) ), false ); diff --git a/src/Mondu/Mondu/Controllers/WebhooksController.php b/src/Mondu/Mondu/Controllers/WebhooksController.php index 535bbf81..d826c8ee 100644 --- a/src/Mondu/Mondu/Controllers/WebhooksController.php +++ b/src/Mondu/Mondu/Controllers/WebhooksController.php @@ -20,22 +20,22 @@ * @package Mondu */ class WebhooksController extends WP_REST_Controller { - /** - * @var MonduRequestWrapper $mondu_request_wrapper - */ + /** + * @var MonduRequestWrapper $mondu_request_wrapper + */ private $mondu_request_wrapper; - /** - * WebhooksController constructor. - */ + /** + * WebhooksController constructor. + */ public function __construct() { $this->namespace = 'mondu/v1/webhooks'; $this->mondu_request_wrapper = new MonduRequestWrapper(); } - /** - * Register routes - */ + /** + * Register routes + */ public function register_routes() { register_rest_route( $this->namespace, '/index', [ [ @@ -46,21 +46,21 @@ public function register_routes() { ] ); } - /** - * Webhooks index - * - * @param WP_REST_Request $request - * - * @return WP_REST_Response - */ + /** + * Webhooks index + * + * @param WP_REST_Request $request + * + * @return WP_REST_Response + */ public function index( WP_REST_Request $request ) { - $verifier = new SignatureVerifier(); - $params = $request->get_json_params(); - $topic = isset( $params['topic'] ) ? $params['topic'] : null; + $verifier = new SignatureVerifier(); + $params = $request->get_json_params(); + $topic = isset( $params['topic'] ) ? $params['topic'] : null; - $body = $request->get_body(); + $body = $request->get_body(); $signature_payload = $request->get_header( 'X-MONDU-SIGNATURE' ); - $signature = $verifier->create_hmac( $body ); + $signature = $verifier->create_hmac( $body ); Helper::log( [ 'webhook_topic' => $topic, @@ -114,14 +114,14 @@ public function index( WP_REST_Request $request ) { return new WP_REST_Response( $res_body, $res_status ); } - /** - * Handle pending - * - * @param array $params - * - * @return array - * @throws MonduException - */ + /** + * Handle pending + * + * @param array $params + * + * @return array + * @throws MonduException + */ private function handle_pending( $params ) { $woocommerce_order_number = $params['external_reference_id']; $mondu_order_id = $params['order_uuid']; @@ -141,14 +141,14 @@ private function handle_pending( $params ) { return $this->return_success(); } - /** - * Handle authorized - * - * @param array $params - * - * @return array - * @throws MonduException - */ + /** + * Handle authorized + * + * @param array $params + * + * @return array + * @throws MonduException + */ private function handle_authorized( $params ) { $woocommerce_order_number = $params['external_reference_id']; $mondu_order_id = $params['order_uuid']; @@ -168,14 +168,14 @@ private function handle_authorized( $params ) { return $this->return_success(); } - /** - * Handle confirmed - * - * @param array $params - * - * @return array - * @throws MonduException - */ + /** + * Handle confirmed + * + * @param array $params + * + * @return array + * @throws MonduException + */ private function handle_confirmed( $params ) { $woocommerce_order_number = $params['external_reference_id']; $mondu_order_id = $params['order_uuid']; @@ -199,14 +199,14 @@ private function handle_confirmed( $params ) { return $this->return_success(); } - /** - * Handle declined - * - * @param array $params - * - * @return array - * @throws MonduException - */ + /** + * Handle declined + * + * @param array $params + * + * @return array + * @throws MonduException + */ private function handle_declined( $params ) { $woocommerce_order_number = $params['external_reference_id']; $mondu_order_id = $params['order_uuid']; @@ -230,14 +230,14 @@ private function handle_declined( $params ) { return $this->return_success(); } - /** - * Handle invoice created - * - * @param array $params - * - * @return array - * @throws MonduException - */ + /** + * Handle invoice created + * + * @param array $params + * + * @return array + * @throws MonduException + */ private function handle_invoice_created( $params ) { $woocommerce_order_number = $params['external_reference_id']; $mondu_order_id = $params['order_uuid']; @@ -257,14 +257,14 @@ private function handle_invoice_created( $params ) { return $this->return_success(); } - /** - * Handle invoice payment - * - * @param array $params - * - * @return array - * @throws MonduException - */ + /** + * Handle invoice payment + * + * @param array $params + * + * @return array + * @throws MonduException + */ private function handle_invoice_payment( $params ) { $woocommerce_order_number = $params['external_reference_id']; $mondu_order_id = $params['order_uuid']; @@ -284,14 +284,14 @@ private function handle_invoice_payment( $params ) { return $this->return_success(); } - /** - * Handle invoice canceled - * - * @param array $params - * - * @return array - * @throws MonduException - */ + /** + * Handle invoice canceled + * + * @param array $params + * + * @return array + * @throws MonduException + */ private function handle_invoice_canceled( $params ) { $woocommerce_order_number = $params['external_reference_id']; $mondu_order_id = $params['order_uuid']; @@ -311,13 +311,13 @@ private function handle_invoice_canceled( $params ) { return $this->return_success(); } - /** - * Handle not found topic - * - * @param array $params - * - * @return array - */ + /** + * Handle not found topic + * + * @param array $params + * + * @return array + */ private function handle_not_found_topic( $params ) { Helper::log( [ 'not_found_topic' => $params, @@ -326,20 +326,20 @@ private function handle_not_found_topic( $params ) { return $this->return_success(); } - /** - * Return success - * - * @return array - */ + /** + * Return success + * + * @return array + */ private function return_success() { return [ [ 'message' => 'Ok' ], 200 ]; } - /** - * Return not found - * - * @return array - */ + /** + * Return not found + * + * @return array + */ private function return_not_found() { return [ [ 'message' => __('Not Found', 'mondu') ], 404 ]; } diff --git a/src/Mondu/Mondu/Models/SignatureVerifier.php b/src/Mondu/Mondu/Models/SignatureVerifier.php index 5732674f..2970a38f 100644 --- a/src/Mondu/Mondu/Models/SignatureVerifier.php +++ b/src/Mondu/Mondu/Models/SignatureVerifier.php @@ -13,55 +13,55 @@ */ class SignatureVerifier { /** @var string $secret */ - private $secret; + private $secret; - /** - * Constructor - */ + /** + * Constructor + */ public function __construct() { $this->secret = get_option( '_mondu_webhook_secret' ); } - /** - * Get secret - * - * @return string - */ + /** + * Get secret + * + * @return string + */ public function get_secret() { return $this->secret; } - /** - * Set secret - * - * @param string $secret Secret. - * - * @return $this - */ + /** + * Set secret + * + * @param string $secret Secret. + * + * @return $this + */ public function set_secret( $secret ) { $this->secret = $secret; return $this; } - /** - * Create HMAC - * - * @param string $payload Payload. - * - * @return string - */ + /** + * Create HMAC + * + * @param string $payload Payload. + * + * @return string + */ public function create_hmac( $payload ) { return hash_hmac( 'sha256', $payload, $this->secret ); } - /** - * Verify signature - * - * @param $signature - * - * @return bool - */ + /** + * Verify signature + * + * @param $signature + * + * @return bool + */ public function verify( $signature ) { return $this->secret === $signature; } diff --git a/src/Mondu/Mondu/MonduGateway.php b/src/Mondu/Mondu/MonduGateway.php index 61bd87de..a667df8a 100644 --- a/src/Mondu/Mondu/MonduGateway.php +++ b/src/Mondu/Mondu/MonduGateway.php @@ -18,18 +18,18 @@ */ class MonduGateway extends WC_Payment_Gateway { - /** - * Mondu Global Settings - * - * @var MonduRequestWrapper - */ + /** + * Mondu Global Settings + * + * @var MonduRequestWrapper + */ protected $global_settings; - /** - * Mondu Method Name - * - * @var MonduRequestWrapper - */ + /** + * Mondu Method Name + * + * @var MonduRequestWrapper + */ protected $method_name; /** @@ -39,11 +39,11 @@ class MonduGateway extends WC_Payment_Gateway { */ private $mondu_request_wrapper; - /** - * MonduGateway constructor. - * - * @param bool $register_hooks - */ + /** + * MonduGateway constructor. + * + * @param bool $register_hooks + */ public function __construct( $register_hooks = true ) { $this->global_settings = get_option( Plugin::OPTION_NAME ); @@ -54,7 +54,7 @@ public function __construct( $register_hooks = true ) { $this->mondu_request_wrapper = new MonduRequestWrapper(); - if ($register_hooks) { + if ( $register_hooks ) { add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, [ $this, 'process_admin_options' ] ); add_action( 'woocommerce_thankyou_' . $this->id, [ $this, 'thankyou_page' ] ); add_action( 'woocommerce_email_before_order_table', [ $this, 'email_instructions' ], 10, 3 ); diff --git a/src/Mondu/Mondu/MonduRequestWrapper.php b/src/Mondu/Mondu/MonduRequestWrapper.php index e9c0a719..7aa234c8 100644 --- a/src/Mondu/Mondu/MonduRequestWrapper.php +++ b/src/Mondu/Mondu/MonduRequestWrapper.php @@ -19,16 +19,16 @@ * @package Mondu */ class MonduRequestWrapper { - /** - * Mondu API - * - * @var Api - */ + /** + * Mondu API + * + * @var Api + */ private $api; - /** - * MonduRequestWrapper constructor. - */ + /** + * MonduRequestWrapper constructor. + */ public function __construct() { $this->api = new Api(); } @@ -184,7 +184,7 @@ public function get_merchant_payment_methods() { } # return only an array with the identifier (invoice, direct_debit, installment, etc) - $merchant_payment_methods = array_map( function( $payment_method ) { + $merchant_payment_methods = array_map( function ( $payment_method ) { return $payment_method['identifier']; }, $response['payment_methods'] ); set_transient( 'mondu_merchant_payment_methods', $merchant_payment_methods, 1 * 60 ); @@ -198,14 +198,14 @@ public function get_merchant_payment_methods() { return $merchant_payment_methods; } - /** - * Confirm Order - * - * @param $order_id - * @param $mondu_order_id - * @return void|WC_Order - * @throws ResponseException - */ + /** + * Confirm Order + * + * @param $order_id + * @param $mondu_order_id + * @return void|WC_Order + * @throws ResponseException + */ public function confirm_order( $order_id, $mondu_order_id ) { $order = new WC_Order( $order_id ); if ( !Plugin::order_has_mondu( $order ) ) { @@ -281,7 +281,7 @@ public function order_refunded( $order_id, $refund_id ) { if ( !$mondu_invoice_id ) { Helper::log([ 'skipping_credit_note_creation' => [ - 'order' => $order_id, + 'order' => $order_id, 'refund' => $refund_id, ], ]); diff --git a/src/Mondu/Mondu/Presenters/PaymentInfo.php b/src/Mondu/Mondu/Presenters/PaymentInfo.php index ea78276b..68a2a6fa 100644 --- a/src/Mondu/Mondu/Presenters/PaymentInfo.php +++ b/src/Mondu/Mondu/Presenters/PaymentInfo.php @@ -47,11 +47,11 @@ class PaymentInfo { */ private $invoices_data; - /** - * PaymentInfo constructor. - * - * @param int $order_id Order ID - */ + /** + * PaymentInfo constructor. + * + * @param int $order_id Order ID + */ public function __construct( $order_id ) { $this->order = new WC_Order($order_id); $this->mondu_request_wrapper = new MonduRequestWrapper(); @@ -59,29 +59,29 @@ public function __construct( $order_id ) { $this->invoices_data = $this->get_invoices(); } - /** - * Get Order Data - * - * @return array - */ + /** + * Get Order Data + * + * @return array + */ public function get_order_data() { return $this->order_data; } - /** - * Get Invoices Data - * - * @return array - */ + /** + * Get Invoices Data + * + * @return array + */ public function get_invoices_data() { return $this->invoices_data; } - /** - * Get WCPDF Shop Name - * - * @return string - */ + /** + * Get WCPDF Shop Name + * + * @return string + */ public function get_wcpdf_shop_name() { $wcpdf = \WPO_WCPDF::instance(); @@ -128,24 +128,24 @@ public function get_mondu_section_html() { - order_data['state'], [ 'authorized' ], true ) && - $this->order->get_status() === 'on-hold' - ) { - ?> - $this->order->get_id(), - 'order_uuid' => $order_data['uuid'], - 'security' => wp_create_nonce( 'mondu-confirm-order' ), - ]; - ?> - - + order_data['state'], [ 'authorized' ], true ) && + $this->order->get_status() === 'on-hold' + ) { + ?> + $this->order->get_id(), + 'order_uuid' => $order_data['uuid'], + 'security' => wp_create_nonce( 'mondu-confirm-order' ), + ]; + ?> + +

    get_mondu_payment_html(); ?> @@ -176,8 +176,8 @@ public function get_mondu_payment_html() { return null; } - $bank_account = $this->order_data['bank_account']; - $net_terms = $this->get_mondu_net_term(); + $bank_account = $this->order_data['bank_account']; + $net_terms = $this->get_mondu_net_term(); $mondu_uk_buyer = $bank_account['account_number'] && $bank_account['sort_code']; if ( $bank_account ) { @@ -234,11 +234,11 @@ public function get_mondu_payment_html() { } } - /** - * Get Mondu Net Term - * - * @return int|null - */ + /** + * Get Mondu Net Term + * + * @return int|null + */ public function get_mondu_net_term() { if ( !in_array( $this->order->get_payment_method(), Plugin::PAYMENT_METHODS, true ) ) { return null; @@ -251,11 +251,11 @@ public function get_mondu_net_term() { return null; } - /** - * Get Mondu Invoices HTML - * - * @return void - */ + /** + * Get Mondu Invoices HTML + * + * @return void + */ public function get_mondu_invoices_html() { foreach ( $this->invoices_data as $invoice ) { ?> @@ -296,12 +296,12 @@ public function get_mondu_invoices_html() { } } - /** - * Get Mondu Credit Note HTML - * - * @param array $invoice Invoice - * @return void - */ + /** + * Get Mondu Credit Note HTML + * + * @param array $invoice Invoice + * @return void + */ public function get_mondu_credit_note_html( $invoice ) { if ( empty( $invoice['credit_notes'] ) ) { return null; @@ -346,11 +346,11 @@ public function get_mondu_wcpdf_section_html() { include $file; } - /** - * Get Invoices - * - * @return array - */ + /** + * Get Invoices + * + * @return array + */ private function get_invoices() { try { return $this->mondu_request_wrapper->get_invoices( $this->order->get_id() ); @@ -359,11 +359,11 @@ private function get_invoices() { } } - /** - * Get Order - * - * @return array - */ + /** + * Get Order + * + * @return array + */ private function get_order() { try { return $this->mondu_request_wrapper->get_order( $this->order->get_id() ); diff --git a/src/Mondu/Mondu/Support/Helper.php b/src/Mondu/Mondu/Support/Helper.php index 8a09d989..a62b79d9 100644 --- a/src/Mondu/Mondu/Support/Helper.php +++ b/src/Mondu/Mondu/Support/Helper.php @@ -128,7 +128,7 @@ public static function get_order_from_order_number( $order_number ) { return $order; } - $search_key = '_order_number'; + $search_key = '_order_number'; $search_term = $order_number; if ( is_plugin_active( 'custom-order-numbers-for-woocommerce/custom-order-numbers-for-woocommerce.php' ) ) { @@ -147,9 +147,9 @@ public static function get_order_from_order_number( $order_number ) { $wcj_order_numbers_enabled = get_option( 'wcj_order_numbers_enabled' ); // Get prefix and suffix options - $prefix = do_shortcode( get_option( 'wcj_order_number_prefix', '' ) ); + $prefix = do_shortcode( get_option( 'wcj_order_number_prefix', '' ) ); $prefix .= date_i18n( get_option( 'wcj_order_number_date_prefix', '' ) ); - $suffix = do_shortcode( get_option( 'wcj_order_number_suffix', '' ) ); + $suffix = do_shortcode( get_option( 'wcj_order_number_suffix', '' ) ); $suffix .= date_i18n( get_option( 'wcj_order_number_date_suffix', '' ) ); // Ignore suffix and prefix from search input @@ -175,51 +175,51 @@ public static function get_order_from_order_number( $order_number ) { } if ( !isset( $order_id ) ) { - $args = array( + $args = [ 'numberposts' => 1, 'post_type' => 'shop_order', 'fields' => 'ids', 'post_status' => 'any', 'update_post_meta_cache' => false, 'update_post_term_cache' => false, - 'meta_query' => array( - array( + 'meta_query' => [ + [ 'key' => $search_key, 'value' => $search_term, 'compare' => '=', - ), - ) - ); + ], + ], + ]; $query = new WP_Query( $args ); if ( !empty( $query->posts ) ) { - $order_id = $query->posts[ 0 ]; + $order_id = $query->posts[0]; } elseif ( isset( $search_term_fallback ) ) { - $args = array( + $args = [ 'numberposts' => 1, 'post_type' => 'shop_order', 'fields' => 'ids', 'post_status' => 'any', 'update_post_meta_cache' => false, 'update_post_term_cache' => false, - 'meta_query' => array( - array( + 'meta_query' => [ + [ 'key' => $search_key, 'value' => $search_term_fallback, 'compare' => '=', - ), - ) - ); + ], + ], + ]; $query = new WP_Query( $args ); if ( !empty( $query->posts ) ) { - $order_id = $query->posts[ 0 ]; + $order_id = $query->posts[0]; } } } if ( !isset( $order_id ) ) { - Helper::log([ + self::log([ 'message' => 'Error trying to fetch the order', 'order_id_isset' => isset( $order_id ), 'order_number' => $order_number, @@ -234,36 +234,36 @@ public static function get_order_from_order_number( $order_number ) { } /** - * Get order from mondu order uuid - * + * Get order from mondu order uuid + * * @param $mondu_order_uuid * @return bool|WC_Order */ public static function get_order_from_mondu_uuid( $mondu_order_uuid ) { - $search_key = Plugin::ORDER_ID_KEY; + $search_key = Plugin::ORDER_ID_KEY; $search_term = $mondu_order_uuid; - $args = array( + $args = [ 'numberposts' => 1, 'post_type' => 'shop_order', 'fields' => 'ids', 'post_status' => 'any', 'update_post_meta_cache' => false, 'update_post_term_cache' => false, - 'meta_query' => array( - array( + 'meta_query' => [ + [ 'key' => $search_key, 'value' => $search_term, 'compare' => '=', - ), - ) - ); - $query = new WP_Query( $args ); - $order_id = $query->posts[ 0 ]; - $order = wc_get_order( $order_id ); + ], + ], + ]; + $query = new WP_Query( $args ); + $order_id = $query->posts[0]; + $order = wc_get_order( $order_id ); if ( !$order ) { - Helper::log([ + self::log([ 'message' => 'Error trying to fetch the order', 'order_id_isset' => isset( $order_id ), 'mondu_order_uuid' => $mondu_order_uuid, @@ -277,8 +277,8 @@ public static function get_order_from_mondu_uuid( $mondu_order_uuid ) { } /** - * Get order from order number - * + * Get order from order number + * * @param $order_number * @param $mondu_order_uuid * @return bool|WC_Order @@ -294,13 +294,13 @@ public static function get_order_from_order_number_or_uuid( $order_number = null } /** - * Get order from order number - * + * Get order from order number + * * @param $order_number * @param $mondu_order_uuid * @return bool|WC_Order */ - private static function get_order_hpos($order_number, $mondu_order_uuid) { + private static function get_order_hpos( $order_number, $mondu_order_uuid ) { $order = wc_get_order( $order_number ); if ( $order ) { @@ -308,8 +308,8 @@ private static function get_order_hpos($order_number, $mondu_order_uuid) { } $orders = wc_get_orders([ - 'meta_key' => Plugin::ORDER_ID_KEY, - 'meta_value' => $mondu_order_uuid + 'meta_key' => Plugin::ORDER_ID_KEY, + 'meta_value' => $mondu_order_uuid, ]); if ( !empty( $orders ) ) { @@ -320,8 +320,8 @@ private static function get_order_hpos($order_number, $mondu_order_uuid) { } /** - * Get order from order number - * + * Get order from order number + * * @param $order_number * @param $mondu_order_uuid * @return bool|WC_Order @@ -358,12 +358,12 @@ public static function is_production() { return false; } - /** - * Log - * - * @param array $message - * @param string $level - */ + /** + * Log + * + * @param array $message + * @param string $level + */ public static function log( array $message, $level = 'DEBUG' ) { $logger = wc_get_logger(); $logger->log( $level, wc_print_r( $message, true ), [ 'source' => 'mondu' ] ); diff --git a/src/Mondu/Mondu/Support/OrderData.php b/src/Mondu/Mondu/Support/OrderData.php index 61e06142..c993fe2a 100644 --- a/src/Mondu/Mondu/Support/OrderData.php +++ b/src/Mondu/Mondu/Support/OrderData.php @@ -28,10 +28,10 @@ public static function create_order( WC_Order $order, $success_url ) { if ( is_wc_endpoint_url('order-pay') ) { $decline_url = $order->get_checkout_payment_url(); - $cancel_url = $order->get_checkout_payment_url(); + $cancel_url = $order->get_checkout_payment_url(); } else { $decline_url = wc_get_checkout_url(); - $cancel_url = wc_get_checkout_url(); + $cancel_url = wc_get_checkout_url(); } $success_url = get_home_url() . '/?rest_route=/mondu/v1/orders/confirm&external_reference_id=' . $order->get_order_number() . '&return_url=' . urlencode( $success_url ); @@ -296,12 +296,12 @@ public static function get_amount_from_wc_order( WC_Order $order ) { * @return string|null */ public static function get_company_name_from_wc_order( WC_Order $order ) { - $billing_company_name = $order->get_billing_company(); + $billing_company_name = $order->get_billing_company(); $shipping_company_name = $order->get_shipping_company(); if ( isset( $billing_company_name ) && Helper::not_null_or_empty( $billing_company_name ) ) { return $billing_company_name; - } else if ( isset( $shipping_company_name ) && Helper::not_null_or_empty( $shipping_company_name ) ) { + } elseif ( isset( $shipping_company_name ) && Helper::not_null_or_empty( $shipping_company_name ) ) { return $shipping_company_name; } else { return null; diff --git a/src/Mondu/Plugin.php b/src/Mondu/Plugin.php index 6c0aba79..481056c1 100644 --- a/src/Mondu/Plugin.php +++ b/src/Mondu/Plugin.php @@ -31,42 +31,42 @@ */ class Plugin { - /** - * Order ID Key - */ - const ORDER_ID_KEY = '_mondu_order_id'; - - /** - * Invoice ID Key - */ - const INVOICE_ID_KEY = '_mondu_invoice_id'; - - /** - * Option Name - */ - const OPTION_NAME = 'mondu_account'; - - /** - * Payment Methods - */ - const PAYMENT_METHODS = [ - 'invoice' => 'mondu_invoice', - 'direct_debit' => 'mondu_direct_debit', - 'installment' => 'mondu_installment', - 'installment_by_invoice' => 'mondu_installment_by_invoice', + /** + * Order ID Key + */ + const ORDER_ID_KEY = '_mondu_order_id'; + + /** + * Invoice ID Key + */ + const INVOICE_ID_KEY = '_mondu_invoice_id'; + + /** + * Option Name + */ + const OPTION_NAME = 'mondu_account'; + + /** + * Payment Methods + */ + const PAYMENT_METHODS = [ + 'invoice' => 'mondu_invoice', + 'direct_debit' => 'mondu_direct_debit', + 'installment' => 'mondu_installment', + 'installment_by_invoice' => 'mondu_installment_by_invoice', ]; - /** - * Available Countries - */ - const AVAILABLE_COUNTRIES = [ 'DE', 'AT', 'NL', 'FR', 'BE', 'GB' ]; + /** + * Available Countries + */ + const AVAILABLE_COUNTRIES = [ 'DE', 'AT', 'NL', 'FR', 'BE', 'GB' ]; - /** - * Global Settings - * - * @var mixed - */ - protected $global_settings; + /** + * Global Settings + * + * @var mixed + */ + protected $global_settings; /** * Mondu Request Wrapper @@ -81,9 +81,9 @@ public function __construct() { $this->mondu_request_wrapper = new MonduRequestWrapper(); } - /** - * Initialize the plugin - */ + /** + * Initialize the plugin + */ public function init() { if ( !class_exists( 'WooCommerce' ) ) { # This file is required to deactivate the plugin. @@ -139,11 +139,11 @@ public function init() { add_action( 'woocommerce_order_status_changed', [ $this->mondu_request_wrapper, 'order_status_changed' ], 10, 3 ); add_action( 'woocommerce_before_order_object_save', [ $this->mondu_request_wrapper, 'update_order_if_changed_some_fields' ] ); add_action( 'woocommerce_order_refunded', [ $this->mondu_request_wrapper, 'order_refunded' ], 10, 2 ); - add_action( 'woocommerce_blocks_loaded', function() { + add_action( 'woocommerce_blocks_loaded', function () { if ( class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) { add_action( 'woocommerce_blocks_payment_method_type_registration', - function( PaymentMethodRegistry $payment_method_registry ) { + function ( PaymentMethodRegistry $payment_method_registry ) { $payment_method_registry->register( new MonduBlocksSupport() ); } ); @@ -185,20 +185,20 @@ function( PaymentMethodRegistry $payment_method_registry ) { } } - /** - * Load the plugin text domain for translation. - */ + /** + * Load the plugin text domain for translation. + */ public function load_textdomain() { $plugin_rel_path = dirname( plugin_basename( __FILE__ ) ) . '/../../languages/'; load_plugin_textdomain( 'mondu', false, $plugin_rel_path ); } - /** - * Check if the order has Mondu data - * - * @param WC_Order $order - * @return bool - */ + /** + * Check if the order has Mondu data + * + * @param WC_Order $order + * @return bool + */ public static function order_has_mondu( WC_Order $order ) { if ( !in_array($order->get_payment_method(), self::PAYMENT_METHODS, true) ) { return false; @@ -207,12 +207,12 @@ public static function order_has_mondu( WC_Order $order ) { return true; } - /** - * Change address warning - * - * @param WC_Order $order - * @return bool - */ + /** + * Change address warning + * + * @param WC_Order $order + * @return bool + */ public function change_address_warning( WC_Order $order ) { if ( !$this->order_has_mondu($order) ) { return; @@ -232,12 +232,12 @@ public function change_address_warning( WC_Order $order ) { echo '

    ' . esc_html__( 'Since this order will be paid via Mondu you will not be able to change the addresses.', 'mondu' ) . '

    '; } - /** - * Remove gateway if country unavailable - * - * @param array $available_gateways - * @return array - */ + /** + * Remove gateway if country unavailable + * + * @param array $available_gateways + * @return array + */ public function remove_gateway_if_country_unavailable( $available_gateways ) { if ( is_admin() || !is_checkout() ) { return $available_gateways; @@ -484,19 +484,19 @@ public function wcpdf_add_status_to_invoice_admin_when_invoice_is_canceled( $doc } } - /** - * WCPDF add Mondu payment language switch - * - * @param $locale - */ + /** + * WCPDF add Mondu payment language switch + * + * @param $locale + */ public function wcpdf_add_mondu_payment_language_switch( $locale ) { unload_textdomain( 'mondu' ); $this->load_textdomain(); } - /** - * WooCommerce notice - */ + /** + * WooCommerce notice + */ public function woocommerce_notice() { $class = 'notice notice-error'; $message = __( 'Mondu requires WooCommerce to be activated.', 'mondu' ); @@ -504,31 +504,31 @@ public function woocommerce_notice() { printf( '

    %2$s

    ', esc_attr( $class ), esc_html( $message ) ); } - /** - * Check if the country is available - * - * @param $country - * @return bool - */ + /** + * Check if the country is available + * + * @param $country + * @return bool + */ private function is_country_available( $country ) { return in_array( $country, self::AVAILABLE_COUNTRIES, true ); } - /** - * Get the WC Customer object - * - * @return WC_Customer - * @throws Exception - */ + /** + * Get the WC Customer object + * + * @return WC_Customer + * @throws Exception + */ private function get_wc_customer() { return isset( WC()->customer ) ? WC()->customer : new WC_Customer( get_current_user_id() ); } - /** - * Get the Mondu order locale - * - * @return mixed - */ + /** + * Get the Mondu order locale + * + * @return mixed + */ public function get_mondu_order_locale() { /** * WPML current language diff --git a/uninstall.php b/uninstall.php index 64c3ea91..74bfc270 100644 --- a/uninstall.php +++ b/uninstall.php @@ -1,7 +1,7 @@ Date: Sun, 26 May 2024 21:46:59 +0400 Subject: [PATCH 3/6] [PT-509] changed pbs-rules-set and fixed more php_cs warnings --- assets/src/js/mondublocks.js | 16 +++++++------- pbs-rules-set.xml | 1 + src/Mondu/Admin/Order.php | 2 +- src/Mondu/Admin/Settings.php | 22 ++++++++++++++----- .../Mondu/Controllers/OrdersController.php | 4 ++-- .../Mondu/Controllers/WebhooksController.php | 4 ++-- src/Mondu/Mondu/MonduRequestWrapper.php | 9 ++++---- src/Mondu/Mondu/Support/Helper.php | 14 ++++++------ src/Mondu/Mondu/Support/OrderData.php | 6 ++--- src/Mondu/Plugin.php | 2 +- src/autoload.php | 2 +- views/admin/js/{invoice.php => invoice.html} | 0 views/pdf/mondu-invoice-section.php | 16 ++++++++++++++ 13 files changed, 63 insertions(+), 35 deletions(-) rename views/admin/js/{invoice.php => invoice.html} (100%) diff --git a/assets/src/js/mondublocks.js b/assets/src/js/mondublocks.js index eb3fdd06..96529280 100644 --- a/assets/src/js/mondublocks.js +++ b/assets/src/js/mondublocks.js @@ -1,13 +1,13 @@ -(function() { - const { __ } = wp.i18n; - const { createElement } = window.wp.element; - const { decodeEntities } = window.wp.htmlEntities; - const { registerPaymentMethod } = window.wc.wcBlocksRegistry; +(function () { + const { __ } = wp.i18n; + const { createElement } = window.wp.element; + const { decodeEntities } = window.wp.htmlEntities; + const { registerPaymentMethod } = window.wc.wcBlocksRegistry; const { registerPaymentMethodExtensionCallbacks } = window.wc.wcBlocksRegistry; - const settings = window.wc.wcSettings.getSetting( 'mondu_blocks_data', {} ); + const settings = window.wc.wcSettings.getSetting( 'mondu_blocks_data', {} ); settings.available_countries = settings.available_countries || []; - settings.gateways = settings.gateways || {}; + settings.gateways = settings.gateways || {}; // This is not used anywhere, it's added for automatic translation generation with wp cli command const translations = [ @@ -56,7 +56,7 @@ registerPaymentMethodExtensionCallbacks('mondu', Object.keys(settings.gateways).reduce((previousValue, currentValue) => { - previousValue[currentValue] = function(arg) { + previousValue[currentValue] = function (arg) { return settings.available_countries.includes(arg.billingAddress.country) } diff --git a/pbs-rules-set.xml b/pbs-rules-set.xml index 60f441fd..2199b8d5 100644 --- a/pbs-rules-set.xml +++ b/pbs-rules-set.xml @@ -28,6 +28,7 @@ + diff --git a/src/Mondu/Admin/Order.php b/src/Mondu/Admin/Order.php index 6791f7e2..99251131 100644 --- a/src/Mondu/Admin/Order.php +++ b/src/Mondu/Admin/Order.php @@ -85,7 +85,7 @@ function ( $post_or_order_object ) { * @return void */ public function invoice_buttons_js() { - require_once MONDU_VIEW_PATH . '/admin/js/invoice.php'; + require_once MONDU_VIEW_PATH . '/admin/js/invoice.html'; } /** diff --git a/src/Mondu/Admin/Settings.php b/src/Mondu/Admin/Settings.php index 19c047a0..3d429e5f 100644 --- a/src/Mondu/Admin/Settings.php +++ b/src/Mondu/Admin/Settings.php @@ -12,6 +12,7 @@ use Mondu\Exceptions\MonduException; use Mondu\Exceptions\CredentialsNotSetException; use Mondu\Mondu\MonduRequestWrapper; +use WP_Filesystem_Base; if ( !defined( 'ABSPATH' ) ) { die( 'Direct access not allowed' ); @@ -71,7 +72,12 @@ public function init() { * @return void */ public function plugin_menu() { - $mondu_icon = 'data:image/svg+xml;base64,' . base64_encode( file_get_contents( 'https://checkout.mondu.ai/logo.svg' ) ); + $response = wp_remote_get('https://checkout.mondu.ai/logo.svg'); + + $mondu_icon = ''; + if ( is_array( $response ) && !is_wp_error( $response ) ) { + $mondu_icon = 'data:image/svg+xml;base64,' . base64_encode($response['body']); //phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode + } add_menu_page( __( 'Mondu Settings', 'mondu' ), @@ -185,6 +191,12 @@ private function register_webhooks_if_not_registered() { * @return void */ public function download_mondu_logs() { + /** + * @var WP_Filesystem_Base $wp_filesystem + */ + global $wp_filesystem; + WP_Filesystem(); + $is_nonce_valid = check_ajax_referer( 'mondu-download-logs', 'security', false ); if ( !$is_nonce_valid ) { status_header( 400 ); @@ -208,10 +220,8 @@ public function download_mondu_logs() { header( 'Content-Type: text/plain' ); header( 'Content-Disposition: attachment; filename="' . $filename . '";' ); - - readfile( $file ); - - die(); + echo $wp_filesystem->get_contents($file); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + die; } /** @@ -224,7 +234,7 @@ private function get_file( $date ) { $base_dir = WP_CONTENT_DIR . '/uploads/wc-logs/'; $dir = opendir( $base_dir ); if ( $dir ) { - while ( $file = readdir( $dir ) ) { + while ( $file = readdir( $dir ) ) { //phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition if ( str_starts_with( $file, 'mondu-' . $date ) && str_ends_with( $file, '.log' ) ) { return $base_dir . $file; } diff --git a/src/Mondu/Mondu/Controllers/OrdersController.php b/src/Mondu/Mondu/Controllers/OrdersController.php index 17c60c05..57dfd3af 100644 --- a/src/Mondu/Mondu/Controllers/OrdersController.php +++ b/src/Mondu/Mondu/Controllers/OrdersController.php @@ -73,7 +73,7 @@ public function confirm( WP_REST_Request $request ) { WC()->cart->empty_cart(); } - if ( in_array( $order->get_status(), [ 'pending', 'failed' ] ) ) { + if ( in_array( $order->get_status(), [ 'pending', 'failed' ], true ) ) { $order->update_status( 'wc-on-hold', __('On hold', 'woocommerce' ) ); $this->mondu_request_wrapper->confirm_order( $order->get_id(), $mondu_order_id ); @@ -101,7 +101,7 @@ public function decline( WP_REST_Request $request ) { $order->add_order_note( esc_html( sprintf( __( 'Order was declined by Mondu.', 'mondu' ) ) ), false ); - if ( $order->get_status() == 'pending' ) { + if ( $order->get_status() === 'pending' ) { $order->update_status('wc-failed', __('Failed', 'woocommerce')); } diff --git a/src/Mondu/Mondu/Controllers/WebhooksController.php b/src/Mondu/Mondu/Controllers/WebhooksController.php index d826c8ee..2c72ae09 100644 --- a/src/Mondu/Mondu/Controllers/WebhooksController.php +++ b/src/Mondu/Mondu/Controllers/WebhooksController.php @@ -192,7 +192,7 @@ private function handle_confirmed( $params ) { $order->add_order_note( esc_html( sprintf( __( 'Mondu order is on confirmed state.', 'mondu' ) ) ), false ); - if ( in_array( $order->get_status(), [ 'pending', 'on-hold' ] ) ) { + if ( in_array( $order->get_status(), [ 'pending', 'on-hold' ], true ) ) { $order->update_status( 'wc-processing', __( 'Processing', 'woocommerce' ) ); } @@ -223,7 +223,7 @@ private function handle_declined( $params ) { $order->add_order_note( esc_html( sprintf( __( 'Mondu order is on declined state.', 'mondu' ) ) ), false ); - if ( $order->get_status() == 'on-hold' ) { + if ( $order->get_status() === 'on-hold' ) { $order->update_status( 'wc-failed', __( 'Failed', 'woocommerce' ) ); } diff --git a/src/Mondu/Mondu/MonduRequestWrapper.php b/src/Mondu/Mondu/MonduRequestWrapper.php index 7aa234c8..00752bd5 100644 --- a/src/Mondu/Mondu/MonduRequestWrapper.php +++ b/src/Mondu/Mondu/MonduRequestWrapper.php @@ -6,6 +6,7 @@ */ namespace Mondu\Mondu; +use Exception; use Mondu\Exceptions\ResponseException; use Mondu\Mondu\Support\Helper; use Mondu\Mondu\Support\OrderData; @@ -189,7 +190,7 @@ public function get_merchant_payment_methods() { }, $response['payment_methods'] ); set_transient( 'mondu_merchant_payment_methods', $merchant_payment_methods, 1 * 60 ); return $merchant_payment_methods; - } catch ( \Exception $e ) { + } catch ( Exception $e ) { $merchant_payment_methods = array_keys( Plugin::PAYMENT_METHODS ); set_transient( 'mondu_merchant_payment_methods', $merchant_payment_methods, 10 * 60 ); return $merchant_payment_methods; @@ -352,12 +353,12 @@ public function webhook_secret() { /** * Log Plugin event * - * @param \Exception $exception + * @param Exception $exception * @param string $event * @param $body * @return void */ - public function log_plugin_event( \Exception $exception, $event, $body = null ) { + public function log_plugin_event( Exception $exception, $event, $body = null ) { global $wp_version; $params = [ 'plugin' => 'woocommerce', @@ -388,7 +389,7 @@ private function wrap_with_mondu_log_event( $action, array $params = [] ) { } catch ( ResponseException $e ) { $this->log_plugin_event( $e, $action, $e->getBody() ); throw $e; - } catch ( \Exception $e ) { + } catch ( Exception $e ) { $this->log_plugin_event( $e, $action ); throw $e; } diff --git a/src/Mondu/Mondu/Support/Helper.php b/src/Mondu/Mondu/Support/Helper.php index a62b79d9..71116f25 100644 --- a/src/Mondu/Mondu/Support/Helper.php +++ b/src/Mondu/Mondu/Support/Helper.php @@ -164,8 +164,8 @@ public static function get_order_from_order_number( $order_number ) { $search_term_fallback = substr( $search_term_fallback, 0, -strlen( $suffix ) ); } - if ( 'yes' == $wcj_order_numbers_enabled ) { - if ( 'no' == get_option( 'wcj_order_number_sequential_enabled' ) ) { + if ( 'yes' === $wcj_order_numbers_enabled ) { + if ( 'no' === get_option( 'wcj_order_number_sequential_enabled' ) ) { $order_id = $final_search; } else { $search_key = '_wcj_order_number'; @@ -182,7 +182,7 @@ public static function get_order_from_order_number( $order_number ) { 'post_status' => 'any', 'update_post_meta_cache' => false, 'update_post_term_cache' => false, - 'meta_query' => [ + 'meta_query' => [ //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query [ 'key' => $search_key, 'value' => $search_term, @@ -202,7 +202,7 @@ public static function get_order_from_order_number( $order_number ) { 'post_status' => 'any', 'update_post_meta_cache' => false, 'update_post_term_cache' => false, - 'meta_query' => [ + 'meta_query' => [ //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query [ 'key' => $search_key, 'value' => $search_term_fallback, @@ -250,7 +250,7 @@ public static function get_order_from_mondu_uuid( $mondu_order_uuid ) { 'post_status' => 'any', 'update_post_meta_cache' => false, 'update_post_term_cache' => false, - 'meta_query' => [ + 'meta_query' => [ //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query [ 'key' => $search_key, 'value' => $search_term, @@ -308,8 +308,8 @@ private static function get_order_hpos( $order_number, $mondu_order_uuid ) { } $orders = wc_get_orders([ - 'meta_key' => Plugin::ORDER_ID_KEY, - 'meta_value' => $mondu_order_uuid, + 'meta_key' => Plugin::ORDER_ID_KEY, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key + 'meta_value' => $mondu_order_uuid, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value ]); if ( !empty( $orders ) ) { diff --git a/src/Mondu/Mondu/Support/OrderData.php b/src/Mondu/Mondu/Support/OrderData.php index c993fe2a..bbf17605 100644 --- a/src/Mondu/Mondu/Support/OrderData.php +++ b/src/Mondu/Mondu/Support/OrderData.php @@ -34,8 +34,8 @@ public static function create_order( WC_Order $order, $success_url ) { $cancel_url = wc_get_checkout_url(); } - $success_url = get_home_url() . '/?rest_route=/mondu/v1/orders/confirm&external_reference_id=' . $order->get_order_number() . '&return_url=' . urlencode( $success_url ); - $decline_url = get_home_url() . '/?rest_route=/mondu/v1/orders/decline&external_reference_id=' . $order->get_order_number() . '&return_url=' . urlencode( $decline_url ); + $success_url = get_home_url() . '/?rest_route=/mondu/v1/orders/confirm&external_reference_id=' . $order->get_order_number() . '&return_url=' . rawurlencode( $success_url ); + $decline_url = get_home_url() . '/?rest_route=/mondu/v1/orders/decline&external_reference_id=' . $order->get_order_number() . '&return_url=' . rawurlencode( $decline_url ); $data['success_url'] = $success_url; $data['cancel_url'] = $cancel_url; @@ -132,7 +132,7 @@ public static function order_data_from_wc_order( WC_Order $order ) { $billing_last_name = $order->get_billing_last_name(); $billing_email = $order->get_billing_email(); $billing_phone = $order->get_billing_phone(); - $customer_id = $order->get_customer_id() ?: null; + $customer_id = $order->get_customer_id() ?: null; //phpcs:ignore Universal.Operators.DisallowShortTernary.Found $billing_address_line1 = $order->get_billing_address_1(); $billing_address_line2 = $order->get_billing_address_2(); diff --git a/src/Mondu/Plugin.php b/src/Mondu/Plugin.php index 481056c1..613ac856 100644 --- a/src/Mondu/Plugin.php +++ b/src/Mondu/Plugin.php @@ -489,7 +489,7 @@ public function wcpdf_add_status_to_invoice_admin_when_invoice_is_canceled( $doc * * @param $locale */ - public function wcpdf_add_mondu_payment_language_switch( $locale ) { + public function wcpdf_add_mondu_payment_language_switch() { unload_textdomain( 'mondu' ); $this->load_textdomain(); } diff --git a/src/autoload.php b/src/autoload.php index d0013ef5..23480aed 100644 --- a/src/autoload.php +++ b/src/autoload.php @@ -4,7 +4,7 @@ * PSR-4 Autoloader from http://www.php-fig.org/psr/psr-4/examples/ */ -spl_autoload_register(static function ( $class ) { +spl_autoload_register(static function ( $class ) { //phpcs:ignore // project-specific namespace prefix $prefix = 'Mondu'; diff --git a/views/admin/js/invoice.php b/views/admin/js/invoice.html similarity index 100% rename from views/admin/js/invoice.php rename to views/admin/js/invoice.html diff --git a/views/pdf/mondu-invoice-section.php b/views/pdf/mondu-invoice-section.php index a5ac29e0..d39ca1ac 100644 --- a/views/pdf/mondu-invoice-section.php +++ b/views/pdf/mondu-invoice-section.php @@ -11,6 +11,22 @@ $wcpdf_shop_name = ''; } +if ( !isset($bank_account) ) { + $bank_account = ''; +} + +if ( !isset($mondu_uk_buyer) ) { + $mondu_uk_buyer = ''; +} + +if ( !isset($net_terms) ) { + $net_terms = ''; +} + +if ( !isset($invoice_number) ) { + $invoice_number = ''; +} + if ( 'mondu_invoice' === $payment_method ) { ?>
    From 41035995463f62fffef528f85e1d80077891648f Mon Sep 17 00:00:00 2001 From: Tigran Date: Mon, 27 May 2024 12:46:36 +0400 Subject: [PATCH 4/6] [PT-509] Changed README and Settings.php --- README.txt | 4 ++-- src/Mondu/Admin/Settings.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.txt b/README.txt index b4bb6db4..93e115e1 100644 --- a/README.txt +++ b/README.txt @@ -2,8 +2,8 @@ Contributors: mondu-ai, arthurmmoreira, tikohov20 Tags: mondu, woocommerce, e-commerce, ecommerce, store, sales, sell, woo, woo commerce, shop, cart, shopping cart, sell online, checkout, payment, payments, bnpl, b2b Requires at least: 5.9.0 -Tested up to: 6.2.2 -Stable tag: 2.1.7 +Tested up to: 6.5.3 +Stable tag: 2.3.0 Requires PHP: 7.4 License: GPLv3 License URI: https://www.gnu.org/licenses/gpl-3.0.html diff --git a/src/Mondu/Admin/Settings.php b/src/Mondu/Admin/Settings.php index 3d429e5f..0540050a 100644 --- a/src/Mondu/Admin/Settings.php +++ b/src/Mondu/Admin/Settings.php @@ -220,7 +220,7 @@ public function download_mondu_logs() { header( 'Content-Type: text/plain' ); header( 'Content-Disposition: attachment; filename="' . $filename . '";' ); - echo $wp_filesystem->get_contents($file); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + echo wp_kses_post(str_replace('>', '', $wp_filesystem->get_contents($file))); die; } From a991ee245a46932b7c4b2909bf12d69e23989e89 Mon Sep 17 00:00:00 2001 From: Tigran Date: Mon, 27 May 2024 13:18:23 +0400 Subject: [PATCH 5/6] [PT-509] Changed plugin version. --- README.txt | 2 +- mondu-buy-now-pay-later.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.txt b/README.txt index 93e115e1..f84109ba 100644 --- a/README.txt +++ b/README.txt @@ -3,7 +3,7 @@ Contributors: mondu-ai, arthurmmoreira, tikohov20 Tags: mondu, woocommerce, e-commerce, ecommerce, store, sales, sell, woo, woo commerce, shop, cart, shopping cart, sell online, checkout, payment, payments, bnpl, b2b Requires at least: 5.9.0 Tested up to: 6.5.3 -Stable tag: 2.3.0 +Stable tag: 3.0.0 Requires PHP: 7.4 License: GPLv3 License URI: https://www.gnu.org/licenses/gpl-3.0.html diff --git a/mondu-buy-now-pay-later.php b/mondu-buy-now-pay-later.php index 6d8ee69f..3aff68a0 100644 --- a/mondu-buy-now-pay-later.php +++ b/mondu-buy-now-pay-later.php @@ -3,7 +3,7 @@ * Plugin Name: Mondu Buy Now Pay Later * Plugin URI: https://github.com/mondu-ai/bnpl-checkout-woocommerce/releases * Description: Mondu provides B2B E-commerce and B2B marketplaces with an online payment solution to buy now and pay later. - * Version: 2.2.2 + * Version: 3.0.0 * Author: Mondu * Author URI: https://mondu.ai * @@ -27,7 +27,7 @@ die( 'Direct access not allowed' ); } -define( 'MONDU_PLUGIN_VERSION', '2.2.2' ); +define( 'MONDU_PLUGIN_VERSION', '3.0.0' ); define( 'MONDU_PLUGIN_FILE', __FILE__ ); define( 'MONDU_PLUGIN_PATH', __DIR__ ); define( 'MONDU_PLUGIN_BASENAME', plugin_basename(MONDU_PLUGIN_FILE) ); From 2774734c9cbe3d93386eb7af2dc953c3563fdcdb Mon Sep 17 00:00:00 2001 From: Tigran Date: Mon, 3 Jun 2024 22:47:14 +0400 Subject: [PATCH 6/6] PT-875 added refund support to Mondu gateways --- README.txt | 2 +- mondu-buy-now-pay-later.php | 4 +- src/Mondu/Admin/Order.php | 4 +- src/Mondu/Mondu/MonduGateway.php | 62 +++++++++++++++++++++++++ src/Mondu/Mondu/MonduRequestWrapper.php | 29 ++---------- src/Mondu/Plugin.php | 1 - 6 files changed, 72 insertions(+), 30 deletions(-) diff --git a/README.txt b/README.txt index f84109ba..67234441 100644 --- a/README.txt +++ b/README.txt @@ -3,7 +3,7 @@ Contributors: mondu-ai, arthurmmoreira, tikohov20 Tags: mondu, woocommerce, e-commerce, ecommerce, store, sales, sell, woo, woo commerce, shop, cart, shopping cart, sell online, checkout, payment, payments, bnpl, b2b Requires at least: 5.9.0 Tested up to: 6.5.3 -Stable tag: 3.0.0 +Stable tag: 3.0.1 Requires PHP: 7.4 License: GPLv3 License URI: https://www.gnu.org/licenses/gpl-3.0.html diff --git a/mondu-buy-now-pay-later.php b/mondu-buy-now-pay-later.php index 3aff68a0..df286ee7 100644 --- a/mondu-buy-now-pay-later.php +++ b/mondu-buy-now-pay-later.php @@ -3,7 +3,7 @@ * Plugin Name: Mondu Buy Now Pay Later * Plugin URI: https://github.com/mondu-ai/bnpl-checkout-woocommerce/releases * Description: Mondu provides B2B E-commerce and B2B marketplaces with an online payment solution to buy now and pay later. - * Version: 3.0.0 + * Version: 3.0.1 * Author: Mondu * Author URI: https://mondu.ai * @@ -27,7 +27,7 @@ die( 'Direct access not allowed' ); } -define( 'MONDU_PLUGIN_VERSION', '3.0.0' ); +define( 'MONDU_PLUGIN_VERSION', '3.0.1' ); define( 'MONDU_PLUGIN_FILE', __FILE__ ); define( 'MONDU_PLUGIN_PATH', __DIR__ ); define( 'MONDU_PLUGIN_BASENAME', plugin_basename(MONDU_PLUGIN_FILE) ); diff --git a/src/Mondu/Admin/Order.php b/src/Mondu/Admin/Order.php index 99251131..6f73ae76 100644 --- a/src/Mondu/Admin/Order.php +++ b/src/Mondu/Admin/Order.php @@ -66,10 +66,10 @@ function ( $post_or_order_object ) { $order = ( $post_or_order_object instanceof \WP_Post ) ? wc_get_order( $post_or_order_object->ID ) : $post_or_order_object; if ( null === $order ) { -return; + return; } if ( !in_array( $order->get_payment_method(), Plugin::PAYMENT_METHODS, true ) ) { -return; + return; } $this->render_meta_box_content( $order ); diff --git a/src/Mondu/Mondu/MonduGateway.php b/src/Mondu/Mondu/MonduGateway.php index a667df8a..6b901399 100644 --- a/src/Mondu/Mondu/MonduGateway.php +++ b/src/Mondu/Mondu/MonduGateway.php @@ -7,9 +7,11 @@ namespace Mondu\Mondu; use Mondu\Exceptions\ResponseException; +use Mondu\Mondu\Support\OrderData; use Mondu\Plugin; use WC_Order; use WC_Payment_Gateway; +use WP_Error; /** * Mondu Gateway @@ -59,6 +61,12 @@ public function __construct( $register_hooks = true ) { add_action( 'woocommerce_thankyou_' . $this->id, [ $this, 'thankyou_page' ] ); add_action( 'woocommerce_email_before_order_table', [ $this, 'email_instructions' ], 10, 3 ); } + + + $this->supports = [ + 'refunds', + 'products', + ]; } /** @@ -153,6 +161,60 @@ public function process_payment( $order_id ) { ]; } + /** + * @param WC_Order $order + * @return bool + */ + public function can_refund_order( $order ) { + $can_refund_parent = parent::can_refund_order( $order ); + + if ( !$can_refund_parent ) { + return false; + } + + return (bool) $order->get_meta( Plugin::INVOICE_ID_KEY ); + } + + /** + * @param $order_id + * @param $amount + * @param $reason + * @return bool|WP_Error + */ + public function process_refund( $order_id, $amount = null, $reason = '' ) { + $order = wc_get_order( $order_id ); + + if ( !$order instanceof WC_Order ) { + return false; + } + + $mondu_invoice_id = $order->get_meta( Plugin::INVOICE_ID_KEY ); + + if ( !$mondu_invoice_id ) { + return false; + } + + $order_refunds = $order->get_refunds(); + /** @noinspection PhpIssetCanBeReplacedWithCoalesceInspection */ + $refund = isset($order_refunds[0]) ? $order_refunds[0] : null; + + if ( !$refund ) { + return false; + } + + try { + $result = $this->mondu_request_wrapper->create_credit_note($mondu_invoice_id, OrderData::create_credit_note($refund)); + } catch ( ResponseException $e ) { + return new WP_Error('error', $e->getMessage() ); + } + + if ( isset($result['credit_note']) ) { + return true; + } + + return false; + } + /** * Check if Mondu has its credentials validated. * diff --git a/src/Mondu/Mondu/MonduRequestWrapper.php b/src/Mondu/Mondu/MonduRequestWrapper.php index 00752bd5..58108fd2 100644 --- a/src/Mondu/Mondu/MonduRequestWrapper.php +++ b/src/Mondu/Mondu/MonduRequestWrapper.php @@ -267,32 +267,13 @@ public function order_status_changed( $order_id, $from_status, $to_status ) { /** * Handle Order Refunded * - * @param $order_id - * @param $refund_id - * @return void + * @param $mondu_invoice_id + * @param $credit_note + * @return array-key|void * @throws ResponseException */ - public function order_refunded( $order_id, $refund_id ) { - $order = new WC_Order( $order_id ); - if ( !Plugin::order_has_mondu( $order ) ) { - return; - } - - $mondu_invoice_id = $order->get_meta( Plugin::INVOICE_ID_KEY ); - if ( !$mondu_invoice_id ) { - Helper::log([ - 'skipping_credit_note_creation' => [ - 'order' => $order_id, - 'refund' => $refund_id, - ], - ]); - return; - } - - $refund = new WC_Order_Refund( $refund_id ); - $credit_note = OrderData::create_credit_note( $refund ); - - $this->wrap_with_mondu_log_event( 'create_credit_note', [ $mondu_invoice_id, $credit_note ] ); + public function create_credit_note( $mondu_invoice_id, $credit_note ) { + return $this->wrap_with_mondu_log_event( 'create_credit_note', [ $mondu_invoice_id, $credit_note ] ); } diff --git a/src/Mondu/Plugin.php b/src/Mondu/Plugin.php index 613ac856..f599309c 100644 --- a/src/Mondu/Plugin.php +++ b/src/Mondu/Plugin.php @@ -138,7 +138,6 @@ public function init() { */ add_action( 'woocommerce_order_status_changed', [ $this->mondu_request_wrapper, 'order_status_changed' ], 10, 3 ); add_action( 'woocommerce_before_order_object_save', [ $this->mondu_request_wrapper, 'update_order_if_changed_some_fields' ] ); - add_action( 'woocommerce_order_refunded', [ $this->mondu_request_wrapper, 'order_refunded' ], 10, 2 ); add_action( 'woocommerce_blocks_loaded', function () { if ( class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) { add_action(