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

275 lines
11 KiB
PHP
Raw Normal View History

2020-08-31 13:38:54 +03:00
<?php
2020-09-01 09:00:45 +03:00
/**
* The services of the API client.
*
2020-09-11 14:11:10 +03:00
* @package WooCommerce\PayPalCommerce\ApiClient
2020-09-01 09:00:45 +03:00
*/
2020-08-31 13:38:54 +03:00
declare(strict_types=1);
2020-09-11 14:11:10 +03:00
namespace WooCommerce\PayPalCommerce\ApiClient;
2020-08-31 13:38:54 +03:00
use Dhii\Data\Container\ContainerInterface;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\IdentityToken;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\LoginSeller;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnerReferrals;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentTokenEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\WebhookEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Factory\AddressFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\AmountFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\ApplicationContextFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\AuthorizationFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\ItemFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PatchCollectionFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayeeFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentsFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentSourceFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\WebhookFactory;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository;
use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PartnerReferralsData;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayeeRepository;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
2020-08-31 13:38:54 +03:00
use WpOop\TransientCache\CachePoolFactory;
2020-09-01 09:00:45 +03:00
return array(
2020-09-11 13:38:02 +03:00
'api.host' => function( $container ) : string {
2020-09-01 09:00:45 +03:00
return 'https://api.paypal.com';
},
2020-09-11 13:38:02 +03:00
'api.paypal-host' => function( $container ) : string {
2020-09-01 09:00:45 +03:00
return 'https://api.paypal.com';
},
'api.partner_merchant_id' => static function () : string {
return '';
},
'api.merchant_email' => function () : string {
return '';
},
'api.merchant_id' => function () : string {
return '';
},
'api.key' => static function (): string {
return '';
},
'api.secret' => static function (): string {
return '';
},
'api.prefix' => static function (): string {
return 'WC-';
},
2020-09-11 13:38:02 +03:00
'api.bearer' => static function ( $container ): Bearer {
2020-09-11 14:24:08 +03:00
$cache = new Cache( 'ppcp-paypal-bearer' );
2020-09-01 09:00:45 +03:00
$key = $container->get( 'api.key' );
$secret = $container->get( 'api.secret' );
2020-08-31 13:38:54 +03:00
2020-09-01 09:00:45 +03:00
$host = $container->get( 'api.host' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
return new PayPalBearer(
2020-09-11 13:38:02 +03:00
$cache,
2020-09-01 09:00:45 +03:00
$host,
$key,
$secret,
$logger
);
},
2020-09-11 13:38:02 +03:00
'api.endpoint.payment-token' => static function ( $container ) : PaymentTokenEndpoint {
2020-09-01 09:00:45 +03:00
return new PaymentTokenEndpoint(
$container->get( 'api.host' ),
$container->get( 'api.bearer' ),
$container->get( 'api.factory.payment-token' ),
$container->get( 'woocommerce.logger.woocommerce' ),
$container->get( 'api.prefix' )
);
},
2020-09-11 13:38:02 +03:00
'api.endpoint.webhook' => static function ( $container ) : WebhookEndpoint {
2020-08-31 13:38:54 +03:00
2020-09-01 09:00:45 +03:00
return new WebhookEndpoint(
$container->get( 'api.host' ),
$container->get( 'api.bearer' ),
$container->get( 'api.factory.webhook' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
2020-09-11 13:38:02 +03:00
'api.endpoint.partner-referrals' => static function ( $container ) : PartnerReferrals {
2020-08-31 13:38:54 +03:00
2020-09-01 09:00:45 +03:00
return new PartnerReferrals(
$container->get( 'api.host' ),
$container->get( 'api.bearer' ),
$container->get( 'api.repository.partner-referrals-data' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
2020-09-11 13:38:02 +03:00
'api.endpoint.identity-token' => static function ( $container ) : IdentityToken {
2020-08-31 13:38:54 +03:00
2020-09-01 09:00:45 +03:00
$logger = $container->get( 'woocommerce.logger.woocommerce' );
$prefix = $container->get( 'api.prefix' );
return new IdentityToken(
$container->get( 'api.host' ),
$container->get( 'api.bearer' ),
$logger,
$prefix
);
},
2020-09-11 13:38:02 +03:00
'api.endpoint.payments' => static function ( $container ): PaymentsEndpoint {
2020-09-01 09:00:45 +03:00
$authorizations_factory = $container->get( 'api.factory.authorization' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
2020-08-31 13:38:54 +03:00
2020-09-01 09:00:45 +03:00
return new PaymentsEndpoint(
$container->get( 'api.host' ),
$container->get( 'api.bearer' ),
$authorizations_factory,
$logger
);
},
2020-09-11 13:38:02 +03:00
'api.endpoint.login-seller' => static function ( $container ) : LoginSeller {
2020-08-31 13:38:54 +03:00
2020-09-01 09:00:45 +03:00
$logger = $container->get( 'woocommerce.logger.woocommerce' );
return new LoginSeller(
$container->get( 'api.paypal-host' ),
$container->get( 'api.partner_merchant_id' ),
$logger
);
},
2020-09-11 13:38:02 +03:00
'api.endpoint.order' => static function ( $container ): OrderEndpoint {
2020-09-01 09:00:45 +03:00
$order_factory = $container->get( 'api.factory.order' );
$patch_collection_factory = $container->get( 'api.factory.patch-collection-factory' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
2020-08-31 13:38:54 +03:00
2020-09-01 09:00:45 +03:00
/**
* The settings.
*
* @var Settings $settings
*/
$settings = $container->get( 'wcgateway.settings' );
$intent = $settings->has( 'intent' ) && strtoupper( (string) $settings->get( 'intent' ) ) === 'AUTHORIZE' ? 'AUTHORIZE' : 'CAPTURE';
$application_context_repository = $container->get( 'api.repository.application-context' );
$paypal_request_id = $container->get( 'api.repository.paypal-request-id' );
return new OrderEndpoint(
$container->get( 'api.host' ),
$container->get( 'api.bearer' ),
$order_factory,
$patch_collection_factory,
$intent,
$logger,
$application_context_repository,
$paypal_request_id
);
},
2020-09-11 13:38:02 +03:00
'api.repository.paypal-request-id' => static function( $container ) : PayPalRequestIdRepository {
2020-09-01 09:00:45 +03:00
return new PayPalRequestIdRepository();
},
2020-09-11 13:38:02 +03:00
'api.repository.application-context' => static function( $container ) : ApplicationContextRepository {
2020-08-31 13:38:54 +03:00
2020-09-01 09:00:45 +03:00
$settings = $container->get( 'wcgateway.settings' );
return new ApplicationContextRepository( $settings );
},
2020-09-11 13:38:02 +03:00
'api.repository.partner-referrals-data' => static function ( $container ) : PartnerReferralsData {
2020-08-31 13:38:54 +03:00
2020-09-01 09:00:45 +03:00
$merchant_email = $container->get( 'api.merchant_email' );
$dcc_applies = $container->get( 'api.helpers.dccapplies' );
return new PartnerReferralsData( $merchant_email, $dcc_applies );
},
2020-09-11 13:38:02 +03:00
'api.repository.cart' => static function ( $container ): CartRepository {
2020-09-01 09:00:45 +03:00
$factory = $container->get( 'api.factory.purchase-unit' );
return new CartRepository( $factory );
},
2020-09-11 13:38:02 +03:00
'api.repository.payee' => static function ( $container ): PayeeRepository {
2020-09-01 09:00:45 +03:00
$merchant_email = $container->get( 'api.merchant_email' );
$merchant_id = $container->get( 'api.merchant_id' );
return new PayeeRepository( $merchant_email, $merchant_id );
},
2020-09-11 13:38:02 +03:00
'api.factory.application-context' => static function ( $container ) : ApplicationContextFactory {
2020-09-01 09:00:45 +03:00
return new ApplicationContextFactory();
},
2020-09-11 13:38:02 +03:00
'api.factory.payment-token' => static function ( $container ) : PaymentTokenFactory {
2020-09-01 09:00:45 +03:00
return new PaymentTokenFactory();
},
2020-09-11 13:38:02 +03:00
'api.factory.webhook' => static function ( $container ): WebhookFactory {
2020-09-01 09:00:45 +03:00
return new WebhookFactory();
},
2020-09-11 13:38:02 +03:00
'api.factory.purchase-unit' => static function ( $container ): PurchaseUnitFactory {
2020-08-31 13:38:54 +03:00
2020-09-01 09:00:45 +03:00
$amount_factory = $container->get( 'api.factory.amount' );
$payee_repository = $container->get( 'api.repository.payee' );
$payee_factory = $container->get( 'api.factory.payee' );
$item_factory = $container->get( 'api.factory.item' );
$shipping_factory = $container->get( 'api.factory.shipping' );
$payments_factory = $container->get( 'api.factory.payments' );
$prefix = $container->get( 'api.prefix' );
2020-08-31 13:38:54 +03:00
2020-09-01 09:00:45 +03:00
return new PurchaseUnitFactory(
$amount_factory,
$payee_repository,
$payee_factory,
$item_factory,
$shipping_factory,
$payments_factory,
$prefix
);
},
2020-09-11 13:38:02 +03:00
'api.factory.patch-collection-factory' => static function ( $container ): PatchCollectionFactory {
2020-09-01 09:00:45 +03:00
return new PatchCollectionFactory();
},
2020-09-11 13:38:02 +03:00
'api.factory.payee' => static function ( $container ): PayeeFactory {
2020-09-01 09:00:45 +03:00
return new PayeeFactory();
},
2020-09-11 13:38:02 +03:00
'api.factory.item' => static function ( $container ): ItemFactory {
2020-09-01 09:00:45 +03:00
return new ItemFactory();
},
2020-09-11 13:38:02 +03:00
'api.factory.shipping' => static function ( $container ): ShippingFactory {
2020-09-01 09:00:45 +03:00
$address_factory = $container->get( 'api.factory.address' );
return new ShippingFactory( $address_factory );
},
2020-09-11 13:38:02 +03:00
'api.factory.amount' => static function ( $container ): AmountFactory {
2020-09-01 09:00:45 +03:00
$item_factory = $container->get( 'api.factory.item' );
return new AmountFactory( $item_factory );
},
2020-09-11 13:38:02 +03:00
'api.factory.payer' => static function ( $container ): PayerFactory {
2020-09-01 09:00:45 +03:00
$address_factory = $container->get( 'api.factory.address' );
return new PayerFactory( $address_factory );
},
2020-09-11 13:38:02 +03:00
'api.factory.address' => static function ( $container ): AddressFactory {
2020-09-01 09:00:45 +03:00
return new AddressFactory();
},
2020-09-11 13:38:02 +03:00
'api.factory.payment-source' => static function ( $container ): PaymentSourceFactory {
2020-09-01 09:00:45 +03:00
return new PaymentSourceFactory();
},
2020-09-11 13:38:02 +03:00
'api.factory.order' => static function ( $container ): OrderFactory {
2020-09-01 09:00:45 +03:00
$purchase_unit_factory = $container->get( 'api.factory.purchase-unit' );
$payer_factory = $container->get( 'api.factory.payer' );
$application_context_repository = $container->get( 'api.repository.application-context' );
$application_context_factory = $container->get( 'api.factory.application-context' );
$payment_source_factory = $container->get( 'api.factory.payment-source' );
return new OrderFactory(
$purchase_unit_factory,
$payer_factory,
$application_context_repository,
$application_context_factory,
$payment_source_factory
);
},
2020-09-11 13:38:02 +03:00
'api.factory.payments' => static function ( $container ): PaymentsFactory {
2020-09-01 09:00:45 +03:00
$authorizations_factory = $container->get( 'api.factory.authorization' );
return new PaymentsFactory( $authorizations_factory );
},
2020-09-11 13:38:02 +03:00
'api.factory.authorization' => static function ( $container ): AuthorizationFactory {
2020-09-01 09:00:45 +03:00
return new AuthorizationFactory();
},
2020-09-11 13:38:02 +03:00
'api.helpers.dccapplies' => static function ( $container ) : DccApplies {
2020-09-01 09:00:45 +03:00
return new DccApplies();
},
);