diff --git a/.editorconfig b/.editorconfig index 146b425f..61696372 100644 --- a/.editorconfig +++ b/.editorconfig @@ -35,7 +35,7 @@ ij_php_align_multiline_for = false ij_php_align_multiline_parameters = false ij_php_align_multiline_parameters_in_calls = false ij_php_align_multiline_ternary_operation = false -ij_php_align_phpdoc_comments = true +ij_php_align_phpdoc_comments = false ij_php_align_phpdoc_param_names = true ij_php_anonymous_brace_style = end_of_line ij_php_api_weight = 28 diff --git a/Readme.txt b/Readme.txt index 1365d607..00eedb53 100755 --- a/Readme.txt +++ b/Readme.txt @@ -18,6 +18,8 @@ The Billwerk+ Payments plugin extends WooCommerce allowing you to take payments See installation guide right here: https://docu.billwerk.plus/help/en/apps/woocommerce/setup-woocommerce-plugin.html == Changelog == +v 1.7.2 - Fixes, refactor, tests +v 1.7.1 - New methods add, WPML detect language support, change order calculation v 1.7.0 - Emoji in title clear, Payment method delete fixes, Fatal and warnings fixes v 1.6.4 - Fix settle string fatal error v 1.6.3 - Woocommerce zero payment fixes diff --git a/assets/images/bancontact.png b/assets/images/bancontact.png new file mode 100644 index 00000000..a05d07aa Binary files /dev/null and b/assets/images/bancontact.png differ diff --git a/assets/images/ideal.png b/assets/images/ideal.png new file mode 100644 index 00000000..f638fd02 Binary files /dev/null and b/assets/images/ideal.png differ diff --git a/assets/images/sepa.png b/assets/images/sepa.png new file mode 100644 index 00000000..f5aeaae2 Binary files /dev/null and b/assets/images/sepa.png differ diff --git a/assets/images/svg/eps.logo.svg b/assets/images/svg/eps.logo.svg new file mode 100644 index 00000000..ba6c159a --- /dev/null +++ b/assets/images/svg/eps.logo.svg @@ -0,0 +1,106 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/assets/images/svg/mbway.logo.svg b/assets/images/svg/mbway.logo.svg new file mode 100644 index 00000000..402fe23f --- /dev/null +++ b/assets/images/svg/mbway.logo.svg @@ -0,0 +1,113 @@ + + + + + + + + + + + + + + + + diff --git a/assets/images/svg/multibanco.logo.svg b/assets/images/svg/multibanco.logo.svg new file mode 100644 index 00000000..ebb888eb --- /dev/null +++ b/assets/images/svg/multibanco.logo.svg @@ -0,0 +1,180 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/assets/images/svg/mybank.logo.svg b/assets/images/svg/mybank.logo.svg new file mode 100644 index 00000000..27eb7d56 --- /dev/null +++ b/assets/images/svg/mybank.logo.svg @@ -0,0 +1,71 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/assets/images/svg/payconiq.logo.svg b/assets/images/svg/payconiq.logo.svg new file mode 100644 index 00000000..6bc8a20d --- /dev/null +++ b/assets/images/svg/payconiq.logo.svg @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + diff --git a/assets/images/svg/paysafecard.logo.svg b/assets/images/svg/paysafecard.logo.svg new file mode 100644 index 00000000..a1371ce5 --- /dev/null +++ b/assets/images/svg/paysafecard.logo.svg @@ -0,0 +1,113 @@ + + + + + + + + + + + + diff --git a/assets/images/svg/paysera.logo.svg b/assets/images/svg/paysera.logo.svg new file mode 100644 index 00000000..20236754 --- /dev/null +++ b/assets/images/svg/paysera.logo.svg @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/assets/images/svg/postfinance.logo.svg b/assets/images/svg/postfinance.logo.svg new file mode 100644 index 00000000..f5f2eda0 --- /dev/null +++ b/assets/images/svg/postfinance.logo.svg @@ -0,0 +1,76 @@ + + + + + + + + + + + + diff --git a/assets/images/svg/santander.logo.svg b/assets/images/svg/santander.logo.svg new file mode 100644 index 00000000..d8d26403 --- /dev/null +++ b/assets/images/svg/santander.logo.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/assets/images/svg/satispay.logo.svg b/assets/images/svg/satispay.logo.svg new file mode 100644 index 00000000..a795445a --- /dev/null +++ b/assets/images/svg/satispay.logo.svg @@ -0,0 +1,80 @@ + + + + + + + + + + + + diff --git a/assets/images/svg/trustly.logo.svg b/assets/images/svg/trustly.logo.svg new file mode 100644 index 00000000..fe0ef24b --- /dev/null +++ b/assets/images/svg/trustly.logo.svg @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + diff --git a/assets/images/svg/wechatpay.logo.svg b/assets/images/svg/wechatpay.logo.svg new file mode 100644 index 00000000..fd39c05d --- /dev/null +++ b/assets/images/svg/wechatpay.logo.svg @@ -0,0 +1,359 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/assets/js/admin.js b/assets/js/admin.js index 7863d5d7..887a0e7f 100755 --- a/assets/js/admin.js +++ b/assets/js/admin.js @@ -172,14 +172,21 @@ jQuery(document).ready(function ($) { }); $(document).on('click', '#woocommerce-order-actions .button', function (e) { - var order_id = $("#reepay_order_id").data('order-id'); - var amount = $('#reepay_order_total').data('order-total'); - var authorized = $('#reepay_order_total_authorized').val(); - var settled = $('#reepay_order_total_settled').val(); - var formatted_amount = $("#reepay_order_total").val(); - if (amount > 0 && settled < authorized && $("#order_status option:selected").val() == 'wc-completed') { + const orderId = $("#reepay_order_id").data('order-id') + const amount = $('#reepay_order_total').data('order-total') + const authorized = $('#reepay_order_total_authorized').val() + const $inputSettled = $('#reepay_order_total_settled') + const settled = $inputSettled.val() + const initialSettled = $inputSettled.data('initial-amount') + const currency = $('#reepay_currency').val() + const formatted_amount = parseFloat(amount) - parseFloat(initialSettled) + let $form = $('#post') + if ($form.length === 0) { + $form = $('#order') + } + if (amount > 0 && settled < authorized && $("#order_status option:selected").val() === 'wc-completed') { e.preventDefault(); - const settle = window.confirm('You are about to change the order status. Do you want to capture the remaining amount of ' + formatted_amount + ' at the same time? Click OK to continue with settle. Click Cancel to continue without settle.'); + const settle = window.confirm(`'You are about to change the order status. Do you want to capture the remaining amount of ${formatted_amount} ${currency} at the same time? Click OK to continue with settle. Click Cancel to continue without settle.'`) $.ajax({ url: Reepay_Admin.ajax_url, @@ -187,7 +194,7 @@ jQuery(document).ready(function ($) { data: { action: 'reepay_set_complete_settle_transient', nonce: Reepay_Admin.nonce, - order_id: order_id, + order_id: orderId, settle_order: Number(settle) }, beforeSend: function () { @@ -195,20 +202,20 @@ jQuery(document).ready(function ($) { success: function () { }, error: function (response) { - alert("error response: " + JSON.stringify(response)); + alert("error response: " + JSON.stringify(response)) }, complete: function () { - $('#post').submit(); + $form.submit() } - }); + }) } else { - $('#post').submit(); + $form.submit() } - }); + }) $('#reepay-capture_partly_amount-field, #reepay-refund_partly_amount-field').inputmask({ alias: "currency", groupSeparator: '' - }); -}); \ No newline at end of file + }) +}) \ No newline at end of file diff --git a/assets/js/thankyou.js b/assets/js/thankyou.js index 1135faf1..8cef5ddd 100644 --- a/assets/js/thankyou.js +++ b/assets/js/thankyou.js @@ -1,5 +1,5 @@ jQuery(function ($) { - 'use strict'; + 'use strict' window.wc_reepay_thankyou = { xhr: false, @@ -12,13 +12,13 @@ jQuery(function ($) { this.checkPayment(function (err, data) { var status_elm = $('#order-status-checking'), success_elm = $('#order-success'), - failed_elm = $('#order-failed'); + failed_elm = $('#order-failed') switch (data.state) { case 'paid': - status_elm.hide(); - success_elm.show(); - break; + status_elm.hide() + success_elm.show() + break case 'reload': $('.woocommerce-order').block({ message: WC_Reepay_Thankyou.check_message, @@ -26,30 +26,30 @@ jQuery(function ($) { background: '#fff', opacity: 0.6 } - }); + }) setTimeout(function () { - location.reload(); - }, 2000); + location.reload() + }, 2000) - break; + break case 'failed': case 'aborted': - status_elm.hide(); - failed_elm.append('

' + data.message + "

"); - failed_elm.show(); - break; + status_elm.hide() + failed_elm.append('

' + data.message + "

