woocommerce-paypal-payments/modules/ppcp-button/services.php

193 lines
7.6 KiB
PHP
Raw Normal View History

2020-04-02 08:38:00 +03:00
<?php
2020-08-31 11:12:46 +03:00
/**
* The button module services.
*
2020-09-11 14:11:10 +03:00
* @package WooCommerce\PayPalCommerce\Button
2020-08-31 11:12:46 +03:00
*/
2020-04-09 14:36:52 +03:00
2020-04-02 08:38:00 +03:00
declare(strict_types=1);
2020-09-11 14:11:10 +03:00
namespace WooCommerce\PayPalCommerce\Button;
2020-04-02 08:38:00 +03:00
use Psr\Container\ContainerInterface;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\Button\Assets\DisabledSmartButton;
use WooCommerce\PayPalCommerce\Button\Assets\SmartButton;
use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
use WooCommerce\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\DataClientIdEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
use WooCommerce\PayPalCommerce\Button\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Button\Helper\EarlyOrderHandler;
use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply;
use WooCommerce\PayPalCommerce\Button\Helper\ThreeDSecure;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State;
2020-04-02 08:38:00 +03:00
2020-08-31 11:12:46 +03:00
return array(
'button.client_id' => static function ( ContainerInterface $container ): string {
2020-08-31 11:12:46 +03:00
$settings = $container->get( 'wcgateway.settings' );
$client_id = $settings->has( 'client_id' ) ? $settings->get( 'client_id' ) : '';
if ( $client_id ) {
return $client_id;
}
2020-08-31 11:12:46 +03:00
$env = $container->get( 'onboarding.environment' );
/**
* The environment.
*
* @var Environment $env
*/
2020-08-31 11:12:46 +03:00
return $env->current_environment_is( Environment::SANDBOX ) ?
CONNECT_WOO_SANDBOX_CLIENT_ID : CONNECT_WOO_CLIENT_ID;
2020-08-31 11:12:46 +03:00
},
'button.smart-button' => static function ( ContainerInterface $container ): SmartButtonInterface {
2020-04-30 15:28:48 +03:00
2020-08-31 11:12:46 +03:00
$state = $container->get( 'onboarding.state' );
/**
* The state.
*
* @var State $state
*/
2022-01-31 18:16:25 +02:00
if ( $state->current_state() !== State::STATE_ONBOARDED ) {
2020-08-31 11:12:46 +03:00
return new DisabledSmartButton();
}
$settings = $container->get( 'wcgateway.settings' );
$paypal_disabled = ! $settings->has( 'enabled' ) || ! $settings->get( 'enabled' );
if ( $paypal_disabled ) {
2020-08-31 11:12:46 +03:00
return new DisabledSmartButton();
}
$payer_factory = $container->get( 'api.factory.payer' );
$request_data = $container->get( 'button.request-data' );
2020-08-31 11:12:46 +03:00
$client_id = $container->get( 'button.client_id' );
$dcc_applies = $container->get( 'api.helpers.dccapplies' );
$subscription_helper = $container->get( 'subscription.helper' );
$messages_apply = $container->get( 'button.helper.messages-apply' );
$environment = $container->get( 'onboarding.environment' );
$payment_token_repository = $container->get( 'vaulting.repository.payment-token' );
$settings_status = $container->get( 'wcgateway.settings.status' );
$currency = $container->get( 'api.shop.currency' );
2020-08-31 11:12:46 +03:00
return new SmartButton(
$container->get( 'button.url' ),
2022-02-17 18:51:24 +02:00
$container->get( 'ppcp.asset-version' ),
2020-08-31 11:12:46 +03:00
$container->get( 'session.handler' ),
$settings,
$payer_factory,
$client_id,
$request_data,
$dcc_applies,
$subscription_helper,
$messages_apply,
$environment,
$payment_token_repository,
$settings_status,
$currency
2020-08-31 11:12:46 +03:00
);
},
'button.url' => static function ( ContainerInterface $container ): string {
2020-08-31 11:12:46 +03:00
return plugins_url(
'/modules/ppcp-button/',
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
2020-08-31 11:12:46 +03:00
);
},
'button.request-data' => static function ( ContainerInterface $container ): RequestData {
2020-08-31 11:12:46 +03:00
return new RequestData();
},
'button.endpoint.change-cart' => static function ( ContainerInterface $container ): ChangeCartEndpoint {
2020-08-31 11:12:46 +03:00
if ( ! \WC()->cart ) {
throw new RuntimeException( 'cant initialize endpoint at this moment' );
}
$cart = WC()->cart;
$shipping = WC()->shipping();
$request_data = $container->get( 'button.request-data' );
$repository = $container->get( 'api.repository.cart' );
$data_store = \WC_Data_Store::load( 'product' );
2021-09-24 10:33:21 +02:00
$logger = $container->get( 'woocommerce.logger.woocommerce' );
return new ChangeCartEndpoint( $cart, $shipping, $request_data, $repository, $data_store, $logger );
2020-08-31 11:12:46 +03:00
},
'button.endpoint.create-order' => static function ( ContainerInterface $container ): CreateOrderEndpoint {
$request_data = $container->get( 'button.request-data' );
$cart_repository = $container->get( 'api.repository.cart' );
$purchase_unit_factory = $container->get( 'api.factory.purchase-unit' );
$order_endpoint = $container->get( 'api.endpoint.order' );
$payer_factory = $container->get( 'api.factory.payer' );
$session_handler = $container->get( 'session.handler' );
$settings = $container->get( 'wcgateway.settings' );
$early_order_handler = $container->get( 'button.helper.early-order-handler' );
$registration_needed = $container->get( 'button.current-user-must-register' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
2020-08-31 11:12:46 +03:00
return new CreateOrderEndpoint(
$request_data,
$cart_repository,
$purchase_unit_factory,
2020-08-31 11:12:46 +03:00
$order_endpoint,
$payer_factory,
$session_handler,
$settings,
2021-09-24 10:33:21 +02:00
$early_order_handler,
$registration_needed,
2021-09-24 10:33:21 +02:00
$logger
2020-08-31 11:12:46 +03:00
);
},
'button.helper.early-order-handler' => static function ( ContainerInterface $container ) : EarlyOrderHandler {
2020-08-25 10:18:53 +03:00
2020-08-31 11:12:46 +03:00
$state = $container->get( 'onboarding.state' );
$order_processor = $container->get( 'wcgateway.order-processor' );
$session_handler = $container->get( 'session.handler' );
$prefix = $container->get( 'api.prefix' );
return new EarlyOrderHandler( $state, $order_processor, $session_handler, $prefix );
},
'button.endpoint.approve-order' => static function ( ContainerInterface $container ): ApproveOrderEndpoint {
2020-08-31 11:12:46 +03:00
$request_data = $container->get( 'button.request-data' );
$order_endpoint = $container->get( 'api.endpoint.order' );
2020-08-31 11:12:46 +03:00
$session_handler = $container->get( 'session.handler' );
$three_d_secure = $container->get( 'button.helper.three-d-secure' );
$settings = $container->get( 'wcgateway.settings' );
$dcc_applies = $container->get( 'api.helpers.dccapplies' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
return new ApproveOrderEndpoint(
$request_data,
$order_endpoint,
$session_handler,
$three_d_secure,
$settings,
$dcc_applies,
$logger
);
2020-08-31 11:12:46 +03:00
},
'button.endpoint.data-client-id' => static function( ContainerInterface $container ) : DataClientIdEndpoint {
2020-08-31 11:12:46 +03:00
$request_data = $container->get( 'button.request-data' );
$identity_token = $container->get( 'api.endpoint.identity-token' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
2020-08-31 11:12:46 +03:00
return new DataClientIdEndpoint(
$request_data,
$identity_token,
$logger
2020-08-31 11:12:46 +03:00
);
},
'button.helper.three-d-secure' => static function ( ContainerInterface $container ): ThreeDSecure {
2021-09-21 11:34:16 +02:00
$logger = $container->get( 'woocommerce.logger.woocommerce' );
return new ThreeDSecure( $logger );
2020-08-31 11:12:46 +03:00
},
'button.helper.messages-apply' => static function ( ContainerInterface $container ): MessagesApply {
return new MessagesApply(
$container->get( 'api.shop.country' )
);
2020-08-31 11:12:46 +03:00
},
'button.is-logged-in' => static function ( ContainerInterface $container ): bool {
return is_user_logged_in();
},
'button.registration-required' => static function ( ContainerInterface $container ): bool {
return WC()->checkout()->is_registration_required();
},
'button.current-user-must-register' => static function ( ContainerInterface $container ): bool {
return ! $container->get( 'button.is-logged-in' ) &&
$container->get( 'button.registration-required' );
},
2020-08-31 11:12:46 +03:00
);