Get store currency from service

This commit is contained in:
Emili Castells Guasch 2023-04-11 11:58:11 +02:00
parent 7d219bbd14
commit 6d400c7bf3
3 changed files with 79 additions and 23 deletions

View file

@ -392,10 +392,10 @@ return array(
return new ProductFactory(); return new ProductFactory();
}, },
'api.factory.billing-cycle' => static function( ContainerInterface $container ): BillingCycleFactory { 'api.factory.billing-cycle' => static function( ContainerInterface $container ): BillingCycleFactory {
return new BillingCycleFactory(); return new BillingCycleFactory( $container->get( 'api.shop.currency' ) );
}, },
'api.factory.payment-preferences' => static function( ContainerInterface $container ):PaymentPreferencesFactory { 'api.factory.payment-preferences' => static function( ContainerInterface $container ):PaymentPreferencesFactory {
return new PaymentPreferencesFactory(); return new PaymentPreferencesFactory( $container->get( 'api.shop.currency' ) );
}, },
'api.factory.plan' => static function( ContainerInterface $container ): PlanFactory { 'api.factory.plan' => static function( ContainerInterface $container ): PlanFactory {
return new PlanFactory( return new PlanFactory(

View file

@ -15,6 +15,28 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\BillingCycle;
class BillingCycleFactory { class BillingCycleFactory {
/**
* The currency.
*
* @var string
*/
private $currency;
/**
* The currency.
*
* @param string $currency
*/
public function __construct( string $currency ) {
$this->currency = $currency;
}
/**
* Returns a BillingCycle object from the given WC product.
*
* @param WC_Product $product WC product.
* @return BillingCycle
*/
public function from_wc_product( WC_Product $product ): BillingCycle { public function from_wc_product( WC_Product $product ): BillingCycle {
return new BillingCycle( return new BillingCycle(
array( array(
@ -26,13 +48,19 @@ class BillingCycleFactory {
array( array(
'fixed_price' => array( 'fixed_price' => array(
'value' => $product->get_meta( '_subscription_price' ), 'value' => $product->get_meta( '_subscription_price' ),
'currency_code' => 'USD', 'currency_code' => $this->currency,
), ),
), ),
(int) $product->get_meta( '_subscription_length' ) (int) $product->get_meta( '_subscription_length' )
); );
} }
/**
* Returns a BillingCycle object based off a PayPal response.
*
* @param stdClass $data the data.
* @return BillingCycle
*/
public function from_paypal_response( stdClass $data ): BillingCycle { public function from_paypal_response( stdClass $data ): BillingCycle {
return new BillingCycle( return new BillingCycle(
array( array(

View file

@ -15,15 +15,43 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentPreferences;
class PaymentPreferencesFactory { class PaymentPreferencesFactory {
/**
* The currency.
*
* @var string
*/
private $currency;
/**
* PaymentPreferencesFactory constructor.
*
* @param string $currency The currency.
*/
public function __construct( string $currency ) {
$this->currency = $currency;
}
/**
* Returns a PaymentPreferences object from the given WC product.
*
* @param WC_Product $product WC product.
* @return PaymentPreferences
*/
public function from_wc_product( WC_Product $product ):PaymentPreferences { public function from_wc_product( WC_Product $product ):PaymentPreferences {
return new PaymentPreferences( return new PaymentPreferences(
array( array(
'value' => $product->get_meta( '_subscription_sign_up_fee' ) ?: '0', 'value' => $product->get_meta( '_subscription_sign_up_fee' ) ?: '0',
'currency_code' => 'USD', 'currency_code' => $this->currency,
) )
); );
} }
/**
* Returns a PaymentPreferences object based off a PayPal response.
*
* @param stdClass $data The data.
* @return PaymentPreferences
*/
public function from_paypal_response( stdClass $data ) { public function from_paypal_response( stdClass $data ) {
return new PaymentPreferences( return new PaymentPreferences(
array( array(