") + failed_elm.show() + break default: - window.wc_reepay_thankyou.attempts++; + window.wc_reepay_thankyou.attempts++ if (window.wc_reepay_thankyou.attempts > 6) { - return; + return } setTimeout(function () { - window.wc_reepay_thankyou.init(); - }, 10000); + window.wc_reepay_thankyou.init() + }, 10000) } - }); + }) }, /** @@ -63,7 +63,7 @@ jQuery(function ($) { background: '#fff', opacity: 0.6 } - }); + }) return $.ajax({ type: 'POST', @@ -76,14 +76,16 @@ jQuery(function ($) { }, dataType: 'json' }).always(function () { - $('.woocommerce-order').unblock(); + $('.woocommerce-order').unblock() }).done(function (response) { - callback(null, response.data); - }); + callback(null, response.data) + }) }, - }; + } $(document).ready(function () { - window.wc_reepay_thankyou.init(); - }); -}); + if($('.woocommerce-order--thankyou').length) { + window.wc_reepay_thankyou.init() + } + }) +}) diff --git a/composer.json b/composer.json index 43d34564..040800cc 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,8 @@ "psr-4": { "Reepay\\Checkout\\": "includes/", "Reepay\\Checkout\\Tests\\Helpers\\": "tests/helpers/", - "Reepay\\Checkout\\Tests\\Mocks\\": "tests/mocks/" + "Reepay\\Checkout\\Tests\\Mocks\\": "tests/mocks/", + "Reepay\\Checkout\\Tests\\Unit\\": "tests/unit/" } }, "authors": [ diff --git a/includes/Admin/MetaBoxes.php b/includes/Admin/MetaBoxes.php index 8505c48b..d62713f6 100644 --- a/includes/Admin/MetaBoxes.php +++ b/includes/Admin/MetaBoxes.php @@ -123,14 +123,14 @@ public function add_meta_boxes() { /** * Function to show customer meta box content * - * @param mixed $x not used. + * @param mixed $x not used. * @param array $meta additional info. Get arguments by 'args' key. */ public function generate_meta_box_content_customer( $x, array $meta ) { /** * Set types of args variables * - * @var WC_Order $order + * @var WC_Order $order * @var ReepayCheckout $gateway */ $order = $meta['args']['order']; @@ -147,8 +147,10 @@ public function generate_meta_box_content_customer( $x, array $meta ) { $handle = reepay()->api( $order )->get_customer_handle_by_order( $order->get_id() ); } + $user = get_user_by( 'id', $order->get_customer_id() ); + $template_args = array( - 'email' => $order->get_billing_email(), + 'email' => $user->get( 'user_email' ), 'handle' => $handle, 'link' => $this->dashboard_url . 'customers/customers/customer/' . $handle, ); @@ -162,14 +164,14 @@ public function generate_meta_box_content_customer( $x, array $meta ) { /** * Function to show customer meta box content * - * @param mixed $x not used. + * @param mixed $x not used. * @param array $meta additional info. Get arguments by 'args' key. */ public function generate_meta_box_content_invoice( $x, array $meta ) { /** * Set types of args variables * - * @var WC_Order $order + * @var WC_Order $order * @var ReepayCheckout $gateway */ $order = $meta['args']['order']; @@ -205,14 +207,14 @@ public function generate_meta_box_content_invoice( $x, array $meta ) { /** * Function to show customer meta box content * - * @param mixed $x not used. + * @param mixed $x not used. * @param array $meta additional info. Get arguments by 'args' key. */ public function generate_meta_box_content_subscription( $x, array $meta ) { /** * Set types of args variables * - * @var WC_Order $order + * @var WC_Order $order * @var ReepayCheckout $gateway */ $order = $meta['args']['order']; diff --git a/includes/Api.php b/includes/Api.php index 873dfc7a..736af930 100644 --- a/includes/Api.php +++ b/includes/Api.php @@ -12,6 +12,7 @@ use Reepay\Checkout\OrderFlow\InstantSettle; use Reepay\Checkout\OrderFlow\OrderCapture; use Reepay\Checkout\Actions\ReepayCustomer; +use Reepay\Checkout\Utils\LoggingTrait; use WC_Order; use Reepay\Checkout\OrderFlow\OrderStatuses; use WC_Order_Item; @@ -559,7 +560,7 @@ public function capture_payment( $order, $amount = null ) { * @return array|mixed|object|WP_Error * @see ReepayGateway::payment_methods. */ - public function recurring( array $payment_methods, WC_Order $order, array $data, $token = false, $payment_text = '' ) { + public function recurring( array $payment_methods, WC_Order $order, array $data, $token = false, string $payment_text = '' ) { $params = array( 'locale' => $data['language'], 'create_customer' => array( @@ -690,7 +691,7 @@ public function charge( WC_Order $order, string $token, ?float $amount = null, $ * * @ToDO refactor function. $amount is useless. */ - public function settle( WC_Order $order, $amount = null, $items_data = false, $line_item = false, bool $instant_note = false ) { + public function settle( WC_Order $order, $amount = null, $items_data = false, $line_item = false, bool $instant_note = true ) { $this->log( sprintf( 'Settle: %s, %s', $order->get_id(), $amount ) ); $handle = rp_get_order_handle( $order ); @@ -711,7 +712,7 @@ public function settle( WC_Order $order, $amount = null, $items_data = false, $l } if ( ! empty( $amount ) && reepay()->get_setting( 'skip_order_lines' ) === 'yes' ) { - $request_data['amount'] = $amount; + $request_data['amount'] = rp_prepare_amount( $amount, $order->get_currency() ); } else { $request_data['order_lines'] = $items_data; } @@ -736,7 +737,7 @@ public function settle( WC_Order $order, $amount = null, $items_data = false, $l $order_data = $this->get_invoice_data( $order ); $remaining = $order_data['authorized_amount'] - $order_data['settled_amount']; - if ( count( $request_data['order_lines'] ) > 1 && is_array( $line_item ) ) { + if ( ! empty( $request_data['order_lines'] ) && count( $request_data['order_lines'] ) > 1 && is_array( $line_item ) ) { $request_data['amount'] = $remaining; unset( $request_data['order_lines'] ); diff --git a/includes/Functions/Main.php b/includes/Functions/Main.php index fd8406e4..ba837838 100644 --- a/includes/Functions/Main.php +++ b/includes/Functions/Main.php @@ -26,5 +26,6 @@ public function __construct() { include_once __DIR__ . '/hpos.php'; include_once __DIR__ . '/order.php'; include_once __DIR__ . '/subscriptions.php'; + include_once __DIR__ . '/woo-product-bundle.php'; } } diff --git a/includes/Functions/currency.php b/includes/Functions/currency.php index c293d788..db0559c5 100644 --- a/includes/Functions/currency.php +++ b/includes/Functions/currency.php @@ -49,7 +49,7 @@ function rp_get_currency_multiplier( string $currency ): int { * key is currency value is minor units * for currencies that doesn't have minor units, value must be 1 * - * @var string[] + * @var string[] $currency_minor_units */ $currency_minor_units = array( 'ISK' => 1 ); diff --git a/includes/Functions/customer.php b/includes/Functions/customer.php index d1c24ce2..b5a4ef2e 100644 --- a/includes/Functions/customer.php +++ b/includes/Functions/customer.php @@ -6,6 +6,7 @@ */ use Reepay\Checkout\Actions\ReepayCustomer; +use Reepay\Checkout\Utils\TimeKeeper; defined( 'ABSPATH' ) || exit(); @@ -18,20 +19,19 @@ * @return string */ function rp_get_customer_handle( int $user_id ): string { + $user = get_user_by( 'id', $user_id ); $handle = get_user_meta( $user_id, 'reepay_customer_id', true ); if ( empty( $handle ) ) { $handle = ReepayCustomer::set_reepay_handle( $user_id ); if ( empty( $handle ) ) { - - if ( $user_id > 0 ) { + if ( false !== $user ) { $handle = 'customer-' . $user_id; + update_user_meta( $user_id, 'reepay_customer_id', $handle ); } else { - $handle = 'cust-' . time(); + $handle = 'cust-' . TimeKeeper::get(); } - - update_user_meta( $user_id, 'reepay_customer_id', $handle ); } } @@ -39,7 +39,7 @@ function rp_get_customer_handle( int $user_id ): string { } } -if ( ! function_exists( 'rp_get_userid_by_handle' ) ) { +if ( ! function_exists( 'rp_get_user_id_by_handle' ) ) { /** * Get user id by reepay user handle. * @@ -47,7 +47,7 @@ function rp_get_customer_handle( int $user_id ): string { * * @return int|false */ - function rp_get_userid_by_handle( string $handle ) { + function rp_get_user_id_by_handle( string $handle ) { if ( strpos( $handle, 'guest-' ) !== false ) { return 0; } diff --git a/includes/Functions/format.php b/includes/Functions/format.php index fc3419ee..fa4b7ee2 100644 --- a/includes/Functions/format.php +++ b/includes/Functions/format.php @@ -5,6 +5,8 @@ * @package Reepay\Checkout\Functions */ +use Reepay\Checkout\Utils\EmojiRemover; + defined( 'ABSPATH' ) || exit(); if ( ! function_exists( 'rp_format_credit_card' ) ) { @@ -13,9 +15,9 @@ * * @param string $cc is the card number to format nicely. * - * @return false|string the nicely formatted value + * @return string the nicely formatted value */ - function rp_format_credit_card( string $cc ) { + function rp_format_credit_card( string $cc ): string { $cc = str_replace( array( '-', ' ' ), '', $cc ); $cc_length = strlen( $cc ); $new_cc = substr( $cc, - 4 ); @@ -36,7 +38,9 @@ function rp_format_credit_card( string $cc ) { return $new_cc; } +} +if ( ! function_exists( 'rp_clear_ordertext' ) ) { /** * Clear product titles emoji * @@ -45,34 +49,6 @@ function rp_format_credit_card( string $cc ) { * @return string the nicely formatted value */ function rp_clear_ordertext( string $title ): string { - // Match Enclosed Alphanumeric Supplement. - $regex_alphanumeric = '/[\x{1F100}-\x{1F1FF}]/u'; - $clear_string = preg_replace( $regex_alphanumeric, '', $title ); - - // Match Miscellaneous Symbols and Pictographs. - $regex_symbols = '/[\x{1F300}-\x{1F5FF}]/u'; - $clear_string = preg_replace( $regex_symbols, '', $clear_string ); - - // Match Emoticons. - $regex_emoticons = '/[\x{1F600}-\x{1F64F}]/u'; - $clear_string = preg_replace( $regex_emoticons, '', $clear_string ); - - // Match Transport And Map Symbols. - $regex_transport = '/[\x{1F680}-\x{1F6FF}]/u'; - $clear_string = preg_replace( $regex_transport, '', $clear_string ); - - // Match Supplemental Symbols and Pictographs. - $regex_supplemental = '/[\x{1F900}-\x{1F9FF}]/u'; - $clear_string = preg_replace( $regex_supplemental, '', $clear_string ); - - // Match Miscellaneous Symbols. - $regex_misc = '/[\x{2600}-\x{26FF}]/u'; - $clear_string = preg_replace( $regex_misc, '', $clear_string ); - - // Match Dingbats. - $regex_dingbats = '/[\x{2700}-\x{27BF}]/u'; - $clear_string = preg_replace( $regex_dingbats, '', $clear_string ); - - return $clear_string; + return EmojiRemover::filter( $title ); } } diff --git a/includes/Functions/order.php b/includes/Functions/order.php index 99bdf9c2..a82d8f9e 100644 --- a/includes/Functions/order.php +++ b/includes/Functions/order.php @@ -5,6 +5,8 @@ * @package Reepay\Checkout\Functions */ +use Reepay\Checkout\Utils\TimeKeeper; + defined( 'ABSPATH' ) || exit(); if ( ! function_exists( 'rp_get_order_handle' ) ) { @@ -16,7 +18,8 @@ * * @return string */ - function rp_get_order_handle( WC_Order $order, $unique = false ): ?string { + function rp_get_order_handle( WC_Order $order, bool $unique = false ): ?string { + $current_time = TimeKeeper::get(); if ( $unique ) { $handle = null; $order->delete_meta_data( '_reepay_order' ); @@ -26,7 +29,7 @@ function rp_get_order_handle( WC_Order $order, $unique = false ): ?string { if ( empty( $handle ) ) { $handle = $unique ? - 'order-' . $order->get_order_number() . '-' . time() : + 'order-' . $order->get_order_number() . '-' . $current_time : 'order-' . $order->get_order_number(); $order->add_meta_data( '_reepay_order', $handle ); diff --git a/includes/Functions/woo-product-bundle.php b/includes/Functions/woo-product-bundle.php new file mode 100644 index 00000000..d69fa8fd --- /dev/null +++ b/includes/Functions/woo-product-bundle.php @@ -0,0 +1,38 @@ +supports = array( 'products', 'refunds', + 'add_payment_method', + 'tokenization', + 'subscriptions', + 'subscription_cancellation', + 'subscription_suspension', + 'subscription_reactivation', + 'subscription_amount_changes', + 'subscription_date_changes', + 'subscription_payment_method_change', + 'subscription_payment_method_change_customer', + 'subscription_payment_method_change_admin', + 'multiple_subscriptions', ); $this->logos = array( 'applepay' ); @@ -55,6 +67,9 @@ public function __construct() { if ( 'yes' === $this->enabled ) { add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_additional_assets' ), 10000 ); } + + add_action( 'wp_ajax_reepay_card_store_' . $this->id, array( $this, 'reepay_card_store' ) ); + add_action( 'wp_ajax_nopriv_reepay_card_store_' . $this->id, array( $this, 'reepay_card_store' ) ); } /** @@ -74,4 +89,17 @@ public function enqueue_additional_assets() { " ); } + + /** + * If There are no payment fields show the description if set. + * + * @return void + */ + public function payment_fields() { + parent::payment_fields(); + + $this->tokenization_script(); + $this->saved_payment_methods(); + $this->save_payment_method_checkbox(); + } } diff --git a/includes/Gateways/KlarnaDBT.php b/includes/Gateways/KlarnaDBT.php new file mode 100644 index 00000000..11c70fe0 --- /dev/null +++ b/includes/Gateways/KlarnaDBT.php @@ -0,0 +1,53 @@ +id = 'reepay_klarna_direct_bank_transfer'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Klarna Direct Bank Transfer', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + $this->logos = array( 'klarna' ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/KlarnaDD.php b/includes/Gateways/KlarnaDD.php new file mode 100644 index 00000000..a84257f9 --- /dev/null +++ b/includes/Gateways/KlarnaDD.php @@ -0,0 +1,53 @@ +id = 'reepay_klarna_direct_debit'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Klarna Direct Debit', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + $this->logos = array( 'klarna' ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/MobilepaySubscriptions.php b/includes/Gateways/MobilepaySubscriptions.php index 175bed73..b116c260 100644 --- a/includes/Gateways/MobilepaySubscriptions.php +++ b/includes/Gateways/MobilepaySubscriptions.php @@ -90,7 +90,6 @@ public function payment_fields() { parent::payment_fields(); $this->tokenization_script(); - $this->saved_payment_methods(); $this->save_payment_method_checkbox(); } } diff --git a/includes/Gateways/OfflineCash.php b/includes/Gateways/OfflineCash.php new file mode 100644 index 00000000..4b2465cd --- /dev/null +++ b/includes/Gateways/OfflineCash.php @@ -0,0 +1,82 @@ +id = 'reepay_offline_cash'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Cash', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + 'add_payment_method', + 'tokenization', + 'subscriptions', + 'subscription_cancellation', + 'subscription_suspension', + 'subscription_reactivation', + 'subscription_amount_changes', + 'subscription_date_changes', + 'subscription_payment_method_change', + 'subscription_payment_method_change_customer', + 'subscription_payment_method_change_admin', + 'multiple_subscriptions', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + + add_action( 'wp_ajax_reepay_card_store_' . $this->id, array( $this, 'reepay_card_store' ) ); + add_action( 'wp_ajax_nopriv_reepay_card_store_' . $this->id, array( $this, 'reepay_card_store' ) ); + } + + + + /** + * If There are no payment fields show the description if set. + * + * @return void + */ + public function payment_fields() { + parent::payment_fields(); + + $this->tokenization_script(); + $this->saved_payment_methods(); + $this->save_payment_method_checkbox(); + } +} diff --git a/includes/Gateways/OfflineTransfer.php b/includes/Gateways/OfflineTransfer.php new file mode 100644 index 00000000..fb15a76f --- /dev/null +++ b/includes/Gateways/OfflineTransfer.php @@ -0,0 +1,82 @@ +id = 'reepay_offline_bank_transfer'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Bank Transfer', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + 'add_payment_method', + 'tokenization', + 'subscriptions', + 'subscription_cancellation', + 'subscription_suspension', + 'subscription_reactivation', + 'subscription_amount_changes', + 'subscription_date_changes', + 'subscription_payment_method_change', + 'subscription_payment_method_change_customer', + 'subscription_payment_method_change_admin', + 'multiple_subscriptions', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + + add_action( 'wp_ajax_reepay_card_store_' . $this->id, array( $this, 'reepay_card_store' ) ); + add_action( 'wp_ajax_nopriv_reepay_card_store_' . $this->id, array( $this, 'reepay_card_store' ) ); + } + + + + /** + * If There are no payment fields show the description if set. + * + * @return void + */ + public function payment_fields() { + parent::payment_fields(); + + $this->tokenization_script(); + $this->saved_payment_methods(); + $this->save_payment_method_checkbox(); + } +} diff --git a/includes/Gateways/Other.php b/includes/Gateways/Other.php new file mode 100644 index 00000000..d3e77748 --- /dev/null +++ b/includes/Gateways/Other.php @@ -0,0 +1,82 @@ +id = 'reepay_offline_other'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Other', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + 'add_payment_method', + 'tokenization', + 'subscriptions', + 'subscription_cancellation', + 'subscription_suspension', + 'subscription_reactivation', + 'subscription_amount_changes', + 'subscription_date_changes', + 'subscription_payment_method_change', + 'subscription_payment_method_change_customer', + 'subscription_payment_method_change_admin', + 'multiple_subscriptions', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + + add_action( 'wp_ajax_reepay_card_store_' . $this->id, array( $this, 'reepay_card_store' ) ); + add_action( 'wp_ajax_nopriv_reepay_card_store_' . $this->id, array( $this, 'reepay_card_store' ) ); + } + + + + /** + * If There are no payment fields show the description if set. + * + * @return void + */ + public function payment_fields() { + parent::payment_fields(); + + $this->tokenization_script(); + $this->saved_payment_methods(); + $this->save_payment_method_checkbox(); + } +} diff --git a/includes/Gateways/PPBancontact.php b/includes/Gateways/PPBancontact.php new file mode 100644 index 00000000..66a0ab8c --- /dev/null +++ b/includes/Gateways/PPBancontact.php @@ -0,0 +1,53 @@ +id = 'reepay_pp_bancontact'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Bancontact', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + $this->logos = array( 'bancontact' ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPBlik.php b/includes/Gateways/PPBlik.php new file mode 100644 index 00000000..1256cd9a --- /dev/null +++ b/includes/Gateways/PPBlik.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_blik_oc'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - BLIK', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPEps.php b/includes/Gateways/PPEps.php new file mode 100644 index 00000000..6b86858a --- /dev/null +++ b/includes/Gateways/PPEps.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_eps'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - EPS', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPEstonianBanks.php b/includes/Gateways/PPEstonianBanks.php new file mode 100644 index 00000000..d31879fa --- /dev/null +++ b/includes/Gateways/PPEstonianBanks.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_estonia_banks'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Estonian Banks', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPGiroPay.php b/includes/Gateways/PPGiroPay.php new file mode 100644 index 00000000..373a0bf2 --- /dev/null +++ b/includes/Gateways/PPGiroPay.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_giropay'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - GiroPay', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPIdeal.php b/includes/Gateways/PPIdeal.php new file mode 100644 index 00000000..240a8b44 --- /dev/null +++ b/includes/Gateways/PPIdeal.php @@ -0,0 +1,85 @@ +id = 'reepay_pp_ideal'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - iDEAL', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + 'add_payment_method', + 'tokenization', + 'subscriptions', + 'subscription_cancellation', + 'subscription_suspension', + 'subscription_reactivation', + 'subscription_amount_changes', + 'subscription_date_changes', + 'subscription_payment_method_change', + 'subscription_payment_method_change_customer', + 'subscription_payment_method_change_admin', + 'multiple_subscriptions', + ); + $this->logos = array( 'ideal' ); + $this->supported_currencies = array( 'EUR' ); + + parent::__construct(); + + $this->apply_parent_settings(); + + add_action( 'wp_ajax_reepay_card_store_' . $this->id, array( $this, 'reepay_card_store' ) ); + add_action( 'wp_ajax_nopriv_reepay_card_store_' . $this->id, array( $this, 'reepay_card_store' ) ); + add_filter( 'woocommerce_available_payment_gateways', array( $this, 'exclude_payment_gateway_based_on_currency' ) ); + } + + /** + * If There are no payment fields show the description if set. + * + * @return void + */ + public function payment_fields() { + parent::payment_fields(); + + $this->tokenization_script(); + $this->saved_payment_methods(); + $this->save_payment_method_checkbox(); + } +} diff --git a/includes/Gateways/PPLatvianBanks.php b/includes/Gateways/PPLatvianBanks.php new file mode 100644 index 00000000..c1842705 --- /dev/null +++ b/includes/Gateways/PPLatvianBanks.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_latvia_banks'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Latvian Banks', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPLithuanianBanks.php b/includes/Gateways/PPLithuanianBanks.php new file mode 100644 index 00000000..bc6d3b5f --- /dev/null +++ b/includes/Gateways/PPLithuanianBanks.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_lithuania_banks'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Lithuanian Banks', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPMBWay.php b/includes/Gateways/PPMBWay.php new file mode 100644 index 00000000..8e95116b --- /dev/null +++ b/includes/Gateways/PPMBWay.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_mb_way'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - MB Way', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPMultibanco.php b/includes/Gateways/PPMultibanco.php new file mode 100644 index 00000000..e4048d59 --- /dev/null +++ b/includes/Gateways/PPMultibanco.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_multibanco'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Multibanco', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPMybank.php b/includes/Gateways/PPMybank.php new file mode 100644 index 00000000..41a63c13 --- /dev/null +++ b/includes/Gateways/PPMybank.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_mybank'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - MBank', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPPaySafeCard.php b/includes/Gateways/PPPaySafeCard.php new file mode 100644 index 00000000..4e27ce07 --- /dev/null +++ b/includes/Gateways/PPPaySafeCard.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_paysafecard'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Paysafecard', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPPaycoinq.php b/includes/Gateways/PPPaycoinq.php new file mode 100644 index 00000000..49e0f262 --- /dev/null +++ b/includes/Gateways/PPPaycoinq.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_paycoinq'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Paycoinq', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPPaysera.php b/includes/Gateways/PPPaysera.php new file mode 100644 index 00000000..7f05de00 --- /dev/null +++ b/includes/Gateways/PPPaysera.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_paysera'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Paysera', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPPostFinance.php b/includes/Gateways/PPPostFinance.php new file mode 100644 index 00000000..f6ec2796 --- /dev/null +++ b/includes/Gateways/PPPostFinance.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_postfinance'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - PostFinance', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPPr24.php b/includes/Gateways/PPPr24.php new file mode 100644 index 00000000..ec58e708 --- /dev/null +++ b/includes/Gateways/PPPr24.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_p24'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Przelewy24', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPSantander.php b/includes/Gateways/PPSantander.php new file mode 100644 index 00000000..60ccac88 --- /dev/null +++ b/includes/Gateways/PPSantander.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_santander'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Santander', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPSatisPay.php b/includes/Gateways/PPSatisPay.php new file mode 100644 index 00000000..d1012700 --- /dev/null +++ b/includes/Gateways/PPSatisPay.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_satispay'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Satisfy', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPSepa.php b/includes/Gateways/PPSepa.php new file mode 100644 index 00000000..0f3c7c9e --- /dev/null +++ b/includes/Gateways/PPSepa.php @@ -0,0 +1,87 @@ +id = 'reepay_pp_sepa'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - SEPA Direct Debit', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + 'add_payment_method', + 'tokenization', + 'subscriptions', + 'subscription_cancellation', + 'subscription_suspension', + 'subscription_reactivation', + 'subscription_amount_changes', + 'subscription_date_changes', + 'subscription_payment_method_change', + 'subscription_payment_method_change_customer', + 'subscription_payment_method_change_admin', + 'multiple_subscriptions', + ); + $this->logos = array( 'sepa' ); + $this->supported_currencies = array( 'EUR' ); + + parent::__construct(); + + $this->apply_parent_settings(); + + add_action( 'wp_ajax_reepay_card_store_' . $this->id, array( $this, 'reepay_card_store' ) ); + add_action( 'wp_ajax_nopriv_reepay_card_store_' . $this->id, array( $this, 'reepay_card_store' ) ); + add_filter( 'woocommerce_available_payment_gateways', array( $this, 'exclude_payment_gateway_based_on_currency' ) ); + } + + + + /** + * If There are no payment fields show the description if set. + * + * @return void + */ + public function payment_fields() { + parent::payment_fields(); + + $this->tokenization_script(); + $this->saved_payment_methods(); + $this->save_payment_method_checkbox(); + } +} diff --git a/includes/Gateways/PPTrustly.php b/includes/Gateways/PPTrustly.php new file mode 100644 index 00000000..10dc4938 --- /dev/null +++ b/includes/Gateways/PPTrustly.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_trustly'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Trustly', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPVerkkoPankki.php b/includes/Gateways/PPVerkkoPankki.php new file mode 100644 index 00000000..7058499f --- /dev/null +++ b/includes/Gateways/PPVerkkoPankki.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_verkkopankki'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Finland Banks', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/PPWeChatPay.php b/includes/Gateways/PPWeChatPay.php new file mode 100644 index 00000000..d3f3bc9f --- /dev/null +++ b/includes/Gateways/PPWeChatPay.php @@ -0,0 +1,52 @@ +id = 'reepay_pp_wechatpay'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - WeChat Pay', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + ); + + parent::__construct(); + + $this->apply_parent_settings(); + } +} diff --git a/includes/Gateways/ReepayCheckout.php b/includes/Gateways/ReepayCheckout.php index d7f7899b..d71c5493 100644 --- a/includes/Gateways/ReepayCheckout.php +++ b/includes/Gateways/ReepayCheckout.php @@ -10,7 +10,7 @@ use Exception; use Reepay\Checkout\Api; -use Reepay\Checkout\LoggingTrait; +use Reepay\Checkout\Utils\LoggingTrait; use Reepay\Checkout\Plugin\Statistics; use Reepay\Checkout\Tokens\ReepayTokens; use Reepay\Checkout\Tokens\TokenReepay; @@ -70,18 +70,19 @@ public function __construct() { parent::__construct(); - $this->private_key = apply_filters( 'woocommerce_reepay_private_key', $this->settings['private_key'] ?: $this->private_key ); - $this->private_key_test = apply_filters( 'woocommerce_reepay_private_key_test', $this->settings['private_key_test'] ?: $this->private_key_test ); - $this->test_mode = $this->settings['test_mode'] ?: $this->test_mode; - $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 = $this->settings['logos'] ?: $this->logos; - $this->payment_type = $this->settings['payment_type'] ?: $this->payment_type; - $this->payment_methods = $this->settings['payment_methods'] ?: $this->payment_methods; - $this->skip_order_lines = $this->settings['skip_order_lines'] ?: $this->skip_order_lines; - $this->failed_webhooks_email = $this->settings['failed_webhooks_email'] ?: $this->failed_webhooks_email; - $this->handle_failover = ! empty( $this->settings['handle_failover'] ) ?: $this->handle_failover; + $this->private_key = apply_filters( 'woocommerce_reepay_private_key', $this->settings['private_key'] ?: $this->private_key ); + $this->private_key_test = apply_filters( 'woocommerce_reepay_private_key_test', $this->settings['private_key_test'] ?: $this->private_key_test ); + $this->test_mode = $this->settings['test_mode'] ?: $this->test_mode; + $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 = $this->settings['logos'] ?: $this->logos; + $this->payment_type = $this->settings['payment_type'] ?: $this->payment_type; + $this->payment_methods = $this->settings['payment_methods'] ?: $this->payment_methods; + $this->skip_order_lines = $this->settings['skip_order_lines'] ?: $this->skip_order_lines; + $this->failed_webhooks_email = $this->settings['failed_webhooks_email'] ?: $this->failed_webhooks_email; + $this->handle_failover = ! empty( $this->settings['handle_failover'] ) ?: $this->handle_failover; + $this->enable_order_autocancel = $this->settings['enable_order_autocancel'] ?: $this->enable_order_autocancel; if ( ! empty( $this->settings['settle'] ) && is_array( $this->settings['settle'] ) ) { $this->settle = $this->settings['settle']; @@ -383,6 +384,16 @@ public function init_form_fields() { ), 'default' => 'no', ), + 'enable_order_autocancel' => array( + 'title' => __( 'The automatic order auto-cancel', 'reepay-checkout-gateway' ), + 'description' => __( 'The automatic order auto-cancel', 'reepay-checkout-gateway' ), + 'type' => 'select', + 'options' => array( + 'yes' => 'Enable auto-cancel', + 'no' => 'Ignore / disable auto-cancel', + ), + 'default' => 'no', + ), 'payment_button_text' => array( 'title' => __( 'Payment button text', 'reepay-checkout-gateway' ), 'type' => 'text', diff --git a/includes/Gateways/ReepayGateway.php b/includes/Gateways/ReepayGateway.php index ada81293..c21f14cf 100644 --- a/includes/Gateways/ReepayGateway.php +++ b/includes/Gateways/ReepayGateway.php @@ -9,8 +9,9 @@ use Exception; use Reepay\Checkout\Api; +use Reepay\Checkout\Integrations\PWGiftCardsIntegration; use SitePress; -use Reepay\Checkout\LoggingTrait; +use Reepay\Checkout\Utils\LoggingTrait; use Reepay\Checkout\Tokens\TokenReepay; use Reepay\Checkout\Tokens\ReepayTokens; use WC_Admin_Settings; @@ -91,7 +92,7 @@ abstract class ReepayGateway extends WC_Payment_Gateway { * * @var string */ - public string $language = 'en_US'; + public string $language = ''; /** * Available payment logos @@ -135,6 +136,13 @@ abstract class ReepayGateway extends WC_Payment_Gateway { */ public string $skip_order_lines = 'no'; + /** + * If automatically cancel unpaid orders should be ignored + * + * @var string + */ + public string $enable_order_autocancel = 'no'; + /** * Email address for notification about failed webhooks * @@ -163,6 +171,21 @@ abstract class ReepayGateway extends WC_Payment_Gateway { */ private string $logging_source; + /** + * Currencies for which payment method is not displayed + * (If $supported_currencies is empty) + * + * @var string[] + */ + protected array $unsupported_currencies = array(); + + /** + * Show payment method only for these currencies + * + * @var string[] + */ + protected array $supported_currencies = array(); + /** * ReepayGateway constructor. */ @@ -250,8 +273,8 @@ public function add_payment_method() { $result = reepay()->api( $this )->request( 'POST', 'https://checkout-api.reepay.com/v1/session/recurring', $params ); if ( is_wp_error( $result ) ) { - if ( $result->get_error_code() === API::ERROR_CODES['Customer has been deleted'] || - $result->get_error_code() === API::ERROR_CODES['Customer not found'] ) { + if ( $result->get_error_code() === Api::ERROR_CODES['Customer has been deleted'] || + $result->get_error_code() === Api::ERROR_CODES['Customer not found'] ) { $params = array( 'locale' => $this->get_language(), 'button_text' => __( 'Add card', 'reepay-checkout-gateway' ), @@ -332,7 +355,7 @@ public function check_is_active(): bool { $current_name = str_replace( 'reepay_', '', $this->id ); - if ( ! empty( $gateways_reepay ) ) { + if ( ! empty( $gateways_reepay ) && ! is_wp_error( $gateways_reepay ) ) { foreach ( $gateways_reepay as $app ) { if ( stripos( $app['type'], $current_name ) !== false ) { return true; @@ -691,8 +714,10 @@ public function refund_payment( $order, $amount = null, $reason = '' ) { public function get_icon(): string { $logos = array_map( function ( $logo ) { + $logo_url = $this->get_logo( $logo ); + return array( - 'src' => esc_url( reepay()->get_setting( 'images_url' ) . $logo . '.png' ), + 'src' => $logo_url, // translators: %s gateway title. 'alt' => esc_attr( sprintf( __( 'Pay with %s on Billwerk+', 'reepay-checkout-gateway' ), $this->get_title() ) ), ); @@ -747,10 +772,9 @@ public function process_payment( $order_id ) { } } } catch ( Exception $e ) { - return array( - 'messages' => __( 'Wrong Request. Try again', 'reepay-checkout-gateway' ), - 'result' => 'failure', - ); + wc_add_notice( __( 'Wrong Request. Try again', 'reepay-checkout-gateway' ), 'error' ); + + return false; } } @@ -925,10 +949,9 @@ public function process_payment( $order_id ) { } catch ( Exception $e ) { $order->add_order_note( $e->getMessage() ); - return array( - 'result' => 'failure', - 'message' => $e->getMessage(), - ); + wc_add_notice( $e->getMessage(), 'error' ); + + return false; } } @@ -941,10 +964,9 @@ public function process_payment( $order_id ) { } catch ( Exception $e ) { $order->add_order_note( $e->getMessage() ); - return array( - 'result' => 'failure', - 'message' => $e->getMessage(), - ); + wc_add_notice( $e->getMessage(), 'error' ); + + return false; } if ( wcr_cart_only_reepay_subscriptions() ) { @@ -954,27 +976,34 @@ public function process_payment( $order_id ) { return false; } elseif ( ! empty( $method ) ) { + if ( 'active' !== $method['state'] ) { + wc_add_notice( __( 'You payment method has failed, please choose another or add new', 'error' ), 'error' ); - if ( 'active' === $method['state'] ) { - $data = array( - 'payment_method' => $method['id'], - 'customer' => $method['customer'], - ); + return false; + } - do_action( 'reepay_create_subscription', $data, $order ); + $data = array( + 'payment_method' => $method['id'], + 'customer' => $method['customer'], + ); - try { - foreach ( $order->get_meta( '_reepay_another_orders' ) ?: array() as $order_id ) { - ReepayTokens::save_card_info_to_order( wc_get_order( $order_id ), $token->get_token() ); - } - } catch ( Exception $e ) { - wc_get_order( $order_id )->add_order_note( $e->getMessage() ); - } - } else { - wc_add_notice( __( 'You payment method has failed, please choose another or add new', 'error' ), 'error' ); + // Check PW Gift cards. + $exist_gift_card = PWGiftCardsIntegration::check_exist_gift_cards_in_order( $order ); + if ( $exist_gift_card ) { + wc_add_notice( __( 'Gift Cards cannot be used with Reepay subscriptions.', 'reepay-checkout-gateway' ), 'error' ); return false; } + + do_action( 'reepay_create_subscription', $data, $order ); + + try { + foreach ( $order->get_meta( '_reepay_another_orders' ) ?: array() as $order_id ) { + ReepayTokens::save_card_info_to_order( wc_get_order( $order_id ), $token->get_token() ); + } + } catch ( Exception $e ) { + wc_get_order( $order_id )->add_order_note( $e->getMessage() ); + } } } elseif ( wcs_cart_have_subscription() ) { return $this->process_session_charge( $params, $order ); @@ -1199,11 +1228,11 @@ public function process_session_charge( array $params, WC_Order $order ): array if ( in_array( $result->get_error_code(), array( - API::ERROR_CODES['Invoice already authorized'], - API::ERROR_CODES['Invoice already settled'], - API::ERROR_CODES['Invoice already cancelled'], - API::ERROR_CODES['Customer cannot be changed on invoice'], - API::ERROR_CODES['Currency change not allowed'], + Api::ERROR_CODES['Invoice already authorized'], + Api::ERROR_CODES['Invoice already settled'], + Api::ERROR_CODES['Invoice already cancelled'], + Api::ERROR_CODES['Customer cannot be changed on invoice'], + Api::ERROR_CODES['Currency change not allowed'], ), true ) ) { @@ -1322,15 +1351,16 @@ public function reepay_cancel() { * Apply settings from Reepay Checkout Gateway to other gateways. Use it in constructor */ protected function apply_parent_settings() { - $this->private_key = (string) reepay()->get_setting( 'private_key' ); - $this->private_key_test = (string) reepay()->get_setting( 'private_key_test' ); - $this->test_mode = (string) reepay()->get_setting( 'test_mode' ); - $this->settle = (array) reepay()->get_setting( 'settle' ); - $this->language = (string) reepay()->get_setting( 'language' ); - $this->debug = (string) reepay()->get_setting( 'debug' ); - $this->payment_type = (string) reepay()->get_setting( 'payment_type' ); - $this->skip_order_lines = (string) reepay()->get_setting( 'skip_order_lines' ); - $this->handle_failover = (string) reepay()->get_setting( 'handle_failover' ); + $this->private_key = (string) reepay()->get_setting( 'private_key' ); + $this->private_key_test = (string) reepay()->get_setting( 'private_key_test' ); + $this->test_mode = (string) reepay()->get_setting( 'test_mode' ); + $this->settle = (array) reepay()->get_setting( 'settle' ); + $this->language = (string) reepay()->get_setting( 'language' ); + $this->debug = (string) reepay()->get_setting( 'debug' ); + $this->payment_type = (string) reepay()->get_setting( 'payment_type' ); + $this->skip_order_lines = (string) reepay()->get_setting( 'skip_order_lines' ); + $this->handle_failover = (string) reepay()->get_setting( 'handle_failover' ); + $this->enable_order_autocancel = (string) reepay()->get_setting( 'enable_order_autocancel' ); } /** @@ -1386,6 +1416,10 @@ public function get_order_items( WC_Order $order, $only_not_settled = false ): a * @var WC_Order_Item_Product $order_item */ + if ( is_product_woosb( $order_item->get_product() ) ) { + continue; + } + if ( $order_item->get_product() && wcr_is_subscription_product( $order_item->get_product() ) ) { $fee = $order_item->get_product()->get_meta( '_reepay_subscription_fee' ); if ( ! empty( $fee ) && ! empty( $fee['enabled'] ) && 'yes' === $fee['enabled'] ) { @@ -1395,44 +1429,10 @@ public function get_order_items( WC_Order $order, $only_not_settled = false ): a continue; } - $price = $order->get_line_subtotal( $order_item, false, false ); - $this->log( - array( - 'source' => 'Price', - 'result' => $price, - ) - ); - - $price_with_tax = $order->get_line_subtotal( $order_item, true, false ); - $this->log( - array( - 'source' => 'Price with tax', - 'result' => $price_with_tax, - ) - ); + $price = OrderCapture::get_item_price( $order_item, $order ); - $tax = $price_with_tax - $price; - $this->log( - array( - 'source' => 'Tax amount', - 'result' => $tax, - ) - ); - - $tax_percent = ( $tax > 0 && $price > 0 ) ? round( 100 / ( $price / $tax ) ) : 0; - $this->log( - array( - 'source' => 'Tax percent', - 'result' => $tax_percent, - ) - ); - $unit_price = round( ( $prices_incl_tax ? $price_with_tax : $price ) / $order_item->get_quantity(), 2 ); - $this->log( - array( - 'source' => 'Unit price', - 'result' => $unit_price, - ) - ); + $tax_percent = $price['tax_percent']; + $unit_price = round( ( $prices_incl_tax ? $price['with_tax'] : $price['original'] ) / $order_item->get_quantity(), 2 ); if ( $only_not_settled && ! empty( $order_item->get_meta( 'settled' ) ) ) { continue; @@ -1454,16 +1454,16 @@ public function get_order_items( WC_Order $order, $only_not_settled = false ): a $price = OrderCapture::get_item_price( $item_shipping, $order ); - $tax = $price['with_tax'] - $price['original']; - $tax_percent = ( $tax > 0 ) ? 100 / ( $price['original'] / $tax ) : 0; - $unit_price = round( ( $prices_incl_tax ? $price['with_tax'] : $price['original'] ) / $item_shipping->get_quantity(), 2 ); + $tax_percent = $price['tax_percent']; + + $unit_price = round( ( $prices_incl_tax ? $price['with_tax'] : $price['original'] ) / $item_shipping->get_quantity(), 2 ); if ( $only_not_settled && ! empty( $item_shipping->get_meta( 'settled' ) ) ) { continue; } $items[] = array( - 'ordertext' => $item_shipping->get_name(), + 'ordertext' => rp_clear_ordertext( $item_shipping->get_name() ), 'quantity' => $item_shipping->get_quantity(), 'amount' => rp_prepare_amount( $unit_price, $order->get_currency() ), 'vat' => round( $tax_percent / 100, 2 ), @@ -1488,7 +1488,7 @@ public function get_order_items( WC_Order $order, $only_not_settled = false ): a } $items[] = array( - 'ordertext' => $order_fee->get_name(), + 'ordertext' => rp_clear_ordertext( $order_fee->get_name() ), 'quantity' => 1, 'amount' => rp_prepare_amount( $prices_incl_tax ? $fee_with_tax : $fee, $order->get_currency() ), 'vat' => round( $tax_percent / 100, 2 ), @@ -1513,18 +1513,7 @@ public function get_order_items( WC_Order $order, $only_not_settled = false ): a } // Add "PW Gift Cards" support. - foreach ( $order->get_items( 'pw_gift_card' ) as $line ) { - $amount = apply_filters( 'pwgc_to_order_currency', floatval( $line->get_amount() ) * -1, $order ); - - $items[] = array( - // translators: gift card code. - 'ordertext' => sprintf( __( 'PW gift card (%s)', 'reepay-checkout-gateway' ), $line->get_card_number() ), - 'quantity' => 1, - 'amount' => rp_prepare_amount( $amount, $order->get_currency() ), - 'vat' => 0, - 'amount_incl_vat' => $prices_incl_tax, - ); - } + $items = array_merge( $items, PWGiftCardsIntegration::get_order_lines_for_reepay( $order, $prices_incl_tax ) ); // Add "Gift Up!" discount. if ( defined( 'GIFTUP_ORDER_META_CODE_KEY' ) && @@ -1561,6 +1550,16 @@ protected function get_language(): string { } $locale = get_locale(); + + // Wpml support. + $languages = apply_filters( 'wpml_active_languages', null, 'orderby=id&order=desc' ); + if ( ! empty( $languages ) && count( $languages ) > 1 ) { + $locale_wpml = apply_filters( 'wpml_current_language', get_locale() ); + if ( ! empty( $languages[ $locale_wpml ] ) ) { + $locale = $languages[ $locale_wpml ]['default_locale']; + } + } + if ( in_array( $locale, array( 'en_US', 'da_DK', 'sv_SE', 'no_NO', 'de_DE', 'es_ES', 'fr_FR', 'it_IT', 'nl_NL' ), @@ -1581,28 +1580,55 @@ protected function get_language(): string { */ public function get_logo( string $card_type ): string { $card_types = array( - 'visa' => 'visa', - 'mc' => 'mastercard', - 'dankort' => 'dankort', - 'visa_dk' => 'dankort', - 'ffk' => 'forbrugsforeningen', - 'visa_elec' => 'visa-electron', - 'maestro' => 'maestro', - 'amex' => 'american-express', - 'diners' => 'diners', - 'discover' => 'discover', - 'jcb' => 'jcb', - 'mobilepay' => 'mobilepay', - 'ms_subscripiton' => 'mobilepay', - 'viabill' => 'viabill', - 'klarna_pay_later' => 'klarna', - 'klarna_pay_now' => 'klarna', - 'resurs' => 'resurs', - 'china_union_pay' => 'cup', - 'paypal' => 'paypal', - 'applepay' => 'applepay', - 'googlepay' => 'googlepay', - 'vipps' => 'vipps', + 'visa' => 'visa', + 'mc' => 'mastercard', + 'dankort' => 'dankort', + 'visa_dk' => 'dankort', + 'ffk' => 'forbrugsforeningen', + 'visa_elec' => 'visa-electron', + 'maestro' => 'maestro', + 'amex' => 'american-express', + 'diners' => 'diners', + 'discover' => 'discover', + 'jcb' => 'jcb', + 'mobilepay' => 'mobilepay', + 'ms_subscripiton' => 'mobilepay', + 'viabill' => 'viabill', + 'klarna_pay_later' => 'klarna', + 'klarna_pay_now' => 'klarna', + 'resurs' => 'resurs', + 'china_union_pay' => 'cup', + 'paypal' => 'paypal', + 'applepay' => 'applepay', + 'googlepay' => 'googlepay', + 'vipps' => 'vipps', + 'ideal' => 'ideal', + 'sepa' => 'sepa', + 'klarna_direct_bank_transfer' => 'klarna', + 'klarna_direct_debit' => 'klarna', + 'bancontact' => 'bancontact', + 'blik_oc' => 'blik', + 'eps' => 'eps', + 'estonia_banks' => 'card', + 'latvia_banks' => 'card', + 'lithuania_banks' => 'card', + 'giropay' => 'giropay', + 'mb_way' => 'mbway', + 'multibanco' => 'multibanco', + 'mybank' => 'mybank', + 'p24' => 'p24', + 'paycoinq' => 'paycoinq', + 'paysafecard' => 'paysafecard', + 'paysera' => 'paysera', + 'postfinance' => 'postfinance', + 'satispay' => 'satispay', + 'trustly' => 'trustly', + 'verkkopankki' => 'verkkopankki', + 'wechatpay' => 'wechatpay', + 'santander' => 'santander', + 'offline_bank_transfer' => 'card', + 'offline_cash' => 'card', + 'offline_other' => 'card', ); if ( isset( $card_types[ $card_type ] ) ) { @@ -1644,4 +1670,41 @@ public function init_form_fields() { ), ); } -} + + /** + * Exclude payment method if the order contains an unsupported currency + * + * @param WC_Payment_Gateway[] $available_gateways gateways. + * + * @return array + */ + public function exclude_payment_gateway_based_on_currency( array $available_gateways ): array { + if ( is_null( WC()->cart ) ) { + return $available_gateways; + } + $current_currencies = array(); + foreach ( WC()->cart->get_cart() as $cart_item ) { + $item_data = $cart_item['data']; + $currency = get_woocommerce_currency(); + if ( method_exists( $item_data, 'get_currency' ) ) { + $currency = $item_data->get_currency(); + } + $current_currencies[] = $currency; + } + foreach ( $available_gateways as $gateway_id => $gateway ) { + if ( $gateway_id === $this->id ) { + if ( ! empty( $this->supported_currencies ) ) { + if ( ! empty( array_diff( $current_currencies, $this->supported_currencies ) ) ) { + unset( $available_gateways[ $gateway_id ] ); + break; + } + } elseif ( ! empty( $this->unsupported_currencies ) && array_intersect( $this->unsupported_currencies, $current_currencies ) ) { + unset( $available_gateways[ $gateway_id ] ); + break; + } + } + } + + return $available_gateways; + } +} \ No newline at end of file diff --git a/includes/Gateways/VippsRecurring.php b/includes/Gateways/VippsRecurring.php new file mode 100644 index 00000000..fcaa14c8 --- /dev/null +++ b/includes/Gateways/VippsRecurring.php @@ -0,0 +1,85 @@ +id = 'reepay_vipps_recurring'; + $this->has_fields = true; + $this->method_title = __( 'Billwerk+ - Vipps Recurring', 'reepay-checkout-gateway' ); + $this->supports = array( + 'products', + 'refunds', + 'add_payment_method', + 'tokenization', + 'subscriptions', + 'subscription_cancellation', + 'subscription_suspension', + 'subscription_reactivation', + 'subscription_amount_changes', + 'subscription_date_changes', + 'subscription_payment_method_change', + 'subscription_payment_method_change_customer', + 'subscription_payment_method_change_admin', + 'multiple_subscriptions', + ); + $this->logos = array( 'vipps' ); + + parent::__construct(); + + $this->apply_parent_settings(); + + add_action( 'wp_ajax_reepay_card_store_' . $this->id, array( $this, 'reepay_card_store' ) ); + add_action( 'wp_ajax_nopriv_reepay_card_store_' . $this->id, array( $this, 'reepay_card_store' ) ); + } + + + + /** + * If There are no payment fields show the description if set. + * + * @return void + */ + public function payment_fields() { + parent::payment_fields(); + + $this->tokenization_script(); + $this->saved_payment_methods(); + $this->save_payment_method_checkbox(); + } +} diff --git a/includes/Integrations/PWGiftCardsIntegration.php b/includes/Integrations/PWGiftCardsIntegration.php new file mode 100644 index 00000000..99d55b57 --- /dev/null +++ b/includes/Integrations/PWGiftCardsIntegration.php @@ -0,0 +1,123 @@ +get_items( self::KEY_PW_GIFT_ITEMS ) ) > 0; + } + + /** + * Create items for Reepay payment + * + * @param WC_Order $order Order. + * @param bool $including_vat If optional custom plan price this parameter tells whether the amount is including VAT. + * + * @return array + */ + public static function get_order_lines_for_reepay( WC_Order $order, bool $including_vat ): array { + $items = array(); + if ( ! class_exists( 'WC_Order_Item_PW_Gift_Card' ) ) { + return $items; + } + + /** + * Class from PW WooCommerce Gift Cards plugin + * + * @var WC_Order_Item_PW_Gift_Card $line + */ + foreach ( $order->get_items( self::KEY_PW_GIFT_ITEMS ) as $line ) { + $amount = self::get_negative_amount_from_order_item( $order, $line ); + + $items[] = array( + // translators: gift card code. + 'ordertext' => sprintf( __( 'PW gift card (%s)', 'reepay-checkout-gateway' ), $line->get_card_number() ), + 'quantity' => 1, + 'amount' => rp_prepare_amount( $amount, $order->get_currency() ), + 'vat' => 0, + 'amount_incl_vat' => $including_vat, + ); + } + + return $items; + } + + /** + * Get full amount of gift cards from order + * + * @param WC_Order $order Order. + * + * @return int + */ + public static function get_amount_gift_cards_from_order( WC_Order $order ): int { + $amount = 0; + + /** + * Class from PW WooCommerce Gift Cards plugin + * + * @var WC_Order_Item_PW_Gift_Card $line + */ + foreach ( $order->get_items( self::KEY_PW_GIFT_ITEMS ) as $line ) { + $amount += self::get_amount_from_order_item( $order, $line ); + } + + return $amount; + } + + /** + * Receives the amount if the class WC_Order_Item_PW_Gift_Card is passed + * + * @param WC_Order $order Order. + * @param WC_Order_Item_PW_Gift_Card|WC_Order_Item $order_item Order item. + * + * @return float|int + */ + public static function get_amount_from_order_item( WC_Order $order, $order_item ) { + if ( class_exists( 'WC_Order_Item_PW_Gift_Card' ) && is_a( $order_item, 'WC_Order_Item_PW_Gift_Card' ) ) { + return apply_filters( 'pwgc_to_order_currency', floatval( $order_item->get_amount() ), $order ); + } else { + return 0; + } + } + + /** + * Receives the negative amount if the class WC_Order_Item_PW_Gift_Card is passed + * + * @param WC_Order $order Order. + * @param WC_Order_Item_PW_Gift_Card|WC_Order_Item $order_item Order item. + * + * @return float|int + */ + public static function get_negative_amount_from_order_item( WC_Order $order, $order_item ) { + return self::get_amount_from_order_item( $order, $order_item ) * - 1; + } +} diff --git a/includes/OrderFlow/InstantSettle.php b/includes/OrderFlow/InstantSettle.php index ab975ec3..38cb80d9 100644 --- a/includes/OrderFlow/InstantSettle.php +++ b/includes/OrderFlow/InstantSettle.php @@ -8,6 +8,7 @@ namespace Reepay\Checkout\OrderFlow; use Reepay\Checkout\Gateways\ReepayGateway; +use Reepay\Checkout\Integrations\PWGiftCardsIntegration; use stdClass; use WC_Order; use WC_Order_Item; @@ -103,7 +104,7 @@ public function process_instant_settle( WC_Order $order ) { } } - self::$order_capture->settle_items( $order, $items_data, $total_all, $settle_items, true ); + self::$order_capture->settle_items( $order, $items_data, $total_all, $settle_items ); $order->add_meta_data( '_is_instant_settled', '1' ); $order->save_meta_data(); } @@ -169,6 +170,12 @@ public static function get_instant_settle_items( WC_Order $order ): array { } } + if ( ! empty( $items_data ) ) { + foreach ( $order->get_items( PWGiftCardsIntegration::KEY_PW_GIFT_ITEMS ) as $line ) { + $items_data[] = $line; + } + } + if ( in_array( self::SETTLE_FEE, $settle_types, true ) ) { foreach ( $order->get_fees() as $i => $order_fee ) { $items_data[ $i ] = $order_fee; diff --git a/includes/OrderFlow/OrderCapture.php b/includes/OrderFlow/OrderCapture.php index a7bbc0bb..863d4367 100644 --- a/includes/OrderFlow/OrderCapture.php +++ b/includes/OrderFlow/OrderCapture.php @@ -8,6 +8,7 @@ namespace Reepay\Checkout\OrderFlow; use Exception; +use Reepay\Checkout\Integrations\PWGiftCardsIntegration; use WC_Order; use WC_Order_Factory; use WC_Order_Item; @@ -83,12 +84,12 @@ public function unset_specific_order_item_meta_data( array $formatted_meta, WC_O /** * Hooked to woocommerce_after_order_fee_item_name. Print capture button. * - * @param int $item_id the id of the item being displayed. - * @param object $item the item being displayed. + * @param int $item_id the id of the item being displayed. + * @param WC_Order_Item|WC_Order_Item_Product $item the item being displayed. * * @throws Exception When `WC_Data_Store::load` validation fails. */ - public function add_item_capture_button( int $item_id, object $item ) { + public function add_item_capture_button( int $item_id, $item ) { $order_id = wc_get_order_id_by_order_item_id( $item_id ); $order = wc_get_order( $order_id ); @@ -97,7 +98,7 @@ public function add_item_capture_button( int $item_id, object $item ) { floatval( $item->get_data()['total'] ) > 0 && $this->check_capture_allowed( $order ) ) { - $price = self::get_item_price( $item_id, $order ); + $price = self::get_item_price( $item, $order ); reepay()->get_template( 'admin/capture-item-button.php', @@ -212,7 +213,8 @@ public function multi_settle( WC_Order $order ): bool { foreach ( $order->get_items() as $item ) { if ( empty( $item->get_meta( 'settled' ) ) ) { $item_data = $this->get_item_data( $item, $order ); - $total = $item_data['amount'] * $item_data['quantity']; + $price = self::get_item_price( $item, $order ); + $total = rp_prepare_amount( $price['with_tax'], $order->get_currency() ); if ( $total <= 0 && method_exists( $item, 'get_product' ) && $item->get_product() && wcs_is_subscription_product( $item->get_product() ) ) { WC_Subscriptions_Manager::activate_subscriptions_for_order( $order ); @@ -224,11 +226,13 @@ public function multi_settle( WC_Order $order ): bool { } } - foreach ( $order->get_items( array( 'shipping', 'fee' ) ) as $item ) { + foreach ( $order->get_items( array( 'shipping', 'fee', PWGiftCardsIntegration::KEY_PW_GIFT_ITEMS ) ) as $item ) { 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 ) ) { + $price = self::get_item_price( $item, $order ); + $total = rp_prepare_amount( $price['with_tax'], $order->get_currency() ); + + if ( 0.0 !== $total && $this->check_capture_allowed( $order ) ) { $items_data[] = $item_data; $line_items[] = $item; $total_all += $total; @@ -254,7 +258,7 @@ public function multi_settle( WC_Order $order ): bool { * * @return bool */ - public function settle_items( WC_Order $order, array $items_data, float $total_all, array $line_items, bool $instant_note = false ): bool { + public function settle_items( WC_Order $order, array $items_data, float $total_all, array $line_items, bool $instant_note = true ): bool { unset( $_POST['post_status'] ); // // Prevent order status changing by WooCommerce $result = reepay()->api( $order )->settle( $order, $total_all, $items_data, $line_items, $instant_note ); @@ -314,7 +318,8 @@ public function settle_item( WC_Order_Item $item, WC_Order $order ): bool { unset( $_POST['post_status'] ); // Prevent order status changing by WooCommerce. $item_data = $this->get_item_data( $item, $order ); - $total = $item_data['amount'] * $item_data['quantity']; + $price = self::get_item_price( $item, $order ); + $total = rp_prepare_amount( $price['with_tax'], $order->get_currency() ); if ( $total <= 0 ) { do_action( 'reepay_order_item_settled', $item, $order ); @@ -380,6 +385,8 @@ public function get_not_settled_amount( WC_Order $order ) { } } + $amount -= PWGiftCardsIntegration::get_amount_gift_cards_from_order( $order ); + return $amount; } @@ -393,12 +400,15 @@ public function get_not_settled_amount( WC_Order $order ) { */ public function get_item_data( WC_Order_Item $order_item, WC_Order $order ): array { $prices_incl_tax = wc_prices_include_tax(); + $price = self::get_item_price( $order_item, $order ); - $price = self::get_item_price( $order_item, $order ); + $tax_percent = $price['tax_percent']; - $tax = $price['with_tax'] - $price['original']; - $tax_percent = ( $tax > 0 ) ? 100 / ( $price['original'] / $tax ) : 0; - $unit_price = round( ( $prices_incl_tax ? $price['with_tax'] : $price['original'] ) / $order_item->get_quantity(), 2 ); + if ( $order_item->is_type( PWGiftCardsIntegration::KEY_PW_GIFT_ITEMS ) ) { + $unit_price = PWGiftCardsIntegration::get_negative_amount_from_order_item( $order, $order_item ); + } else { + $unit_price = round( ( $prices_incl_tax ? $price['with_tax'] : $price['original'] ) / $order_item->get_quantity(), 2 ); + } return array( 'ordertext' => rp_clear_ordertext( $order_item->get_name() ), @@ -412,39 +422,36 @@ public function get_item_data( WC_Order_Item $order_item, WC_Order $order ): arr /** * Get order item price for reepay. * - * @param WC_Order_Item|int $order_item order item to get price and tax. - * @param WC_Order $order current order. + * @param WC_Order_Item|WC_Order_Item_Product|int $order_item order item to get price and tax. + * @param WC_Order $order current order. * * @return array * @noinspection PhpCastIsUnnecessaryInspection */ public static function get_item_price( $order_item, WC_Order $order ): array { - if ( is_int( $order_item ) ) { - $order_item = WC_Order_Factory::get_order_item( $order_item ); + $discount = floatval( $order_item->get_meta( '_line_discount' ) ); + if ( empty( $discount ) ) { + $discount = 0; } - $price = array( - // get_line_total can return string. - 'original' => (float) $order->get_line_total( $order_item, false, false ), - ); - - $price['with_tax'] = $price['original']; + $price['original'] = floatval( $order->get_line_total( $order_item, false, false ) ); + $price['with_tax'] = floatval( $order->get_line_total( $order_item, true, false ) ); - if ( ! empty( $order_item ) && ! is_array( $order_item ) && empty( $order_item->get_meta( '_is_card_fee' ) ) ) { - $tax_data = wc_tax_enabled() && method_exists( $order_item, 'get_taxes' ) ? $order_item->get_taxes() : false; - $taxes = method_exists( $order, 'get_taxes' ) ? $order->get_taxes() : false; + if ( is_active_plugin_woo_product_bundle() ) { + $price_bundle = floatval( $order_item->get_meta( '_woosb_price' ) ); + if ( ! empty( $price_bundle ) ) { + $price['original'] = $price_bundle; - if ( ! empty( $tax_data ) && ! empty( $taxes ) ) { - foreach ( $taxes as $tax ) { - $tax_item_id = $tax->get_rate_id(); - $tax_item_total = $tax_data['total'][ $tax_item_id ] ?? ''; - if ( ! empty( $tax_item_total ) ) { - $price['with_tax'] += (float) $tax_item_total; - } - } + $price['with_tax'] += $price_bundle; } } + $tax = $price['with_tax'] - $price['original']; + $price['tax_percent'] = ( $tax > 0 && $price['original'] > 0 ) ? round( 100 / ( $price['original'] / $tax ) ) : 0; + + $price['original'] += $discount; + $price['with_tax'] += $discount; + return $price; } } diff --git a/includes/OrderFlow/OrderStatuses.php b/includes/OrderFlow/OrderStatuses.php index 6a330322..a8bc495b 100644 --- a/includes/OrderFlow/OrderStatuses.php +++ b/includes/OrderFlow/OrderStatuses.php @@ -373,7 +373,7 @@ public function is_paid( bool $is_paid, WC_Order $order ): bool { * @see wc_cancel_unpaid_orders() */ public function cancel_unpaid_order( bool $maybe_cancel, WC_Order $order ): bool { - if ( $maybe_cancel && rp_is_order_paid_via_reepay( $order ) ) { + if ( $maybe_cancel && rp_is_order_paid_via_reepay( $order ) && 'yes' !== reepay()->get_setting( 'enable_order_autocancel' ) ) { $maybe_cancel = false; } diff --git a/includes/OrderFlow/ThankyouPage.php b/includes/OrderFlow/ThankyouPage.php index b9c659d6..b2fdf6a3 100644 --- a/includes/OrderFlow/ThankyouPage.php +++ b/includes/OrderFlow/ThankyouPage.php @@ -8,7 +8,7 @@ namespace Reepay\Checkout\OrderFlow; use Exception; -use Reepay\Checkout\LoggingTrait; +use Reepay\Checkout\Utils\LoggingTrait; use WC_Order_Item_Product; defined( 'ABSPATH' ) || exit(); @@ -76,10 +76,6 @@ public function override_thankyou_template( $located, $template_name, $args, $te * @return void */ public function thankyou_scripts() { - if ( ! is_order_received_page() ) { - return; - } - $order_key = isset( $_GET['key'] ) ? wc_clean( wp_unslash( $_GET['key'] ) ) : ''; $order = wc_get_order( get_query_var( 'order-received', 0 ) ); @@ -143,7 +139,7 @@ public function ajax_check_payment() { * * @var WC_Order_Item_Product $item */ - if ( $order->get_total() <= 0 && wcs_is_subscription_product( $item->get_product() ) ) { + if ( intval( $order->get_total() ) <= 0 && wcs_is_subscription_product( $item->get_product() ) ) { $ret = array( 'state' => 'paid', 'message' => 'Subscription is activated in trial', diff --git a/includes/OrderFlow/Webhook.php b/includes/OrderFlow/Webhook.php index f579408b..d158df00 100644 --- a/includes/OrderFlow/Webhook.php +++ b/includes/OrderFlow/Webhook.php @@ -8,7 +8,7 @@ namespace Reepay\Checkout\OrderFlow; use Exception; -use Reepay\Checkout\LoggingTrait; +use Reepay\Checkout\Utils\LoggingTrait; use Reepay\Checkout\Tokens\ReepayTokens; use WC_Order_Item_Fee; use WC_Subscriptions_Manager; @@ -413,7 +413,7 @@ public function process( array $data ) { break; case 'customer_created': $customer = $data['customer']; - $user_id = rp_get_userid_by_handle( $customer ); + $user_id = rp_get_user_id_by_handle( $customer ); if ( ! $user_id ) { if ( strpos( $customer, 'customer-' ) !== false ) { $user_id = (int) str_replace( 'customer-', '', $customer ); diff --git a/includes/Plugin/Statistics.php b/includes/Plugin/Statistics.php index eaaf970e..1f0863ef 100644 --- a/includes/Plugin/Statistics.php +++ b/includes/Plugin/Statistics.php @@ -7,7 +7,7 @@ namespace Reepay\Checkout\Plugin; -use Reepay\Checkout\LoggingTrait; +use Reepay\Checkout\Utils\LoggingTrait; use WP_Error; use WP_Upgrader; diff --git a/includes/Tokens/ReepayTokens.php b/includes/Tokens/ReepayTokens.php index 969530c1..19f2203c 100644 --- a/includes/Tokens/ReepayTokens.php +++ b/includes/Tokens/ReepayTokens.php @@ -8,6 +8,7 @@ namespace Reepay\Checkout\Tokens; use Exception; +use Reepay\Checkout\Api; use WC_Order; use WC_Subscription; use WC_Payment_Token; @@ -288,7 +289,7 @@ public static function get_payment_token( string $token ) { public static function delete_card( WC_Payment_Token $token ) { $result = reepay()->api( 'api-delete-card' )->delete_payment_method( $token->get_token() ); - if ( is_wp_error( $result ) ) { + if ( is_wp_error( $result ) && $result->get_error_code() !== Api::ERROR_CODES['Payment method not found'] ) { return false; } diff --git a/includes/Tokens/TokenReepay.php b/includes/Tokens/TokenReepay.php index db7e4328..1b08315b 100644 --- a/includes/Tokens/TokenReepay.php +++ b/includes/Tokens/TokenReepay.php @@ -80,25 +80,6 @@ public function get_display_name( $deprecated = '' ): string { return $display; } - /** - * Delete payment method in reepay side. - * - * @param bool $force_delete From parent. - * - * @return bool - */ - public function delete( $force_delete = false ) { - global $wp; - - if ( isset( $wp->query_vars['delete-payment-method'] ) ) { - $token_id = absint( $wp->query_vars['delete-payment-method'] ); - $token = WC_Payment_Tokens::get( $token_id ); - ReepayTokens::delete_card( $token ); - } - - return parent::delete(); - } - /** * Get card image url * diff --git a/includes/Utils/EmojiRemover.php b/includes/Utils/EmojiRemover.php new file mode 100644 index 00000000..8896258c --- /dev/null +++ b/includes/Utils/EmojiRemover.php @@ -0,0 +1,2161 @@ +./tests/unit/actions - ./tests/unit/functions + ./tests/unit/Functions ./tests/unit/orderFlow diff --git a/reepay-woocommerce-payment.php b/reepay-woocommerce-payment.php index ce8118cb..8585557e 100755 --- a/reepay-woocommerce-payment.php +++ b/reepay-woocommerce-payment.php @@ -4,7 +4,7 @@ * 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. * Author: Billwerk+ * Author URI: http://billwerk.plus - * Version: 1.7.0 + * Version: 1.7.2 * Text Domain: reepay-checkout-gateway * Domain Path: /languages * WC requires at least: 3.0.0 @@ -160,6 +160,7 @@ public function get_setting( string $name ) { 'debug' => ! empty( $gateway_settings['debug'] ) ? $gateway_settings['debug'] : '', 'payment_type' => ! empty( $gateway_settings['payment_type'] ) ? $gateway_settings['payment_type'] : '', 'skip_order_lines' => ! empty( $gateway_settings['skip_order_lines'] ) ? $gateway_settings['skip_order_lines'] : '', + 'enable_order_autocancel' => ! empty( $gateway_settings['enable_order_autocancel'] ) ? $gateway_settings['enable_order_autocancel'] : '', 'is_webhook_configured' => ! empty( $gateway_settings['is_webhook_configured'] ) ? $gateway_settings['is_webhook_configured'] : '', 'handle_failover' => ! empty( $gateway_settings['handle_failover'] ) ? $gateway_settings['handle_failover'] : '', 'payment_button_text' => ! empty( $gateway_settings['payment_button_text'] ) ? $gateway_settings['payment_button_text'] : '', @@ -222,7 +223,7 @@ public function api( $source ): Api { * * @var Api $api */ - $api = $this->di()->get( API::class ); + $api = $this->di()->get( Api::class ); $api->set_logging_source( $source ); return $api; diff --git a/templates/checkout/thankyou.php b/templates/checkout/thankyou.php index bf18fd15..98fb4298 100644 --- a/templates/checkout/thankyou.php +++ b/templates/checkout/thankyou.php @@ -27,7 +27,7 @@ color: red; } -
+
    + - - + +
  • diff --git a/tests/helpers/OrderGenerator.php b/tests/helpers/OrderGenerator.php index d9ebd2ed..caa59ce3 100644 --- a/tests/helpers/OrderGenerator.php +++ b/tests/helpers/OrderGenerator.php @@ -7,12 +7,14 @@ namespace Reepay\Checkout\Tests\Helpers; +use Exception; use WC_Order; use WC_Order_Factory; use WC_Order_Item; use WC_Order_Item_Fee; use WC_Order_Item_Shipping; use WC_Order_Item_Tax; +use WC_Subscription; use WC_Tax; /** @@ -103,7 +105,7 @@ public function set_props( array $props ) { } /** - * Get Meta Data by Key. + * Get metadata by key. * * @param string $key meta key. * @@ -116,11 +118,12 @@ public function get_meta( string $key ) { /** * Add product to order * - * @param string $type product type. - * @param array $product_data product meta data. + * @param string $type product type. + * @param array $product_data product meta data. * @param array $order_item_data order item meta data. * * @return int order item id + * @throws Exception */ public function add_product( string $type, array $product_data = array(), array $order_item_data = array() ): int { $product_generator = new ProductGenerator( $type, $product_data ); @@ -136,7 +139,13 @@ public function add_product( string $type, array $product_data = array(), array return $order_item_id; } - private function generate_woo_subscription( $product ) { + /** + * @param $product + * + * @return WC_Subscription + * @throws Exception + */ + private function generate_woo_subscription( $product ): WC_Subscription { $sub = wcs_create_subscription( array( 'order_id' => $this->order->get_id(), diff --git a/tests/helpers/Reepay_UnitTestCase.php b/tests/helpers/Reepay_UnitTestCase.php index d9b8ca07..4e87caec 100644 --- a/tests/helpers/Reepay_UnitTestCase.php +++ b/tests/helpers/Reepay_UnitTestCase.php @@ -7,6 +7,11 @@ namespace Reepay\Checkout\Tests\Helpers; +use Reepay\Checkout\Api; +use Reepay\Checkout\OrderFlow\InstantSettle; +use Reepay\Checkout\OrderFlow\OrderCapture; +use Reepay\Checkout\OrderFlow\OrderStatuses; +use Reepay\Checkout\Tests\Mocks\OrderFlow\OrderCaptureMock; use WP_UnitTestCase; /** @@ -14,4 +19,22 @@ */ class Reepay_UnitTestCase extends WP_UnitTestCase { use Reepay_UnitTestCase_Trait; -} \ No newline at end of file + + /** + * Runs the routine before setting up all tests. + */ + public static function set_up_before_class() { + parent::set_up_before_class(); + + self::set_up_data_before_class(); + } + + /** + * Runs the routine before each test is executed. + */ + public function set_up() { + parent::set_up(); + + $this->set_up_data(); + } +} diff --git a/tests/helpers/Reepay_UnitTestCase_Trait.php b/tests/helpers/Reepay_UnitTestCase_Trait.php index 38102fff..491c6c99 100644 --- a/tests/helpers/Reepay_UnitTestCase_Trait.php +++ b/tests/helpers/Reepay_UnitTestCase_Trait.php @@ -75,26 +75,22 @@ trait Reepay_UnitTestCase_Trait { protected Api $api_mock; /** - * Runs the routine before setting up all tests. + * Initializes necessary components and mocks */ - public static function set_up_before_class() { - parent::set_up_before_class(); - + public static function set_up_data_before_class() { self::$options = new OptionsController(); self::$product_generator = new ProductGenerator(); self::$instant_settle_instance = new InstantSettle(); - self::$instant_settle_instance::set_order_capture( + InstantSettle::set_order_capture( new OrderCaptureMock() ); } /** - * Runs the routine before each test is executed. + * Sets the data for each test method */ - public function set_up() { - parent::set_up(); - + public function set_up_data() { $this->order_statuses = new OrderStatuses(); $this->order_capture = new OrderCapture(); $this->order_generator = new OrderGenerator(); @@ -109,4 +105,4 @@ public function set_up() { $this->api_mock = $this->getMockBuilder( Api::class )->getMock(); reepay()->di()->set( Api::class, $this->api_mock ); } -} \ No newline at end of file +} diff --git a/tests/unit/Functions/CurrencyTest.php b/tests/unit/Functions/CurrencyTest.php new file mode 100644 index 00000000..5f88d641 --- /dev/null +++ b/tests/unit/Functions/CurrencyTest.php @@ -0,0 +1,82 @@ +api_mock + ->method( 'request' ) + ->willReturn( new WP_Error() ); + + $user = self::factory()->user->create(); + + self::assertSame( + 'customer-' . $user, + rp_get_customer_handle( $user ), + 'Wrong customer handle' + ); + } + + /** + * Test rp_get_customer_handle + * + * @see rp_get_customer_handle + * @group functions_customer + */ + public function test_rp_get_customer_handle_for_non_existent_user() { + $this->api_mock + ->method( 'request' ) + ->willReturn( new WP_Error() ); + + $this::assertSame( + 'cust-' . TimeKeeper::get(), + rp_get_customer_handle( 0 ), + 'Wrong customer handle for a non-existent user with id 0' + ); + $this::assertSame( + 'cust-' . TimeKeeper::get(), + rp_get_customer_handle( 1984 ), + 'Wrong customer handle for a non-existent user' + ); + } + + /** + * Test rp_get_customer_handle + * + * @see rp_get_customer_handle + * @group functions_customer + */ + public function test_rp_get_customer_handle_exists() { + $this->api_mock + ->method( 'request' ) + ->willReturn( new WP_Error() ); + + $user = self::factory()->user->create(); + + rp_get_customer_handle( $user ); + + $this::assertSame( + 'customer-' . $user, + get_user_meta( $user, 'reepay_customer_id', true ), + 'Wrong customer handle in user meta' + ); + } + + /** + * Test rp_get_customer_handle + * + * @see rp_get_customer_handle + * @group functions_customer + */ + public function test_rp_get_customer_exists_in_api() { + $this->api_mock + ->method( 'request' ) + ->willReturn( + array( + 'content' => array( + array( + 'handle' => 'rp-custom-handle-1', + ), + ), + ) + ); + + $user = self::factory()->user->create(); + + $this::assertSame( + 'rp-custom-handle-1', + rp_get_customer_handle( $user ), + 'Wrong customer handle from API' + ); + } + + /** + * Test rp_get_user_id_by_handle with guest handle + * + * @see rp_get_user_id_by_handle + * @group functions_customer + */ + public function test_rp_get_user_id_by_handle_guest() { + $this::assertSame( + 0, + rp_get_user_id_by_handle( 'guest-0' ), + 'Wrong user id at guest by handle customer' + ); + } + + /** + * Test rp_get_user_id_by_handle with customer handle + * + * @see rp_get_user_id_by_handle + * @group functions_customer + */ + public function test_rp_get_user_id_by_handle_customer() { + $user = self::factory()->user->create(); + rp_get_customer_handle( $user ); + + $this::assertSame( + $user, + rp_get_user_id_by_handle( "customer-$user" ), + 'Wrong user id by handle customer' + ); + } +} diff --git a/tests/unit/Functions/FormatTest.php b/tests/unit/Functions/FormatTest.php new file mode 100644 index 00000000..6f2a1b6c --- /dev/null +++ b/tests/unit/Functions/FormatTest.php @@ -0,0 +1,69 @@ +order_generator->set_prop( 'payment_method', $method_name ); $payment_method = rp_get_payment_method( $this->order_generator->order() ); - $this->assertSame( - $class, + self::assertSame( + $class_name, $payment_method ? get_class( $payment_method ) : false ); } /** - * Test @see rp_is_reepay_payment_method + * Test rp_is_reepay_payment_method * * @param string $method_name payment method name. - * @param string|false $class payment method class name. + * @param string|false $class_name payment method class name. * @param bool $is_reepay is reepay gateway. * * @dataProvider \Reepay\Checkout\Tests\Helpers\DataProvider::payment_methods + * @see rp_is_reepay_payment_method + * @group functions_gateways */ - public function test_rp_is_reepay_payment_method( string $method_name, $class, bool $is_reepay ) { - $this->assertSame( + public function test_rp_is_reepay_payment_method( string $method_name, $class_name, bool $is_reepay ) { + self::assertSame( $is_reepay, rp_is_reepay_payment_method( $method_name ) ); diff --git a/tests/unit/functions/HposTest.php b/tests/unit/Functions/HposTest.php similarity index 100% rename from tests/unit/functions/HposTest.php rename to tests/unit/Functions/HposTest.php diff --git a/tests/unit/functions/OrderTest.php b/tests/unit/Functions/OrderTest.php similarity index 82% rename from tests/unit/functions/OrderTest.php rename to tests/unit/Functions/OrderTest.php index a97772cd..b6a2364d 100644 --- a/tests/unit/functions/OrderTest.php +++ b/tests/unit/Functions/OrderTest.php @@ -6,41 +6,51 @@ */ use Reepay\Checkout\Tests\Helpers\Reepay_UnitTestCase; +use Reepay\Checkout\Utils\TimeKeeper; /** * CurrencyTest. */ class OrderTest extends Reepay_UnitTestCase { + /** + * Set up before class + */ + public static function set_up_before_class() { + parent::set_up_before_class(); + + TimeKeeper::set( 1713387085 ); + } + /** * Test @see rp_get_order_handle */ public function test_rp_get_order_handle() { - $this->assertSame( + self::assertSame( 'order-' . $this->order_generator->order()->get_id(), rp_get_order_handle( $this->order_generator->order() ), 'Wrong order handle generated' ); - $this->assertSame( + self::assertSame( 'order-' . $this->order_generator->order()->get_id(), $this->order_generator->get_meta( '_reepay_order' ), 'Wrong order handle saved in meta' ); - $this->assertSame( + self::assertSame( 'order-' . $this->order_generator->order()->get_id(), rp_get_order_handle( $this->order_generator->order() ), 'Wrong order handle returned' ); - $this->assertSame( - 'order-' . $this->order_generator->order()->get_id() . '-' . time(), + self::assertSame( + 'order-' . $this->order_generator->order()->get_id() . '-' . TimeKeeper::get(), rp_get_order_handle( $this->order_generator->order(), true ), 'Wrong new unique order handle generated' ); - $this->assertSame( - 'order-' . $this->order_generator->order()->get_id() . '-' . time(), + self::assertSame( + 'order-' . $this->order_generator->order()->get_id() . '-' . TimeKeeper::get(), $this->order_generator->get_meta( '_reepay_order' ), 'Wrong unique order handle saved in meta' ); @@ -55,7 +65,7 @@ public function test_rp_get_order_by_handle_found() { $order = rp_get_order_by_handle( $handle ); - $this->assertSame( + self::assertSame( $this->order_generator->order()->get_id(), $order ? $order->get_id() : false ); @@ -67,7 +77,7 @@ public function test_rp_get_order_by_handle_found() { public function test_rp_get_order_by_handle_not_found() { $handle = 'order-1234'; - $this->assertFalse( rp_get_order_by_handle( $handle ) ); + self::assertFalse( rp_get_order_by_handle( $handle ) ); } /** @@ -81,7 +91,7 @@ public function test_rp_get_order_by_handle_cache() { rp_get_order_by_handle( $handle ); $order = rp_get_order_by_handle( $handle ); - $this->assertSame( + self::assertSame( $this->order_generator->order()->get_id(), $order ? $order->get_id() : false ); @@ -96,7 +106,7 @@ public function test_rp_get_order_by_session_found() { $order = rp_get_order_by_session( $session_id ); - $this->assertSame( + self::assertSame( $this->order_generator->order()->get_id(), $order ? $order->get_id() : false ); @@ -108,7 +118,7 @@ public function test_rp_get_order_by_session_found() { public function test_rp_get_order_by_session_not_found() { $session_id = 'sid_1234'; - $this->assertFalse( rp_get_order_by_session( $session_id ) ); + self::assertFalse( rp_get_order_by_session( $session_id ) ); } /** @@ -122,25 +132,26 @@ public function test_rp_get_order_by_session_cache() { rp_get_order_by_session( $session_id ); $order = rp_get_order_by_session( $session_id ); - $this->assertSame( + self::assertSame( $this->order_generator->order()->get_id(), $order ? $order->get_id() : false ); } /** - * Test @see rp_is_order_paid_via_reepay + * Test * * @param string $method_name payment method name. - * @param string|false $class payment method class name. - * @param bool $is_reepay is reepay gateway. + * @param string|false $class payment method class name. + * @param bool $is_reepay is reepay gateway. * * @dataProvider \Reepay\Checkout\Tests\Helpers\DataProvider::payment_methods + * @see rp_is_order_paid_via_reepay */ public function test_rp_is_order_paid_via_reepay( string $method_name, $class, bool $is_reepay ) { $this->order_generator->set_prop( 'payment_method', $method_name ); - $this->assertSame( + self::assertSame( $is_reepay, rp_is_order_paid_via_reepay( $this->order_generator->order() ) ); diff --git a/tests/unit/functions/SubscriptionsTest.php b/tests/unit/Functions/SubscriptionsTest.php similarity index 100% rename from tests/unit/functions/SubscriptionsTest.php rename to tests/unit/Functions/SubscriptionsTest.php diff --git a/tests/unit/functions/CurrencyTest.php b/tests/unit/functions/CurrencyTest.php deleted file mode 100644 index 37c2bad9..00000000 --- a/tests/unit/functions/CurrencyTest.php +++ /dev/null @@ -1,69 +0,0 @@ -assertSame( - $result, - rp_prepare_amount( $amount, $currency ) - ); - } - - /** - * Test @see rp_make_initial_amount - * - * @param int $amount amount for calculation. - * @param string $currency currency for calculation. - * @param float $result expected result. - * - * @testWith - * [1234, "ISK", 1234] - * [1234, "USD", 12.34] - * [1234, "EUR", 12.34] - */ - public function test_rp_make_initial_amount( int $amount, string $currency, float $result ) { - $this->assertSame( - $result, - rp_make_initial_amount( $amount, $currency ) - ); - } - - /** - * Test @see rp_get_currency_multiplier - * - * @param int $multiplier multiplier value. - * @param string $currency currency for calculation. - * - * @testWith - * [1, "ISK"] - * [100, "USD"] - * [100, "EUR"] - */ - public function test_rp_get_currency_multiplier( int $multiplier, string $currency ) { - $this->assertSame( - $multiplier, - rp_get_currency_multiplier( $currency ) - ); - } -} diff --git a/tests/unit/functions/CustomerTest.php b/tests/unit/functions/CustomerTest.php deleted file mode 100644 index f3b50b5e..00000000 --- a/tests/unit/functions/CustomerTest.php +++ /dev/null @@ -1,98 +0,0 @@ -api_mock->method('request')->willReturn( new WP_Error() ); - - $user = $this->factory()->user->create(); - - $this->assertSame( - 'customer-' . $user, - rp_get_customer_handle( $user ) - ); - } - - /** - * Test @see rp_get_customer_handle_generation - */ - public function test_rp_get_customer_handle_generation_zero_customer() { - $this->api_mock->method('request')->willReturn( new WP_Error() ); - - $this->assertSame( - 'cust-' . time(), - rp_get_customer_handle( 0 ) - ); - } - - /** - * Test @see rp_get_customer_handle_exists - */ - public function test_rp_get_customer_handle_exists() { - $this->api_mock->method('request')->willReturn( new WP_Error() ); - - $user = $this->factory()->user->create(); - - rp_get_customer_handle( $user ); - - $this->assertSame( - 'customer-' . $user, - rp_get_customer_handle( $user ) - ); - } - - /** - * Test @see rp_get_customer_handle_exists - */ - public function test_rp_get_customer_exists_in_api() { - $this->api_mock->method('request')->willReturn( array( - 'content' => array( - array( - 'handle' => 'rp-custom-handle-1' - ) - ) - ) ); - - $user = $this->factory()->user->create(); - - $this->assertSame( - 'rp-custom-handle-1', - rp_get_customer_handle( $user ) - ); - } - - /** - * Test rp_get_userid_by_handle with guest handle - */ - public function test_rp_get_userid_by_handle_guest() { - $this->assertSame( - 0, - rp_get_userid_by_handle( 'guest-0' ) - ); - } - - /** - * Test rp_get_userid_by_handle with customer handle - */ - public function test_rp_get_userid_by_handle_customer() { - $user = $this->factory()->user->create(); - rp_get_customer_handle( $user ); - - $this->assertSame( - $user, - rp_get_userid_by_handle( "customer-$user" ) - ); - } -} diff --git a/tests/unit/functions/FormatTest.php b/tests/unit/functions/FormatTest.php deleted file mode 100644 index 666fa2b5..00000000 --- a/tests/unit/functions/FormatTest.php +++ /dev/null @@ -1,33 +0,0 @@ -assertSame( - $result, - rp_format_credit_card( $card_number ) - ); - } -} diff --git a/tests/unit/orderFlow/OrderCaptureTest.php b/tests/unit/orderFlow/OrderCaptureTest.php index 84a9b5e2..8dcd7636 100644 --- a/tests/unit/orderFlow/OrderCaptureTest.php +++ b/tests/unit/orderFlow/OrderCaptureTest.php @@ -50,7 +50,7 @@ public function test_add_item_capture_button() { $this->order_generator->order()->save(); - $price = OrderCapture::get_item_price( $order_item_id, $this->order_generator->order() ); + $price = OrderCapture::get_item_price( WC_Order_Factory::get_order_item( $order_item_id ), $this->order_generator->order() ); $this->expectOutputString( reepay()->get_template( @@ -891,7 +891,7 @@ public function test_get_item_data_prices_not_include_tax() { */ public function test_get_item_data_prices_include_tax() { $product_name = 'test product'; - $price = 12.34; + $price = 12; $qty = 2; $tax_rate = 10; @@ -911,11 +911,12 @@ public function test_get_item_data_prices_include_tax() { $this->order_generator->add_tax( $tax_rate ); + $this->assertEqualsCanonicalizing( array( 'ordertext' => $product_name, 'quantity' => $qty, - 'amount' => round( $price * ( 100 + $tax_rate ) ), + 'amount' => round( $price * ( 100 + $tax_rate )), 'vat' => $tax_rate / 100, 'amount_incl_vat' => true, ), @@ -944,8 +945,9 @@ public function test_get_item_price_product() { array( 'original' => $price * $qty, 'with_tax' => $price * $qty, + 'tax_percent' => 0, ), - OrderCapture::get_item_price( $order_item_id, $this->order_generator->order() ) + OrderCapture::get_item_price( WC_Order_Factory::get_order_item( $order_item_id ), $this->order_generator->order() ) ); } @@ -972,8 +974,9 @@ public function test_get_item_price_product_with_discount() { array( 'original' => $sale_price * $qty, 'with_tax' => $sale_price * $qty, + 'tax_percent' => 0, ), - OrderCapture::get_item_price( $order_item_id, $this->order_generator->order() ) + OrderCapture::get_item_price( WC_Order_Factory::get_order_item( $order_item_id ), $this->order_generator->order() ) ); } @@ -1004,9 +1007,10 @@ public function test_get_item_price_product_with_taxes() { $this->assertEqualsCanonicalizing( array( 'original' => $sale_price * $qty, - 'with_tax' => ( $sale_price * $qty ) * ( 1 + $tax_rate / 100 ), + 'with_tax' => round(( $sale_price * $qty ) * ( 1 + $tax_rate / 100 ), 2), + 'tax_percent' => round($tax_rate), ), - OrderCapture::get_item_price( $order_item_id, $this->order_generator->order() ) + OrderCapture::get_item_price( WC_Order_Factory::get_order_item( $order_item_id ), $this->order_generator->order() ) ); } @@ -1026,8 +1030,9 @@ public function test_get_item_price_shipping() { array( 'original' => $price, 'with_tax' => $price, + 'tax_percent' => 0, ), - OrderCapture::get_item_price( $order_item_id, $this->order_generator->order() ) + OrderCapture::get_item_price( WC_Order_Factory::get_order_item( $order_item_id ), $this->order_generator->order() ) ); } @@ -1047,8 +1052,9 @@ public function test_get_item_price_fee() { array( 'original' => $price, 'with_tax' => $price, + 'tax_percent' => 0, ), - OrderCapture::get_item_price( $order_item_id, $this->order_generator->order() ) + OrderCapture::get_item_price( WC_Order_Factory::get_order_item( $order_item_id ), $this->order_generator->order() ) ); } } diff --git a/tests/unit/tokens/MainTest.php b/tests/unit/tokens/MainTest.php index 2eda7b1a..37443d80 100644 --- a/tests/unit/tokens/MainTest.php +++ b/tests/unit/tokens/MainTest.php @@ -21,6 +21,9 @@ class MainTest extends Reepay_UnitTestCase { private Main $tokens_main; + /** + * Set up + */ public function set_up() { parent::set_up();