Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion modules/ppcp-store-sync/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use WooCommerce\PayPalCommerce\StoreSync\Config\AgenticWebhookConfiguration;
use WooCommerce\PayPalCommerce\StoreSync\Config\IngestionConfiguration;
use WooCommerce\PayPalCommerce\StoreSync\Config\StoreCurrencyValue;
use WooCommerce\PayPalCommerce\StoreSync\Auth\AuthServiceProvider;
use WooCommerce\PayPalCommerce\StoreSync\Auth\PayPalJwkProvider;
use WooCommerce\PayPalCommerce\StoreSync\Endpoint\CreateCartEndpoint;
Expand Down Expand Up @@ -85,6 +86,9 @@
'agentic.config.ingestion' => static function (): IngestionConfiguration {
return new IngestionConfiguration();
},
'agentic.config.store-currency' => static function (): StoreCurrencyValue {
return new StoreCurrencyValue();
},

// Registration and merchant identification.
'agentic.merchant.provider' => static function ( ContainerInterface $c ): MerchantMetadataProvider {
Expand Down Expand Up @@ -226,7 +230,8 @@
return new ResponseFactory(
$c->get( 'agentic.helper.cart-builder' ),
$c->get( 'agentic.response.applied-coupons-builder' ),
$c->get( 'agentic.helper.shipping-options-builder' )
$c->get( 'agentic.helper.shipping-options-builder' ),
$c->get( 'agentic.config.store-currency' )
);
},

Expand Down
9 changes: 9 additions & 0 deletions modules/ppcp-store-sync/src/Config/StoreCurrencyValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
Comment thread
stracker-phil marked this conversation as resolved.

namespace WooCommerce\PayPalCommerce\StoreSync\Config;

class StoreCurrencyValue {
public function value(): string {
return get_woocommerce_currency();
}
}
21 changes: 19 additions & 2 deletions modules/ppcp-store-sync/src/Response/CartResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use WooCommerce\PayPalCommerce\StoreSync\Validation\ValidationIssue;
use WooCommerce\PayPalCommerce\StoreSync\Helper\CartHelper;
use WooCommerce\PayPalCommerce\StoreSync\Enums\ErrorCode;
use WooCommerce\PayPalCommerce\StoreSync\Config\StoreCurrencyValue;

class CartResponse {
private const ALLOWED_STATUS = array(
Expand All @@ -33,6 +34,8 @@ class CartResponse {

private PayPalCart $cart;

private string $default_currency = '';

private ?WC_Cart $wc_cart = null;

private array $applied_coupons = array();
Expand Down Expand Up @@ -160,6 +163,18 @@ public function wc_cart( ?WC_Cart $wc_cart ): self {
return $this;
}

/**
* Configures the CartResponse instance - only used by the ResponseFactory.
*
* @param StoreCurrencyValue $store_currency Resolves the WooCommerce currency code.
* @return $this
*/
public function store_currency( StoreCurrencyValue $store_currency ): self {
$this->default_currency = $store_currency->value();

return $this;
}

/**
* Convert to array for API response.
*
Expand Down Expand Up @@ -216,9 +231,11 @@ private function calculate_totals(): ?array {
return null;
}

$currency_code = CartHelper::currency( $this->cart );
return CartHelper::calculate_totals( $this->wc_cart, $this->currency_code() );
}

return CartHelper::calculate_totals( $this->wc_cart, $currency_code );
private function currency_code(): string {
return CartHelper::currency( $this->cart, $this->default_currency );
}

private function status(): string {
Expand Down
16 changes: 8 additions & 8 deletions modules/ppcp-store-sync/src/Response/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,25 @@
use WooCommerce\PayPalCommerce\StoreSync\Helper\AgenticCartBuilder;
use WooCommerce\PayPalCommerce\StoreSync\Helper\ShippingOptionsBuilder;
use WooCommerce\PayPalCommerce\StoreSync\Schema\PayPalCart;
use WooCommerce\PayPalCommerce\StoreSync\Config\StoreCurrencyValue;

class ResponseFactory {

private AgenticCartBuilder $cart_builder;
private AppliedCouponsBuilder $applied_coupons_builder;
private ShippingOptionsBuilder $shipping_options_builder;
private StoreCurrencyValue $store_currency;

/**
* Constructor.
*
* @param AgenticCartBuilder $cart_builder Cart builder service.
* @param AppliedCouponsBuilder $applied_coupons_builder Applied coupons builder service.
* @param ShippingOptionsBuilder $shipping_options_builder Shipping options builder service.
*/
public function __construct(
AgenticCartBuilder $cart_builder,
AppliedCouponsBuilder $applied_coupons_builder,
ShippingOptionsBuilder $shipping_options_builder
ShippingOptionsBuilder $shipping_options_builder,
StoreCurrencyValue $store_currency
) {
$this->cart_builder = $cart_builder;
$this->applied_coupons_builder = $applied_coupons_builder;
$this->shipping_options_builder = $shipping_options_builder;
$this->store_currency = $store_currency;
}

/**
Expand All @@ -52,6 +49,7 @@ public function new_cart( PayPalCart $cart, string $cart_id, string $token ): Ca

return CartResponse::create_new( $cart, $cart_id, $token )
->wc_cart( $wc_cart )
->store_currency( $this->store_currency )
->applied_coupons( $this->build_applied_coupons( $cart ) )
->shipping_options( $this->shipping_options_builder->build( $wc_cart ) );
}
Expand All @@ -69,6 +67,7 @@ public function from_order( WC_Order $order, PayPalCart $cart, string $cart_id )

return CartResponse::create_completed( $cart, $cart_id, $order )
->wc_cart( $wc_cart )
->store_currency( $this->store_currency )
->applied_coupons( $this->build_applied_coupons( $cart ) )
->shipping_options( $this->shipping_options_builder->build( $wc_cart ) );
}
Expand All @@ -85,6 +84,7 @@ public function from_cart( PayPalCart $cart, string $cart_id ): CartResponse {

return CartResponse::create( $cart, $cart_id )
->wc_cart( $wc_cart )
->store_currency( $this->store_currency )
->applied_coupons( $this->build_applied_coupons( $cart ) )
->shipping_options( $this->shipping_options_builder->build( $wc_cart ) );
}
Expand Down
25 changes: 25 additions & 0 deletions tests/PHPUnit/StoreSync/Config/StoreCurrencyValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare( strict_types = 1 );

namespace WooCommerce\PayPalCommerce\StoreSync\Config;

use WooCommerce\PayPalCommerce\TestCase;
use function Brain\Monkey\Functions\when;

/**
* @covers \WooCommerce\PayPalCommerce\StoreSync\Config\StoreCurrencyValue
*/
class StoreCurrencyValueTest extends TestCase {
/**
* GIVEN the WooCommerce store is configured with USD as the active currency
* WHEN value() is called
* THEN it returns the string 'USD'
*/
public function test_value_returns_woocommerce_currency_code(): void {
when( 'get_woocommerce_currency' )->justReturn( 'USD' );

$testee = new StoreCurrencyValue();

$this->assertSame( 'USD', $testee->value() );
}
}
Loading