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

@ -220,7 +220,7 @@ return array(
return new CatalogProducts(
$container->get( 'api.host' ),
$container->get( 'api.bearer' ),
$container->get('api.factory.product'),
$container->get( 'api.factory.product' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
@ -228,8 +228,8 @@ return array(
return new BillingPlans(
$container->get( 'api.host' ),
$container->get( 'api.bearer' ),
$container->get('api.factory.billing-cycle'),
$container->get('api.factory.plan'),
$container->get( 'api.factory.billing-cycle' ),
$container->get( 'api.factory.plan' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
@ -388,19 +388,19 @@ return array(
'api.factory.fraud-processor-response' => static function ( ContainerInterface $container ): FraudProcessorResponseFactory {
return new FraudProcessorResponseFactory();
},
'api.factory.product' => static function(ContainerInterface $container): ProductFactory {
'api.factory.product' => static function( ContainerInterface $container ): ProductFactory {
return new ProductFactory();
},
'api.factory.billing-cycle' => static function(ContainerInterface $container): BillingCycleFactory {
return new BillingCycleFactory();
'api.factory.billing-cycle' => static function( ContainerInterface $container ): BillingCycleFactory {
return new BillingCycleFactory( $container->get( 'api.shop.currency' ) );
},
'api.factory.payment-preferences' => static function(ContainerInterface $container):PaymentPreferencesFactory {
return new PaymentPreferencesFactory();
'api.factory.payment-preferences' => static function( ContainerInterface $container ):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(
$container->get('api.factory.billing-cycle'),
$container->get('api.factory.payment-preferences')
$container->get( 'api.factory.billing-cycle' ),
$container->get( 'api.factory.payment-preferences' )
);
},
'api.helpers.dccapplies' => static function ( ContainerInterface $container ) : DccApplies {

View file

@ -15,35 +15,63 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\BillingCycle;
class BillingCycleFactory {
public function from_wc_product(WC_Product $product): BillingCycle {
/**
* 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 {
return new BillingCycle(
array(
'interval_unit' => $product->get_meta('_subscription_period'),
'interval_count' => $product->get_meta('_subscription_period_interval'),
'interval_unit' => $product->get_meta( '_subscription_period' ),
'interval_count' => $product->get_meta( '_subscription_period_interval' ),
),
1,
'REGULAR',
array(
'fixed_price' => array(
'value' => $product->get_meta('_subscription_price'),
'currency_code' => 'USD',
'value' => $product->get_meta( '_subscription_price' ),
'currency_code' => $this->currency,
),
),
(int)$product->get_meta('_subscription_length')
(int) $product->get_meta( '_subscription_length' )
);
}
public function from_paypal_response(stdClass $data): BillingCycle {
/**
* Returns a BillingCycle object based off a PayPal response.
*
* @param stdClass $data the data.
* @return BillingCycle
*/
public function from_paypal_response( stdClass $data ): BillingCycle {
return new BillingCycle(
array(
'interval_unit' => $data->frequency->interval_unit,
'interval_unit' => $data->frequency->interval_unit,
'interval_count' => $data->frequency->interval_count,
),
$data->sequence,
$data->tenure_type,
array(
'fixed_price' => array(
'value' => $data->pricing_scheme->fixed_price->value,
'value' => $data->pricing_scheme->fixed_price->value,
'currency_code' => $data->pricing_scheme->fixed_price->currency_code,
),
),

View file

@ -15,16 +15,44 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentPreferences;
class PaymentPreferencesFactory {
public function from_wc_product(WC_Product $product):PaymentPreferences {
/**
* 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 {
return new PaymentPreferences(
array(
'value' => $product->get_meta( '_subscription_sign_up_fee' ) ?: '0',
'currency_code' => 'USD',
'currency_code' => $this->currency,
)
);
}
public function from_paypal_response(stdClass $data) {
/**
* Returns a PaymentPreferences object based off a PayPal response.
*
* @param stdClass $data The data.
* @return PaymentPreferences
*/
public function from_paypal_response( stdClass $data ) {
return new PaymentPreferences(
array(
'value' => $data->setup_fee->value,