Add missing order meta

This commit is contained in:
Alex P 2021-10-06 17:44:41 +03:00
parent df68c948b9
commit 45249966e9
8 changed files with 153 additions and 31 deletions

View file

@ -53,6 +53,7 @@ return array(
$transaction_url_provider = $container->get( 'wcgateway.transaction-url-provider' ); $transaction_url_provider = $container->get( 'wcgateway.transaction-url-provider' );
$subscription_helper = $container->get( 'subscription.helper' ); $subscription_helper = $container->get( 'subscription.helper' );
$page_id = $container->get( 'wcgateway.current-ppcp-settings-page-id' ); $page_id = $container->get( 'wcgateway.current-ppcp-settings-page-id' );
$environment = $container->get( 'onboarding.environment' );
return new PayPalGateway( return new PayPalGateway(
$settings_renderer, $settings_renderer,
$order_processor, $order_processor,
@ -64,7 +65,8 @@ return array(
$state, $state,
$transaction_url_provider, $transaction_url_provider,
$subscription_helper, $subscription_helper,
$page_id $page_id,
$environment
); );
}, },
'wcgateway.credit-card-gateway' => static function ( $container ): CreditCardGateway { 'wcgateway.credit-card-gateway' => static function ( $container ): CreditCardGateway {
@ -83,7 +85,8 @@ return array(
$payer_factory = $container->get( 'api.factory.payer' ); $payer_factory = $container->get( 'api.factory.payer' );
$order_endpoint = $container->get( 'api.endpoint.order' ); $order_endpoint = $container->get( 'api.endpoint.order' );
$subscription_helper = $container->get( 'subscription.helper' ); $subscription_helper = $container->get( 'subscription.helper' );
$logger = $container->get( 'woocommerce.logger.woocommerce' ); $logger = $container->get( 'woocommerce.logger.woocommerce' );
$environment = $container->get( 'onboarding.environment' );
return new CreditCardGateway( return new CreditCardGateway(
$settings_renderer, $settings_renderer,
$order_processor, $order_processor,
@ -100,7 +103,8 @@ return array(
$payer_factory, $payer_factory,
$order_endpoint, $order_endpoint,
$subscription_helper, $subscription_helper,
$logger $logger,
$environment
); );
}, },
'wcgateway.disabler' => static function ( $container ): DisableGateways { 'wcgateway.disabler' => static function ( $container ): DisableGateways {
@ -209,7 +213,7 @@ return array(
$authorized_payments_processor, $authorized_payments_processor,
$settings, $settings,
$logger, $logger,
$environment->current_environment_is( Environment::SANDBOX ) $environment
); );
}, },
'wcgateway.processor.refunds' => static function ( $container ): RefundProcessor { 'wcgateway.processor.refunds' => static function ( $container ): RefundProcessor {

View file

@ -13,6 +13,7 @@ use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper; use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
@ -96,6 +97,13 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
*/ */
private $order_endpoint; private $order_endpoint;
/**
* The environment.
*
* @var Environment
*/
protected $environment;
/** /**
* CreditCardGateway constructor. * CreditCardGateway constructor.
* *
@ -115,6 +123,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
* @param OrderEndpoint $order_endpoint The order endpoint. * @param OrderEndpoint $order_endpoint The order endpoint.
* @param SubscriptionHelper $subscription_helper The subscription helper. * @param SubscriptionHelper $subscription_helper The subscription helper.
* @param LoggerInterface $logger The logger. * @param LoggerInterface $logger The logger.
* @param Environment $environment The environment.
*/ */
public function __construct( public function __construct(
SettingsRenderer $settings_renderer, SettingsRenderer $settings_renderer,
@ -132,7 +141,8 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
PayerFactory $payer_factory, PayerFactory $payer_factory,
OrderEndpoint $order_endpoint, OrderEndpoint $order_endpoint,
SubscriptionHelper $subscription_helper, SubscriptionHelper $subscription_helper,
LoggerInterface $logger LoggerInterface $logger,
Environment $environment
) { ) {
$this->id = self::ID; $this->id = self::ID;
@ -143,6 +153,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
$this->config = $config; $this->config = $config;
$this->session_handler = $session_handler; $this->session_handler = $session_handler;
$this->refund_processor = $refund_processor; $this->refund_processor = $refund_processor;
$this->environment = $environment;
if ( $state->current_state() === State::STATE_ONBOARDED ) { if ( $state->current_state() === State::STATE_ONBOARDED ) {
$this->supports = array( 'refunds' ); $this->supports = array( 'refunds' );
@ -424,4 +435,13 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
private function is_enabled(): bool { private function is_enabled(): bool {
return $this->config->has( 'dcc_enabled' ) && $this->config->get( 'dcc_enabled' ); return $this->config->has( 'dcc_enabled' ) && $this->config->get( 'dcc_enabled' );
} }
/**
* Returns the environment.
*
* @return Environment
*/
protected function environment(): Environment {
return $this->environment;
}
} }

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway; namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper; use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
@ -111,6 +112,13 @@ class PayPalGateway extends \WC_Payment_Gateway {
*/ */
protected $page_id; protected $page_id;
/**
* The environment.
*
* @var Environment
*/
protected $environment;
/** /**
* PayPalGateway constructor. * PayPalGateway constructor.
* *
@ -125,6 +133,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
* @param TransactionUrlProvider $transaction_url_provider Service providing transaction view URL based on order. * @param TransactionUrlProvider $transaction_url_provider Service providing transaction view URL based on order.
* @param SubscriptionHelper $subscription_helper The subscription helper. * @param SubscriptionHelper $subscription_helper The subscription helper.
* @param string $page_id ID of the current PPCP gateway settings page, or empty if it is not such page. * @param string $page_id ID of the current PPCP gateway settings page, or empty if it is not such page.
* @param Environment $environment The environment.
*/ */
public function __construct( public function __construct(
SettingsRenderer $settings_renderer, SettingsRenderer $settings_renderer,
@ -137,7 +146,8 @@ class PayPalGateway extends \WC_Payment_Gateway {
State $state, State $state,
TransactionUrlProvider $transaction_url_provider, TransactionUrlProvider $transaction_url_provider,
SubscriptionHelper $subscription_helper, SubscriptionHelper $subscription_helper,
string $page_id string $page_id,
Environment $environment
) { ) {
$this->id = self::ID; $this->id = self::ID;
@ -150,6 +160,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
$this->refund_processor = $refund_processor; $this->refund_processor = $refund_processor;
$this->transaction_url_provider = $transaction_url_provider; $this->transaction_url_provider = $transaction_url_provider;
$this->page_id = $page_id; $this->page_id = $page_id;
$this->environment = $environment;
$this->onboarded = $state->current_state() === State::STATE_ONBOARDED; $this->onboarded = $state->current_state() === State::STATE_ONBOARDED;
if ( $this->onboarded ) { if ( $this->onboarded ) {
@ -430,4 +441,13 @@ class PayPalGateway extends \WC_Payment_Gateway {
return $ret; return $ret;
} }
/**
* Returns the environment.
*
* @return Environment
*/
protected function environment(): Environment {
return $this->environment;
}
} }

View file

@ -13,11 +13,16 @@ use Exception;
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderMetaTrait;
/** /**
* Trait ProcessPaymentTrait * Trait ProcessPaymentTrait
*/ */
trait ProcessPaymentTrait { trait ProcessPaymentTrait {
use OrderMetaTrait;
/** /**
* Process a payment for an WooCommerce order. * Process a payment for an WooCommerce order.
* *
@ -74,6 +79,8 @@ trait ProcessPaymentTrait {
$selected_token $selected_token
); );
$this->add_paypal_meta( $wc_order, $order, $this->environment() );
if ( $order->status()->is( OrderStatus::COMPLETED ) && $order->intent() === 'CAPTURE' ) { if ( $order->status()->is( OrderStatus::COMPLETED ) && $order->intent() === 'CAPTURE' ) {
$wc_order->update_status( $wc_order->update_status(
'processing', 'processing',
@ -90,7 +97,6 @@ trait ProcessPaymentTrait {
if ( $order->status()->is( OrderStatus::COMPLETED ) && $order->intent() === 'AUTHORIZE' ) { if ( $order->status()->is( OrderStatus::COMPLETED ) && $order->intent() === 'AUTHORIZE' ) {
$this->order_endpoint->authorize( $order ); $this->order_endpoint->authorize( $order );
$wc_order->update_meta_data( PayPalGateway::CAPTURED_META_KEY, 'false' ); $wc_order->update_meta_data( PayPalGateway::CAPTURED_META_KEY, 'false' );
$wc_order->update_meta_data( PayPalGateway::ORDER_ID_META_KEY, $order->id() );
$wc_order->update_status( $wc_order->update_status(
'on-hold', 'on-hold',
__( 'Awaiting payment.', 'woocommerce-paypal-payments' ) __( 'Awaiting payment.', 'woocommerce-paypal-payments' )
@ -246,4 +252,11 @@ trait ProcessPaymentTrait {
wc_add_notice( $error->getMessage(), 'error' ); wc_add_notice( $error->getMessage(), 'error' );
} }
/**
* Returns the environment.
*
* @return Environment
*/
abstract protected function environment(): Environment;
} }

View file

@ -0,0 +1,41 @@
<?php
/**
* Adds common metadata to the order.
*
* @package WooCommerce\PayPalCommerce\WcGateway\Processor
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Processor;
use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
/**
* Trait OrderMetaTrait.
*/
trait OrderMetaTrait {
/**
* Adds common metadata to the order.
*
* @param WC_Order $wc_order The WC order to which metadata will be added.
* @param Order $order The PayPal order.
* @param Environment $environment The environment.
*/
protected function add_paypal_meta(
WC_Order $wc_order,
Order $order,
Environment $environment
): void {
$wc_order->update_meta_data( PayPalGateway::ORDER_ID_META_KEY, $order->id() );
$wc_order->update_meta_data( PayPalGateway::INTENT_META_KEY, $order->intent() );
$wc_order->update_meta_data(
PayPalGateway::ORDER_PAYMENT_MODE_META_KEY,
$environment->current_environment_is( Environment::SANDBOX ) ? 'sandbox' : 'live'
);
}
}

View file

@ -15,6 +15,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory;
use WooCommerce\PayPalCommerce\Button\Helper\ThreeDSecure; use WooCommerce\PayPalCommerce\Button\Helper\ThreeDSecure;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository; use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
@ -25,12 +26,14 @@ use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
*/ */
class OrderProcessor { class OrderProcessor {
use OrderMetaTrait;
/** /**
* Whether current payment mode is sandbox. * The environment.
* *
* @var bool * @var Environment
*/ */
protected $sandbox_mode; protected $environment;
/** /**
* The payment token repository. * The payment token repository.
@ -105,7 +108,7 @@ class OrderProcessor {
* @param AuthorizedPaymentsProcessor $authorized_payments_processor The Authorized Payments Processor. * @param AuthorizedPaymentsProcessor $authorized_payments_processor The Authorized Payments Processor.
* @param Settings $settings The Settings. * @param Settings $settings The Settings.
* @param LoggerInterface $logger A logger service. * @param LoggerInterface $logger A logger service.
* @param bool $sandbox_mode Whether sandbox mode enabled. * @param Environment $environment The environment.
*/ */
public function __construct( public function __construct(
SessionHandler $session_handler, SessionHandler $session_handler,
@ -115,7 +118,7 @@ class OrderProcessor {
AuthorizedPaymentsProcessor $authorized_payments_processor, AuthorizedPaymentsProcessor $authorized_payments_processor,
Settings $settings, Settings $settings,
LoggerInterface $logger, LoggerInterface $logger,
bool $sandbox_mode Environment $environment
) { ) {
$this->session_handler = $session_handler; $this->session_handler = $session_handler;
@ -124,7 +127,7 @@ class OrderProcessor {
$this->threed_secure = $three_d_secure; $this->threed_secure = $three_d_secure;
$this->authorized_payments_processor = $authorized_payments_processor; $this->authorized_payments_processor = $authorized_payments_processor;
$this->settings = $settings; $this->settings = $settings;
$this->sandbox_mode = $sandbox_mode; $this->environment = $environment;
$this->logger = $logger; $this->logger = $logger;
} }
@ -140,12 +143,8 @@ class OrderProcessor {
if ( ! $order ) { if ( ! $order ) {
return false; return false;
} }
$wc_order->update_meta_data( PayPalGateway::ORDER_ID_META_KEY, $order->id() );
$wc_order->update_meta_data( PayPalGateway::INTENT_META_KEY, $order->intent() ); $this->add_paypal_meta( $wc_order, $order, $this->environment );
$wc_order->update_meta_data(
PayPalGateway::ORDER_PAYMENT_MODE_META_KEY,
$this->sandbox_mode ? 'sandbox' : 'live'
);
$error_message = null; $error_message = null;
if ( ! $this->order_is_approved( $order ) ) { if ( ! $this->order_is_approved( $order ) ) {
@ -164,6 +163,7 @@ class OrderProcessor {
} }
$order = $this->patch_order( $wc_order, $order ); $order = $this->patch_order( $wc_order, $order );
if ( $order->intent() === 'CAPTURE' ) { if ( $order->intent() === 'CAPTURE' ) {
$order = $this->order_endpoint->capture( $order ); $order = $this->order_endpoint->capture( $order );
} }

View file

@ -5,6 +5,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper; use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
@ -21,8 +22,15 @@ use function Brain\Monkey\Functions\when;
class WcGatewayTest extends TestCase class WcGatewayTest extends TestCase
{ {
private $environment;
public function testProcessPaymentSuccess() { public function setUp(): void {
parent::setUp();
$this->environment = Mockery::mock(Environment::class);
}
public function testProcessPaymentSuccess() {
expect('is_admin')->andReturn(false); expect('is_admin')->andReturn(false);
$orderId = 1; $orderId = 1;
@ -69,7 +77,8 @@ class WcGatewayTest extends TestCase
$state, $state,
$transactionUrlProvider, $transactionUrlProvider,
$subscriptionHelper, $subscriptionHelper,
PayPalGateway::ID PayPalGateway::ID,
$this->environment
); );
expect('wc_get_order') expect('wc_get_order')
@ -118,7 +127,8 @@ class WcGatewayTest extends TestCase
$state, $state,
$transactionUrlProvider, $transactionUrlProvider,
$subscriptionHelper, $subscriptionHelper,
PayPalGateway::ID PayPalGateway::ID,
$this->environment
); );
expect('wc_get_order') expect('wc_get_order')
@ -184,7 +194,8 @@ class WcGatewayTest extends TestCase
$state, $state,
$transactionUrlProvider, $transactionUrlProvider,
$subscriptionHelper, $subscriptionHelper,
PayPalGateway::ID PayPalGateway::ID,
$this->environment
); );
expect('wc_get_order') expect('wc_get_order')
@ -255,7 +266,8 @@ class WcGatewayTest extends TestCase
$state, $state,
$transactionUrlProvider, $transactionUrlProvider,
$subscriptionHelper, $subscriptionHelper,
PayPalGateway::ID PayPalGateway::ID,
$this->environment
); );
$this->assertTrue($testee->capture_authorized_payment($wcOrder)); $this->assertTrue($testee->capture_authorized_payment($wcOrder));
@ -310,7 +322,8 @@ class WcGatewayTest extends TestCase
$state, $state,
$transactionUrlProvider, $transactionUrlProvider,
$subscriptionHelper, $subscriptionHelper,
PayPalGateway::ID PayPalGateway::ID,
$this->environment
); );
$this->assertTrue($testee->capture_authorized_payment($wcOrder)); $this->assertTrue($testee->capture_authorized_payment($wcOrder));
@ -359,7 +372,8 @@ class WcGatewayTest extends TestCase
$state, $state,
$transactionUrlProvider, $transactionUrlProvider,
$subscriptionHelper, $subscriptionHelper,
PayPalGateway::ID PayPalGateway::ID,
$this->environment
); );
$this->assertFalse($testee->capture_authorized_payment($wcOrder)); $this->assertFalse($testee->capture_authorized_payment($wcOrder));
@ -399,7 +413,8 @@ class WcGatewayTest extends TestCase
$onboardingState, $onboardingState,
$transactionUrlProvider, $transactionUrlProvider,
$subscriptionHelper, $subscriptionHelper,
PayPalGateway::ID PayPalGateway::ID,
$this->environment
); );
$this->assertSame($needSetup, $testee->needs_setup()); $this->assertSame($needSetup, $testee->needs_setup());

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Processor; namespace WooCommerce\PayPalCommerce\WcGateway\Processor;
use Dhii\Container\Dictionary;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use Woocommerce\PayPalCommerce\ApiClient\Entity\Capture; use Woocommerce\PayPalCommerce\ApiClient\Entity\Capture;
@ -13,6 +14,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Payments;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit; use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory;
use WooCommerce\PayPalCommerce\Button\Helper\ThreeDSecure; use WooCommerce\PayPalCommerce\Button\Helper\ThreeDSecure;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\TestCase; use WooCommerce\PayPalCommerce\TestCase;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
@ -22,6 +24,13 @@ use function Brain\Monkey\Functions\when;
class OrderProcessorTest extends TestCase class OrderProcessorTest extends TestCase
{ {
private $environment;
public function setUp(): void {
parent::setUp();
$this->environment = new Environment(new Dictionary([]));
}
public function testAuthorize() { public function testAuthorize() {
$transactionId = 'ABC123'; $transactionId = 'ABC123';
@ -112,7 +121,7 @@ class OrderProcessorTest extends TestCase
$authorizedPaymentProcessor, $authorizedPaymentProcessor,
$settings, $settings,
$logger, $logger,
false $this->environment
); );
$cart = Mockery::mock(\WC_Cart::class); $cart = Mockery::mock(\WC_Cart::class);
@ -240,7 +249,7 @@ class OrderProcessorTest extends TestCase
$authorizedPaymentProcessor, $authorizedPaymentProcessor,
$settings, $settings,
$logger, $logger,
false $this->environment
); );
$cart = Mockery::mock(\WC_Cart::class); $cart = Mockery::mock(\WC_Cart::class);
@ -340,7 +349,7 @@ class OrderProcessorTest extends TestCase
$authorizedPaymentProcessor, $authorizedPaymentProcessor,
$settings, $settings,
$logger, $logger,
false $this->environment
); );
$wcOrder $wcOrder
@ -355,7 +364,7 @@ class OrderProcessorTest extends TestCase
PayPalGateway::INTENT_META_KEY, PayPalGateway::INTENT_META_KEY,
$orderIntent $orderIntent
); );
$this->assertFalse($testee->process($wcOrder)); $this->assertFalse($testee->process($wcOrder));
$this->assertNotEmpty($testee->last_error()); $this->assertNotEmpty($testee->last_error());
} }