From 56d02f67485b9a4fc3244c3faefda9a6d24c9a7a Mon Sep 17 00:00:00 2001 From: 0zd0 <67220210+0zd0@users.noreply.github.com> Date: Wed, 19 Jun 2024 01:25:21 +0300 Subject: [PATCH] refactor/webhook api + tests --- .github/workflows/download.yml | 8 +- .gitignore | 4 +- composer.json | 2 + composer.lock | 29 ++-- includes/Gateways/ReepayCheckout.php | 142 ++++++++++-------- includes/Gateways/ReepayGateway.php | 84 ++--------- includes/{ => Utils}/DIContainer.php | 2 +- phpunit.xml | 4 +- reepay-woocommerce-payment.php | 2 +- .../ReepayCheckoutTest.php | 8 +- .../ReepayGatewayTest.php | 35 ++++- tests/unit/{Gateways.php => GatewaysTest.php} | 6 +- vite/bun.lockb | Bin 206185 -> 205863 bytes vite/package.json | 1 - 14 files changed, 156 insertions(+), 171 deletions(-) rename includes/{ => Utils}/DIContainer.php (98%) rename tests/unit/{GatewaysTests => Gateways}/ReepayCheckoutTest.php (80%) rename tests/unit/{GatewaysTests => Gateways}/ReepayGatewayTest.php (94%) rename tests/unit/{Gateways.php => GatewaysTest.php} (88%) diff --git a/.github/workflows/download.yml b/.github/workflows/download.yml index 494e2907..beabf977 100644 --- a/.github/workflows/download.yml +++ b/.github/workflows/download.yml @@ -37,9 +37,7 @@ jobs: - name: Cache npm dependencies uses: actions/cache@v4 with: - path: | - ~/.npm - node_modules + path: node_modules key: "${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}" restore-keys: | ${{ runner.os }}-node- @@ -47,9 +45,7 @@ jobs: - name: Cache vite dependencies uses: actions/cache@v4 with: - path: | - ~/.npm - vite/node_modules + path: vite/node_modules key: "${{ runner.os }}-vite-${{ hashFiles('vite/bun.lockb') }}" restore-keys: | ${{ runner.os }}-vite- diff --git a/.gitignore b/.gitignore index 7ea562e6..c248f96d 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,6 @@ dist-ssr *.ntvs* *.njsproj *.sln -*.sw? \ No newline at end of file +*.sw? + +.cache \ No newline at end of file diff --git a/composer.json b/composer.json index 9220e616..488f553f 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,7 @@ { "name": "reepay/checkout", "description": "Get a plug-n-play payment solution for WooCommerce, that is easy to use, highly secure and is built to maximize the potential of your e-commerce.", + "version": "1.0.0", "type": "wordpress-plugin", "license": "GPL", "autoload": { @@ -25,6 +26,7 @@ "wp-coding-standards/wpcs": "^3.0" }, "config": { + "cache-dir": ".cache/composer", "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true } diff --git a/composer.lock b/composer.lock index 3e1f1e00..ca6b77ac 100644 --- a/composer.lock +++ b/composer.lock @@ -4,20 +4,20 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "48f4ba2911d22f4aa2f162726bec87af", + "content-hash": "07ac69b8c135aa1b98e5ddde8ab3b6e2", "packages": [ { "name": "billwerk/php-sdk", - "version": "v1.0.7", + "version": "v1.0.9", "source": { "type": "git", "url": "https://github.com/0zd0/billwerk-sdk-php.git", - "reference": "e8632117fe9b4e0328e64678477f5a281c0ee198" + "reference": "beb3c6255a9790695011c8f266d055d053bbeb5b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/0zd0/billwerk-sdk-php/zipball/e8632117fe9b4e0328e64678477f5a281c0ee198", - "reference": "e8632117fe9b4e0328e64678477f5a281c0ee198", + "url": "https://api.github.com/repos/0zd0/billwerk-sdk-php/zipball/beb3c6255a9790695011c8f266d055d053bbeb5b", + "reference": "beb3c6255a9790695011c8f266d055d053bbeb5b", "shasum": "" }, "require": { @@ -49,9 +49,9 @@ "description": "Api SDK for Billwerk+", "support": { "issues": "https://github.com/0zd0/billwerk-sdk-php/issues", - "source": "https://github.com/0zd0/billwerk-sdk-php/tree/v1.0.7" + "source": "https://github.com/0zd0/billwerk-sdk-php/tree/v1.0.9" }, - "time": "2024-06-11T18:34:11+00:00" + "time": "2024-06-18T18:14:56+00:00" }, { "name": "guzzlehttp/guzzle", @@ -801,16 +801,16 @@ }, { "name": "myclabs/deep-copy", - "version": "1.11.1", + "version": "1.12.0", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "7284c22080590fb39f2ffa3e9057f10a4ddd0e0c" + "reference": "3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/7284c22080590fb39f2ffa3e9057f10a4ddd0e0c", - "reference": "7284c22080590fb39f2ffa3e9057f10a4ddd0e0c", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c", + "reference": "3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c", "shasum": "" }, "require": { @@ -818,11 +818,12 @@ }, "conflict": { "doctrine/collections": "<1.6.8", - "doctrine/common": "<2.13.3 || >=3,<3.2.2" + "doctrine/common": "<2.13.3 || >=3 <3.2.2" }, "require-dev": { "doctrine/collections": "^1.6.8", "doctrine/common": "^2.13.3 || ^3.2.2", + "phpspec/prophecy": "^1.10", "phpunit/phpunit": "^7.5.20 || ^8.5.23 || ^9.5.13" }, "type": "library", @@ -848,7 +849,7 @@ ], "support": { "issues": "https://github.com/myclabs/DeepCopy/issues", - "source": "https://github.com/myclabs/DeepCopy/tree/1.11.1" + "source": "https://github.com/myclabs/DeepCopy/tree/1.12.0" }, "funding": [ { @@ -856,7 +857,7 @@ "type": "tidelift" } ], - "time": "2023-03-08T13:26:56+00:00" + "time": "2024-06-12T14:39:25+00:00" }, { "name": "phar-io/manifest", diff --git a/includes/Gateways/ReepayCheckout.php b/includes/Gateways/ReepayCheckout.php index 51ccc93a..747e6cf1 100644 --- a/includes/Gateways/ReepayCheckout.php +++ b/includes/Gateways/ReepayCheckout.php @@ -520,25 +520,29 @@ public function generate_account_info_html( string $key, array $data ): string { $account_info = null; } - ob_start(); - ?> - - - - - -
- - - - - -
- - - + + + + +
+ %s +
+ + + HTML; + + $label = wp_kses_post( $data['title'] ); + $span = <<%s + HTML; + + if ( ! is_null( $account_info ) && ! is_null( $data['getter'] ) && ! empty( call_user_func( array( $account_info, $data['getter'] ) ) ) ) { + $span = sprintf( $span, call_user_func( array( $account_info, $data['getter'] ) ) ); + } + + return sprintf( $html, $label, $span ); } /** @@ -564,20 +568,20 @@ public function generate_verify_key_html( string $key, array $data ): string { return ''; } - ob_start(); - ?> - - - -
- -
- - - + + +
+ +
+ + + HTML; + + return sprintf( $html, __( 'Save and verify', 'reepay-checkout-gateway' ) ); } /** @@ -587,6 +591,7 @@ public function generate_verify_key_html( string $key, array $data ): string { * @param array $data Field data. * * @return string + * @throws Exception Exception. * @see WC_Settings_API::generate_settings_html */ public function generate_webhook_status_html( string $key, array $data ): string { @@ -609,35 +614,48 @@ public function generate_webhook_status_html( string $key, array $data ): string $is_webhook_configured = $this->is_webhook_configured(); $this->test_mode = $default_test_mode; - ob_start(); - ?> - - - - - -
- - - - - - - -

- -

- - - -
- - - + + + + +
+ %s + +
+ + + HTML; + + $label = __( 'Webhook', 'reepay-checkout-gateway' ); + $input_name = esc_attr( $this->get_field_key( $key ) ); + $input_value = esc_attr( $is_webhook_configured ); + + if ( $is_webhook_configured ) { + $span = << + %s + + HTML; + $span = sprintf( $span, esc_html__( 'Active', 'reepay-checkout-gateway' ) ); + } else { + $span = << + %s + +

+ %s +

+ HTML; + $span = sprintf( + $span, + esc_html__( 'Configuration is required.', 'reepay-checkout-gateway' ), + esc_html__( 'Please check api credentials and save the settings. Webhook will be installed automatically.', 'reepay-checkout-gateway' ) + ); + } + + return sprintf( $html, $label, $span, $input_name, $input_value ); } /** diff --git a/includes/Gateways/ReepayGateway.php b/includes/Gateways/ReepayGateway.php index f8e92c55..a8d2ed36 100644 --- a/includes/Gateways/ReepayGateway.php +++ b/includes/Gateways/ReepayGateway.php @@ -9,6 +9,7 @@ use Billwerk\Sdk\Exception\BillwerkApiException; use Billwerk\Sdk\Model\Account\AccountModel; +use Billwerk\Sdk\Model\Account\WebhookSettingsUpdateModel; use Exception; use Reepay\Checkout\Api; use Reepay\Checkout\Integrations\PWGiftCardsIntegration; @@ -442,93 +443,30 @@ public static function get_webhook_url(): string { * Valid keys entered and webhook url registered in reepay * * @return bool - * - * @throws Exception Never, just for phpcs. */ public function is_webhook_configured(): bool { try { - $response = reepay()->api( $this )->request( 'GET', 'https://api.reepay.com/v1/account/webhook_settings' ); - if ( is_wp_error( $response ) ) { - if ( ! empty( $response->get_error_code() ) ) { - throw new Exception( $response->get_error_message(), intval( $response->get_error_code() ) ); - } - } - + $settings = reepay()->sdk()->account()->getWebHookSettings(); $webhook_url = self::get_webhook_url(); + $alert_email = is_email( $this->failed_webhooks_email ) ? $this->failed_webhooks_email : ''; - $alert_emails = $response['alert_emails']; - - // The webhook settings of the payment plugin. - $alert_email = ''; - if ( ! empty( $this->settings['failed_webhooks_email'] ) && - is_email( $this->settings['failed_webhooks_email'] ) - ) { - $alert_email = $this->settings['failed_webhooks_email']; - } - - $exist_waste_urls = false; - - $urls = array(); - - foreach ( $response['urls'] as $url ) { - if ( ( strpos( $url, $webhook_url ) === false || // either another site or exact url match. - $url === $webhook_url ) && - strpos( $url, 'WC_Gateway_Reepay_Checkout' ) === false ) { - $urls[] = $url; - } else { - $exist_waste_urls = true; - } - } - - // Verify the webhook settings. - if ( ! $exist_waste_urls && - in_array( $webhook_url, $urls, true ) - && ( empty( $alert_email ) || in_array( $alert_email, $alert_emails, true ) ) - ) { + if ( in_array( $webhook_url, $settings->getUrls(), true ) ) { return true; } - // Update the webhook settings. - if ( ! in_array( $webhook_url, $urls, true ) ) { - $urls[] = $webhook_url; - } - - if ( ! empty( $alert_email ) && is_email( $alert_email ) ) { - $alert_emails[] = $alert_email; - } - - $data = array( - 'urls' => array_unique( $urls ), - 'disabled' => false, - 'alert_emails' => array_unique( $alert_emails ), - ); - - $response = reepay()->api( $this )->request( 'PUT', 'https://api.reepay.com/v1/account/webhook_settings', $data ); - if ( is_wp_error( $response ) ) { - throw new Exception( $response->get_error_message(), $response->get_error_code() ); - } - - $this->log( - array( - 'source' => 'WebHook has been successfully created/updated', - $response, - ) + reepay()->sdk()->account()->updateWebHookSettings( + ( new WebhookSettingsUpdateModel() ) + ->setUrls( array_unique( array( ...$settings->getUrls(), $webhook_url ) ) ) + ->setDisabled( false ) + ->setAlertEmails( array_unique( array( ...$settings->getAlertEmails(), $alert_email ) ) ) ); WC_Admin_Settings::add_message( __( 'Billwerk+: WebHook has been successfully created/updated', 'reepay-checkout-gateway' ) ); } catch ( Exception $e ) { - $this->log( - array( - 'source' => 'WebHook creation/update has been failed', - 'error' => $e->getMessage(), - ) - ); - WC_Admin_Settings::add_error( __( 'Unable to retrieve the webhook settings. Wrong api credentials?', 'reepay-checkout-gateway' ) ); return false; } - return true; } @@ -569,7 +507,7 @@ public function generate_gateway_status_html( string $key, array $data ): string $esc_is_active = esc_attr( $is_active ); $title = wp_kses_post( $data['title'] ); $tooltip_html = $this->get_tooltip_html( $data ); - $html_output = <<