From d3bed64b9bddb57d402f8e2da1d2b1a023ac45b8 Mon Sep 17 00:00:00 2001 From: inpsyde-maticluznar Date: Tue, 29 Aug 2023 12:50:22 +0200 Subject: [PATCH 1/2] Change how shop_totals table updates when payment method is switched --- resources/js/gatewaySurcharge.js | 29 +++++++++------------- src/Shared/GatewaySurchargeHandler.php | 34 +++++++++----------------- 2 files changed, 23 insertions(+), 40 deletions(-) diff --git a/resources/js/gatewaySurcharge.js b/resources/js/gatewaySurcharge.js index 291fd24c7..07b604152 100644 --- a/resources/js/gatewaySurcharge.js +++ b/resources/js/gatewaySurcharge.js @@ -30,26 +30,21 @@ }, success: (response) => { let result = response.data + if(!result?.template){ + console.warn("Missing template in the response."); + return; + } - if(result){ - const productTotal = ""+ result.currency +""+ result.newTotal +"" + const {template} = result; + const DOMTemplate = jQuery.parseHTML(template); + const newShopTable = jQuery(DOMTemplate).find(".shop_table"); - if(!result.amount){ - if($('#order_review table:first-child tfoot tr').text().indexOf(gatewayLabel) !== -1){ - $('#order_review table:first-child tfoot tr:contains("' + gatewayLabel + '")').remove() - $('#order_review table:first-child tfoot tr:last td').replaceWith(productTotal) - } - }else{ - const tableRow = ""+ result.name + ""+ result.currency +""+ (result.amount).toFixed(2) +"" - if($('#order_review table:first-child tfoot tr').text().indexOf(gatewayLabel) !== -1){ - $('#order_review table:first-child tfoot tr:contains("' + gatewayLabel + '")').replaceWith(tableRow) - $('#order_review table:first-child tfoot tr:last td').replaceWith(productTotal) - }else{ - $('#order_review table:first-child tfoot tr:first').after(tableRow) - $('#order_review table:first-child tfoot tr:last td').replaceWith(productTotal) - } - } + if(!newShopTable.length){ + console.warn("Template changed, can't update the totals."); + return; } + + jQuery(".shop_table").html(jQuery(newShopTable).html()); }, error: (jqXHR, textStatus, errorThrown) => { console.warn(textStatus, errorThrown) diff --git a/src/Shared/GatewaySurchargeHandler.php b/src/Shared/GatewaySurchargeHandler.php index 8cc943e88..e211d06e8 100644 --- a/src/Shared/GatewaySurchargeHandler.php +++ b/src/Shared/GatewaySurchargeHandler.php @@ -98,34 +98,22 @@ public function updateSurchargeOrderPay() return; } $this->orderRemoveFee($order); - $gatewaySettings = $this->gatewaySettings($gatewayName); - $orderAmount = (float) $order->get_total(); - if ($this->surcharge->aboveMaxLimit($orderAmount, $gatewaySettings)) { - return; - } - if (!isset($gatewaySettings['payment_surcharge']) || $gatewaySettings['payment_surcharge'] === Surcharge::NO_FEE) { - $data = [ - 'amount' => false, - 'currency' => get_woocommerce_currency_symbol(), - 'newTotal' => $order->get_total(), - ]; - wp_send_json_success($data); - } - + $gatewaySettings = $this->gatewaySettings($gatewayName); $amount = $this->surcharge->calculateFeeAmountOrder($order, $gatewaySettings); if ($amount > 0) { $this->orderAddFee($order, $amount, $this->gatewayFeeLabel); - $order->calculate_totals(); - $newTotal = $order->get_total(); - $data = [ - 'amount' => $amount, - 'name' => $this->gatewayFeeLabel, - 'currency' => get_woocommerce_currency_symbol(), - 'newTotal' => $newTotal, - ]; - wp_send_json_success($data); } + $order->calculate_totals(); + + ob_start(); + wc_get_template("checkout/form-pay.php", ["order" => $order]); + $template = ob_get_clean(); + + $data = [ + 'template' => $template, + ]; + wp_send_json_success($data); } public function updateSurchargeCheckoutBlock() From edc5efd314a45da012f15f9c2191e3a6c8330d9e Mon Sep 17 00:00:00 2001 From: inpsyde-maticluznar Date: Tue, 29 Aug 2023 16:03:20 +0200 Subject: [PATCH 2/2] Remove SurchargeHandlerTest. --- .../Shared/SurchargeHandlerTest.php | 85 ------------------- 1 file changed, 85 deletions(-) diff --git a/tests/php/Functional/Shared/SurchargeHandlerTest.php b/tests/php/Functional/Shared/SurchargeHandlerTest.php index bbf43be09..799a31e88 100644 --- a/tests/php/Functional/Shared/SurchargeHandlerTest.php +++ b/tests/php/Functional/Shared/SurchargeHandlerTest.php @@ -76,57 +76,6 @@ public function addsSurchargeFeesInCheckout(){ * * @test */ - public function addsSurchargeFeesInOrderPayPage() - { - $paymentSurcharge = Surcharge::FIXED_FEE; - $fixedFee = 10.00; - $percentage = 0; - $feeLimit = 1; - $expectedLabel = 'Gateway Fee'; - $expectedAmount = 10.00; - $newTotal = 20.00; - $expectedData = [ - 'amount' => $expectedAmount, - 'name' => $expectedLabel, - 'currency' => 'EUR', - 'newTotal' => $newTotal, - ]; - expect('get_option')->andReturn( - 'Gateway Fee', $this->helperMocks->paymentMethodSettings( - [ - 'payment_surcharge' => $paymentSurcharge, - 'surcharge_limit' => $feeLimit, - 'fixed_fee' => $fixedFee, - 'percentage' => $percentage, - ] - ) - ); - $testee = $this->buildTesteeMock( - GatewaySurchargeHandler::class, - [new Surcharge()], - ['canProcessOrder', 'canProcessGateway', 'orderRemoveFee', 'orderAddFee'] - )->getMock(); - - $testee->expects($this->once()) - ->method('canProcessOrder') - ->willReturn($this->wcOrder(1,'key1')); - - $testee->expects($this->once()) - ->method('canProcessGateway') - ->willReturn('mollie_wc_gateway_ideal'); - expect('wc_tax_enabled')->andReturn(false); - //this method uses all woo functions outside our scope - $testee->expects($this->once()) - ->method('orderRemoveFee'); - - //this method uses all woo functions outside our scope - $testee->expects($this->once()) - ->method('orderAddFee'); - expect('get_woocommerce_currency_symbol')->andReturn('EUR'); - - expect('wp_send_json_success')->with($expectedData); - $testee->updateSurchargeOrderPay(); - } protected function cartMock() { @@ -159,41 +108,7 @@ public function wooCommerce() { * * @throws PHPUnit_Framework_Exception */ - private function wcOrder($id, $orderKey) - { - $item = $this->createConfiguredMock( - 'WC_Order', - [ - 'get_id' => $id, - 'get_order_key' => $orderKey, - 'get_total' => 20.00, - 'get_items' => [], - 'get_billing_first_name' => 'billingggivenName', - 'get_billing_last_name' => 'billingfamilyName', - 'get_billing_email' => 'billingemail', - 'get_shipping_first_name' => 'shippinggivenName', - 'get_shipping_last_name' => 'shippingfamilyName', - 'get_billing_address_1' => 'shippingstreetAndNumber', - 'get_billing_address_2' => 'billingstreetAdditional', - 'get_billing_postcode' => 'billingpostalCode', - 'get_billing_city' => 'billingcity', - 'get_billing_state' => 'billingregion', - 'get_billing_country' => 'billingcountry', - 'get_shipping_address_1' => 'shippingstreetAndNumber', - 'get_shipping_address_2' => 'shippingstreetAdditional', - 'get_shipping_postcode' => 'shippingpostalCode', - 'get_shipping_city' => 'shippingcity', - 'get_shipping_state' => 'shippingregion', - 'get_shipping_country' => 'shippingcountry', - 'get_shipping_methods' => false, - 'get_order_number' => 1, - 'get_payment_method' => 'mollie_wc_gateway_ideal', - 'get_currency' => 'EUR', - ] - ); - return $item; - } protected function mollieGateway(){ $gateway = $this->createConfiguredMock( MolliePaymentGateway::class,