From 06c9b32475cfe9823746aa17650bca9ef07aef9b Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Wed, 28 Feb 2024 16:59:52 +0100 Subject: [PATCH] Refactor aplication context into experience context --- modules/ppcp-api-client/services.php | 20 ++- .../src/Endpoint/OrderEndpoint.php | 75 ++++------ ...ationContext.php => ExperienceContext.php} | 4 +- modules/ppcp-api-client/src/Entity/Order.php | 56 ++++---- .../src/Factory/ApplicationContextFactory.php | 44 ------ .../src/Factory/ExperienceContextFactory.php | 38 +++++ .../src/Factory/OrderFactory.php | 36 ++--- .../src/Factory/ShippingPreferenceFactory.php | 14 +- ...ry.php => ExperienceContextRepository.php} | 24 ++-- modules/ppcp-button/services.php | 1 + .../src/Endpoint/CreateOrderEndpoint.php | 72 ++++++---- .../src/VaultedCreditCardHandler.php | 14 +- modules/ppcp-wc-gateway/services.php | 11 +- .../src/Processor/OrderProcessor.php | 55 ++++++- .../src/RenewalHandler.php | 16 +-- .../ApiClient/Endpoint/OrderEndpointTest.php | 76 +--------- .../PHPUnit/ApiClient/Endpoint/OrderTest.php | 4 +- .../ApiClient/Factory/OrderFactoryTest.php | 22 ++- .../Factory/ShippingPreferenceFactoryTest.php | 20 +-- .../ApplicationContextRepositoryTest.php | 2 +- .../Endpoint/CreateOrderEndpointTest.php | 3 + .../Vaulting/VaultedCreditCardHandlerTest.php | 134 ------------------ .../Processor/OrderProcessorTest.php | 10 +- .../ApplicationContextRepositoryTest.php | 20 +-- 24 files changed, 300 insertions(+), 471 deletions(-) rename modules/ppcp-api-client/src/Entity/{ApplicationContext.php => ExperienceContext.php} (99%) delete mode 100644 modules/ppcp-api-client/src/Factory/ApplicationContextFactory.php create mode 100644 modules/ppcp-api-client/src/Factory/ExperienceContextFactory.php rename modules/ppcp-api-client/src/Repository/{ApplicationContextRepository.php => ExperienceContextRepository.php} (73%) delete mode 100644 tests/PHPUnit/Vaulting/VaultedCreditCardHandlerTest.php diff --git a/modules/ppcp-api-client/services.php b/modules/ppcp-api-client/services.php index 0ae8ed7af..a2c997259 100644 --- a/modules/ppcp-api-client/services.php +++ b/modules/ppcp-api-client/services.php @@ -42,7 +42,7 @@ 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\ExperienceContextFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\AuthorizationFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\CaptureFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\ExchangeRateFactory; @@ -69,7 +69,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies; use WooCommerce\PayPalCommerce\ApiClient\Helper\OrderHelper; use WooCommerce\PayPalCommerce\ApiClient\Helper\OrderTransient; use WooCommerce\PayPalCommerce\ApiClient\Helper\PurchaseUnitSanitizer; -use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository; +use WooCommerce\PayPalCommerce\ApiClient\Repository\ExperienceContextRepository; use WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository; use WooCommerce\PayPalCommerce\ApiClient\Repository\OrderRepository; use WooCommerce\PayPalCommerce\ApiClient\Repository\PartnerReferralsData; @@ -222,7 +222,6 @@ return array( assert( $settings instanceof Settings ); $intent = $settings->has( 'intent' ) && strtoupper( (string) $settings->get( 'intent' ) ) === 'AUTHORIZE' ? 'AUTHORIZE' : 'CAPTURE'; - $application_context_repository = $container->get( 'api.repository.application-context' ); $subscription_helper = $container->get( 'wc-subscriptions.helper' ); return new OrderEndpoint( $container->get( 'api.host' ), @@ -231,7 +230,6 @@ return array( $patch_collection_factory, $intent, $logger, - $application_context_repository, $subscription_helper, $container->get( 'wcgateway.is-fraudnet-enabled' ), $container->get( 'wcgateway.fraudnet' ), @@ -276,13 +274,11 @@ return array( $container->get( 'woocommerce.logger.woocommerce' ) ); }, - 'api.repository.application-context' => static function( ContainerInterface $container ) : ApplicationContextRepository { - + 'api.repository.experience-context' => static function( ContainerInterface $container ) : ExperienceContextRepository { $settings = $container->get( 'wcgateway.settings' ); - return new ApplicationContextRepository( $settings ); + return new ExperienceContextRepository( $settings ); }, 'api.repository.partner-referrals-data' => static function ( ContainerInterface $container ) : PartnerReferralsData { - $dcc_applies = $container->get( 'api.helpers.dccapplies' ); return new PartnerReferralsData( $dcc_applies ); }, @@ -300,8 +296,8 @@ return array( $container->get( 'api.endpoint.order' ) ); }, - 'api.factory.application-context' => static function ( ContainerInterface $container ) : ApplicationContextFactory { - return new ApplicationContextFactory(); + 'api.factory.experience-context' => static function ( ContainerInterface $container ) : ExperienceContextFactory { + return new ExperienceContextFactory(); }, 'api.factory.payment-token' => static function ( ContainerInterface $container ) : PaymentTokenFactory { return new PaymentTokenFactory(); @@ -401,8 +397,8 @@ return array( 'api.factory.order' => static function ( ContainerInterface $container ): OrderFactory { $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' ); + $application_context_repository = $container->get( 'api.repository.experience-context' ); + $application_context_factory = $container->get( 'api.factory.experience-context' ); return new OrderFactory( $purchase_unit_factory, $payer_factory, diff --git a/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php b/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php index 43e9af9ea..b025a9e78 100644 --- a/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php +++ b/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php @@ -11,7 +11,7 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint; use stdClass; use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer; -use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext; +use WooCommerce\PayPalCommerce\ApiClient\Entity\ExperienceContext; use WooCommerce\PayPalCommerce\ApiClient\Entity\AuthorizationStatus; use WooCommerce\PayPalCommerce\ApiClient\Entity\CaptureStatus; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; @@ -19,14 +19,12 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; use WooCommerce\PayPalCommerce\ApiClient\Entity\PatchCollection; use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource; -use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken; use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit; use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\PatchCollectionFactory; use WooCommerce\PayPalCommerce\ApiClient\Helper\ErrorResponse; -use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository; use Psr\Log\LoggerInterface; use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper; use WooCommerce\PayPalCommerce\WcGateway\FraudNet\FraudNet; @@ -88,13 +86,6 @@ class OrderEndpoint { */ private $logger; - /** - * The application context repository. - * - * @var ApplicationContextRepository - */ - private $application_context_repository; - /** * True if FraudNet support is enabled in settings, otherwise false. * @@ -119,17 +110,16 @@ class OrderEndpoint { /** * OrderEndpoint constructor. * - * @param string $host The host. - * @param Bearer $bearer The bearer. - * @param OrderFactory $order_factory The order factory. - * @param PatchCollectionFactory $patch_collection_factory The patch collection factory. - * @param string $intent The intent. - * @param LoggerInterface $logger The logger. - * @param ApplicationContextRepository $application_context_repository The application context repository. - * @param SubscriptionHelper $subscription_helper The subscription helper. - * @param bool $is_fraudnet_enabled true if FraudNet support is enabled in settings, otherwise false. - * @param FraudNet $fraudnet The FraudNet entity. - * @param string $bn_code The BN Code. + * @param string $host The host. + * @param Bearer $bearer The bearer. + * @param OrderFactory $order_factory The order factory. + * @param PatchCollectionFactory $patch_collection_factory The patch collection factory. + * @param string $intent The intent. + * @param LoggerInterface $logger The logger. + * @param SubscriptionHelper $subscription_helper The subscription helper. + * @param bool $is_fraudnet_enabled true if FraudNet support is enabled in settings, otherwise false. + * @param FraudNet $fraudnet The FraudNet entity. + * @param string $bn_code The BN Code. */ public function __construct( string $host, @@ -138,24 +128,22 @@ class OrderEndpoint { PatchCollectionFactory $patch_collection_factory, string $intent, LoggerInterface $logger, - ApplicationContextRepository $application_context_repository, SubscriptionHelper $subscription_helper, bool $is_fraudnet_enabled, FraudNet $fraudnet, string $bn_code = '' ) { - $this->host = $host; - $this->bearer = $bearer; - $this->order_factory = $order_factory; - $this->patch_collection_factory = $patch_collection_factory; - $this->intent = $intent; - $this->logger = $logger; - $this->application_context_repository = $application_context_repository; - $this->bn_code = $bn_code; - $this->is_fraudnet_enabled = $is_fraudnet_enabled; - $this->subscription_helper = $subscription_helper; - $this->fraudnet = $fraudnet; + $this->host = $host; + $this->bearer = $bearer; + $this->order_factory = $order_factory; + $this->patch_collection_factory = $patch_collection_factory; + $this->intent = $intent; + $this->logger = $logger; + $this->bn_code = $bn_code; + $this->is_fraudnet_enabled = $is_fraudnet_enabled; + $this->subscription_helper = $subscription_helper; + $this->fraudnet = $fraudnet; } /** @@ -178,11 +166,9 @@ class OrderEndpoint { * @param PurchaseUnit[] $items The purchase unit items for the order. * @param string $shipping_preference One of ApplicationContext::SHIPPING_PREFERENCE_ values. * @param Payer|null $payer The payer off the order. - * @param PaymentToken|null $payment_token The payment token. - * @param string $user_action The user action. + * @param PaymentSource|null $payment_source The payment source. * @param string $payment_method WC payment method. * @param array $request_data Request data. - * @param PaymentSource|null $payment_source The payment source. * * @return Order * @throws RuntimeException If the request fails. @@ -191,20 +177,18 @@ class OrderEndpoint { array $items, string $shipping_preference, Payer $payer = null, - PaymentToken $payment_token = null, - string $user_action = ApplicationContext::USER_ACTION_CONTINUE, + PaymentSource $payment_source = null, string $payment_method = '', - array $request_data = array(), - PaymentSource $payment_source = null + array $request_data = array() ): Order { $bearer = $this->bearer->bearer(); $data = array( - 'intent' => apply_filters( 'woocommerce_paypal_payments_order_intent', $this->intent ), - 'purchase_units' => array_map( + 'intent' => apply_filters( 'woocommerce_paypal_payments_order_intent', $this->intent ), + 'purchase_units' => array_map( static function ( PurchaseUnit $item ) use ( $shipping_preference ): array { $data = $item->to_array(); - if ( $shipping_preference !== ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE ) { + if ( $shipping_preference !== ExperienceContext::SHIPPING_PREFERENCE_GET_FROM_FILE ) { // Shipping options are not allowed to be sent when not getting the address from PayPal. unset( $data['shipping']['options'] ); } @@ -213,15 +197,10 @@ class OrderEndpoint { }, $items ), - 'application_context' => $this->application_context_repository - ->current_context( $shipping_preference, $user_action )->to_array(), ); if ( $payer && ! empty( $payer->email_address() ) ) { $data['payer'] = $payer->to_array(); } - if ( $payment_token ) { - $data['payment_source']['token'] = $payment_token->to_array(); - } if ( $payment_source ) { $data['payment_source'] = array( $payment_source->name() => $payment_source->properties(), diff --git a/modules/ppcp-api-client/src/Entity/ApplicationContext.php b/modules/ppcp-api-client/src/Entity/ExperienceContext.php similarity index 99% rename from modules/ppcp-api-client/src/Entity/ApplicationContext.php rename to modules/ppcp-api-client/src/Entity/ExperienceContext.php index c70bbf850..b2f3a2991 100644 --- a/modules/ppcp-api-client/src/Entity/ApplicationContext.php +++ b/modules/ppcp-api-client/src/Entity/ExperienceContext.php @@ -12,9 +12,9 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Entity; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; /** - * Class ApplicationContext + * Class ExperienceContext */ -class ApplicationContext { +class ExperienceContext { const LANDING_PAGE_LOGIN = 'LOGIN'; const LANDING_PAGE_BILLING = 'BILLING'; diff --git a/modules/ppcp-api-client/src/Entity/Order.php b/modules/ppcp-api-client/src/Entity/Order.php index d40e7a4e6..9592e4cc2 100644 --- a/modules/ppcp-api-client/src/Entity/Order.php +++ b/modules/ppcp-api-client/src/Entity/Order.php @@ -65,11 +65,11 @@ class Order { private $update_time; /** - * The application context. + * The experience context. * - * @var ApplicationContext|null + * @var ExperienceContext|null */ - private $application_context; + private $experience_context; /** * The payment source. @@ -83,21 +83,21 @@ class Order { * * @see https://developer.paypal.com/docs/api/orders/v2/#orders-create-response * - * @param string $id The ID. - * @param PurchaseUnit[] $purchase_units The purchase units. - * @param OrderStatus $order_status The order status. - * @param ApplicationContext|null $application_context The application context. - * @param PaymentSource|null $payment_source The payment source. - * @param Payer|null $payer The payer. - * @param string $intent The intent. - * @param \DateTime|null $create_time The create time. - * @param \DateTime|null $update_time The update time. + * @param string $id The ID. + * @param PurchaseUnit[] $purchase_units The purchase units. + * @param OrderStatus $order_status The order status. + * @param ExperienceContext|null $experience_context The application context. + * @param PaymentSource|null $payment_source The payment source. + * @param Payer|null $payer The payer. + * @param string $intent The intent. + * @param \DateTime|null $create_time The create time. + * @param \DateTime|null $update_time The update time. */ public function __construct( string $id, array $purchase_units, OrderStatus $order_status, - ApplicationContext $application_context = null, + ExperienceContext $experience_context = null, PaymentSource $payment_source = null, Payer $payer = null, string $intent = 'CAPTURE', @@ -105,15 +105,15 @@ class Order { \DateTime $update_time = null ) { - $this->id = $id; - $this->application_context = $application_context; - $this->payer = $payer; - $this->order_status = $order_status; - $this->intent = ( 'CAPTURE' === $intent ) ? 'CAPTURE' : 'AUTHORIZE'; - $this->purchase_units = $purchase_units; - $this->create_time = $create_time; - $this->update_time = $update_time; - $this->payment_source = $payment_source; + $this->id = $id; + $this->experience_context = $experience_context; + $this->payer = $payer; + $this->order_status = $order_status; + $this->intent = ( 'CAPTURE' === $intent ) ? 'CAPTURE' : 'AUTHORIZE'; + $this->purchase_units = $purchase_units; + $this->create_time = $create_time; + $this->update_time = $update_time; + $this->payment_source = $payment_source; } /** @@ -180,13 +180,13 @@ class Order { } /** - * Returns the application context. + * Returns the experience context. * - * @return ApplicationContext|null + * @return ExperienceContext|null */ - public function application_context() { + public function experience_context() { - return $this->application_context; + return $this->experience_context; } /** @@ -225,8 +225,8 @@ class Order { if ( $this->update_time() ) { $order['update_time'] = $this->update_time()->format( 'Y-m-d\TH:i:sO' ); } - if ( $this->application_context() ) { - $order['application_context'] = $this->application_context()->to_array(); + if ( $this->experience_context() ) { + $order['application_context'] = $this->experience_context()->to_array(); } return $order; diff --git a/modules/ppcp-api-client/src/Factory/ApplicationContextFactory.php b/modules/ppcp-api-client/src/Factory/ApplicationContextFactory.php deleted file mode 100644 index e9a418504..000000000 --- a/modules/ppcp-api-client/src/Factory/ApplicationContextFactory.php +++ /dev/null @@ -1,44 +0,0 @@ -return_url ) ? - $data->return_url : '', - isset( $data->cancel_url ) ? - $data->cancel_url : '', - isset( $data->brand_name ) ? - $data->brand_name : '', - isset( $data->locale ) ? - $data->locale : '', - isset( $data->landing_page ) ? - $data->landing_page : ApplicationContext::LANDING_PAGE_NO_PREFERENCE, - isset( $data->shipping_preference ) ? - $data->shipping_preference : ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE, - isset( $data->user_action ) ? - $data->user_action : ApplicationContext::USER_ACTION_CONTINUE - ); - } -} diff --git a/modules/ppcp-api-client/src/Factory/ExperienceContextFactory.php b/modules/ppcp-api-client/src/Factory/ExperienceContextFactory.php new file mode 100644 index 000000000..fd1054cd7 --- /dev/null +++ b/modules/ppcp-api-client/src/Factory/ExperienceContextFactory.php @@ -0,0 +1,38 @@ +return_url ?? '', + $data->cancel_url ?? '', + $data->brand_name ?? '', + $data->locale ?? '', + $data->landing_page ?? ExperienceContext::LANDING_PAGE_NO_PREFERENCE, + $data->shipping_preference ?? ExperienceContext::SHIPPING_PREFERENCE_GET_FROM_FILE, + $data->user_action ?? ExperienceContext::USER_ACTION_CONTINUE + ); + } +} diff --git a/modules/ppcp-api-client/src/Factory/OrderFactory.php b/modules/ppcp-api-client/src/Factory/OrderFactory.php index 0b8876594..cc2c08fe7 100644 --- a/modules/ppcp-api-client/src/Factory/OrderFactory.php +++ b/modules/ppcp-api-client/src/Factory/OrderFactory.php @@ -14,7 +14,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource; use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; -use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository; +use WooCommerce\PayPalCommerce\ApiClient\Repository\ExperienceContextRepository; /** * Class OrderFactory @@ -36,38 +36,38 @@ class OrderFactory { private $payer_factory; /** - * The ApplicationContext repository. + * The ExperienceContextRepository. * - * @var ApplicationContextRepository + * @var ExperienceContextRepository */ - private $application_context_repository; + private $experience_context_repository; /** * The ApplicationContext factory. * - * @var ApplicationContextFactory + * @var ExperienceContextFactory */ - private $application_context_factory; + private $experience_context_factory; /** * OrderFactory constructor. * - * @param PurchaseUnitFactory $purchase_unit_factory The PurchaseUnit factory. - * @param PayerFactory $payer_factory The Payer factory. - * @param ApplicationContextRepository $application_context_repository The Application Context repository. - * @param ApplicationContextFactory $application_context_factory The Application Context factory. + * @param PurchaseUnitFactory $purchase_unit_factory The PurchaseUnit factory. + * @param PayerFactory $payer_factory The Payer factory. + * @param ExperienceContextRepository $experience_context_repository The Application Context repository. + * @param ExperienceContextFactory $experience_context_factory The Application Context factory. */ public function __construct( PurchaseUnitFactory $purchase_unit_factory, PayerFactory $payer_factory, - ApplicationContextRepository $application_context_repository, - ApplicationContextFactory $application_context_factory + ExperienceContextRepository $experience_context_repository, + ExperienceContextFactory $experience_context_factory ) { - $this->purchase_unit_factory = $purchase_unit_factory; - $this->payer_factory = $payer_factory; - $this->application_context_repository = $application_context_repository; - $this->application_context_factory = $application_context_factory; + $this->purchase_unit_factory = $purchase_unit_factory; + $this->payer_factory = $payer_factory; + $this->experience_context_repository = $experience_context_repository; + $this->experience_context_factory = $experience_context_factory; } /** @@ -85,7 +85,7 @@ class OrderFactory { $order->id(), $purchase_units, $order->status(), - $order->application_context(), + $order->experience_context(), $order->payment_source(), $order->payer(), $order->intent(), @@ -141,7 +141,7 @@ class OrderFactory { $this->payer_factory->from_paypal_response( $order_data->payer ) : null; $application_context = ( isset( $order_data->application_context ) ) ? - $this->application_context_factory->from_paypal_response( $order_data->application_context ) + $this->experience_context_factory->from_paypal_response( $order_data->application_context ) : null; $payment_source = null; diff --git a/modules/ppcp-api-client/src/Factory/ShippingPreferenceFactory.php b/modules/ppcp-api-client/src/Factory/ShippingPreferenceFactory.php index 0b433d6c4..ce08833ce 100644 --- a/modules/ppcp-api-client/src/Factory/ShippingPreferenceFactory.php +++ b/modules/ppcp-api-client/src/Factory/ShippingPreferenceFactory.php @@ -10,7 +10,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\ApiClient\Factory; use WC_Cart; -use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext; +use WooCommerce\PayPalCommerce\ApiClient\Entity\ExperienceContext; use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit; /** @@ -35,7 +35,7 @@ class ShippingPreferenceFactory { ): string { $contains_physical_goods = $purchase_unit->contains_physical_goods(); if ( ! $contains_physical_goods ) { - return ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING; + return ExperienceContext::SHIPPING_PREFERENCE_NO_SHIPPING; } $has_shipping = null !== $purchase_unit->shipping(); @@ -45,20 +45,20 @@ class ShippingPreferenceFactory { if ( $shipping_address_is_fixed ) { // Checkout + no address given? Probably something weird happened, like no form validation? if ( ! $has_shipping ) { - return ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING; + return ExperienceContext::SHIPPING_PREFERENCE_NO_SHIPPING; } - return ApplicationContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS; + return ExperienceContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS; } if ( 'card' === $funding_source ) { if ( ! $has_shipping ) { - return ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING; + return ExperienceContext::SHIPPING_PREFERENCE_NO_SHIPPING; } // Looks like GET_FROM_FILE does not work for the vaulted card button. - return ApplicationContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS; + return ExperienceContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS; } - return ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE; + return ExperienceContext::SHIPPING_PREFERENCE_GET_FROM_FILE; } } diff --git a/modules/ppcp-api-client/src/Repository/ApplicationContextRepository.php b/modules/ppcp-api-client/src/Repository/ExperienceContextRepository.php similarity index 73% rename from modules/ppcp-api-client/src/Repository/ApplicationContextRepository.php rename to modules/ppcp-api-client/src/Repository/ExperienceContextRepository.php index 244b955a0..c6017982e 100644 --- a/modules/ppcp-api-client/src/Repository/ApplicationContextRepository.php +++ b/modules/ppcp-api-client/src/Repository/ExperienceContextRepository.php @@ -9,14 +9,14 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\ApiClient\Repository; -use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext; +use WooCommerce\PayPalCommerce\ApiClient\Entity\ExperienceContext; use WooCommerce\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; /** - * Class ApplicationContextRepository + * Class ExperienceContextRepository */ -class ApplicationContextRepository { +class ExperienceContextRepository { /** * The Settings. @@ -26,7 +26,7 @@ class ApplicationContextRepository { private $settings; /** - * ApplicationContextRepository constructor. + * ExperienceContextRepository constructor. * * @param ContainerInterface $settings The settings. */ @@ -35,25 +35,25 @@ class ApplicationContextRepository { } /** - * Returns the current application context. + * Returns the current experience context. * * @param string $shipping_preferences The shipping preferences. * @param string $user_action The user action. * - * @return ApplicationContext + * @return ExperienceContext */ public function current_context( - string $shipping_preferences = ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING, - string $user_action = ApplicationContext::USER_ACTION_CONTINUE - ): ApplicationContext { + string $shipping_preferences = ExperienceContext::SHIPPING_PREFERENCE_NO_SHIPPING, + string $user_action = ExperienceContext::USER_ACTION_CONTINUE + ): ExperienceContext { $brand_name = $this->settings->has( 'brand_name' ) ? $this->settings->get( 'brand_name' ) : ''; $locale = $this->valid_bcp47_code(); $landingpage = $this->settings->has( 'landing_page' ) ? - $this->settings->get( 'landing_page' ) : ApplicationContext::LANDING_PAGE_NO_PREFERENCE; + $this->settings->get( 'landing_page' ) : ExperienceContext::LANDING_PAGE_NO_PREFERENCE; $payment_preference = $this->settings->has( 'payee_preferred' ) && $this->settings->get( 'payee_preferred' ) ? - ApplicationContext::PAYMENT_METHOD_IMMEDIATE_PAYMENT_REQUIRED : ApplicationContext::PAYMENT_METHOD_UNRESTRICTED; - $context = new ApplicationContext( + ExperienceContext::PAYMENT_METHOD_IMMEDIATE_PAYMENT_REQUIRED : ExperienceContext::PAYMENT_METHOD_UNRESTRICTED; + $context = new ExperienceContext( network_home_url( \WC_AJAX::get_endpoint( ReturnUrlEndpoint::ENDPOINT ) ), (string) wc_get_checkout_url(), (string) $brand_name, diff --git a/modules/ppcp-button/services.php b/modules/ppcp-button/services.php index a812dc53e..557284558 100644 --- a/modules/ppcp-button/services.php +++ b/modules/ppcp-button/services.php @@ -208,6 +208,7 @@ return array( $container->get( 'button.pay-now-contexts' ), $container->get( 'button.handle-shipping-in-paypal' ), $container->get( 'wcgateway.funding-sources-without-redirect' ), + $container->get( 'api.repository.experience-context' ), $logger ); }, diff --git a/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php b/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php index 5a1092fe4..1f506f00e 100644 --- a/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php +++ b/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php @@ -15,16 +15,18 @@ use stdClass; use Throwable; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Entity\Amount; -use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext; +use WooCommerce\PayPalCommerce\ApiClient\Entity\ExperienceContext; use WooCommerce\PayPalCommerce\ApiClient\Entity\Money; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer; +use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource; use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit; use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory; +use WooCommerce\PayPalCommerce\ApiClient\Repository\ExperienceContextRepository; use WooCommerce\PayPalCommerce\Button\Exception\ValidationException; use WooCommerce\PayPalCommerce\Button\Validation\CheckoutFormValidator; use WooCommerce\PayPalCommerce\Button\Helper\EarlyOrderHandler; @@ -157,6 +159,13 @@ class CreateOrderEndpoint implements EndpointInterface { */ private $funding_sources_without_redirect; + /** + * Experience context repository. + * + * @var ExperienceContextRepository + */ + private $experience_context_repository; + /** * The logger. * @@ -164,31 +173,25 @@ class CreateOrderEndpoint implements EndpointInterface { */ protected $logger; - /** - * The form data, or empty if not available. - * - * @var array - */ - private $form = array(); - /** * CreateOrderEndpoint constructor. * - * @param RequestData $request_data The RequestData object. - * @param PurchaseUnitFactory $purchase_unit_factory The PurchaseUnit factory. - * @param ShippingPreferenceFactory $shipping_preference_factory The shipping_preference factory. - * @param OrderEndpoint $order_endpoint The OrderEndpoint object. - * @param PayerFactory $payer_factory The PayerFactory object. - * @param SessionHandler $session_handler The SessionHandler object. - * @param Settings $settings The Settings object. - * @param EarlyOrderHandler $early_order_handler The EarlyOrderHandler object. - * @param bool $registration_needed Whether a new user must be registered during checkout. - * @param string $card_billing_data_mode The value of card_billing_data_mode from the settings. - * @param bool $early_validation_enabled Whether to execute WC validation of the checkout form. - * @param string[] $pay_now_contexts The contexts that should have the Pay Now button. - * @param bool $handle_shipping_in_paypal If true, the shipping methods are sent to PayPal allowing the customer to select it inside the popup. - * @param string[] $funding_sources_without_redirect The sources that do not cause issues about redirecting (on mobile, ...) and sometimes not returning back. - * @param LoggerInterface $logger The logger. + * @param RequestData $request_data The RequestData object. + * @param PurchaseUnitFactory $purchase_unit_factory The PurchaseUnit factory. + * @param ShippingPreferenceFactory $shipping_preference_factory The shipping_preference factory. + * @param OrderEndpoint $order_endpoint The OrderEndpoint object. + * @param PayerFactory $payer_factory The PayerFactory object. + * @param SessionHandler $session_handler The SessionHandler object. + * @param Settings $settings The Settings object. + * @param EarlyOrderHandler $early_order_handler The EarlyOrderHandler object. + * @param bool $registration_needed Whether a new user must be registered during checkout. + * @param string $card_billing_data_mode The value of card_billing_data_mode from the settings. + * @param bool $early_validation_enabled Whether to execute WC validation of the checkout form. + * @param string[] $pay_now_contexts The contexts that should have the Pay Now button. + * @param bool $handle_shipping_in_paypal If true, the shipping methods are sent to PayPal allowing the customer to select it inside the popup. + * @param string[] $funding_sources_without_redirect The sources that do not cause issues about redirecting (on mobile, ...) and sometimes not returning back. + * @param ExperienceContextRepository $experience_context_repository Experience context repository. + * @param LoggerInterface $logger The logger. */ public function __construct( RequestData $request_data, @@ -205,6 +208,7 @@ class CreateOrderEndpoint implements EndpointInterface { array $pay_now_contexts, bool $handle_shipping_in_paypal, array $funding_sources_without_redirect, + ExperienceContextRepository $experience_context_repository, LoggerInterface $logger ) { @@ -222,6 +226,7 @@ class CreateOrderEndpoint implements EndpointInterface { $this->pay_now_contexts = $pay_now_contexts; $this->handle_shipping_in_paypal = $handle_shipping_in_paypal; $this->funding_sources_without_redirect = $funding_sources_without_redirect; + $this->experience_context_repository = $experience_context_repository; $this->logger = $logger; } @@ -438,17 +443,14 @@ class CreateOrderEndpoint implements EndpointInterface { $funding_source ); - $action = in_array( $this->parsed_request_data['context'], $this->pay_now_contexts, true ) ? - ApplicationContext::USER_ACTION_PAY_NOW : ApplicationContext::USER_ACTION_CONTINUE; - if ( 'card' === $funding_source ) { if ( CardBillingMode::MINIMAL_INPUT === $this->card_billing_data_mode ) { - if ( ApplicationContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS === $shipping_preference ) { + if ( ExperienceContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS === $shipping_preference ) { if ( $payer ) { $payer->set_address( null ); } } - if ( ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING === $shipping_preference ) { + if ( ExperienceContext::SHIPPING_PREFERENCE_NO_SHIPPING === $shipping_preference ) { if ( $payer ) { $payer->set_name( null ); } @@ -460,13 +462,23 @@ class CreateOrderEndpoint implements EndpointInterface { } } + $payment_source = null; + $experience_context = $this->experience_context_repository->current_context( $shipping_preference ); + if ( $funding_source ) { + $payment_source = new PaymentSource( + $funding_source, + (object) array( + 'experience_context' => (object) $experience_context->to_array(), + ) + ); + } + try { return $this->api_endpoint->create( array( $this->purchase_unit ), $shipping_preference, $payer, - null, - $action, + $payment_source, $payment_method, $data ); diff --git a/modules/ppcp-vaulting/src/VaultedCreditCardHandler.php b/modules/ppcp-vaulting/src/VaultedCreditCardHandler.php index 3261e4b8e..dee9e55bb 100644 --- a/modules/ppcp-vaulting/src/VaultedCreditCardHandler.php +++ b/modules/ppcp-vaulting/src/VaultedCreditCardHandler.php @@ -9,8 +9,8 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Vaulting; +use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; -use WC_Customer; use WC_Order; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; @@ -144,14 +144,18 @@ class VaultedCreditCardHandler { WC_Order $wc_order ): WC_Order { $tokens = $this->payment_token_repository->all_for_user_id( $wc_order->get_customer_id() ); - $selected_token = null; + $payment_source = null; foreach ( $tokens as $token ) { if ( $token->id() === $saved_credit_card ) { - $selected_token = $token; + $payment_source = new PaymentSource( + 'token', + (object) $token->to_array() + ); + break; } } - if ( ! $selected_token ) { + if ( ! $payment_source ) { throw new RuntimeException( 'Saved card token not found.' ); } @@ -167,7 +171,7 @@ class VaultedCreditCardHandler { array( $purchase_unit ), $shipping_preference, $payer, - $selected_token + $payment_source ); $this->add_paypal_meta( $wc_order, $order, $this->environment ); diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 1af10b4ed..4305c7788 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -13,7 +13,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\BillingAgreementsEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PayUponInvoiceOrderEndpoint; -use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext; +use WooCommerce\PayPalCommerce\ApiClient\Entity\ExperienceContext; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies; @@ -362,7 +362,8 @@ return array( $order_helper, $container->get( 'api.factory.purchase-unit' ), $container->get( 'api.factory.payer' ), - $container->get( 'api.factory.shipping-preference' ) + $container->get( 'api.factory.shipping-preference' ), + $container->get( 'api.repository.experience-context' ) ); }, 'wcgateway.processor.refunds' => static function ( ContainerInterface $container ): RefundProcessor { @@ -642,15 +643,15 @@ return array( 'type' => 'select', 'class' => array(), 'input_class' => array( 'wc-enhanced-select' ), - 'default' => ApplicationContext::LANDING_PAGE_LOGIN, + 'default' => ExperienceContext::LANDING_PAGE_LOGIN, 'desc_tip' => true, 'description' => __( 'Type of PayPal page to display.', 'woocommerce-paypal-payments' ), 'options' => array( - ApplicationContext::LANDING_PAGE_LOGIN => __( 'Login (PayPal account login)', 'woocommerce-paypal-payments' ), - ApplicationContext::LANDING_PAGE_BILLING => __( 'Billing (Non-PayPal account)', 'woocommerce-paypal-payments' ), + ExperienceContext::LANDING_PAGE_LOGIN => __( 'Login (PayPal account login)', 'woocommerce-paypal-payments' ), + ExperienceContext::LANDING_PAGE_BILLING => __( 'Billing (Non-PayPal account)', 'woocommerce-paypal-payments' ), ), 'screens' => array( State::STATE_START, diff --git a/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php b/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php index bf6161e07..c4f4e9d83 100644 --- a/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php +++ b/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php @@ -13,7 +13,7 @@ use Exception; use Psr\Log\LoggerInterface; use WC_Order; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; -use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext; +use WooCommerce\PayPalCommerce\ApiClient\Entity\ExperienceContext; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource; @@ -23,9 +23,11 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory; use WooCommerce\PayPalCommerce\ApiClient\Helper\OrderHelper; +use WooCommerce\PayPalCommerce\ApiClient\Repository\ExperienceContextRepository; use WooCommerce\PayPalCommerce\Button\Helper\ThreeDSecure; use WooCommerce\PayPalCommerce\Onboarding\Environment; use WooCommerce\PayPalCommerce\Session\SessionHandler; +use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper; use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository; use WooCommerce\PayPalCommerce\WcGateway\Exception\PayPalOrderMissingException; @@ -144,6 +146,14 @@ class OrderProcessor { */ private $restore_order_data = array(); + /** + * Experience context repository. + * + * @var ExperienceContextRepository + */ + private $experience_context_repository; + + /** * OrderProcessor constructor. * @@ -160,6 +170,7 @@ class OrderProcessor { * @param PurchaseUnitFactory $purchase_unit_factory The PurchaseUnit factory. * @param PayerFactory $payer_factory The payer factory. * @param ShippingPreferenceFactory $shipping_preference_factory The shipping_preference factory. + * @param ExperienceContextRepository $experience_context_repository Experience context repository. */ public function __construct( SessionHandler $session_handler, @@ -174,7 +185,8 @@ class OrderProcessor { OrderHelper $order_helper, PurchaseUnitFactory $purchase_unit_factory, PayerFactory $payer_factory, - ShippingPreferenceFactory $shipping_preference_factory + ShippingPreferenceFactory $shipping_preference_factory, + ExperienceContextRepository $experience_context_repository ) { $this->session_handler = $session_handler; @@ -190,6 +202,7 @@ class OrderProcessor { $this->purchase_unit_factory = $purchase_unit_factory; $this->payer_factory = $payer_factory; $this->shipping_preference_factory = $shipping_preference_factory; + $this->experience_context_repository = $experience_context_repository; } /** @@ -280,15 +293,25 @@ class OrderProcessor { public function create_order( WC_Order $wc_order ): Order { $pu = $this->purchase_unit_factory->from_wc_order( $wc_order ); $shipping_preference = $this->shipping_preference_factory->from_state( $pu, 'checkout' ); - $order = $this->order_endpoint->create( + + $payment_source = null; + $payment_source_name = $this->payment_source_name( $wc_order->get_payment_method() ); + $experience_context = $this->experience_context_repository->current_context( $shipping_preference, ExperienceContext::USER_ACTION_PAY_NOW ); + if ( $payment_source_name ) { + $payment_source = new PaymentSource( + $payment_source_name, + (object) array( + 'experience_context' => (object) $experience_context->to_array(), + ) + ); + } + + return $this->order_endpoint->create( array( $pu ), $shipping_preference, $this->payer_factory->from_wc_order( $wc_order ), - null, - ApplicationContext::USER_ACTION_PAY_NOW + $payment_source ); - - return $order; } /** @@ -417,4 +440,22 @@ class OrderProcessor { } } } + + /** + * Returns the payment source name from the give payment method. + * + * @param string $payment_method WC Order payment method. + * @return string + */ + private function payment_source_name( string $payment_method ): string { + if ( $payment_method === PayPalGateway::ID ) { + return 'paypal'; + } + + if ( $payment_method === CreditCardGateway::ID ) { + return 'card'; + } + + return ''; + } } diff --git a/modules/ppcp-wc-subscriptions/src/RenewalHandler.php b/modules/ppcp-wc-subscriptions/src/RenewalHandler.php index 30bd99bf5..b9f0ecf0f 100644 --- a/modules/ppcp-wc-subscriptions/src/RenewalHandler.php +++ b/modules/ppcp-wc-subscriptions/src/RenewalHandler.php @@ -13,7 +13,6 @@ use WC_Order; use WC_Subscription; use WC_Payment_Tokens; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; -use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken; @@ -282,10 +281,6 @@ class RenewalHandler { array( $purchase_unit ), $shipping_preference, $payer, - null, - ApplicationContext::USER_ACTION_CONTINUE, - '', - array(), $payment_source ); @@ -325,10 +320,6 @@ class RenewalHandler { array( $purchase_unit ), $shipping_preference, $payer, - null, - ApplicationContext::USER_ACTION_CONTINUE, - '', - array(), $payment_source ); @@ -345,11 +336,16 @@ class RenewalHandler { } if ( $wc_order->get_payment_method() === PayPalGateway::ID ) { + $payment_source = new PaymentSource( + 'token', + (object) $token->to_array() + ); + $order = $this->order_endpoint->create( array( $purchase_unit ), $shipping_preference, $payer, - $token + $payment_source ); $this->handle_paypal_order( $wc_order, $order ); diff --git a/tests/PHPUnit/ApiClient/Endpoint/OrderEndpointTest.php b/tests/PHPUnit/ApiClient/Endpoint/OrderEndpointTest.php index 557551f8f..1e9e0ab51 100644 --- a/tests/PHPUnit/ApiClient/Endpoint/OrderEndpointTest.php +++ b/tests/PHPUnit/ApiClient/Endpoint/OrderEndpointTest.php @@ -7,7 +7,7 @@ use Hamcrest\Matchers; use Requests_Utility_CaseInsensitiveDictionary; use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer; use WooCommerce\PayPalCommerce\ApiClient\Entity\Address; -use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext; +use WooCommerce\PayPalCommerce\ApiClient\Entity\ExperienceContext; use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture; use WooCommerce\PayPalCommerce\ApiClient\Entity\CaptureStatus; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; @@ -23,7 +23,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\PatchCollectionFactory; use WooCommerce\PayPalCommerce\ApiClient\Helper\ErrorResponse; -use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository; +use WooCommerce\PayPalCommerce\ApiClient\Repository\ExperienceContextRepository; use Mockery; use Psr\Log\LoggerInterface; use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper; @@ -66,7 +66,6 @@ class OrderEndpointTest extends TestCase $logger = Mockery::mock(LoggerInterface::class); $logger->shouldNotReceive('log'); $logger->shouldReceive('debug'); - $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); $headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class); $headers->shouldReceive('getAll'); $subscription_helper = Mockery::mock(SubscriptionHelper::class); @@ -80,7 +79,6 @@ class OrderEndpointTest extends TestCase $patchCollectionFactory, $intent, $logger, - $applicationContextRepository, $subscription_helper, false, $fraudnet @@ -127,7 +125,6 @@ class OrderEndpointTest extends TestCase $logger = Mockery::mock(LoggerInterface::class); $logger->shouldReceive('warning'); $logger->shouldReceive('debug'); - $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); $headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class); $headers->shouldReceive('getAll'); $subscription_helper = Mockery::mock(SubscriptionHelper::class); @@ -141,7 +138,6 @@ class OrderEndpointTest extends TestCase $patchCollectionFactory, $intent, $logger, - $applicationContextRepository, $subscription_helper, false, $fraudnet @@ -181,7 +177,6 @@ class OrderEndpointTest extends TestCase $logger = Mockery::mock(LoggerInterface::class); $logger->shouldReceive('log'); $logger->shouldReceive('debug'); - $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); $subscription_helper = Mockery::mock(SubscriptionHelper::class); $fraudnet = Mockery::mock(FraudNet::class); @@ -193,7 +188,6 @@ class OrderEndpointTest extends TestCase $patchCollectionFactory, $intent, $logger, - $applicationContextRepository, $subscription_helper, false, $fraudnet @@ -247,7 +241,6 @@ class OrderEndpointTest extends TestCase $logger = Mockery::mock(LoggerInterface::class); $logger->shouldNotReceive('log'); $logger->shouldReceive('debug'); - $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); $subscription_helper = Mockery::mock(SubscriptionHelper::class); $fraudnet = Mockery::mock(FraudNet::class); @@ -259,7 +252,6 @@ class OrderEndpointTest extends TestCase $patchCollectionFactory, $intent, $logger, - $applicationContextRepository, $subscription_helper, false, $fraudnet @@ -316,7 +308,6 @@ class OrderEndpointTest extends TestCase $logger = Mockery::mock(LoggerInterface::class); $logger->shouldNotReceive('log'); - $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); $subscription_helper = Mockery::mock(SubscriptionHelper::class); $fraudnet = Mockery::mock(FraudNet::class); @@ -328,7 +319,6 @@ class OrderEndpointTest extends TestCase $patchCollectionFactory, $intent, $logger, - $applicationContextRepository, $subscription_helper, false, $fraudnet @@ -361,7 +351,6 @@ class OrderEndpointTest extends TestCase $logger = Mockery::mock(LoggerInterface::class); $logger->shouldReceive('log'); $logger->shouldReceive('debug'); - $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); $subscription_helper = Mockery::mock(SubscriptionHelper::class); $fraudnet = Mockery::mock(FraudNet::class); @@ -373,7 +362,6 @@ class OrderEndpointTest extends TestCase $patchCollectionFactory, $intent, $logger, - $applicationContextRepository, $subscription_helper, false, $fraudnet @@ -413,7 +401,6 @@ class OrderEndpointTest extends TestCase $logger = Mockery::mock(LoggerInterface::class); $logger->shouldReceive('log'); $logger->shouldReceive('debug'); - $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); $subscription_helper = Mockery::mock(SubscriptionHelper::class); $fraudnet = Mockery::mock(FraudNet::class); @@ -425,7 +412,6 @@ class OrderEndpointTest extends TestCase $patchCollectionFactory, $intent, $logger, - $applicationContextRepository, $subscription_helper, false, $fraudnet @@ -467,7 +453,6 @@ class OrderEndpointTest extends TestCase $logger = Mockery::mock(LoggerInterface::class); $logger->shouldNotReceive('log'); $logger->shouldReceive('debug'); - $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); $subscription_helper = Mockery::mock(SubscriptionHelper::class); $fraudnet = Mockery::mock(FraudNet::class); @@ -481,7 +466,6 @@ class OrderEndpointTest extends TestCase $patchCollectionFactory, $intent, $logger, - $applicationContextRepository, $subscription_helper, false, $fraudnet @@ -547,7 +531,6 @@ class OrderEndpointTest extends TestCase $logger = Mockery::mock(LoggerInterface::class); $logger->shouldNotReceive('log'); $logger->shouldReceive('debug'); - $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); $subscription_helper = Mockery::mock(SubscriptionHelper::class); $fraudnet = Mockery::mock(FraudNet::class); @@ -561,7 +544,6 @@ class OrderEndpointTest extends TestCase $patchCollectionFactory, $intent, $logger, - $applicationContextRepository, $subscription_helper, false, $fraudnet @@ -649,7 +631,6 @@ class OrderEndpointTest extends TestCase $logger = Mockery::mock(LoggerInterface::class); $logger->shouldReceive('warning'); $logger->shouldReceive('debug'); - $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); $subscription_helper = Mockery::mock(SubscriptionHelper::class); $fraudnet = Mockery::mock(FraudNet::class); @@ -661,7 +642,6 @@ class OrderEndpointTest extends TestCase $patchCollectionFactory, $intent, $logger, - $applicationContextRepository, $subscription_helper, false, $fraudnet @@ -745,7 +725,6 @@ class OrderEndpointTest extends TestCase $logger->shouldReceive('warning'); $logger->shouldReceive('debug'); - $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); $subscription_helper = Mockery::mock(SubscriptionHelper::class); $fraudnet = Mockery::mock(FraudNet::class); @@ -759,7 +738,6 @@ class OrderEndpointTest extends TestCase $patchCollectionFactory, $intent, $logger, - $applicationContextRepository, $subscription_helper, false, $fraudnet @@ -820,7 +798,6 @@ class OrderEndpointTest extends TestCase $logger = Mockery::mock(LoggerInterface::class); $logger->shouldNotReceive('log'); - $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); $subscription_helper = Mockery::mock(SubscriptionHelper::class); $fraudnet = Mockery::mock(FraudNet::class); @@ -832,7 +809,6 @@ class OrderEndpointTest extends TestCase $patchCollectionFactory, $intent, $logger, - $applicationContextRepository, $subscription_helper, false, $fraudnet @@ -875,15 +851,6 @@ class OrderEndpointTest extends TestCase $logger = Mockery::mock(LoggerInterface::class); $logger->shouldNotReceive('log'); $logger->shouldReceive('debug'); - $applicationContext = Mockery::mock(ApplicationContext::class); - $applicationContext - ->expects('to_array') - ->andReturn(['applicationContext']); - $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); - $applicationContextRepository - ->expects('current_context') - ->with(ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING, ApplicationContext::USER_ACTION_CONTINUE) - ->andReturn($applicationContext); $subscription_helper = Mockery::mock(SubscriptionHelper::class); $subscription_helper->shouldReceive('cart_contains_subscription')->andReturn(true); @@ -896,7 +863,6 @@ class OrderEndpointTest extends TestCase $patchCollectionFactory, $intent, $logger, - $applicationContextRepository, $subscription_helper, false, $fraudnet @@ -941,7 +907,7 @@ class OrderEndpointTest extends TestCase ->expects('email_address') ->andReturn(''); - $result = $testee->create([$purchaseUnit], ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING, $payer); + $result = $testee->create([$purchaseUnit], ExperienceContext::SHIPPING_PREFERENCE_NO_SHIPPING, $payer); $this->assertEquals($expectedOrder, $result); } @@ -978,15 +944,6 @@ class OrderEndpointTest extends TestCase $logger = Mockery::mock(LoggerInterface::class); $logger->shouldNotReceive('log'); $logger->shouldReceive('debug'); - $applicationContext = Mockery::mock(ApplicationContext::class); - $applicationContext - ->expects('to_array') - ->andReturn(['applicationContext']); - $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); - $applicationContextRepository - ->expects('current_context') - ->with(ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE, ApplicationContext::USER_ACTION_CONTINUE) - ->andReturn($applicationContext); $subscription_helper = Mockery::mock(SubscriptionHelper::class); $subscription_helper->shouldReceive('cart_contains_subscription')->andReturn(true); @@ -999,7 +956,6 @@ class OrderEndpointTest extends TestCase $patchCollectionFactory, $intent, $logger, - $applicationContextRepository, $subscription_helper, false, $fraudnet @@ -1030,7 +986,7 @@ class OrderEndpointTest extends TestCase $payer = Mockery::mock(Payer::class); $payer->expects('email_address')->andReturn('email@email.com'); $payer->expects('to_array')->andReturn(['payer']); - $result = $testee->create([$purchaseUnit], ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE, $payer); + $result = $testee->create([$purchaseUnit], ExperienceContext::SHIPPING_PREFERENCE_GET_FROM_FILE, $payer); $this->assertEquals($expectedOrder, $result); } @@ -1058,15 +1014,6 @@ class OrderEndpointTest extends TestCase $logger = Mockery::mock(LoggerInterface::class); $logger->shouldReceive('log'); $logger->shouldReceive('debug'); - $applicationContext = Mockery::mock(ApplicationContext::class); - $applicationContext - ->expects('to_array') - ->andReturn(['applicationContext']); - $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); - $applicationContextRepository - ->expects('current_context') - ->with(ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING, ApplicationContext::USER_ACTION_CONTINUE) - ->andReturn($applicationContext); $subscription_helper = Mockery::mock(SubscriptionHelper::class); $subscription_helper->shouldReceive('cart_contains_subscription')->andReturn(true); @@ -1079,7 +1026,6 @@ class OrderEndpointTest extends TestCase $patchCollectionFactory, $intent, $logger, - $applicationContextRepository, $subscription_helper, false, $fraudnet @@ -1122,7 +1068,7 @@ class OrderEndpointTest extends TestCase $payer = Mockery::mock(Payer::class); $payer->expects('email_address')->andReturn('email@email.com'); $payer->expects('to_array')->andReturn(['payer']); - $testee->create([$purchaseUnit], ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING, $payer); + $testee->create([$purchaseUnit], ExperienceContext::SHIPPING_PREFERENCE_NO_SHIPPING, $payer); } public function testCreateForPurchaseUnitsIsNot201() @@ -1150,15 +1096,6 @@ class OrderEndpointTest extends TestCase $logger->shouldReceive('log'); $logger->shouldReceive('debug'); $logger->shouldReceive('warning'); - $applicationContext = Mockery::mock(ApplicationContext::class); - $applicationContext - ->expects('to_array') - ->andReturn(['applicationContext']); - $applicationContextRepository = Mockery::mock(ApplicationContextRepository::class); - $applicationContextRepository - ->expects('current_context') - ->with(ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE, ApplicationContext::USER_ACTION_CONTINUE) - ->andReturn($applicationContext); $subscription_helper = Mockery::mock(SubscriptionHelper::class); $subscription_helper->shouldReceive('cart_contains_subscription')->andReturn(true); @@ -1171,7 +1108,6 @@ class OrderEndpointTest extends TestCase $patchCollectionFactory, $intent, $logger, - $applicationContextRepository, $subscription_helper, false, $fraudnet @@ -1214,7 +1150,7 @@ class OrderEndpointTest extends TestCase $payer = Mockery::mock(Payer::class); $payer->expects('email_address')->andReturn('email@email.com'); $payer->expects('to_array')->andReturn(['payer']); - $testee->create([$purchaseUnit], ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE, $payer); + $testee->create([$purchaseUnit], ExperienceContext::SHIPPING_PREFERENCE_GET_FROM_FILE, $payer); } } diff --git a/tests/PHPUnit/ApiClient/Endpoint/OrderTest.php b/tests/PHPUnit/ApiClient/Endpoint/OrderTest.php index 6c8abb05f..5c68505fd 100644 --- a/tests/PHPUnit/ApiClient/Endpoint/OrderTest.php +++ b/tests/PHPUnit/ApiClient/Endpoint/OrderTest.php @@ -3,7 +3,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint; -use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext; +use WooCommerce\PayPalCommerce\ApiClient\Entity\ExperienceContext; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer; @@ -29,7 +29,7 @@ class OrderTest extends TestCase $payer ->expects('to_array')->andReturn(['payer']); $intent = 'AUTHORIZE'; - $applicationContext = Mockery::mock(ApplicationContext::class); + $applicationContext = Mockery::mock(ExperienceContext::class); $applicationContext ->expects('to_array') ->andReturn(['applicationContext']); diff --git a/tests/PHPUnit/ApiClient/Factory/OrderFactoryTest.php b/tests/PHPUnit/ApiClient/Factory/OrderFactoryTest.php index 614fe555a..ad52a9be3 100644 --- a/tests/PHPUnit/ApiClient/Factory/OrderFactoryTest.php +++ b/tests/PHPUnit/ApiClient/Factory/OrderFactoryTest.php @@ -6,10 +6,9 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Factory; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer; -use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource; use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; -use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository; +use WooCommerce\PayPalCommerce\ApiClient\Repository\ExperienceContextRepository; use WooCommerce\PayPalCommerce\TestCase; use Mockery; @@ -29,24 +28,21 @@ class OrderFactoryTest extends TestCase $order->expects('intent')->andReturn('intent'); $order->expects('create_time')->andReturn($createTime); $order->expects('update_time')->andReturn($updateTime); - $order->expects('application_context')->andReturnNull(); + $order->expects('experience_context')->andReturnNull(); $order->expects('payment_source')->andReturnNull(); $wcOrder = Mockery::mock(\WC_Order::class); $purchaseUnitFactory = Mockery::mock(PurchaseUnitFactory::class); $purchaseUnit = Mockery::mock(PurchaseUnit::class); $purchaseUnitFactory->expects('from_wc_order')->with($wcOrder)->andReturn($purchaseUnit); $payerFactory = Mockery::mock(PayerFactory::class); - $applicationRepository = Mockery::mock(ApplicationContextRepository::class); - $applicationFactory = Mockery::mock(ApplicationContextFactory::class); - $paymentSourceFactory = Mockery::mock(PaymentSourceFactory::class); - + $applicationRepository = Mockery::mock(ExperienceContextRepository::class); + $applicationFactory = Mockery::mock(ExperienceContextFactory::class); $testee = new OrderFactory( $purchaseUnitFactory, $payerFactory, $applicationRepository, - $applicationFactory, - $paymentSourceFactory + $applicationFactory ); $result = $testee->from_wc_order($wcOrder, $order); $resultPurchaseUnit = current($result->purchase_units()); @@ -72,8 +68,8 @@ class OrderFactoryTest extends TestCase ->expects('from_paypal_response') ->andReturn(Mockery::mock(Payer::class)); } - $applicationRepository = Mockery::mock(ApplicationContextRepository::class); - $applicationFactory = Mockery::mock(ApplicationContextFactory::class); + $applicationRepository = Mockery::mock(ExperienceContextRepository::class); + $applicationFactory = Mockery::mock(ExperienceContextFactory::class); $paymentSourceFactory = Mockery::mock(PaymentSourceFactory::class); $testee = new OrderFactory( @@ -161,8 +157,8 @@ class OrderFactoryTest extends TestCase { $purchaseUnitFactory = Mockery::mock(PurchaseUnitFactory::class); $payerFactory = Mockery::mock(PayerFactory::class); - $applicationRepository = Mockery::mock(ApplicationContextRepository::class); - $applicationFactory = Mockery::mock(ApplicationContextFactory::class); + $applicationRepository = Mockery::mock(ExperienceContextRepository::class); + $applicationFactory = Mockery::mock(ExperienceContextFactory::class); $paymentSourceFactory = Mockery::mock(PaymentSourceFactory::class); $testee = new OrderFactory( diff --git a/tests/PHPUnit/ApiClient/Factory/ShippingPreferenceFactoryTest.php b/tests/PHPUnit/ApiClient/Factory/ShippingPreferenceFactoryTest.php index 4da631f06..d16dc2dac 100644 --- a/tests/PHPUnit/ApiClient/Factory/ShippingPreferenceFactoryTest.php +++ b/tests/PHPUnit/ApiClient/Factory/ShippingPreferenceFactoryTest.php @@ -5,7 +5,7 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Factory; use Mockery; use WC_Cart; -use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext; +use WooCommerce\PayPalCommerce\ApiClient\Entity\ExperienceContext; use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit; use WooCommerce\PayPalCommerce\ApiClient\Entity\Shipping; use WooCommerce\PayPalCommerce\TestCase; @@ -43,63 +43,63 @@ class ShippingPreferenceFactoryTest extends TestCase 'checkout', $this->createCart(true), '', - ApplicationContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS, + ExperienceContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS, ]; yield [ $this->createPurchaseUnit(false, Mockery::mock(Shipping::class)), 'checkout', $this->createCart(false), '', - ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING, + ExperienceContext::SHIPPING_PREFERENCE_NO_SHIPPING, ]; yield [ $this->createPurchaseUnit(true, null), 'checkout', $this->createCart(true), '', - ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING, + ExperienceContext::SHIPPING_PREFERENCE_NO_SHIPPING, ]; yield [ $this->createPurchaseUnit(true, Mockery::mock(Shipping::class)), 'checkout', $this->createCart(true), 'card', - ApplicationContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS, + ExperienceContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS, ]; yield [ $this->createPurchaseUnit(true, null), 'product', null, '', - ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE, + ExperienceContext::SHIPPING_PREFERENCE_GET_FROM_FILE, ]; yield [ $this->createPurchaseUnit(true, null), 'pay-now', null, 'venmo', - ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE, + ExperienceContext::SHIPPING_PREFERENCE_GET_FROM_FILE, ]; yield [ $this->createPurchaseUnit(true, Mockery::mock(Shipping::class)), 'pay-now', null, 'venmo', - ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE, + ExperienceContext::SHIPPING_PREFERENCE_GET_FROM_FILE, ]; yield [ $this->createPurchaseUnit(true, Mockery::mock(Shipping::class)), 'pay-now', null, 'card', - ApplicationContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS, + ExperienceContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS, ]; yield [ $this->createPurchaseUnit(true, null), 'pay-now', null, 'card', - ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING, + ExperienceContext::SHIPPING_PREFERENCE_NO_SHIPPING, ]; } diff --git a/tests/PHPUnit/ApiClient/Repository/ApplicationContextRepositoryTest.php b/tests/PHPUnit/ApiClient/Repository/ApplicationContextRepositoryTest.php index f75e149eb..9f9b12a0f 100644 --- a/tests/PHPUnit/ApiClient/Repository/ApplicationContextRepositoryTest.php +++ b/tests/PHPUnit/ApiClient/Repository/ApplicationContextRepositoryTest.php @@ -13,7 +13,7 @@ class ApplicationContextRepositoryTest extends TestCase */ public function test_valid_bcp47_code($input, $output) { - $testee = $this->getMockBuilder(ApplicationContextRepository::class) + $testee = $this->getMockBuilder(ExperienceContextRepository::class) ->disableOriginalConstructor() ->getMock(); diff --git a/tests/PHPUnit/Button/Endpoint/CreateOrderEndpointTest.php b/tests/PHPUnit/Button/Endpoint/CreateOrderEndpointTest.php index 79f80de3f..897181597 100644 --- a/tests/PHPUnit/Button/Endpoint/CreateOrderEndpointTest.php +++ b/tests/PHPUnit/Button/Endpoint/CreateOrderEndpointTest.php @@ -11,6 +11,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory; +use WooCommerce\PayPalCommerce\ApiClient\Repository\ExperienceContextRepository; use WooCommerce\PayPalCommerce\Button\Helper\EarlyOrderHandler; use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\TestCase; @@ -154,6 +155,7 @@ class CreateOrderEndpointTest extends TestCase $settings = Mockery::mock(Settings::class); $early_order_handler = Mockery::mock(EarlyOrderHandler::class); $settings->shouldReceive('has')->andReturnFalse(); + $experience_context_repository = Mockery::mock(ExperienceContextRepository::class); $testee = new CreateOrderEndpoint( $request_data, @@ -170,6 +172,7 @@ class CreateOrderEndpointTest extends TestCase ['checkout'], false, ['paypal'], + $experience_context_repository, new NullLogger() ); return array($payer_factory, $testee); diff --git a/tests/PHPUnit/Vaulting/VaultedCreditCardHandlerTest.php b/tests/PHPUnit/Vaulting/VaultedCreditCardHandlerTest.php deleted file mode 100644 index fb728709a..000000000 --- a/tests/PHPUnit/Vaulting/VaultedCreditCardHandlerTest.php +++ /dev/null @@ -1,134 +0,0 @@ -subscriptionHelper = Mockery::mock(SubscriptionHelper::class); - $this->paymentTokenRepository = Mockery::mock(PaymentTokenRepository::class); - $this->purchaseUnitFactory = Mockery::mock(PurchaseUnitFactory::class); - $this->payerFactory = Mockery::mock(PayerFactory::class); - $this->shippingPreferenceFactory = Mockery::mock(ShippingPreferenceFactory::class); - $this->orderEndpoint = Mockery::mock(OrderEndpoint::class); - $this->environment = Mockery::mock(Environment::class); - $this->authorizedPaymentProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class); - $this->config = Mockery::mock(ContainerInterface::class); - - $this->testee = new VaultedCreditCardHandler( - $this->subscriptionHelper, - $this->paymentTokenRepository, - $this->purchaseUnitFactory, - $this->payerFactory, - $this->shippingPreferenceFactory, - $this->orderEndpoint, - $this->environment, - $this->authorizedPaymentProcessor, - $this->config - ); - } - - public function testHandlePayment() - { - $_POST['woocommerce_change_payment'] = null; - - $wcOrder = Mockery::mock(\WC_Order::class); - $wcOrder->shouldReceive('get_id')->andReturn(1); - $wcOrder->shouldReceive('get_customer_id')->andReturn(1); - $wcOrder->shouldReceive('update_meta_data')->andReturn(1); - $wcOrder->shouldReceive('save')->once(); - $wcOrder->shouldReceive('payment_complete')->andReturn(true); - - $token = Mockery::mock(PaymentToken::class); - $tokenId = 'abc123'; - $token->shouldReceive('id')->andReturn($tokenId); - $this->paymentTokenRepository->shouldReceive('all_for_user_id') - ->andReturn([$token]); - - $purchaseUnit = Mockery::mock(PurchaseUnit::class); - $this->purchaseUnitFactory->shouldReceive('from_wc_order') - ->andReturn($purchaseUnit); - - $customer = Mockery::mock(WC_Customer::class); - - $payer = Mockery::mock(Payer::class); - $this->payerFactory->shouldReceive('from_wc_order') - ->andReturn($payer); - $this->shippingPreferenceFactory->shouldReceive('from_state') - ->andReturn('some_preference'); - - $order = Mockery::mock(Order::class); - $order->shouldReceive('id')->andReturn('1'); - $order->shouldReceive('intent')->andReturn('CAPTURE'); - - $paymentSource = Mockery::mock(PaymentSource::class); - $paymentSource->shouldReceive('name')->andReturn('card'); - $order->shouldReceive('payment_source')->andReturn($paymentSource); - - $orderStatus = Mockery::mock(OrderStatus::class); - $orderStatus->shouldReceive('is')->andReturn(true); - $order->shouldReceive('status')->andReturn($orderStatus); - - $order->shouldReceive('purchase_units')->andReturn([$purchaseUnit]); - $payments = Mockery::mock(Payments::class); - $capture = Mockery::mock(Capture::class); - $capture->shouldReceive('id')->andReturn('1'); - $captureStatus = Mockery::mock(CaptureStatus::class); - $captureStatus->shouldReceive('details')->andReturn(null); - $captureStatus->shouldReceive('name')->andReturn(CaptureStatus::COMPLETED); - $capture->shouldReceive('status')->andReturn($captureStatus); - $payments->shouldReceive('captures')->andReturn([$capture]); - $purchaseUnit->shouldReceive('payments')->andReturn($payments); - - $this->orderEndpoint->shouldReceive('create') - ->with([$purchaseUnit], 'some_preference', $payer, $token) - ->andReturn($order); - - $this->environment->shouldReceive('current_environment_is')->andReturn(true); - - $this->config->shouldReceive('has')->andReturn(false); - - $result = $this->testee->handle_payment($tokenId, $wcOrder, $customer); - $this->assertInstanceOf(\WC_Order::class, $result); - } -} diff --git a/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php b/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php index d27ec6ff7..f102727fa 100644 --- a/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php +++ b/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php @@ -7,6 +7,7 @@ use Exception; use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory; +use WooCommerce\PayPalCommerce\ApiClient\Repository\ExperienceContextRepository; use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\Dictionary; use Psr\Log\LoggerInterface; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; @@ -149,7 +150,8 @@ class OrderProcessorTest extends TestCase $order_helper, Mockery::mock(PurchaseUnitFactory::class), Mockery::mock(PayerFactory::class), - Mockery::mock(ShippingPreferenceFactory::class) + Mockery::mock(ShippingPreferenceFactory::class), + Mockery::mock(ExperienceContextRepository::class) ); $wcOrder @@ -279,7 +281,8 @@ class OrderProcessorTest extends TestCase $order_helper, Mockery::mock(PurchaseUnitFactory::class), Mockery::mock(PayerFactory::class), - Mockery::mock(ShippingPreferenceFactory::class) + Mockery::mock(ShippingPreferenceFactory::class), + Mockery::mock(ExperienceContextRepository::class) ); $wcOrder @@ -391,7 +394,8 @@ class OrderProcessorTest extends TestCase $order_helper, Mockery::mock(PurchaseUnitFactory::class), Mockery::mock(PayerFactory::class), - Mockery::mock(ShippingPreferenceFactory::class) + Mockery::mock(ShippingPreferenceFactory::class), + Mockery::mock(ExperienceContextRepository::class) ); $wcOrder diff --git a/tests/PHPUnit/WcGateway/Repository/ApplicationContextRepositoryTest.php b/tests/PHPUnit/WcGateway/Repository/ApplicationContextRepositoryTest.php index 1bd5b59f3..a3e44a08f 100644 --- a/tests/PHPUnit/WcGateway/Repository/ApplicationContextRepositoryTest.php +++ b/tests/PHPUnit/WcGateway/Repository/ApplicationContextRepositoryTest.php @@ -5,8 +5,8 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\WcGateway\Repository; use Hamcrest\Matchers; -use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext; -use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository; +use WooCommerce\PayPalCommerce\ApiClient\Entity\ExperienceContext; +use WooCommerce\PayPalCommerce\ApiClient\Repository\ExperienceContextRepository; use WooCommerce\PayPalCommerce\TestCase; use Mockery\MockInterface; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; @@ -44,13 +44,13 @@ class ApplicationContextRepositoryTest extends TestCase expect('get_user_locale') ->andReturn($userLocale); - /* @var ApplicationContextRepository $testee */ + /* @var ExperienceContextRepository $testee */ $testee = $this->buildTestee()[1]; $context = $testee->current_context($shippingPreference); self::assertInstanceOf( - ApplicationContext::class, + ExperienceContext::class, $context ); @@ -72,17 +72,17 @@ class ApplicationContextRepositoryTest extends TestCase 'default test' => [ 'container' => [ 'brand_name' => 'Acme corp.', - 'landing_page' => ApplicationContext::LANDING_PAGE_BILLING, + 'landing_page' => ExperienceContext::LANDING_PAGE_BILLING, 'payee_preferred' => '', ], 'user_locale' => 'de_DE', - 'shippingPreference' => ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING, + 'shippingPreference' => ExperienceContext::SHIPPING_PREFERENCE_NO_SHIPPING, 'expected' => [ 'locale' => 'de-DE', 'brand_name' => 'Acme corp.', - 'landing_page' => ApplicationContext::LANDING_PAGE_BILLING, - 'shipping_preference' => ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING, - 'payment_method_preference' => ApplicationContext::PAYMENT_METHOD_UNRESTRICTED, + 'landing_page' => ExperienceContext::LANDING_PAGE_BILLING, + 'shipping_preference' => ExperienceContext::SHIPPING_PREFERENCE_NO_SHIPPING, + 'payment_method_preference' => ExperienceContext::PAYMENT_METHOD_UNRESTRICTED, ], ], ]; @@ -101,7 +101,7 @@ class ApplicationContextRepositoryTest extends TestCase { if (! self::$mocks) { $config = \Mockery::mock(ContainerInterface::class); - $testee = new ApplicationContextRepository($config); + $testee = new ExperienceContextRepository($config); self::$mocks = [ $config,