woocommerce-paypal-payments/modules.local/ppcp-api-client/services.php

128 lines
5.5 KiB
PHP
Raw Normal View History

2020-04-02 08:38:00 +03:00
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\ApiClient;
use Dhii\Data\Container\ContainerInterface;
2020-04-10 12:55:50 +03:00
use Inpsyde\CacheModule\Provider\CacheProviderInterface;
2020-04-02 08:38:00 +03:00
use Inpsyde\PayPalCommerce\ApiClient\Authentication\Bearer;
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
2020-04-06 10:51:56 +03:00
use Inpsyde\PayPalCommerce\ApiClient\Factory\AddressFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\AmountFactory;
2020-04-13 11:52:50 +03:00
use Inpsyde\PayPalCommerce\ApiClient\Factory\ErrorResponseCollectionFactory;
2020-04-06 10:51:56 +03:00
use Inpsyde\PayPalCommerce\ApiClient\Factory\ItemFactory;
2020-04-02 08:38:00 +03:00
use Inpsyde\PayPalCommerce\ApiClient\Factory\LineItemFactory;
2020-04-06 10:51:56 +03:00
use Inpsyde\PayPalCommerce\ApiClient\Factory\OrderFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\PatchCollectionFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\PayeeFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\PayerFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\ShippingFactory;
2020-04-02 08:38:00 +03:00
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
return [
2020-04-06 11:16:18 +03:00
'api.host' => function (ContainerInterface $container) : string {
2020-04-02 08:38:00 +03:00
return 'https://api.sandbox.paypal.com';
},
2020-04-06 11:16:18 +03:00
'api.key' => function (ContainerInterface $container) : string {
2020-04-02 08:38:00 +03:00
return 'AQB97CzMsd58-It1vxbcDAGvMuXNCXRD9le_XUaMlHB_U7XsU9IiItBwGQOtZv9sEeD6xs2vlIrL4NiD';
},
2020-04-06 11:16:18 +03:00
'api.secret' => function (ContainerInterface $container) : string {
2020-04-02 08:38:00 +03:00
return 'EILGMYK_0iiSbja8hT-nCBGl0BvKxEB4riHgyEO7QWDeUzCJ5r42JUEvrI7gpGyw0Qww8AIXxSdCIAny';
},
2020-04-06 11:16:18 +03:00
'api.bearer' => function (ContainerInterface $container) : Bearer {
2020-04-10 12:55:50 +03:00
$provider = $container->get('cache.provider');
/**
* @var CacheProviderInterface $provider
*/
$cache = $provider->cacheOrTransientForKey('');
2020-04-02 08:38:00 +03:00
return new Bearer(
2020-04-10 12:55:50 +03:00
$cache,
2020-04-02 08:38:00 +03:00
$container->get('api.host'),
$container->get('api.key'),
$container->get('api.secret')
);
},
2020-04-06 11:16:18 +03:00
'api.endpoint.order' => function (ContainerInterface $container) : OrderEndpoint {
2020-04-06 10:51:56 +03:00
$orderFactory = $container->get('api.factory.order');
$patchCollectionFactory = $container->get('api.factory.patch-collection-factory');
2020-04-13 11:52:50 +03:00
$errorResponseFactory = $container->get('api.factory.response-error');
// TODO: get the settings using the class
// Using it now throws a maximum nested error because they share the same dependency
$intent = strtoupper(get_option('woocommerce_ppcp-gateway_settings')['intent']);
2020-04-02 08:38:00 +03:00
return new OrderEndpoint(
$container->get('api.host'),
$container->get('api.bearer'),
2020-04-06 10:51:56 +03:00
$orderFactory,
$patchCollectionFactory,
$intent,
2020-04-13 11:52:50 +03:00
$errorResponseFactory
2020-04-02 08:38:00 +03:00
);
},
2020-04-06 11:16:18 +03:00
'api.cart-repository' => function (ContainerInterface $container) : CartRepository {
2020-04-06 10:51:56 +03:00
/*
2020-04-06 11:16:18 +03:00
* ToDo: We need to watch out and see, if we can load the Cart Repository
* only on specific situations.
* @see http://ppc-dev-website.localhost/wp-admin/admin.php?page=wc-settings&tab=tax
2020-04-06 10:51:56 +03:00
*/
2020-04-02 08:38:00 +03:00
$cart = WC()->cart;
2020-04-06 10:51:56 +03:00
if (! $cart) {
2020-04-06 11:16:18 +03:00
/**
* ToDo: The cart repository gets pulled in the wp-admin,
* where there is no WC Cart loaded. Rethink.
**/
2020-04-06 10:51:56 +03:00
$cart = new \WC_Cart();
}
$factory = $container->get('api.factory.purchase-unit');
2020-04-02 08:38:00 +03:00
return new CartRepository($cart, $factory);
},
2020-04-06 11:16:18 +03:00
'api.factory.purchase-unit' => function (ContainerInterface $container) : PurchaseUnitFactory {
2020-04-06 10:51:56 +03:00
$amountFactory = $container->get('api.factory.amount');
$payeeFactory = $container->get('api.factory.payee');
$itemFactory = $container->get('api.factory.item');
$shippingFactory = $container->get('api.factory.shipping');
return new PurchaseUnitFactory(
$amountFactory,
$payeeFactory,
$itemFactory,
$shippingFactory
);
},
2020-04-06 11:16:18 +03:00
'api.factory.patch-collection-factory' => function (ContainerInterface $container)
: PatchCollectionFactory {
2020-04-06 10:51:56 +03:00
return new PatchCollectionFactory();
},
2020-04-06 11:16:18 +03:00
'api.factory.payee' => function (ContainerInterface $container) : PayeeFactory {
2020-04-06 10:51:56 +03:00
return new PayeeFactory();
},
2020-04-06 11:16:18 +03:00
'api.factory.item' => function (ContainerInterface $container) : ItemFactory {
2020-04-06 10:51:56 +03:00
return new ItemFactory();
},
2020-04-06 11:16:18 +03:00
'api.factory.shipping' => function (ContainerInterface $container) : ShippingFactory {
2020-04-06 10:51:56 +03:00
$addressFactory = $container->get('api.factory.address');
return new ShippingFactory($addressFactory);
},
2020-04-06 11:16:18 +03:00
'api.factory.amount' => function (ContainerInterface $container) : AmountFactory {
2020-04-06 10:51:56 +03:00
return new AmountFactory();
},
2020-04-06 11:16:18 +03:00
'api.factory.payer' => function (ContainerInterface $container) : PayerFactory {
2020-04-06 10:51:56 +03:00
$addressFactory = $container->get('api.factory.address');
return new PayerFactory($addressFactory);
},
2020-04-06 11:16:18 +03:00
'api.factory.address' => function (ContainerInterface $container) : AddressFactory {
2020-04-06 10:51:56 +03:00
return new AddressFactory();
},
2020-04-13 11:52:50 +03:00
'api.factory.response-error' => function (ContainerInterface $container) : ErrorResponseCollectionFactory {
return new ErrorResponseCollectionFactory();
},
2020-04-06 11:16:18 +03:00
'api.factory.order' => function (ContainerInterface $container) : OrderFactory {
2020-04-06 10:51:56 +03:00
$purchaseUnitFactory = $container->get('api.factory.purchase-unit');
$payerFactory = $container->get('api.factory.payer');
return new OrderFactory($purchaseUnitFactory, $payerFactory);
2020-04-02 08:38:00 +03:00
},
];