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

124 lines
5.3 KiB
PHP
Raw Normal View History

2020-04-02 08:38:00 +03:00
<?php
2020-04-09 14:36:52 +03:00
2020-04-02 08:38:00 +03:00
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Button;
use Dhii\Data\Container\ContainerInterface;
use Inpsyde\PayPalCommerce\Button\Assets\DisabledSmartButton;
use Inpsyde\PayPalCommerce\Button\Assets\SmartButton;
use Inpsyde\PayPalCommerce\Button\Assets\SmartButtonInterface;
use Inpsyde\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
2020-04-02 08:38:00 +03:00
use Inpsyde\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\DataClientIdEndpoint;
2020-04-02 08:38:00 +03:00
use Inpsyde\PayPalCommerce\Button\Endpoint\RequestData;
use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
2020-07-15 10:27:40 +03:00
use Inpsyde\PayPalCommerce\Button\Helper\ThreeDSecure;
use Inpsyde\PayPalCommerce\Onboarding\Environment;
2020-06-15 11:48:37 +03:00
use Inpsyde\PayPalCommerce\Onboarding\State;
2020-04-02 08:38:00 +03:00
return [
2020-07-02 13:11:47 +03:00
'button.client_id' => static function (ContainerInterface $container): string {
$settings = $container->get('wcgateway.settings');
$clientId = $settings->has('client_id') ? $settings->get('client_id') : '';
if ($clientId) {
return $clientId;
}
$env = $container->get('onboarding.environment');
/**
* @var Environment $env
*/
/**
* ToDo: Add production platform client Id.
*/
return $env->currentEnvironmentIs(Environment::SANDBOX) ?
'AQB97CzMsd58-It1vxbcDAGvMuXNCXRD9le_XUaMlHB_U7XsU9IiItBwGQOtZv9sEeD6xs2vlIrL4NiD' : '';
},
2020-04-28 12:31:12 +03:00
'button.smart-button' => static function (ContainerInterface $container): SmartButtonInterface {
2020-04-30 15:28:48 +03:00
2020-06-15 11:48:37 +03:00
$state = $container->get('onboarding.state');
/**
* @var State $state
*/
if ($state->currentState() < State::STATE_PROGRESSIVE) {
return new DisabledSmartButton();
}
$settings = $container->get('wcgateway.settings');
$paypalDisabled = !$settings->has('enabled') || ! $settings->get('enabled');
$creditCardDisabled = !$settings->has('dcc_gateway_enabled') || ! $settings->get('dcc_gateway_enabled');
if ($paypalDisabled && $creditCardDisabled) {
2020-04-30 15:28:48 +03:00
return new DisabledSmartButton();
}
2020-04-30 15:28:48 +03:00
$payeeRepository = $container->get('api.repository.payee');
$identityToken = $container->get('api.endpoint.identity-token');
$payerFactory = $container->get('api.factory.payer');
2020-07-10 12:33:13 +03:00
$requestData = $container->get('button.request-data');
$clientId = $container->get('button.client_id');
$dccApplies = $container->get('api.helpers.dccapplies');
2020-07-28 12:27:42 +03:00
$subscriptionHelper = $container->get('subscription.helper');
2020-04-30 15:28:48 +03:00
return new SmartButton(
$container->get('button.url'),
$container->get('session.handler'),
$settings,
$payeeRepository,
$identityToken,
$payerFactory,
2020-07-10 12:33:13 +03:00
$clientId,
$requestData,
2020-07-28 12:27:42 +03:00
$dccApplies,
$subscriptionHelper
2020-04-30 15:28:48 +03:00
);
2020-04-02 08:38:00 +03:00
},
2020-04-28 12:31:12 +03:00
'button.url' => static function (ContainerInterface $container): string {
2020-04-06 11:16:18 +03:00
return plugins_url(
2020-06-29 11:26:18 +03:00
'/modules.local/ppcp-button/',
2020-04-06 11:16:18 +03:00
dirname(__FILE__, 3) . '/woocommerce-paypal-commerce-gateway.php'
);
2020-04-02 08:38:00 +03:00
},
2020-04-28 12:31:12 +03:00
'button.request-data' => static function (ContainerInterface $container): RequestData {
2020-04-02 08:38:00 +03:00
return new RequestData();
},
2020-04-28 12:31:12 +03:00
'button.endpoint.change-cart' => static function (ContainerInterface $container): ChangeCartEndpoint {
2020-04-09 14:36:52 +03:00
if (!\WC()->cart) {
2020-04-02 08:38:00 +03:00
throw new RuntimeException('cant initialize endpoint at this moment');
}
$cart = WC()->cart;
$shipping = WC()->shipping();
$requestData = $container->get('button.request-data');
2020-04-10 16:39:25 +03:00
$repository = $container->get('api.repository.cart');
2020-04-29 12:11:53 +03:00
$dataStore = \WC_Data_Store::load('product');
return new ChangeCartEndpoint($cart, $shipping, $requestData, $repository, $dataStore);
2020-04-02 08:38:00 +03:00
},
2020-04-28 12:31:12 +03:00
'button.endpoint.create-order' => static function (ContainerInterface $container): CreateOrderEndpoint {
2020-04-02 08:38:00 +03:00
$requestData = $container->get('button.request-data');
2020-04-10 16:39:25 +03:00
$repository = $container->get('api.repository.cart');
2020-04-02 08:38:00 +03:00
$apiClient = $container->get('api.endpoint.order');
$payerFactory = $container->get('api.factory.payer');
2020-07-16 09:28:00 +03:00
$sessionHandler = $container->get('session.handler');
return new CreateOrderEndpoint($requestData, $repository, $apiClient, $payerFactory, $sessionHandler);
2020-04-06 11:16:18 +03:00
},
2020-04-28 12:31:12 +03:00
'button.endpoint.approve-order' => static function (ContainerInterface $container): ApproveOrderEndpoint {
$requestData = $container->get('button.request-data');
$apiClient = $container->get('api.endpoint.order');
$sessionHandler = $container->get('session.handler');
$threeDSecure = $container->get('button.helper.three-d-secure');
2020-07-15 10:27:40 +03:00
return new ApproveOrderEndpoint($requestData, $apiClient, $sessionHandler, $threeDSecure);
},
'button.endpoint.data-client-id' => static function(ContainerInterface $container) : DataClientIdEndpoint {
$requestData = $container->get('button.request-data');
$tokenEndpoint = $container->get('api.endpoint.identity-token');
return new DataClientIdEndpoint(
$requestData,
$tokenEndpoint
);
},
'button.helper.three-d-secure' => static function (ContainerInterface $container): ThreeDSecure {
2020-07-15 10:27:40 +03:00
return new ThreeDSecure();
},
2020-04-02 08:38:00 +03:00
];