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
|
|
|
|
2021-02-17 14:35:37 +01: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(
|
2021-02-17 14:35:37 +01:00
|
|
|
'button.client_id' => static function ( ContainerInterface $container ): string {
|
2020-06-30 11:56:13 +03:00
|
|
|
|
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-06-30 11:56:13 +03:00
|
|
|
|
2020-08-31 11:12:46 +03:00
|
|
|
$env = $container->get( 'onboarding.environment' );
|
|
|
|
/**
|
|
|
|
* The environment.
|
|
|
|
*
|
|
|
|
* @var Environment $env
|
|
|
|
*/
|
2020-06-30 11:56:13 +03:00
|
|
|
|
2020-08-31 11:12:46 +03:00
|
|
|
return $env->current_environment_is( Environment::SANDBOX ) ?
|
2020-10-13 09:42:34 +03:00
|
|
|
CONNECT_WOO_SANDBOX_CLIENT_ID : CONNECT_WOO_CLIENT_ID;
|
2020-08-31 11:12:46 +03:00
|
|
|
},
|
2021-02-17 14:35:37 +01: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
|
|
|
|
*/
|
2021-07-27 11:07:51 +02:00
|
|
|
if ( $state->current_state() <= State::STATE_PROGRESSIVE ) {
|
2020-08-31 11:12:46 +03:00
|
|
|
return new DisabledSmartButton();
|
|
|
|
}
|
|
|
|
$settings = $container->get( 'wcgateway.settings' );
|
|
|
|
$paypal_disabled = ! $settings->has( 'enabled' ) || ! $settings->get( 'enabled' );
|
2020-09-03 11:25:02 +03:00
|
|
|
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-06-30 11:56:13 +03:00
|
|
|
|
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' );
|
2020-10-16 13:32:26 +03:00
|
|
|
$environment = $container->get( 'onboarding.environment' );
|
2021-09-17 17:11:36 +02:00
|
|
|
$payment_token_repository = $container->get( 'vaulting.repository.payment-token' );
|
2021-06-17 11:51:43 +02:00
|
|
|
$settings_status = $container->get( 'wcgateway.settings.status' );
|
2021-11-30 10:40:38 +02:00
|
|
|
$currency = $container->get( 'api.shop.currency' );
|
2020-08-31 11:12:46 +03:00
|
|
|
return new SmartButton(
|
|
|
|
$container->get( 'button.url' ),
|
|
|
|
$container->get( 'session.handler' ),
|
|
|
|
$settings,
|
|
|
|
$payer_factory,
|
|
|
|
$client_id,
|
|
|
|
$request_data,
|
|
|
|
$dcc_applies,
|
|
|
|
$subscription_helper,
|
2020-10-16 13:32:26 +03:00
|
|
|
$messages_apply,
|
2021-03-25 16:11:45 +01:00
|
|
|
$environment,
|
2021-06-17 11:51:43 +02:00
|
|
|
$payment_token_repository,
|
2021-11-30 10:40:38 +02:00
|
|
|
$settings_status,
|
|
|
|
$currency
|
2020-08-31 11:12:46 +03:00
|
|
|
);
|
|
|
|
},
|
2021-02-17 14:35:37 +01:00
|
|
|
'button.url' => static function ( ContainerInterface $container ): string {
|
2020-08-31 11:12:46 +03:00
|
|
|
return plugins_url(
|
2020-09-01 14:53:03 +03:00
|
|
|
'/modules/ppcp-button/',
|
2022-01-14 16:35:13 +02:00
|
|
|
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
|
2020-08-31 11:12:46 +03:00
|
|
|
);
|
|
|
|
},
|
2021-02-17 14:35:37 +01:00
|
|
|
'button.request-data' => static function ( ContainerInterface $container ): RequestData {
|
2020-08-31 11:12:46 +03:00
|
|
|
return new RequestData();
|
|
|
|
},
|
2021-02-17 14:35:37 +01:00
|
|
|
'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
|
|
|
},
|
2021-02-17 14:35:37 +01:00
|
|
|
'button.endpoint.create-order' => static function ( ContainerInterface $container ): CreateOrderEndpoint {
|
2020-09-30 14:24:31 +03:00
|
|
|
$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' );
|
2021-10-20 10:13:27 +03:00
|
|
|
$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,
|
2020-09-30 14:24:31 +03:00
|
|
|
$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,
|
2021-10-20 10:13:27 +03:00
|
|
|
$registration_needed,
|
2021-09-24 10:33:21 +02:00
|
|
|
$logger
|
2020-08-31 11:12:46 +03:00
|
|
|
);
|
|
|
|
},
|
2021-02-17 14:35:37 +01: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 );
|
|
|
|
},
|
2021-02-17 14:35:37 +01:00
|
|
|
'button.endpoint.approve-order' => static function ( ContainerInterface $container ): ApproveOrderEndpoint {
|
2020-08-31 11:12:46 +03:00
|
|
|
$request_data = $container->get( 'button.request-data' );
|
2020-09-18 11:55:25 +03:00
|
|
|
$order_endpoint = $container->get( 'api.endpoint.order' );
|
2020-08-31 11:12:46 +03:00
|
|
|
$session_handler = $container->get( 'session.handler' );
|
2020-09-18 11:55:25 +03:00
|
|
|
$three_d_secure = $container->get( 'button.helper.three-d-secure' );
|
|
|
|
$settings = $container->get( 'wcgateway.settings' );
|
2020-09-29 09:59:22 +03:00
|
|
|
$dcc_applies = $container->get( 'api.helpers.dccapplies' );
|
2021-05-10 17:12:46 +02:00
|
|
|
$logger = $container->get( 'woocommerce.logger.woocommerce' );
|
2020-09-29 09:59:22 +03:00
|
|
|
return new ApproveOrderEndpoint(
|
|
|
|
$request_data,
|
|
|
|
$order_endpoint,
|
|
|
|
$session_handler,
|
|
|
|
$three_d_secure,
|
|
|
|
$settings,
|
2021-05-10 17:12:46 +02:00
|
|
|
$dcc_applies,
|
|
|
|
$logger
|
2020-09-29 09:59:22 +03:00
|
|
|
);
|
2020-08-31 11:12:46 +03:00
|
|
|
},
|
2021-02-17 14:35:37 +01: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' );
|
2021-09-24 10:09:17 +02:00
|
|
|
$logger = $container->get( 'woocommerce.logger.woocommerce' );
|
2020-08-31 11:12:46 +03:00
|
|
|
return new DataClientIdEndpoint(
|
|
|
|
$request_data,
|
2021-09-24 10:09:17 +02:00
|
|
|
$identity_token,
|
|
|
|
$logger
|
2020-08-31 11:12:46 +03:00
|
|
|
);
|
|
|
|
},
|
2021-02-17 14:35:37 +01: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
|
|
|
},
|
2021-02-17 14:35:37 +01:00
|
|
|
'button.helper.messages-apply' => static function ( ContainerInterface $container ): MessagesApply {
|
2021-11-30 10:40:38 +02:00
|
|
|
return new MessagesApply(
|
|
|
|
$container->get( 'api.shop.country' )
|
|
|
|
);
|
2020-08-31 11:12:46 +03:00
|
|
|
},
|
2021-10-20 10:13:27 +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
|
|
|
);
|