From 38d6a89406c18ff047c33f65d6607daa4f309ee5 Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Tue, 6 Jun 2023 14:04:41 +0300 Subject: [PATCH 001/101] Gateway tests --- phpunit.xml | 5 +- .../unit/gatewaysTests/ReepayCheckoutTest.php | 82 +++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 tests/unit/gatewaysTests/ReepayCheckoutTest.php diff --git a/phpunit.xml b/phpunit.xml index bf368b10..ae8b5dd3 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -14,7 +14,7 @@ ./tests/unit/ApiConfiguredTest.php - ./tests/unit/GatewaysTest.php + ./tests/unit/Gateways.php ./tests/unit/functions @@ -22,5 +22,8 @@ ./tests/unit/orderFlow + + ./tests/unit/gatewaysTests + diff --git a/tests/unit/gatewaysTests/ReepayCheckoutTest.php b/tests/unit/gatewaysTests/ReepayCheckoutTest.php new file mode 100644 index 00000000..8804dc1e --- /dev/null +++ b/tests/unit/gatewaysTests/ReepayCheckoutTest.php @@ -0,0 +1,82 @@ +user = get_user_by( 'id', $user_id ); + } + + /** + * After a test method runs, resets any state in WordPress the test method might have changed. + */ + public function tear_down() { + parent::tear_down(); + + $this->user->delete( true ); + } + + /** + * Test function test_rp_card_store + * + * @param string $token card token from callback. + * + * @testWith + * ["ca_f73e13e5784a5dff32f2f93be7a8130f"] + */ + public function test_rp_card_store( string $token ) { + $this->expectException( Exception::class ); + self::$gateway->add_payment_token_to_customer( $this->user->ID, $token ); + } + + /** + * Test function test_rp_finalize + * + * @param string $token amount for calculation. + * + * @testWith + * ["ca_f73e13e5784a5dff32f2f93be7a8130f"] + */ + public function test_rp_finalize( string $token ) { + $this->expectException( Exception::class ); + self::$gateway->add_payment_token_to_customer( $this->user->ID, $token ); + } + +} From 256dd2d34bb1a9d614be78ad520e1315adcc5718 Mon Sep 17 00:00:00 2001 From: daniel21703 Date: Sun, 16 Jul 2023 19:56:26 +0300 Subject: [PATCH 002/101] beatify code --- includes/OrderFlow/ThankyouPage.php | 80 +++++++++-------------------- 1 file changed, 25 insertions(+), 55 deletions(-) diff --git a/includes/OrderFlow/ThankyouPage.php b/includes/OrderFlow/ThankyouPage.php index 7182056a..5f687fef 100644 --- a/includes/OrderFlow/ThankyouPage.php +++ b/includes/OrderFlow/ThankyouPage.php @@ -9,9 +9,7 @@ use Exception; use Reepay\Checkout\LoggingTrait; -use WC_Order; use WC_Order_Item_Product; -use WC_Subscriptions_Product; defined( 'ABSPATH' ) || exit(); @@ -34,7 +32,7 @@ class ThankyouPage { * Constructor */ public function __construct() { - add_filter( 'wc_get_template', array( $this, 'override_template' ), 5, 20 ); + add_filter( 'wc_get_template', array( $this, 'override_template_thankyou_template' ), 5, 20 ); add_action( 'woocommerce_before_thankyou', array( $this, 'thankyou_page' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'thankyou_scripts' ) ); @@ -55,27 +53,14 @@ public function __construct() { * @param string $default_path Default path. * * @return string - * @noinspection PhpMissingParamTypeInspection */ - public function override_template( $located, $template_name, $args, $template_path, $default_path ): string { - if ( ! is_array( $args ) || ! is_string( $template_path ) ) { - return $located; - } - - if ( strpos( $located, 'checkout/thankyou.php' ) !== false ) { - if ( ! isset( $args['order'] ) ) { - return $located; - } - - $order = wc_get_order( $args['order'] ); - if ( ! $order ) { - return $located; - } - - if ( ! rp_is_order_paid_via_reepay( $order ) ) { - return $located; - } - + public function override_template_thankyou_template( $located, $template_name, $args, $template_path, $default_path ): string { + if ( is_array( $args ) && + is_string( $template_path ) && + strpos( $located, 'checkout/thankyou.php' ) !== false && + ! empty( $args['order'] ) && + rp_is_order_paid_via_reepay( $args['order'] ) + ) { $located = wc_locate_template( 'checkout/thankyou.php', $template_path, @@ -92,7 +77,6 @@ public function override_template( $located, $template_name, $args, $template_pa * @param int $order_id current order id. * * @return void - * @throws Exception If error with order confirmation. */ public function thankyou_page( int $order_id ) { $order = wc_get_order( $order_id ); @@ -124,11 +108,8 @@ public function thankyou_scripts() { return; } - global $wp; - $order_key = isset( $_GET['key'] ) ? wc_clean( wp_unslash( $_GET['key'] ) ) : ''; - - $order = wc_get_order( absint( $wp->query_vars['order-received'] ) ); + $order = wc_get_order( get_query_var( 'order-received', 0 ) ); if ( empty( $order ) || ! $order->key_is_valid( $order_key ) @@ -139,7 +120,7 @@ public function thankyou_scripts() { $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; - wp_register_script( + wp_enqueue_script( 'wc-gateway-reepay-thankyou', reepay()->get_setting( 'js_url' ) . 'thankyou' . $suffix . '.js', array( @@ -164,8 +145,6 @@ public function thankyou_scripts() { ), ) ); - - wp_enqueue_script( 'wc-gateway-reepay-thankyou' ); } /** @@ -190,31 +169,23 @@ public function ajax_check_payment() { * * @var WC_Order_Item_Product $item */ - if ( class_exists( WC_Subscriptions_Product::class ) && WC_Subscriptions_Product::is_subscription( $item->get_product() ) ) { - if ( .0 === $order->get_total() ) { - $ret = array( - 'state' => 'paid', - 'message' => 'Subscription is activated in trial', - ); - - wp_send_json_success( apply_filters( 'woocommerce_reepay_check_payment', $ret, $order->get_id() ) ); - } + if ( .0 === $order->get_total() && wcs_is_subscription_product( $item->get_product() ) ) { + $ret = array( + 'state' => 'paid', + 'message' => 'Subscription is activated in trial', + ); + + wp_send_json_success( apply_filters( 'woocommerce_reepay_check_payment', $ret, $order->get_id() ) ); } } - $ret = array(); + $invoice_data = reepay()->api( $order )->get_invoice_data( $order ); - $result = reepay()->api( $order )->get_invoice_data( $order ); - if ( is_wp_error( $result ) ) { - // No information. - $ret = array( - 'state' => 'unknown', - ); - - wp_send_json_success( apply_filters( 'woocommerce_reepay_check_payment', $ret, $order->get_id() ) ); + if ( is_wp_error( $invoice_data ) ) { + wp_send_json_success( apply_filters( 'woocommerce_reepay_check_payment', array( 'state' => 'unknown' ), $order->get_id() ) ); } - switch ( $result['state'] ) { + switch ( $invoice_data['state'] ) { case 'pending': $ret = array( 'state' => 'pending', @@ -238,10 +209,10 @@ public function ajax_check_payment() { case 'failed': $message = 'Order has been failed'; - if ( count( $result['transactions'] ) > 0 && - isset( $result['transactions'][0]['card_transaction']['acquirer_message'] ) + if ( count( $invoice_data['transactions'] ) > 0 && + isset( $invoice_data['transactions'][0]['card_transaction']['acquirer_message'] ) ) { - $message = $result['transactions'][0]['card_transaction']['acquirer_message']; + $message = $invoice_data['transactions'][0]['card_transaction']['acquirer_message']; } $ret = array( @@ -252,7 +223,7 @@ public function ajax_check_payment() { break; } - wp_send_json_success( apply_filters( 'woocommerce_reepay_check_payment', $ret, $order->get_id() ) ); + wp_send_json_success( apply_filters( 'woocommerce_reepay_check_payment', $ret ?? array(), $order->get_id() ) ); } /** @@ -261,7 +232,6 @@ public function ajax_check_payment() { * @param string $invoice_id invoice id. * * @return void - * @throws Exception If status update failed. */ private function process_order_confirmation( string $invoice_id ) { $this->log( sprintf( 'accept_url: Processing status update %s', $invoice_id ) ); From e1dad30b88a2bf063995f347df9e605bc7ad2e7c Mon Sep 17 00:00:00 2001 From: daniel21703 Date: Sun, 16 Jul 2023 19:57:15 +0300 Subject: [PATCH 003/101] remove thankyou_page and process_order_confirmation methods --- includes/OrderFlow/ThankyouPage.php | 122 ---------------------------- 1 file changed, 122 deletions(-) diff --git a/includes/OrderFlow/ThankyouPage.php b/includes/OrderFlow/ThankyouPage.php index 5f687fef..dd9bbbc2 100644 --- a/includes/OrderFlow/ThankyouPage.php +++ b/includes/OrderFlow/ThankyouPage.php @@ -33,7 +33,6 @@ class ThankyouPage { */ public function __construct() { add_filter( 'wc_get_template', array( $this, 'override_template_thankyou_template' ), 5, 20 ); - add_action( 'woocommerce_before_thankyou', array( $this, 'thankyou_page' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'thankyou_scripts' ) ); @@ -71,33 +70,6 @@ public function override_template_thankyou_template( $located, $template_name, $ return $located; } - /** - * Thank you page - * - * @param int $order_id current order id. - * - * @return void - */ - public function thankyou_page( int $order_id ) { - $order = wc_get_order( $order_id ); - if ( ! $order || ! rp_is_order_paid_via_reepay( $order ) ) { - return; - } - - $gateway = rp_get_payment_method( $order ); - - if ( abs( $order->get_total() ) < 0.01 ) { - $order->payment_complete(); - } - - // Update the order status if webhook wasn't configured. - if ( 'no' === $gateway->is_webhook_configured - && ! empty( $_GET['invoice'] ) - ) { - $this->process_order_confirmation( wc_clean( $_GET['invoice'] ) ); - } - } - /** * Outputs scripts used for "thankyou" page * @@ -225,98 +197,4 @@ public function ajax_check_payment() { wp_send_json_success( apply_filters( 'woocommerce_reepay_check_payment', $ret ?? array(), $order->get_id() ) ); } - - /** - * Process the order confirmation using accept_url. - * - * @param string $invoice_id invoice id. - * - * @return void - */ - private function process_order_confirmation( string $invoice_id ) { - $this->log( sprintf( 'accept_url: Processing status update %s', $invoice_id ) ); - - $order = rp_get_order_by_handle( $invoice_id ); - if ( ! $order ) { - $this->log( sprintf( 'accept_url: Order is not found. Invoice: %s', $invoice_id ) ); - - return; - } - - $result = reepay()->api( $order )->get_invoice_by_handle( $invoice_id ); - if ( is_wp_error( $result ) ) { - return; - } - - $this->log( sprintf( 'accept_url: invoice state: %s. Invoice ID: %s ', $result['state'], $invoice_id ) ); - - switch ( $result['state'] ) { - case 'pending': - OrderStatuses::update_order_status( - $order, - 'pending', - sprintf( - // translators: %1$s order amount, %2$s transaction id. - __( 'Transaction is pending. Amount: %1$s. Transaction: %2$s', 'reepay-checkout-gateway' ), - wc_price( rp_make_initial_amount( $result['amount'], $result['currency'] ) ), - $result['transaction'] - ), - $result['transaction'] - ); - break; - case 'authorized': - if ( $order->has_status( OrderStatuses::$status_sync_enabled ? OrderStatuses::$status_authorized : 'on-hold' ) ) { - $this->log( sprintf( 'accept_url: Order #%s has been authorized before', $order->get_id() ) ); - - return; - } - - OrderStatuses::set_authorized_status( - $order, - sprintf( - // translators: %s order amount. - __( 'Payment has been authorized. Amount: %s.', 'reepay-checkout-gateway' ), - wc_price( rp_make_initial_amount( $result['amount'], $result['currency'] ) ) - ), - null - ); - - do_action( 'reepay_instant_settle', $order ); - - $this->log( sprintf( 'accept_url: Order #%s has been marked as authorized', $order->get_id() ) ); - break; - case 'settled': - if ( $order->get_status() === OrderStatuses::$status_settled ) { - $this->log( sprintf( 'accept_url: Order #%s has been settled before', $order->get_id() ) ); - - return; - } - - OrderStatuses::set_settled_status( - $order, - sprintf( - // translators: %s order amount. - __( 'Payment has been settled. Amount: %s.', 'reepay-checkout-gateway' ), - wc_price( rp_make_initial_amount( $result['amount'], $result['currency'] ) ) - ), - null - ); - - $this->log( sprintf( 'accept_url: Order #%s has been marked as settled', $order->get_id() ) ); - - break; - case 'cancelled': - $order->update_status( 'cancelled', __( 'Cancelled.', 'reepay-checkout-gateway' ) ); - - $this->log( sprintf( 'accept_url: Order #%s has been marked as cancelled', $order->get_id() ) ); - - break; - case 'failed': - $order->update_status( 'failed', __( 'Failed.', 'reepay-checkout-gateway' ) ); - - $this->log( sprintf( 'accept_url: Order #%s has been marked as failed', $order->get_id() ) ); - - break; - } - } } From b7455c6baa6ac7e1254199a8eac8c3114a7d6e39 Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Tue, 25 Jul 2023 12:59:26 +0300 Subject: [PATCH 004/101] Fix MS tokens saving --- includes/Api.php | 22 +++++++++++++--------- includes/Tokens/Main.php | 2 -- includes/Tokens/TokenReepayTrait.php | 4 +++- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/includes/Api.php b/includes/Api.php index c4991137..5b0fff65 100644 --- a/includes/Api.php +++ b/includes/Api.php @@ -1020,7 +1020,7 @@ public function get_reepay_cards( string $customer_handle, $reepay_token = null return $result; } - if ( ! isset( $result['cards'] ) ) { + if ( ! isset( $result['cards'] ) && ! isset( $result['mps_subscriptions'] ) ) { return new WP_Error( 0, 'Unable to retrieve customer payment methods' ); } @@ -1028,17 +1028,21 @@ public function get_reepay_cards( string $customer_handle, $reepay_token = null return $result['cards']; } - $cards = $result['cards']; - foreach ( $cards as $card ) { - if ( $card['id'] === $reepay_token && 'active' === $card['state'] ) { - return $card; + if ( ! empty( $result['cards'] ) ) { + $cards = $result['cards']; + foreach ( $cards as $card ) { + if ( $card['id'] === $reepay_token && 'active' === $card['state'] ) { + return $card; + } } } - $mps_subscriptions = $result['mps_subscriptions']; - foreach ( $mps_subscriptions as $subscription ) { - if ( $subscription['id'] === $reepay_token && 'active' === $subscription['state'] ) { - return $subscription; + if ( ! empty( $result['mps_subscriptions'] ) ) { + $mps_subscriptions = $result['mps_subscriptions']; + foreach ( $mps_subscriptions as $subscription ) { + if ( ( $subscription['id'] === $reepay_token || $subscription['reference'] === $reepay_token ) && 'active' === $subscription['state'] ) { + return $subscription; + } } } diff --git a/includes/Tokens/Main.php b/includes/Tokens/Main.php index 183b98dd..4a1ec8df 100644 --- a/includes/Tokens/Main.php +++ b/includes/Tokens/Main.php @@ -59,8 +59,6 @@ public function add_reepay_cards_to_list( array $tokens, int $customer_id, strin * * @see WC_Payment_Tokens::get_customer_tokens */ - __log( '!1!' ); - __log( $tokens, $customer_id, $gateway_id ); if ( ! empty( $gateway_id ) && reepay()->gateways()->checkout()->id !== $gateway_id ) { return $tokens; diff --git a/includes/Tokens/TokenReepayTrait.php b/includes/Tokens/TokenReepayTrait.php index d28d816d..5a6b099d 100644 --- a/includes/Tokens/TokenReepayTrait.php +++ b/includes/Tokens/TokenReepayTrait.php @@ -129,6 +129,8 @@ public function add_payment_token_to_customer( int $customer_id, string $reepay_ $customer_handle = rp_get_customer_handle( $customer_id ); $card_info = reepay()->api( $this->id )->get_reepay_cards( $customer_handle, $reepay_token ); + $this->log( sprintf( 'Card receiving data: %s', wp_json_encode( $card_info, JSON_PRETTY_PRINT ) ) ); + if ( is_wp_error( $card_info ) || empty( $card_info ) ) { throw new Exception( __( 'Card not found', 'reepay-checkout-gateway' ) ); } @@ -136,7 +138,7 @@ public function add_payment_token_to_customer( int $customer_id, string $reepay_ if ( 'ms_' === substr( $card_info['id'], 0, 3 ) ) { $token = new TokenReepayMS(); $token->set_gateway_id( reepay()->gateways()->get_gateway( 'reepay_mobilepay_subscriptions' )->id ); - $token->set_token( $reepay_token ); + $token->set_token( $card_info['id'] ); $token->set_user_id( $customer_id ); } else { $expiry_date = explode( '-', $card_info['exp_date'] ); From 77e52977771cbb32d1eaa1f82cf5dfa7611a1e35 Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Fri, 28 Jul 2023 14:19:21 +0300 Subject: [PATCH 005/101] Test finalize --- .../unit/gatewaysTests/ReepayCheckoutTest.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tests/unit/gatewaysTests/ReepayCheckoutTest.php b/tests/unit/gatewaysTests/ReepayCheckoutTest.php index 8804dc1e..42fa1f73 100644 --- a/tests/unit/gatewaysTests/ReepayCheckoutTest.php +++ b/tests/unit/gatewaysTests/ReepayCheckoutTest.php @@ -6,11 +6,12 @@ */ use Reepay\Checkout\Gateways\ReepayCheckout; +use Reepay\Checkout\Tests\Helpers\Reepay_UnitTestCase; /** * AnydayTest. */ -class ReepayCheckoutTest extends WP_UnitTestCase { +class ReepayCheckoutTest extends Reepay_UnitTestCase { /** * ReepayCheckout * @@ -62,6 +63,7 @@ public function tear_down() { * ["ca_f73e13e5784a5dff32f2f93be7a8130f"] */ public function test_rp_card_store( string $token ) { + // Выяснить как делать запросы к апи в тестах или обойти их $this->expectException( Exception::class ); self::$gateway->add_payment_token_to_customer( $this->user->ID, $token ); } @@ -69,14 +71,20 @@ public function test_rp_card_store( string $token ) { /** * Test function test_rp_finalize * - * @param string $token amount for calculation. + * @param string $token card token from callback. * * @testWith * ["ca_f73e13e5784a5dff32f2f93be7a8130f"] */ - public function test_rp_finalize( string $token ) { - $this->expectException( Exception::class ); - self::$gateway->add_payment_token_to_customer( $this->user->ID, $token ); + public function test_rp_finalize( string $token ) { // Выяснить как делать запросы к апи в тестах или обойти их + $_GET['payment_method'] = $token; + + $this->order_generator->set_prop( 'payment_method', reepay()->gateways()->checkout() ); + + $_GET['key'] = $this->order_generator->order()->get_order_key(); + self::$gateway->reepay_finalize(); } + + } From 52996b4df077646ff174b6c1c852ea1af526a737 Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Mon, 7 Aug 2023 20:07:48 +0300 Subject: [PATCH 006/101] Gateway tests --- includes/Gateways/ReepayCheckout.php | 4 +- includes/Gateways/ReepayGateway.php | 2 +- includes/Tokens/Main.php | 62 +++++----- .../unit/gatewaysTests/ReepayCheckoutTest.php | 114 ++++++++++++++++-- 4 files changed, 137 insertions(+), 45 deletions(-) diff --git a/includes/Gateways/ReepayCheckout.php b/includes/Gateways/ReepayCheckout.php index 3b8fca64..9d71ebe9 100644 --- a/includes/Gateways/ReepayCheckout.php +++ b/includes/Gateways/ReepayCheckout.php @@ -731,10 +731,10 @@ public function reepay_finalize() { } } - wp_redirect( $this->get_return_url( $order ) ); + wp_safe_redirect( $this->get_return_url( $order ) ); } catch ( Exception $e ) { wc_add_notice( $e->getMessage(), 'error' ); - wp_redirect( $this->get_return_url() ); + wp_safe_redirect( $this->get_return_url() ); } exit(); diff --git a/includes/Gateways/ReepayGateway.php b/includes/Gateways/ReepayGateway.php index e1b1f51f..bbc3a327 100644 --- a/includes/Gateways/ReepayGateway.php +++ b/includes/Gateways/ReepayGateway.php @@ -335,7 +335,7 @@ public function reepay_card_store() { } /** - * Check if payment method activated in repay + * Check if payment method activated in reepay * * @return bool */ diff --git a/includes/Tokens/Main.php b/includes/Tokens/Main.php index 183b98dd..50bf0263 100644 --- a/includes/Tokens/Main.php +++ b/includes/Tokens/Main.php @@ -59,8 +59,6 @@ public function add_reepay_cards_to_list( array $tokens, int $customer_id, strin * * @see WC_Payment_Tokens::get_customer_tokens */ - __log( '!1!' ); - __log( $tokens, $customer_id, $gateway_id ); if ( ! empty( $gateway_id ) && reepay()->gateways()->checkout()->id !== $gateway_id ) { return $tokens; @@ -76,38 +74,40 @@ public function add_reepay_cards_to_list( array $tokens, int $customer_id, strin $tokens = array(); // Only Reepay tokens if Reepay gateway specified. } - foreach ( $reepay_cards as $card_info ) { - $token = TokenReepayTrait::get_payment_token( $card_info['id'] ); - - if ( empty( $token ) ) { - if ( 'ms_' === substr( $card_info['id'], 0, 3 ) ) { - $token = new TokenReepayMS(); - $token->set_gateway_id( $gateway_id ); - $token->set_token( $card_info['id'] ); - $token->set_user_id( $customer_id ); - $token->set_gateway_id( reepay()->gateways()->get_gateway( 'reepay_mobilepay_subscriptions' )->id ); - } else { - $expiry_date = explode( '-', $card_info['exp_date'] ); - - $token = new TokenReepay(); - $token->set_gateway_id( $gateway_id ); - $token->set_token( $card_info['id'] ); - $token->set_last4( substr( $card_info['masked_card'], - 4 ) ); - $token->set_expiry_year( 2000 + $expiry_date[1] ); - $token->set_expiry_month( $expiry_date[0] ); - $token->set_card_type( $card_info['card_type'] ); - $token->set_user_id( $customer_id ); - $token->set_masked_card( $card_info['masked_card'] ); - $token->set_gateway_id( reepay()->gateways()->checkout()->id ); + if ( ! empty( $reepay_cards[0] ) ) { + foreach ( $reepay_cards as $card_info ) { + $token = TokenReepayTrait::get_payment_token( $card_info['id'] ); + + if ( empty( $token ) ) { + if ( 'ms_' === substr( $card_info['id'], 0, 3 ) ) { + $token = new TokenReepayMS(); + $token->set_gateway_id( $gateway_id ); + $token->set_token( $card_info['id'] ); + $token->set_user_id( $customer_id ); + $token->set_gateway_id( reepay()->gateways()->get_gateway( 'reepay_mobilepay_subscriptions' )->id ); + } else { + $expiry_date = explode( '-', $card_info['exp_date'] ); + + $token = new TokenReepay(); + $token->set_gateway_id( $gateway_id ); + $token->set_token( $card_info['id'] ); + $token->set_last4( substr( $card_info['masked_card'], - 4 ) ); + $token->set_expiry_year( 2000 + $expiry_date[1] ); + $token->set_expiry_month( $expiry_date[0] ); + $token->set_card_type( $card_info['card_type'] ); + $token->set_user_id( $customer_id ); + $token->set_masked_card( $card_info['masked_card'] ); + $token->set_gateway_id( reepay()->gateways()->checkout()->id ); + } + + // Save Credit Card. + if ( ! $token->save() ) { + throw new Exception( __( 'There was a problem adding the card.', 'reepay-checkout-gateway' ) ); + } } - // Save Credit Card. - if ( ! $token->save() ) { - throw new Exception( __( 'There was a problem adding the card.', 'reepay-checkout-gateway' ) ); - } + $tokens[ $token->get_id() ] = $token; } - - $tokens[ $token->get_id() ] = $token; } return $tokens; diff --git a/tests/unit/gatewaysTests/ReepayCheckoutTest.php b/tests/unit/gatewaysTests/ReepayCheckoutTest.php index 42fa1f73..b9cbeb88 100644 --- a/tests/unit/gatewaysTests/ReepayCheckoutTest.php +++ b/tests/unit/gatewaysTests/ReepayCheckoutTest.php @@ -57,34 +57,126 @@ public function tear_down() { /** * Test function test_rp_card_store * - * @param string $token card token from callback. + * @param string $token card token. + * @param string $exp_date card expiry date. + * @param string $masked_card card number. + * @param string $card_type card type. * * @testWith - * ["ca_f73e13e5784a5dff32f2f93be7a8130f"] + * ["ca_f73e13e5784a5dff32f2f93be7a8130f", "05-43", "411111XXXXXX1111", "Visa"] */ - public function test_rp_card_store( string $token ) { - // Выяснить как делать запросы к апи в тестах или обойти их - $this->expectException( Exception::class ); - self::$gateway->add_payment_token_to_customer( $this->user->ID, $token ); + public function test_rp_card_store( string $token, string $exp_date, string $masked_card, string $card_type ) { + $this->api_mock->method( 'get_reepay_cards' )->willReturn( + array( + 'id' => $token, + 'exp_date' => $exp_date, + 'masked_card' => $masked_card, + 'card_type' => $card_type, + ) + ); + + $token_data = self::$gateway->add_payment_token_to_customer( $this->user->ID, $token ); + + $this->assertNotEmpty( + $token_data['token'], + 'Token is not exist' + ); + + $this->assertSame( + $token, + $token_data['token']->get_token() + ); + } + + /** + * Test function test_rp_card_store + * + * @param string $token card token. + * + * @testWith + * ["ms_4a9a9480922ab74ad8bb4a0b5856dc9f"] + */ + public function test_rp_card_store_mobilepay( string $token ) { + $this->api_mock->method( 'get_reepay_cards' )->willReturn( + array( + 'id' => $token, + ) + ); + + $token_data = self::$gateway->add_payment_token_to_customer( $this->user->ID, $token ); + + $this->assertNotEmpty( + $token_data['token'], + 'Token is not exist' + ); + + $this->assertSame( + $token, + $token_data['token']->get_token() + ); } /** * Test function test_rp_finalize * - * @param string $token card token from callback. + * @param string $token card token. + * @param string $exp_date card expiry date. + * @param string $masked_card card number. + * @param string $card_type card type. * * @testWith - * ["ca_f73e13e5784a5dff32f2f93be7a8130f"] + * ["ca_f73e13e5784a5dff32f2f93be7a8130f", "05-43", "411111XXXXXX1111", "Visa"] */ - public function test_rp_finalize( string $token ) { // Выяснить как делать запросы к апи в тестах или обойти их + public function test_rp_finalize( string $token, string $exp_date, string $masked_card, string $card_type ) { $_GET['payment_method'] = $token; $this->order_generator->set_prop( 'payment_method', reepay()->gateways()->checkout() ); + $this->api_mock->method( 'get_reepay_cards' )->willReturn( + array( + 'id' => $token, + 'exp_date' => $exp_date, + 'masked_card' => $masked_card, + 'card_type' => $card_type, + ) + ); + $_GET['key'] = $this->order_generator->order()->get_order_key(); - self::$gateway->reepay_finalize(); - } + $expected_error = 'Cannot modify header information'; + $this->expectError( $expected_error ); + self::$gateway->reepay_finalize(); + } + /** + * Test @see ReepayGateway::check_is_active + * + * @param string $token card token. + * + * @testWith + * ["ms_4a9a9480922ab74ad8bb4a0b5856dc9f"] + */ + public function test_rp_check_is_active( string $token ) { + self::$gateway->id = 'reepay_anyday'; + + $this->api_mock->method( 'request' )->willReturn( + array( + 'anyday', + 'applepay', + 'googlepay', + 'klarna_pay_later', + 'klarna_pay_now', + 'klarna_slice_it', + 'mobilepay', + 'mobilepay_subscriptions', + 'paypal', + 'checkout', + 'resurs', + 'swish', + 'viabill', + 'vipps', + ) + ); + } } From ef1c04381f547d7cf032c3cbebadea5ab3aca6a3 Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Wed, 16 Aug 2023 14:55:55 +0300 Subject: [PATCH 007/101] Gateways tests --- .../unit/gatewaysTests/ReepayCheckoutTest.php | 72 ++++++++++++++----- 1 file changed, 53 insertions(+), 19 deletions(-) diff --git a/tests/unit/gatewaysTests/ReepayCheckoutTest.php b/tests/unit/gatewaysTests/ReepayCheckoutTest.php index b9cbeb88..dafcf890 100644 --- a/tests/unit/gatewaysTests/ReepayCheckoutTest.php +++ b/tests/unit/gatewaysTests/ReepayCheckoutTest.php @@ -150,33 +150,67 @@ public function test_rp_finalize( string $token, string $exp_date, string $maske } /** - * Test @see ReepayGateway::check_is_active + * Test function rp_check_is_active * - * @param string $token card token. + * Test @param string $gateway gateway id. * * @testWith - * ["ms_4a9a9480922ab74ad8bb4a0b5856dc9f"] + * ["anyday"] + * ["applepay"] + * ["googlepay"] + * ["klarna_pay_later"] + * ["klarna_pay_now"] + * ["klarna_slice_it"] + * ["mobilepay"] + * ["mobilepay_subscriptions"] + * ["paypal"] + * ["checkout"] + * ["resurs"] + * ["swish"] + * ["viabill"] + * ["vipps"] + * @see ReepayGateway::check_is_active */ - public function test_rp_check_is_active( string $token ) { - self::$gateway->id = 'reepay_anyday'; + public function test_rp_check_is_active( string $gateway ) { + self::$gateway->id = 'reepay_' . $gateway; $this->api_mock->method( 'request' )->willReturn( array( - 'anyday', - 'applepay', - 'googlepay', - 'klarna_pay_later', - 'klarna_pay_now', - 'klarna_slice_it', - 'mobilepay', - 'mobilepay_subscriptions', - 'paypal', - 'checkout', - 'resurs', - 'swish', - 'viabill', - 'vipps', + array( + 'type' => $gateway, + ), ) ); + + $this->assertTrue( self::$gateway->check_is_active() ); + } + + /** + * Test function is_gateway_settings_page + * + * Test @param string $gateway gateway id. + * + * @testWith + * ["anyday"] + * ["applepay"] + * ["googlepay"] + * ["klarna_pay_later"] + * ["klarna_pay_now"] + * ["klarna_slice_it"] + * ["mobilepay"] + * ["mobilepay_subscriptions"] + * ["paypal"] + * ["checkout"] + * ["resurs"] + * ["swish"] + * ["viabill"] + * ["vipps"] + */ + public function test_rp_is_gateway_settings_page( string $gateway ) { + $_GET['tab'] = 'checkout'; + $_GET['section'] = $gateway; + self::$gateway->id = $gateway; + + $this->assertTrue( self::$gateway->is_gateway_settings_page() ); } } From ae91f9cd0c4fa9af2788c59e2ec3829e9daa14eb Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Fri, 25 Aug 2023 14:07:37 +0300 Subject: [PATCH 008/101] Tests --- includes/Gateways/ReepayGateway.php | 1 + .../unit/gatewaysTests/ReepayCheckoutTest.php | 76 +++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/includes/Gateways/ReepayGateway.php b/includes/Gateways/ReepayGateway.php index bbc3a327..40e87f8a 100644 --- a/includes/Gateways/ReepayGateway.php +++ b/includes/Gateways/ReepayGateway.php @@ -409,6 +409,7 @@ public static function get_webhook_url(): string { if ( class_exists( SitePress::class ) && ! is_multisite() ) { $languages = apply_filters( 'wpml_active_languages', null, 'orderby=id&order=desc' ); + var_dump($languages); $languages = wp_list_pluck( $languages, 'default_locale' ); if ( ! empty( $languages ) ) { diff --git a/tests/unit/gatewaysTests/ReepayCheckoutTest.php b/tests/unit/gatewaysTests/ReepayCheckoutTest.php index dafcf890..6fb4f377 100644 --- a/tests/unit/gatewaysTests/ReepayCheckoutTest.php +++ b/tests/unit/gatewaysTests/ReepayCheckoutTest.php @@ -213,4 +213,80 @@ public function test_rp_is_gateway_settings_page( string $gateway ) { $this->assertTrue( self::$gateway->is_gateway_settings_page() ); } + + /** + * Test function rp_get_account_info + * + * Test @param bool $is_test use test or live reepay api keys. + * Test @param string $handle handle of account. + * + * @testWith + * [true, "test-account-handle"] + * [false, "live-account-handle"] + * @see ReepayGateway::get_account_info + */ + public function test_rp_get_account_info( bool $is_test, string $handle ) { + $_GET['tab'] = 'checkout'; + $_GET['section'] = 'checkout'; + self::$gateway->id = 'checkout'; + + $result = array( + 'handle' => $handle, + ); + + $this->api_mock->method( 'request' )->willReturn( + $result + ); + + $this->assertSame( + $result, + self::$gateway->get_account_info( $is_test ) + ); + } + + /** + * Test function rp_get_webhook_url + * + * Test @param bool $is_test use test or live reepay api keys. + * Test @param string $handle handle of account. + * + * @see ReepayGateway::get_webhook_url + */ + /* + public function test_rp_get_webhook_url() { + self::$gateway->id = 'checkout'; + $this->getMockBuilder( SitePress::class )->getMock(); + + $time = $this->getFunctionMock( __NAMESPACE__, 'apply_filters' ); + + $url = self::$gateway::get_webhook_url(); + }*/ + + + /** + * Test function rp_is_webhook_configured + * + * @testWith + * ["http://example.com/wc-api/WC_Gateway_Reepay/"] + * ["http://example.com/wc-api/WC_Gateway_Reepay_Checkout/"] + * ["http://example.com/wc-api/not_configured/"] + * @see ReepayGateway::is_webhook_configured + */ + public function test_rp_is_webhook_configured( string $webhook_url ) { + self::$gateway->id = 'checkout'; + + $this->api_mock->method( 'request' )->willReturn( + array( + 'urls' => array( + $webhook_url, + ), + 'alert_emails' => array( + 'test@test.com', + ), + ) + ); + + var_dump( self::$gateway->is_webhook_configured() ); + } + } From 8c9199b33f35bcd821ffb6b4497a50ca96044f8d Mon Sep 17 00:00:00 2001 From: daniel21703 Date: Tue, 29 Aug 2023 11:39:56 +0300 Subject: [PATCH 009/101] update tokens --- includes/Actions/Subscriptions.php | 10 +-- includes/Tokens/ReepayTokens.php | 120 ++++++++++++++--------------- 2 files changed, 65 insertions(+), 65 deletions(-) diff --git a/includes/Actions/Subscriptions.php b/includes/Actions/Subscriptions.php index d0235630..50699609 100644 --- a/includes/Actions/Subscriptions.php +++ b/includes/Actions/Subscriptions.php @@ -119,11 +119,11 @@ public function add_subscription_card_id( int $order_id ) { continue; } - $token = ReepayTokens::get_payment_token_order( $subscription ); + $token = ReepayTokens::get_payment_token_by_order( $subscription ); if ( ! $token ) { // Copy tokens from parent order. $order = wc_get_order( $order_id ); - $token = ReepayTokens::get_payment_token_order( $order ); + $token = ReepayTokens::get_payment_token_by_order( $order ); if ( $token ) { ReepayTokens::assign_payment_token( $subscription, $token ); @@ -300,15 +300,15 @@ public function scheduled_subscription_payment( float $amount_to_charge, WC_Orde $gateway->log( 'WCS process scheduled payment' ); // Lookup token. try { - $token = ReepayTokens::get_payment_token_order( $renewal_order ); + $token = ReepayTokens::get_payment_token_by_order( $renewal_order ); // Try to find token in parent orders. if ( ! $token ) { // Get Subscriptions. $subscriptions = wcs_get_subscriptions_for_order( $renewal_order, array( 'order_type' => 'any' ) ); foreach ( $subscriptions as $subscription ) { - $token = ReepayTokens::get_payment_token_order( $subscription ); + $token = ReepayTokens::get_payment_token_by_order( $subscription ); if ( ! $token ) { - $token = ReepayTokens::get_payment_token_order( $subscription->get_parent() ); + $token = ReepayTokens::get_payment_token_by_order( $subscription->get_parent() ); } } } diff --git a/includes/Tokens/ReepayTokens.php b/includes/Tokens/ReepayTokens.php index c8392146..f79dbf64 100644 --- a/includes/Tokens/ReepayTokens.php +++ b/includes/Tokens/ReepayTokens.php @@ -25,7 +25,7 @@ abstract class ReepayTokens { * Assign payment token to order. * * @param WC_Order $order order to assign. - * @param WC_Payment_Token|int $token token to assign. + * @param TokenReepay|TokenReepayMS|int|string $token token class, token id or token string. * * @return void * @@ -33,8 +33,12 @@ abstract class ReepayTokens { */ public static function assign_payment_token( WC_Order $order, $token ) { if ( is_numeric( $token ) ) { - $token = new TokenReepay( $token ); - } elseif ( ! $token instanceof TokenReepay && ! $token instanceof TokenReepayMS ) { + $token = WC_Payment_Tokens::get( $token ); + } else if ( is_string( $token ) ) { + $token = self::get_payment_token( $token ); + } + + if ( ! $token instanceof TokenReepay && ! $token instanceof TokenReepayMS ) { throw new Exception( 'Invalid token parameter' ); } @@ -61,11 +65,11 @@ public static function assign_payment_token( WC_Order $order, $token ) { * @param WC_Order $order order to save. * @param string $reepay_token token to save. * - * @return bool|WC_Payment_Token + * @return WC_Payment_Token * * @throws Exception If invalid token or order. */ - public static function reepay_save_token( WC_Order $order, string $reepay_token ) { + public static function reepay_save_token( WC_Order $order, string $reepay_token ): ?WC_Payment_Token { // Check if token is exists in WooCommerce. $token = self::get_payment_token( $reepay_token ); @@ -83,28 +87,53 @@ public static function reepay_save_token( WC_Order $order, string $reepay_token /** * Save Payment Data (card type and masked card) * - * @param WC_Order $order order to save. - * @param string $reepay_token token to save. + * @param WC_Order $order order to save. + * @param string|array $card_info card token or card info. * * @throws Exception If invalid token or order. */ - public static function reepay_save_card_info( WC_Order $order, string $reepay_token ) { - $customer_handle = rp_get_customer_handle( $order->get_customer_id() ); - $card_info = reepay()->api( 'tokens' )->get_reepay_cards( $customer_handle, $reepay_token ); - - if ( is_wp_error( $card_info ) ) { - throw new Exception( $card_info->get_error_message() ); + public static function reepay_save_card_info( WC_Order $order, $card_info ) { + if( is_string( $card_info ) ) { + $customer_handle = rp_get_customer_handle( $order->get_customer_id() ); + $card_info = reepay()->api( 'tokens' )->get_reepay_cards( $customer_handle, $card_info ); + + if ( is_wp_error( $card_info ) ) { + throw new Exception( $card_info->get_error_message() ); + } } if ( ! empty( $card_info['masked_card'] ) ) { - update_post_meta( $order->get_id(), 'reepay_masked_card', $card_info['masked_card'] ); + $order->update_meta_data( 'reepay_masked_card', $card_info['masked_card'] ); } if ( ! empty( $card_info['card_type'] ) ) { - update_post_meta( $order->get_id(), 'reepay_card_type', $card_info['card_type'] ); + $order->update_meta_data( 'reepay_card_type', $card_info['card_type'] ); } - update_post_meta( $order->get_id(), '_reepay_source', $card_info ); + $order->update_meta_data( '_reepay_source', $card_info ); + $order->save_meta_data(); + } + + /** + * Add Payment Token. + * + * @param WC_Order $order order to add token. + * @param string $reepay_token token to add. + * + * @return WC_Payment_Token + * @throws Exception If invalid token or order. + */ + public static function add_payment_token_to_order( WC_Order $order, string $reepay_token ): WC_Payment_Token { + [ //phpcs:ignore Generic.Arrays.DisallowShortArraySyntax.Found + 'token' => $token, + 'card_info' => $card_info, + ] = self::add_payment_token_to_customer( $order->get_customer_id(), $reepay_token ); + + self::reepay_save_card_info( $order, $card_info ); + + self::assign_payment_token( $order, $token ); + + return $token; } /** @@ -130,23 +159,21 @@ public static function add_payment_token_to_customer( int $customer_id, string $ $token->set_token( $reepay_token ); $token->set_user_id( $customer_id ); } else { - $expiry_date = explode( '-', $card_info['exp_date'] ); - $token = new TokenReepay(); $token->set_gateway_id( reepay()->gateways()->checkout()->id ); $token->set_token( $reepay_token ); + $token->set_user_id( $customer_id ); + + $expiry_date = explode( '-', $card_info['exp_date'] ); + $token->set_last4( substr( $card_info['masked_card'], - 4 ) ); $token->set_expiry_year( 2000 + $expiry_date[1] ); $token->set_expiry_month( $expiry_date[0] ); $token->set_card_type( $card_info['card_type'] ); - $token->set_user_id( $customer_id ); $token->set_masked_card( $card_info['masked_card'] ); } - // Save Credit Card. - if ( ! $token->save() ) { - throw new Exception( __( 'There was a problem adding the card.', 'reepay-checkout-gateway' ) ); - } + $token->save(); return array( 'token' => $token, @@ -154,45 +181,16 @@ public static function add_payment_token_to_customer( int $customer_id, string $ ); } - /** - * Add Payment Token. - * - * @param WC_Order $order order to add token. - * @param string $reepay_token token to add. - * - * @return WC_Payment_Token - * @throws Exception If invalid token or order. - */ - public static function add_payment_token_to_order( WC_Order $order, string $reepay_token ): WC_Payment_Token { - [ //phpcs:ignore Generic.Arrays.DisallowShortArraySyntax.Found - 'token' => $token, - 'card_info' => $card_info, - ] = self::add_payment_token_to_customer( $order->get_customer_id(), $reepay_token ); - - if ( ! empty( $card_info['masked_card'] ) ) { - update_post_meta( $order->get_id(), 'reepay_masked_card', $card_info['masked_card'] ); - } - - if ( ! empty( $card_info['card_type'] ) ) { - update_post_meta( $order->get_id(), 'reepay_card_type', $card_info['card_type'] ); - } - - update_post_meta( $order->get_id(), '_reepay_source', $card_info ); - - self::assign_payment_token( $order, $token ); - - return $token; - } - /** * Get payment token. * * @param WC_Order $order order to get token. * - * @return bool|WC_Payment_Token|null + * @return bool|WC_Payment_Token */ - public static function get_payment_token_order( WC_Order $order ) { + public static function get_payment_token_by_order( WC_Order $order ) { $token = $order->get_meta( '_reepay_token' ); + if ( empty( $token ) ) { return false; } @@ -210,20 +208,24 @@ public static function get_payment_token_order( WC_Order $order ) { public static function get_payment_token( string $token ) { global $wpdb; + if( empty( $token ) ) { + return null; + } + $token_id = wp_cache_get( $token, 'reepay_tokens' ); if ( ! empty( $token_id ) ) { return WC_Payment_Tokens::get( $token_id ); } - $token_id = $wpdb->get_var( //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery + $token_id = (int) $wpdb->get_var( //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $wpdb->prepare( "SELECT token_id FROM {$wpdb->prefix}woocommerce_payment_tokens WHERE token = %s;", $token ) ); - if ( ! $token_id ) { + if ( empty( $token_id ) ) { return null; } @@ -246,9 +248,7 @@ public static function delete_card( WC_Payment_Token $token ): bool { return false; } - WC_Payment_Tokens::delete( $token->get_id() ); - - return true; + return $token->delete(); } /** From 27e45ddfb33da844cc3396b93e80147782566512 Mon Sep 17 00:00:00 2001 From: daniel21703 Date: Tue, 29 Aug 2023 11:40:05 +0300 Subject: [PATCH 010/101] add tokens test helper --- .../Reepay_UnitTestCase_Trait_Tokens.php | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 tests/helpers/Reepay_UnitTestCase_Trait_Tokens.php diff --git a/tests/helpers/Reepay_UnitTestCase_Trait_Tokens.php b/tests/helpers/Reepay_UnitTestCase_Trait_Tokens.php new file mode 100644 index 00000000..e61bffdd --- /dev/null +++ b/tests/helpers/Reepay_UnitTestCase_Trait_Tokens.php @@ -0,0 +1,79 @@ +generate_token_reepay( $args ); + } else if ( 'reepay_ms' === $type ) { + return $this->generate_token_reepay_ms( $args ); + } + + return $this->generate_token_simple(); + } + + private function generate_token_reepay( array $args = array() ): TokenReepay { + if( empty( $args['user_id'] ) ) { + $args['user_id'] = $this->generate_user_for_token(); + } + + $token = new TokenReepay(); + $token->set_gateway_id( reepay()->gateways()->checkout()->id ); + $token->set_token( $args['token'] ?? 'rp_1234567890' ); + $token->set_last4( $args['last4'] ?? '1234' ); + $token->set_expiry_year( $args['expiry_year'] ?? 2077 ); + $token->set_expiry_month( $args['expiry_month'] ?? 12 ); + $token->set_card_type( $args['card_type'] ?? 'reepay' ); + $token->set_user_id( $args['user_id'] ); + $token->set_masked_card( $args['masked_card'] ?? rp_format_credit_card( '1234567890123456' ) ); + $token->save(); + + return $token; + } + + private function generate_token_reepay_ms( array $args = array() ): TokenReepayMS { + if( empty( $args['user_id'] ) ) { + $args['user_id'] = $this->generate_user_for_token(); + } + + $token = new TokenReepayMS(); + $token->set_gateway_id( reepay()->gateways()->get_gateway( 'reepay_mobilepay_subscriptions' )->id ); + $token->set_token( $args['token'] ?? 'ms_1234567890' ); + $token->set_user_id( $args['user_id'] ); + $token->save(); + + return $token; + } + + private function generate_token_simple( array $args = array() ): WC_Payment_Token_CC { + if( empty( $args['user_id'] ) ) { + $args['user_id'] = $this->generate_user_for_token(); + } + + $token = new WC_Payment_Token_CC(); + + + return $token; + } + + private function generate_user_for_token(): int { + return $this->factory()->user->create_object( array( + 'user_login' => 'johndoe', + 'user_email' => 'mail@example.com', + 'user_pass' => 'password', + ) ); + } +} \ No newline at end of file From be8dbcafff250f27148bc5d52be8e8e8d20d4b61 Mon Sep 17 00:00:00 2001 From: daniel21703 Date: Tue, 29 Aug 2023 11:40:12 +0300 Subject: [PATCH 011/101] add tokens tests --- phpunit.xml | 3 + tests/unit/tokens/ReepayTokensTest.php | 285 +++++++++++++++++++++++++ 2 files changed, 288 insertions(+) create mode 100644 tests/unit/tokens/ReepayTokensTest.php diff --git a/phpunit.xml b/phpunit.xml index 58273eca..babcc513 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -19,6 +19,9 @@ ./tests/unit/orderFlow + + ./tests/unit/tokens + diff --git a/tests/unit/tokens/ReepayTokensTest.php b/tests/unit/tokens/ReepayTokensTest.php new file mode 100644 index 00000000..26af25c1 --- /dev/null +++ b/tests/unit/tokens/ReepayTokensTest.php @@ -0,0 +1,285 @@ +generate_token( $type ); + + if ( $expect_exception ) { + $this->expectException( Exception::class ); + } + + ReepayTokens::assign_payment_token( + $this->order_generator->order(), + $token_arg === 'object' ? + $token : + ( $token_arg === 'string' ? + $token->get_token() : + $token->get_id() ) + ); + + $this->order_generator->reset_order(); + + $this->assertSame( $token->get_id(), (int) $this->order_generator->get_meta( '_reepay_token_id' ) ); + $this->assertSame( array( $token->get_id() ), $this->order_generator->order()->get_payment_tokens() ); + $this->assertSame( $token->get_token(), $this->order_generator->get_meta( 'reepay_token' ) ); + $this->assertSame( $token->get_token(), $this->order_generator->get_meta( '_reepay_token' ) ); + } + + public function test_add_payment_token_to_customer_wp_error() { + $this->api_mock->method( 'get_reepay_cards' )->willReturn( new WP_Error() ); + $this->expectException( Exception::class ); + $this->expectExceptionMessage( 'Card not found' ); + ReepayTokens::add_payment_token_to_customer( 0, 'test' ); + } + + public function test_add_payment_token_to_customer_empty_cards() { + $this->api_mock->method( 'get_reepay_cards' )->willReturn( array() ); + $this->expectException( Exception::class ); + $this->expectExceptionMessage( 'Card not found' ); + ReepayTokens::add_payment_token_to_customer( 0, 'test' ); + } + + public function test_add_payment_token_unsaved() { + $this->api_mock->method( 'get_reepay_cards' )->willReturn( array( + 'id' => 'ms_123456789' + ) ); + $this->expectException( Exception::class ); + $this->expectExceptionMessage( 'Invalid or missing payment token fields.' ); + ReepayTokens::add_payment_token_to_customer( 0, '' ); + } + + public function test_add_payment_token_ms() { + $token_string = 'ms_123456789'; + + $api_response = array( + 'id' => $token_string + ); + $this->api_mock->method( 'get_reepay_cards' )->willReturn( $api_response ); + + /** + * @var TokenReepayMS $token + * @var array $card_info + */ + [ 'token' => $token, 'card_info' => $card_info ] = ReepayTokens::add_payment_token_to_customer( 0, $token_string ); + + + $this->assertInstanceOf( TokenReepayMS::class, $token ); + $this->assertSame( + reepay()->gateways()->get_gateway( 'reepay_mobilepay_subscriptions' )->id, + $token->get_gateway_id() + ); + $this->assertSame( $token_string, $token->get_token() ); + $this->assertSame( 0, $token->get_user_id() ); + + $this->assertSame( $api_response, $card_info ); + } + + public function test_add_payment_token_checkout() { + $api_response = array( + 'id' => 'ca_123456789', + 'exp_date' => '20-77', + 'masked_card' => '457111XXXXXX2077', + 'card_type' => 'visa_dk' + ); + $this->api_mock->method( 'get_reepay_cards' )->willReturn( $api_response ); + + /** + * @var TokenReepay $token + * @var array $card_info + */ + [ 'token' => $token, 'card_info' => $card_info ] = ReepayTokens::add_payment_token_to_customer( 0, $api_response['id'] ); + + + $this->assertInstanceOf( TokenReepay::class, $token ); + $this->assertSame( + reepay()->gateways()->checkout()->id, + $token->get_gateway_id() + ); + $this->assertSame( $api_response['id'], $token->get_token() ); + $this->assertSame( 0, $token->get_user_id() ); + + $expiry_date = explode( '-', $card_info['exp_date'] ); + + $this->assertSame( substr( $card_info['masked_card'], - 4 ), $token->get_last4() ); + $this->assertSame( 2000 + $expiry_date[1], $token->get_expiry_year() ); + $this->assertSame( $expiry_date[0], $token->get_expiry_month() ); + $this->assertSame( $card_info['card_type'], $token->get_card_type() ); + $this->assertSame( $card_info['masked_card'], $token->get_masked_card() ); + + $this->assertSame( $api_response, $card_info ); + } + + public function test_reepay_save_card_info() { + $api_response = array( + 'id' => 'ca_123456789', + 'exp_date' => '20-77', + 'masked_card' => '457111XXXXXX2077', + 'card_type' => 'visa_dk' + ); + + $this->api_mock->method( 'get_reepay_cards' )->willReturn( $api_response ); + + ReepayTokens::reepay_save_card_info( + $this->order_generator->order(), + $api_response['id'] + ); + + $this->order_generator->reset_order(); + + $this->assertSame( $api_response['masked_card'], $this->order_generator->get_meta('reepay_masked_card') ); + $this->assertSame( $api_response['card_type'], $this->order_generator->get_meta('reepay_card_type') ); + $this->assertSame( $api_response, $this->order_generator->get_meta('_reepay_source') ); + } + + /** + * @testWith + * [false, false] + * [false, true] + * [true, false] + * [true, true] + * + */ + public function test_get_payment_token_order( bool $generate_token, $add_token_to_order ) { + $token_string = 'rp_1234'; + + $token = null; + + if( $generate_token ) { + $token = $this->generate_token( 'reepay', array( + 'token' => $token_string + ) ); + } + + if( $add_token_to_order ) { + $this->order_generator->set_meta( '_reepay_token', $token_string ); + } + + $result_token = ReepayTokens::get_payment_token_by_order( $this->order_generator->order() ); + + $this->assertSame( + is_null( $token ) || ! $add_token_to_order ? false : $token->get_id(), + empty( $result_token ) ? false : $result_token->get_id() + ); + } + + /** + * Test @see ReepayTokens::get_payment_token with empty token string + */ + public function test_get_payment_token_with_empty_token_string() { + + $token_object = ReepayTokens::get_payment_token( "" ); + + $this->assertSame( null, $token_object); + } + + /** + * Test @see ReepayTokens::get_payment_token with undefined token + */ + public function test_get_payment_token_with_undefined_token() { + $token_object = ReepayTokens::get_payment_token( "rp_1" ); + + $this->assertSame( null, $token_object ); + } + + /** + * Test @see ReepayTokens::get_payment_token + */ + public function test_get_payment_token() { + $token_string = "rp_1"; + + $token_id = $this->generate_token( 'reepay', array( + 'token' => $token_string + ) )->get_id(); + + $token_object = ReepayTokens::get_payment_token( $token_string ); + + $this->assertSame( $token_id, $token_object->get_id() ); + + //Test cache set + $this->assertSame( $token_id ?: false, wp_cache_get( $token_string, 'reepay_tokens' ) ); + + //Test cache get + $num_queries = get_num_queries(); + + $token_object = ReepayTokens::get_payment_token( $token_string ); + + $this->assertSame( $token_id, $token_object->get_id() ); + $this->assertSame( 3, get_num_queries() - $num_queries ); // Token cached, so WC_Payment_Tokens::get make 3 requests + } + + /** + * Test @see ReepayTokens::delete_card + * + * @testWith + * [true, false] + * [false, true] + */ + public function test_delete_cart( bool $api_error, bool $result ) { + $this->api_mock->method( 'delete_payment_method' )->willReturn( $api_error ? new WP_Error() : array() ); + + $token = $this->generate_token( 'reepay' ); + + $this->assertSame( $result, ReepayTokens::delete_card( $token ) ); + + if ( ! $api_error ) { + $this->assertSame( 0, $token->get_id() ); + } + } + + /** + * Test @see ReepayTokens::is_reepay_token + * + * @param bool $generate_token + * + * @testWith + * [null, false] + * ["reepay", true] + * ["reepay_ms", true] + * ["simple", false] + */ + public function test_is_reepay_token( ?string $token_type, bool $result ) { + if( is_null( $token_type ) ) { + $token = null; + } else { + $token = $this->generate_token( $token_type ); + } + + $this->assertSame( $result, ReepayTokens::is_reepay_token( $token ) ); + } +} \ No newline at end of file From 3dbc741ee3beb2553d8a1b931c0c57566a45bc76 Mon Sep 17 00:00:00 2001 From: daniel21703 Date: Tue, 29 Aug 2023 15:17:39 +0300 Subject: [PATCH 012/101] add Tokens\Main tests --- tests/unit/tokens/MainTest.php | 230 +++++++++++++++++++++++++++++++++ 1 file changed, 230 insertions(+) create mode 100644 tests/unit/tokens/MainTest.php diff --git a/tests/unit/tokens/MainTest.php b/tests/unit/tokens/MainTest.php new file mode 100644 index 00000000..2eda7b1a --- /dev/null +++ b/tests/unit/tokens/MainTest.php @@ -0,0 +1,230 @@ +tokens_main = new Main(); + } + + /** + * Test @see Main::set_token_class_name + */ + public function test_set_token_class_name_checkout() { + $this->assertSame( + TokenReepay::class, + $this->tokens_main->set_token_class_name( 'WC_Payment_Token_Reepay' ) + ); + } + + /** + * Test @see Main::set_token_class_name + */ + public function test_set_token_class_name_ms() { + $this->assertSame( + TokenReepayMS::class, + $this->tokens_main->set_token_class_name( 'WC_Payment_Token_Reepay_MS' ) + ); + } + + /** + * Test @see Main::set_token_class_name + */ + public function test_set_token_class_name_other() { + $this->assertSame( + WC_Payment_Token_Other::class, + $this->tokens_main->set_token_class_name( 'WC_Payment_Token_Other' ) + ); + } + + /** + * Test @see Main::add_reepay_cards_to_list + */ + public function test_add_reepay_cards_to_list_with_non_reepay_gateway() { + $token = $this->generate_token( 'simple' ); + $tokens = array( $token->get_id() => $token ); + + $this->assertSame( + $tokens, + $this->tokens_main->add_reepay_cards_to_list( $tokens, 0, 'cod' ) + ); + } + + /** + * Test @see Main::add_reepay_cards_to_list + */ + public function test_add_reepay_cards_to_list_with_api_error() { + $this->api_mock->method( 'get_reepay_cards' )->willReturn( new WP_Error() ); + + $token = $this->generate_token( 'simple' ); + $tokens = array( $token->get_id() => $token ); + + $this->assertSame( + $tokens, + $this->tokens_main->add_reepay_cards_to_list( $tokens, 0, 'cod' ) + ); + } + + /** + * Test @see Main::add_reepay_cards_to_list + */ + public function test_add_reepay_cards_to_list_with_empty_api_cards() { + $this->api_mock->method( 'get_reepay_cards' )->willReturn( array() ); + + $token = $this->generate_token( 'simple' ); + $tokens = array( $token->get_id() => $token ); + + $this->assertSame( + $tokens, + $this->tokens_main->add_reepay_cards_to_list( $tokens, 0, 'cod' ) + ); + } + + + /** + * Test @see Main::add_reepay_cards_to_list + */ + public function test_add_reepay_cards_to_list_reepay_gateway_already_saved_token() { + $customer_id = $this->generate_user_for_token(); + + $token_reepay_string = 'rp_1234567890'; + $this->generate_token( 'reepay', array( + 'token' => $token_reepay_string, + 'user_id' => $customer_id + ) ); + + $api_response = array( + array( 'id' => $token_reepay_string ) + ); + $this->api_mock->method( 'get_reepay_cards' )->willReturn( $api_response ); + + $result_tokens = $this->tokens_main->add_reepay_cards_to_list( + array( + $this->generate_token( 'simple', array( 'user_id' => $customer_id ) ), + $this->generate_token( 'simple', array( 'user_id' => $customer_id ) ) + ), + 0, + reepay()->gateways()->checkout()->id + ); + + $this->assertSame( 1, count( $result_tokens ) ); + $result_token = current( $result_tokens ); + $this->assertInstanceOf( TokenReepay::class, $result_token ); + $this->assertSame( $token_reepay_string, $result_token->get_token() ); + $this->assertSame( 3, count( WC_Payment_Tokens::get_customer_tokens( $customer_id ) ) ); + } + + /** + * Test @see Main::add_reepay_cards_to_list + */ + public function test_add_reepay_cards_to_list_reepay_gateway_save_token() { + $customer_id = $this->generate_user_for_token(); + + $token_reepay_string = 'rp_1234567890'; + + $api_response = array( + array( + 'id' => $token_reepay_string, + 'exp_date' => '20-77', + 'masked_card' => '457111XXXXXX2077', + 'card_type' => 'visa_dk' + ) + ); + $this->api_mock->method( 'get_reepay_cards' )->willReturn( $api_response ); + + $result_tokens = $this->tokens_main->add_reepay_cards_to_list( + array( + $this->generate_token( 'simple', array( 'user_id' => $customer_id ) ), + $this->generate_token( 'simple', array( 'user_id' => $customer_id ) ) + ), + 0, + reepay()->gateways()->checkout()->id + ); + + $this->assertSame( 1, count( $result_tokens ) ); + $result_token = current( $result_tokens ); + $this->assertInstanceOf( TokenReepay::class, $result_token ); + $this->assertSame( $token_reepay_string, $result_token->get_token() ); + $this->assertSame( 3, count( WC_Payment_Tokens::get_customer_tokens( $customer_id ) ) ); + } + + /** + * Test @see Main::add_reepay_cards_to_list + */ + public function test_add_reepay_cards_to_list_ms_gateway_already_saved_token() { + $customer_id = $this->generate_user_for_token(); + + $token_reepay_string = 'ms_1234567890'; + $this->generate_token( 'reepay_ms', array( + 'token' => $token_reepay_string, + 'user_id' => $customer_id + ) ); + + $api_response = array( + array( 'id' => $token_reepay_string ) + ); + $this->api_mock->method( 'get_reepay_cards' )->willReturn( $api_response ); + + $result_tokens = $this->tokens_main->add_reepay_cards_to_list( + array( + $this->generate_token( 'simple', array( 'user_id' => $customer_id ) ), + $this->generate_token( 'simple', array( 'user_id' => $customer_id ) ) + ), + 0, + reepay()->gateways()->checkout()->id + ); + + $this->assertSame( 1, count( $result_tokens ) ); + $result_token = current( $result_tokens ); + $this->assertInstanceOf( TokenReepayMS::class, $result_token ); + $this->assertSame( $token_reepay_string, $result_token->get_token() ); + $this->assertSame( 3, count( WC_Payment_Tokens::get_customer_tokens( $customer_id ) ) ); + } + + /** + * Test @see Main::add_reepay_cards_to_list + */ + public function test_add_reepay_cards_to_list_ms_gateway_save_token() { + $customer_id = $this->generate_user_for_token(); + + $token_reepay_string = 'ms_1234567890'; + + $api_response = array( array( 'id' => $token_reepay_string ) ); + $this->api_mock->method( 'get_reepay_cards' )->willReturn( $api_response ); + + $result_tokens = $this->tokens_main->add_reepay_cards_to_list( + array( + $this->generate_token( 'simple', array( 'user_id' => $customer_id ) ), + $this->generate_token( 'simple', array( 'user_id' => $customer_id ) ) + ), + 0, + reepay()->gateways()->checkout()->id + ); + + $this->assertSame( 1, count( $result_tokens ) ); + $result_token = current( $result_tokens ); + $this->assertInstanceOf( TokenReepayMS::class, $result_token ); + $this->assertSame( $token_reepay_string, $result_token->get_token() ); + $this->assertSame( 3, count( WC_Payment_Tokens::get_customer_tokens( $customer_id ) ) ); + } +} \ No newline at end of file From 5c9b51901f45580a7d3cd1723f2302367848f045 Mon Sep 17 00:00:00 2001 From: daniel21703 Date: Tue, 29 Aug 2023 15:17:49 +0300 Subject: [PATCH 013/101] update ReepayTokens tests --- tests/unit/tokens/ReepayTokensTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/unit/tokens/ReepayTokensTest.php b/tests/unit/tokens/ReepayTokensTest.php index 26af25c1..11095832 100644 --- a/tests/unit/tokens/ReepayTokensTest.php +++ b/tests/unit/tokens/ReepayTokensTest.php @@ -77,7 +77,10 @@ public function test_add_payment_token_to_customer_empty_cards() { public function test_add_payment_token_unsaved() { $this->api_mock->method( 'get_reepay_cards' )->willReturn( array( - 'id' => 'ms_123456789' + 'id' => 'rp_123456789', + 'exp_date' => '20-77', + 'masked_card' => '457111XXXXXX2077', + 'card_type' => '' // Empty 'card_type' value cause an exception. ) ); $this->expectException( Exception::class ); $this->expectExceptionMessage( 'Invalid or missing payment token fields.' ); @@ -230,6 +233,7 @@ public function test_get_payment_token() { $token_object = ReepayTokens::get_payment_token( $token_string ); $this->assertSame( $token_id, $token_object->get_id() ); + $this->assertInstanceOf( TokenReepay::class, $token_object ); //Test cache set $this->assertSame( $token_id ?: false, wp_cache_get( $token_string, 'reepay_tokens' ) ); From 88c2627075ed1781dae0e7fc58b56b6a5ae9add6 Mon Sep 17 00:00:00 2001 From: daniel21703 Date: Tue, 29 Aug 2023 15:17:57 +0300 Subject: [PATCH 014/101] update Reepay_UnitTestCase_Trait_Tokens --- tests/helpers/Reepay_UnitTestCase_Trait_Tokens.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/helpers/Reepay_UnitTestCase_Trait_Tokens.php b/tests/helpers/Reepay_UnitTestCase_Trait_Tokens.php index e61bffdd..c19ba3c7 100644 --- a/tests/helpers/Reepay_UnitTestCase_Trait_Tokens.php +++ b/tests/helpers/Reepay_UnitTestCase_Trait_Tokens.php @@ -22,7 +22,7 @@ public function generate_token( string $type, array $args = array() ) { return $this->generate_token_reepay_ms( $args ); } - return $this->generate_token_simple(); + return $this->generate_token_simple( $args ); } private function generate_token_reepay( array $args = array() ): TokenReepay { @@ -64,12 +64,19 @@ private function generate_token_simple( array $args = array() ): WC_Payment_Toke } $token = new WC_Payment_Token_CC(); - + $token->set_gateway_id( 'cod' ); + $token->set_token( $args['token'] ?? 'cc_1234567890' ); + $token->set_last4( $args['last4'] ?? '1234' ); + $token->set_expiry_year( $args['expiry_year'] ?? 2077 ); + $token->set_expiry_month( $args['expiry_month'] ?? 12 ); + $token->set_card_type( $args['card_type'] ?? 'simple_woo' ); + $token->set_user_id( $args['user_id'] ); + $token->save(); return $token; } - private function generate_user_for_token(): int { + public function generate_user_for_token(): int { return $this->factory()->user->create_object( array( 'user_login' => 'johndoe', 'user_email' => 'mail@example.com', From ba4b769408bc97cdff2c306a95e9771a70bdc560 Mon Sep 17 00:00:00 2001 From: daniel21703 Date: Tue, 29 Aug 2023 15:18:11 +0300 Subject: [PATCH 015/101] update ReepayTokens --- includes/Tokens/ReepayTokens.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/includes/Tokens/ReepayTokens.php b/includes/Tokens/ReepayTokens.php index f79dbf64..f530fbca 100644 --- a/includes/Tokens/ReepayTokens.php +++ b/includes/Tokens/ReepayTokens.php @@ -98,7 +98,7 @@ public static function reepay_save_card_info( WC_Order $order, $card_info ) { $card_info = reepay()->api( 'tokens' )->get_reepay_cards( $customer_handle, $card_info ); if ( is_wp_error( $card_info ) ) { - throw new Exception( $card_info->get_error_message() ); + throw new Exception( __( 'Card not found', 'reepay-checkout-gateway' ) ); } } @@ -139,15 +139,17 @@ public static function add_payment_token_to_order( WC_Order $order, string $reep /** * Add payment token to customer * - * @param int $customer_id customer id to add token. - * @param string $reepay_token card token from reepay. + * @param int $customer_id customer id to add token. + * @param string|array $card_info card token or card info. * * @return array * @throws Exception If invalid token or order. */ - public static function add_payment_token_to_customer( int $customer_id, string $reepay_token ): array { - $customer_handle = rp_get_customer_handle( $customer_id ); - $card_info = reepay()->api( 'tokens' )->get_reepay_cards( $customer_handle, $reepay_token ); + public static function add_payment_token_to_customer( int $customer_id, $card_info ): array { + if ( is_string( $card_info ) ) { + $customer_handle = rp_get_customer_handle( $customer_id ); + $card_info = reepay()->api( 'tokens' )->get_reepay_cards( $customer_handle, $card_info ); + } if ( is_wp_error( $card_info ) || empty( $card_info ) ) { throw new Exception( __( 'Card not found', 'reepay-checkout-gateway' ) ); @@ -156,12 +158,12 @@ public static function add_payment_token_to_customer( int $customer_id, string $ if ( 'ms_' === substr( $card_info['id'], 0, 3 ) ) { $token = new TokenReepayMS(); $token->set_gateway_id( reepay()->gateways()->get_gateway( 'reepay_mobilepay_subscriptions' )->id ); - $token->set_token( $reepay_token ); + $token->set_token( $card_info['id'] ); $token->set_user_id( $customer_id ); } else { $token = new TokenReepay(); $token->set_gateway_id( reepay()->gateways()->checkout()->id ); - $token->set_token( $reepay_token ); + $token->set_token( $card_info['id'] ); $token->set_user_id( $customer_id ); $expiry_date = explode( '-', $card_info['exp_date'] ); From adf6ca16e6d5947649b4606403dfcafdd403c400 Mon Sep 17 00:00:00 2001 From: daniel21703 Date: Tue, 29 Aug 2023 15:18:24 +0300 Subject: [PATCH 016/101] update Tokens\Main --- includes/Tokens/Main.php | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/includes/Tokens/Main.php b/includes/Tokens/Main.php index 6991cea2..649f8f5e 100644 --- a/includes/Tokens/Main.php +++ b/includes/Tokens/Main.php @@ -22,7 +22,7 @@ public function __construct() { TokenReepay::register_view_actions(); TokenReepayMS::register_view_actions(); - add_action( 'woocommerce_payment_token_class', array( $this, 'set_token_class_name' ), 10, 1 ); + add_filter( 'woocommerce_payment_token_class', array( $this, 'set_token_class_name' ), 10, 1 ); add_filter( 'woocommerce_get_customer_payment_tokens', array( $this, 'add_reepay_cards_to_list' ), 1000, 3 ); } @@ -78,31 +78,7 @@ public function add_reepay_cards_to_list( array $tokens, int $customer_id, strin $token = ReepayTokens::get_payment_token( $card_info['id'] ); if ( empty( $token ) ) { - if ( 'ms_' === substr( $card_info['id'], 0, 3 ) ) { - $token = new TokenReepayMS(); - $token->set_gateway_id( $gateway_id ); - $token->set_token( $card_info['id'] ); - $token->set_user_id( $customer_id ); - $token->set_gateway_id( reepay()->gateways()->get_gateway( 'reepay_mobilepay_subscriptions' )->id ); - } else { - $expiry_date = explode( '-', $card_info['exp_date'] ); - - $token = new TokenReepay(); - $token->set_gateway_id( $gateway_id ); - $token->set_token( $card_info['id'] ); - $token->set_last4( substr( $card_info['masked_card'], - 4 ) ); - $token->set_expiry_year( 2000 + $expiry_date[1] ); - $token->set_expiry_month( $expiry_date[0] ); - $token->set_card_type( $card_info['card_type'] ); - $token->set_user_id( $customer_id ); - $token->set_masked_card( $card_info['masked_card'] ); - $token->set_gateway_id( reepay()->gateways()->checkout()->id ); - } - - // Save Credit Card. - if ( ! $token->save() ) { - throw new Exception( __( 'There was a problem adding the card.', 'reepay-checkout-gateway' ) ); - } + $token = ReepayTokens::add_payment_token_to_customer( $customer_id, $card_info )['token']; } $tokens[ $token->get_id() ] = $token; From 1ca377ba65ac4229af6e97468d2ce4b33a445f77 Mon Sep 17 00:00:00 2001 From: daniel21703 Date: Wed, 30 Aug 2023 11:30:45 +0300 Subject: [PATCH 017/101] Revert "remove error notice" This reverts commit 50ea0b1721e30e2d4cea14efa1f45ef5048b3d60. --- includes/OrderFlow/OrderCapture.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/OrderFlow/OrderCapture.php b/includes/OrderFlow/OrderCapture.php index 2438c82f..e97e792f 100644 --- a/includes/OrderFlow/OrderCapture.php +++ b/includes/OrderFlow/OrderCapture.php @@ -248,6 +248,7 @@ public function settle_items( WC_Order $order, array $items_data, float $total_a if ( is_wp_error( $result ) ) { rp_get_payment_method( $order )->log( sprintf( '%s Error: %s', __METHOD__, $result->get_error_message() ) ); + set_transient( 'reepay_api_action_error', $result->get_error_message(), MINUTE_IN_SECONDS / 2 ); return false; } @@ -316,6 +317,7 @@ public function settle_item( WC_Order_Item $item, WC_Order $order ): bool { if ( is_wp_error( $result ) ) { rp_get_payment_method( $order )->log( sprintf( '%s Error: %s', __METHOD__, $result->get_error_message() ) ); + set_transient( 'reepay_api_action_error', $result->get_error_message(), MINUTE_IN_SECONDS / 2 ); return false; } From dad898d29ff1664613fa1fb4b9cd7c6ca2350ad7 Mon Sep 17 00:00:00 2001 From: daniel21703 Date: Wed, 30 Aug 2023 15:43:28 +0300 Subject: [PATCH 018/101] Revert "phpcs fixes" This reverts commit faf4e1c07d215ce8d73bc1c5d8e31c53f14d5767. --- includes/Actions/Subscriptions.php | 4 ++-- includes/Api.php | 2 +- includes/Gateways/ReepayCheckout.php | 2 +- includes/OrderFlow/OrderCapture.php | 2 +- includes/Tokens/ReepayTokens.php | 5 ++--- templates/checkout/thankyou.php | 2 +- 6 files changed, 8 insertions(+), 9 deletions(-) diff --git a/includes/Actions/Subscriptions.php b/includes/Actions/Subscriptions.php index 77724f6d..2445221a 100644 --- a/includes/Actions/Subscriptions.php +++ b/includes/Actions/Subscriptions.php @@ -211,7 +211,7 @@ public function delete_resubscribe_meta( WC_Order $resubscribe_order ) { public function add_subscription_payment_meta( array $payment_meta, WC_Subscription $subscription ): array { $token = ReepayTokens::get_payment_token_subscription( $subscription ); - if ( ! empty( $token ) ) { + if(!empty($token)){ $token = $token->get_token(); } @@ -248,7 +248,7 @@ public function validate_subscription_payment_meta( string $payment_method_id, a throw new Exception( 'Only one "Billwerk+ Token" is allowed.' ); } - $token = ReepayTokens::get_payment_token( $tokens[0] ); + $token = ReepayTokens::get_payment_token( $tokens[0] ); if ( empty( $token ) ) { $token = ReepayTokens::add_payment_token_to_order( $subscription, $tokens[0] ); diff --git a/includes/Api.php b/includes/Api.php index 8e8c4333..eabf09e2 100644 --- a/includes/Api.php +++ b/includes/Api.php @@ -1062,7 +1062,7 @@ public function get_customer_handle_by_order( $order ) { if ( $order->get_customer_id() === 0 ) { $handle = $order->get_meta( '_reepay_customer' ); - if ( ! empty( $handle ) ) { + if(!empty($handle)){ return $handle; } } diff --git a/includes/Gateways/ReepayCheckout.php b/includes/Gateways/ReepayCheckout.php index f70de783..0d20bb20 100644 --- a/includes/Gateways/ReepayCheckout.php +++ b/includes/Gateways/ReepayCheckout.php @@ -77,7 +77,7 @@ public function __construct() { $this->language = $this->settings['language'] ?: $this->language; $this->save_cc = $this->settings['save_cc'] ?: $this->save_cc; $this->debug = $this->settings['debug'] ?: $this->debug; - $this->logos = (array) ( $this->settings['logos'] ?: $this->logos ); + $this->logos = (array) ($this->settings['logos'] ?: $this->logos); $this->payment_type = $this->settings['payment_type'] ?: $this->payment_type; $this->payment_methods = (array) ( $this->settings['payment_methods'] ?: $this->payment_methods ); $this->skip_order_lines = $this->settings['skip_order_lines'] ?: $this->skip_order_lines; diff --git a/includes/OrderFlow/OrderCapture.php b/includes/OrderFlow/OrderCapture.php index e97e792f..9ac2861b 100644 --- a/includes/OrderFlow/OrderCapture.php +++ b/includes/OrderFlow/OrderCapture.php @@ -216,7 +216,7 @@ public function multi_settle( WC_Order $order ): bool { if ( empty( $item->get_meta( 'settled' ) ) ) { $item_data = $this->get_item_data( $item, $order ); $total = $item_data['amount'] * $item_data['quantity']; - if ( 0 !== $total && $this->check_capture_allowed( $order ) ) { + if ( $total !== 0 && $this->check_capture_allowed( $order ) ) { $items_data[] = $item_data; $line_items[] = $item; $total_all += $total; diff --git a/includes/Tokens/ReepayTokens.php b/includes/Tokens/ReepayTokens.php index 7a60ba1e..fc12e9e6 100644 --- a/includes/Tokens/ReepayTokens.php +++ b/includes/Tokens/ReepayTokens.php @@ -90,9 +90,8 @@ public static function reepay_save_token( WC_Order $order, string $reepay_token * @throws Exception If invalid token or order. */ public static function reepay_save_card_info( WC_Order $order, string $reepay_token ) { - $customer_handle = reepay()->api( $order )->get_customer_handle_by_order( $order ); - - $card_info = reepay()->api( 'tokens' )->get_reepay_cards( $customer_handle, $reepay_token ); + $customer_handle = reepay()->api( $order )->get_customer_handle_by_order( $order );; + $card_info = reepay()->api( 'tokens' )->get_reepay_cards( $customer_handle, $reepay_token ); if ( is_wp_error( $card_info ) ) { throw new Exception( $card_info->get_error_message() ); diff --git a/templates/checkout/thankyou.php b/templates/checkout/thankyou.php index b3ef34a6..d24e444a 100644 --- a/templates/checkout/thankyou.php +++ b/templates/checkout/thankyou.php @@ -73,7 +73,7 @@