Skip to content

Commit 5b0f6d0

Browse files
authored
PES-2555: payment method restriction set for the carrier implemented in the block checkout (#785)
2 parents 9f43b83 + 9825dd8 commit 5b0f6d0

File tree

3 files changed

+173
-19
lines changed

3 files changed

+173
-19
lines changed

src/Packetery/Module/Checkout/Checkout.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,8 @@ private function calcTaxExclusiveFeeAmount( float $taxInclusiveFeeAmount, string
305305
* @return WC_Payment_Gateway[]
306306
*/
307307
public function filterPaymentGateways( array $availableGateways ): array {
308-
global $wp;
309-
310-
if ( ! is_checkout() ) {
311-
return $availableGateways;
312-
}
313-
314-
$order = null;
315-
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
316-
$wpOrderPay = $wp->query_vars['order-pay'] ?? null;
308+
$order = null;
309+
$wpOrderPay = $this->checkoutService->getOrderPayParameter();
317310
if ( is_numeric( $wpOrderPay ) ) {
318311
$order = $this->orderRepository->getByIdWithValidCarrier( (int) $wpOrderPay );
319312
}

src/Packetery/Module/Checkout/CheckoutService.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,14 @@ public function areBlocksUsedInCheckout(): bool {
262262

263263
return false;
264264
}
265+
266+
/**
267+
* @return string|null|int
268+
*/
269+
public function getOrderPayParameter() {
270+
global $wp;
271+
272+
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
273+
return $wp->query_vars['order-pay'] ?? null;
274+
}
265275
}

tests/Module/Checkout/CheckoutTest.php

Lines changed: 161 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Tests\Module\Checkout;
44

5-
use Packetery\Core\Entity\Carrier;
5+
use Packetery\Core\Entity;
6+
use Packetery\Core\Entity\Order;
7+
use Packetery\Module\Carrier;
68
use Packetery\Module\Carrier\CarrierOptionsFactory;
79
use Packetery\Module\Carrier\EntityRepository;
8-
use Packetery\Module\Carrier\Options;
910
use Packetery\Module\Checkout\CartService;
1011
use Packetery\Module\Checkout\Checkout;
1112
use Packetery\Module\Checkout\CheckoutRenderer;
@@ -22,6 +23,7 @@
2223
use Packetery\Module\Payment\PaymentHelper;
2324
use PHPUnit\Framework\MockObject\MockObject;
2425
use PHPUnit\Framework\TestCase;
26+
use Tests\Core\DummyFactory;
2527
use WC_Cart;
2628

2729
class CheckoutTest extends TestCase {
@@ -43,7 +45,7 @@ class CheckoutTest extends TestCase {
4345
private Checkout $checkout;
4446
private WC_Cart|MockObject $WCCart;
4547

46-
protected function creatCheckout(): void {
48+
protected function createCheckout(): void {
4749
$this->wpAdapter = $this->createMock( WpAdapter::class );
4850
$this->wcAdapter = $this->createMock( WcAdapter::class );
4951
$this->carrierOptionsFactory = $this->createMock( CarrierOptionsFactory::class );
@@ -81,7 +83,7 @@ protected function creatCheckout(): void {
8183
}
8284

8385
public function testDoesNothingIfNoChosenShippingMethod(): void {
84-
$this->creatCheckout();
86+
$this->createCheckout();
8587
$this->checkoutService->method( 'calculateShippingAndGetOptionId' )->willReturn( null );
8688

8789
$this->rateCalculator->expects( $this->never() )->method( 'getCODSurcharge' );
@@ -90,7 +92,7 @@ public function testDoesNothingIfNoChosenShippingMethod(): void {
9092
}
9193

9294
public function testDoesNothingIfShippingMethodNotPacketery(): void {
93-
$this->creatCheckout();
95+
$this->createCheckout();
9496
$this->checkoutService->method( 'calculateShippingAndGetOptionId' )->willReturn( 'non_packetery_method' );
9597
$this->checkoutService->method( 'isPacketeryShippingMethod' )->willReturn( false );
9698

@@ -100,11 +102,11 @@ public function testDoesNothingIfShippingMethodNotPacketery(): void {
100102
}
101103

102104
public function testSkipsWhenCouponAllowsFreeShipping(): void {
103-
$this->creatCheckout();
105+
$this->createCheckout();
104106
$this->checkoutService->method( 'calculateShippingAndGetOptionId' )->willReturn( 'packetery_method' );
105107
$this->checkoutService->method( 'isPacketeryShippingMethod' )->willReturn( true );
106108

107-
$carrierOptions = $this->createMock( Options::class );
109+
$carrierOptions = $this->createMock( Carrier\Options::class );
108110
$carrierOptions->method( 'hasCouponFreeShippingForFeesAllowed' )->willReturn( true );
109111

110112
$this->carrierOptionsFactory->method( 'createByOptionId' )->willReturn( $carrierOptions );
@@ -115,11 +117,11 @@ public function testSkipsWhenCouponAllowsFreeShipping(): void {
115117
}
116118

117119
public function testAddsFeesSuccessfully(): void {
118-
$this->creatCheckout();
120+
$this->createCheckout();
119121
$this->checkoutService->method( 'calculateShippingAndGetOptionId' )->willReturn( 'packetery_method' );
120122
$this->checkoutService->method( 'isPacketeryShippingMethod' )->willReturn( true );
121123

122-
$carrierOptions = $this->createMock( Options::class );
124+
$carrierOptions = $this->createMock( Carrier\Options::class );
123125
$carrierOptions->method( 'hasCouponFreeShippingForFeesAllowed' )->willReturn( false );
124126
$carrierOptions->method( 'toArray' )->willReturn( [ 'key' => 'value' ] );
125127

@@ -132,7 +134,7 @@ public function testAddsFeesSuccessfully(): void {
132134
->with( [ 'key' => 'value' ], $this->anything() )
133135
->willReturn( 5.00 );
134136

135-
$carrier = $this->createMock( Carrier::class );
137+
$carrier = $this->createMock( Entity\Carrier::class );
136138
$carrier->method( 'supportsAgeVerification' )->willReturn( true );
137139
$this->carrierEntityRepository->method( 'getAnyById' )->willReturn( $carrier );
138140
$carrierOptions->method( 'getAgeVerificationFee' )->willReturn( 10.0 );
@@ -147,4 +149,153 @@ public function testAddsFeesSuccessfully(): void {
147149

148150
$this->checkout->actionCalculateFees( $this->WCCart );
149151
}
152+
153+
public static function filterPaymentGatewaysProvider(): array {
154+
$gateway1 = (object) [ 'id' => 'gateway1' ];
155+
$gateway2 = (object) [ 'id' => 'gateway2' ];
156+
$codGateway = (object) [ 'id' => 'cod_gateway' ];
157+
158+
$carrierSupportingCod = DummyFactory::createCarrierCzechPp();
159+
$carrierWithoutCod = DummyFactory::createCarDeliveryCarrier();
160+
161+
$order = DummyFactory::createOrderCzPp();
162+
163+
return [
164+
'no packetery method selected' => [
165+
'availableGateways' => [ $gateway1, $gateway2, $codGateway ],
166+
'orderPayParameter' => null,
167+
'order' => null,
168+
'chosenMethod' => null,
169+
'isPacketeryMethod' => false,
170+
'carrier' => null,
171+
'disallowedPaymentMethods' => [],
172+
'codPaymentMethods' => [],
173+
'expectedResult' => [ $gateway1, $gateway2, $codGateway ],
174+
],
175+
'packetery method with valid carrier' => [
176+
'availableGateways' => [ $gateway1, $gateway2, $codGateway ],
177+
'orderPayParameter' => 123,
178+
'order' => $order,
179+
'chosenMethod' => 'dummyRate',
180+
'isPacketeryMethod' => true,
181+
'carrier' => $carrierSupportingCod,
182+
'disallowedPaymentMethods' => [],
183+
'codPaymentMethods' => [
184+
'cod_gateway',
185+
],
186+
'expectedResult' => [ $gateway1, $gateway2, $codGateway ],
187+
],
188+
'packetery method with disallowed gateway' => [
189+
'availableGateways' => [ $gateway1, $gateway2, $codGateway ],
190+
'orderPayParameter' => null,
191+
'order' => null,
192+
'chosenMethod' => 'dummyRate',
193+
'isPacketeryMethod' => true,
194+
'carrier' => $carrierSupportingCod,
195+
'disallowedPaymentMethods' => [
196+
'gateway1',
197+
],
198+
'codPaymentMethods' => [
199+
'cod_gateway',
200+
],
201+
'expectedResult' => [ $gateway2, $codGateway ],
202+
],
203+
'packetery method, carrier without COD support' => [
204+
'availableGateways' => [ $gateway1, $gateway2, $codGateway ],
205+
'orderPayParameter' => null,
206+
'order' => null,
207+
'chosenMethod' => 'dummyRate',
208+
'isPacketeryMethod' => true,
209+
'carrier' => $carrierWithoutCod,
210+
'disallowedPaymentMethods' => [],
211+
'codPaymentMethods' => [
212+
'cod_gateway',
213+
],
214+
'expectedResult' => [ $gateway1, $gateway2 ],
215+
],
216+
'packetery method with invalid carrier' => [
217+
'availableGateways' => [ $gateway1, $gateway2, $codGateway ],
218+
'orderPayParameter' => null,
219+
'order' => null,
220+
'chosenMethod' => 'dummyRate',
221+
'isPacketeryMethod' => true,
222+
'carrier' => null,
223+
'disallowedPaymentMethods' => [],
224+
'codPaymentMethods' => [
225+
'cod_gateway',
226+
],
227+
'expectedResult' => [ $gateway1, $gateway2, $codGateway ],
228+
],
229+
];
230+
}
231+
232+
/**
233+
* @dataProvider filterPaymentGatewaysProvider
234+
*/
235+
public function testFilterPaymentGateways(
236+
array $availableGateways,
237+
?int $orderPayParameter,
238+
?Order $order,
239+
?string $chosenMethod,
240+
bool $isPacketeryMethod,
241+
?Entity\Carrier $carrier,
242+
array $disallowedPaymentMethods,
243+
array $codPaymentMethods,
244+
array $expectedResult
245+
): void {
246+
$this->createCheckout();
247+
248+
$this->checkoutService
249+
->method( 'getOrderPayParameter' )
250+
->willReturn( $orderPayParameter );
251+
252+
$this->orderRepository
253+
->method( 'getByIdWithValidCarrier' )
254+
->willReturn( $order );
255+
256+
$this->checkoutService
257+
->method( 'isPacketeryShippingMethod' )
258+
->willReturn( $isPacketeryMethod );
259+
260+
if ( $carrier !== null ) {
261+
$this->checkoutService
262+
->method( 'getCarrierIdFromPacketeryShippingMethod' )
263+
->willReturn( $carrier->getId() );
264+
}
265+
266+
$this->carrierEntityRepository
267+
->method( 'getAnyById' )
268+
->willReturn( $carrier );
269+
270+
if ( $chosenMethod !== null ) {
271+
$this->sessionService
272+
->method( 'getChosenMethodFromSession' )
273+
->willReturn( $chosenMethod );
274+
}
275+
276+
$carrierOptions = $this->createMock( Carrier\Options::class );
277+
$this->carrierOptionsFactory
278+
->method( 'createByCarrierId' )
279+
->willReturn( $carrierOptions );
280+
281+
$carrierOptions
282+
->method( 'hasCheckoutPaymentMethodDisallowed' )
283+
->willReturnCallback(
284+
function ( $gatewayId ) use ( $disallowedPaymentMethods ) {
285+
return in_array( $gatewayId, $disallowedPaymentMethods, true );
286+
}
287+
);
288+
289+
$this->paymentHelper
290+
->method( 'isCodPaymentMethod' )
291+
->willReturnCallback(
292+
function ( $gatewayId ) use ( $codPaymentMethods ) {
293+
return in_array( $gatewayId, $codPaymentMethods, true );
294+
}
295+
);
296+
297+
$result = $this->checkout->filterPaymentGateways( $availableGateways );
298+
299+
$this->assertEqualsCanonicalizing( $expectedResult, $result );
300+
}
150301
}

0 commit comments

Comments
 (0)