mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-04 08:47:23 +08:00
Add missing order meta
This commit is contained in:
parent
df68c948b9
commit
45249966e9
8 changed files with 153 additions and 31 deletions
|
@ -53,6 +53,7 @@ return array(
|
|||
$transaction_url_provider = $container->get( 'wcgateway.transaction-url-provider' );
|
||||
$subscription_helper = $container->get( 'subscription.helper' );
|
||||
$page_id = $container->get( 'wcgateway.current-ppcp-settings-page-id' );
|
||||
$environment = $container->get( 'onboarding.environment' );
|
||||
return new PayPalGateway(
|
||||
$settings_renderer,
|
||||
$order_processor,
|
||||
|
@ -64,7 +65,8 @@ return array(
|
|||
$state,
|
||||
$transaction_url_provider,
|
||||
$subscription_helper,
|
||||
$page_id
|
||||
$page_id,
|
||||
$environment
|
||||
);
|
||||
},
|
||||
'wcgateway.credit-card-gateway' => static function ( $container ): CreditCardGateway {
|
||||
|
@ -84,6 +86,7 @@ return array(
|
|||
$order_endpoint = $container->get( 'api.endpoint.order' );
|
||||
$subscription_helper = $container->get( 'subscription.helper' );
|
||||
$logger = $container->get( 'woocommerce.logger.woocommerce' );
|
||||
$environment = $container->get( 'onboarding.environment' );
|
||||
return new CreditCardGateway(
|
||||
$settings_renderer,
|
||||
$order_processor,
|
||||
|
@ -100,7 +103,8 @@ return array(
|
|||
$payer_factory,
|
||||
$order_endpoint,
|
||||
$subscription_helper,
|
||||
$logger
|
||||
$logger,
|
||||
$environment
|
||||
);
|
||||
},
|
||||
'wcgateway.disabler' => static function ( $container ): DisableGateways {
|
||||
|
@ -209,7 +213,7 @@ return array(
|
|||
$authorized_payments_processor,
|
||||
$settings,
|
||||
$logger,
|
||||
$environment->current_environment_is( Environment::SANDBOX )
|
||||
$environment
|
||||
);
|
||||
},
|
||||
'wcgateway.processor.refunds' => static function ( $container ): RefundProcessor {
|
||||
|
|
|
@ -13,6 +13,7 @@ use Psr\Log\LoggerInterface;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\State;
|
||||
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
||||
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
|
||||
|
@ -96,6 +97,13 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
|
|||
*/
|
||||
private $order_endpoint;
|
||||
|
||||
/**
|
||||
* The environment.
|
||||
*
|
||||
* @var Environment
|
||||
*/
|
||||
protected $environment;
|
||||
|
||||
/**
|
||||
* CreditCardGateway constructor.
|
||||
*
|
||||
|
@ -115,6 +123,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
|
|||
* @param OrderEndpoint $order_endpoint The order endpoint.
|
||||
* @param SubscriptionHelper $subscription_helper The subscription helper.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param Environment $environment The environment.
|
||||
*/
|
||||
public function __construct(
|
||||
SettingsRenderer $settings_renderer,
|
||||
|
@ -132,7 +141,8 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
|
|||
PayerFactory $payer_factory,
|
||||
OrderEndpoint $order_endpoint,
|
||||
SubscriptionHelper $subscription_helper,
|
||||
LoggerInterface $logger
|
||||
LoggerInterface $logger,
|
||||
Environment $environment
|
||||
) {
|
||||
|
||||
$this->id = self::ID;
|
||||
|
@ -143,6 +153,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
|
|||
$this->config = $config;
|
||||
$this->session_handler = $session_handler;
|
||||
$this->refund_processor = $refund_processor;
|
||||
$this->environment = $environment;
|
||||
|
||||
if ( $state->current_state() === State::STATE_ONBOARDED ) {
|
||||
$this->supports = array( 'refunds' );
|
||||
|
@ -424,4 +435,13 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
|
|||
private function is_enabled(): bool {
|
||||
return $this->config->has( 'dcc_enabled' ) && $this->config->get( 'dcc_enabled' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the environment.
|
||||
*
|
||||
* @return Environment
|
||||
*/
|
||||
protected function environment(): Environment {
|
||||
return $this->environment;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
|
||||
|
||||
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\State;
|
||||
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
||||
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
|
||||
|
@ -111,6 +112,13 @@ class PayPalGateway extends \WC_Payment_Gateway {
|
|||
*/
|
||||
protected $page_id;
|
||||
|
||||
/**
|
||||
* The environment.
|
||||
*
|
||||
* @var Environment
|
||||
*/
|
||||
protected $environment;
|
||||
|
||||
/**
|
||||
* 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 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 Environment $environment The environment.
|
||||
*/
|
||||
public function __construct(
|
||||
SettingsRenderer $settings_renderer,
|
||||
|
@ -137,7 +146,8 @@ class PayPalGateway extends \WC_Payment_Gateway {
|
|||
State $state,
|
||||
TransactionUrlProvider $transaction_url_provider,
|
||||
SubscriptionHelper $subscription_helper,
|
||||
string $page_id
|
||||
string $page_id,
|
||||
Environment $environment
|
||||
) {
|
||||
|
||||
$this->id = self::ID;
|
||||
|
@ -150,6 +160,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
|
|||
$this->refund_processor = $refund_processor;
|
||||
$this->transaction_url_provider = $transaction_url_provider;
|
||||
$this->page_id = $page_id;
|
||||
$this->environment = $environment;
|
||||
$this->onboarded = $state->current_state() === State::STATE_ONBOARDED;
|
||||
|
||||
if ( $this->onboarded ) {
|
||||
|
@ -430,4 +441,13 @@ class PayPalGateway extends \WC_Payment_Gateway {
|
|||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the environment.
|
||||
*
|
||||
* @return Environment
|
||||
*/
|
||||
protected function environment(): Environment {
|
||||
return $this->environment;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,11 +13,16 @@ use Exception;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderMetaTrait;
|
||||
|
||||
/**
|
||||
* Trait ProcessPaymentTrait
|
||||
*/
|
||||
trait ProcessPaymentTrait {
|
||||
|
||||
use OrderMetaTrait;
|
||||
|
||||
/**
|
||||
* Process a payment for an WooCommerce order.
|
||||
*
|
||||
|
@ -74,6 +79,8 @@ trait ProcessPaymentTrait {
|
|||
$selected_token
|
||||
);
|
||||
|
||||
$this->add_paypal_meta( $wc_order, $order, $this->environment() );
|
||||
|
||||
if ( $order->status()->is( OrderStatus::COMPLETED ) && $order->intent() === 'CAPTURE' ) {
|
||||
$wc_order->update_status(
|
||||
'processing',
|
||||
|
@ -90,7 +97,6 @@ trait ProcessPaymentTrait {
|
|||
if ( $order->status()->is( OrderStatus::COMPLETED ) && $order->intent() === 'AUTHORIZE' ) {
|
||||
$this->order_endpoint->authorize( $order );
|
||||
$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(
|
||||
'on-hold',
|
||||
__( 'Awaiting payment.', 'woocommerce-paypal-payments' )
|
||||
|
@ -246,4 +252,11 @@ trait ProcessPaymentTrait {
|
|||
|
||||
wc_add_notice( $error->getMessage(), 'error' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the environment.
|
||||
*
|
||||
* @return Environment
|
||||
*/
|
||||
abstract protected function environment(): Environment;
|
||||
}
|
||||
|
|
|
@ -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'
|
||||
);
|
||||
}
|
||||
}
|
|
@ -15,6 +15,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory;
|
||||
use WooCommerce\PayPalCommerce\Button\Helper\ThreeDSecure;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
||||
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
|
@ -25,12 +26,14 @@ use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
|||
*/
|
||||
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.
|
||||
|
@ -105,7 +108,7 @@ class OrderProcessor {
|
|||
* @param AuthorizedPaymentsProcessor $authorized_payments_processor The Authorized Payments Processor.
|
||||
* @param Settings $settings The Settings.
|
||||
* @param LoggerInterface $logger A logger service.
|
||||
* @param bool $sandbox_mode Whether sandbox mode enabled.
|
||||
* @param Environment $environment The environment.
|
||||
*/
|
||||
public function __construct(
|
||||
SessionHandler $session_handler,
|
||||
|
@ -115,7 +118,7 @@ class OrderProcessor {
|
|||
AuthorizedPaymentsProcessor $authorized_payments_processor,
|
||||
Settings $settings,
|
||||
LoggerInterface $logger,
|
||||
bool $sandbox_mode
|
||||
Environment $environment
|
||||
) {
|
||||
|
||||
$this->session_handler = $session_handler;
|
||||
|
@ -124,7 +127,7 @@ class OrderProcessor {
|
|||
$this->threed_secure = $three_d_secure;
|
||||
$this->authorized_payments_processor = $authorized_payments_processor;
|
||||
$this->settings = $settings;
|
||||
$this->sandbox_mode = $sandbox_mode;
|
||||
$this->environment = $environment;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
|
@ -140,12 +143,8 @@ class OrderProcessor {
|
|||
if ( ! $order ) {
|
||||
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() );
|
||||
$wc_order->update_meta_data(
|
||||
PayPalGateway::ORDER_PAYMENT_MODE_META_KEY,
|
||||
$this->sandbox_mode ? 'sandbox' : 'live'
|
||||
);
|
||||
|
||||
$this->add_paypal_meta( $wc_order, $order, $this->environment );
|
||||
|
||||
$error_message = null;
|
||||
if ( ! $this->order_is_approved( $order ) ) {
|
||||
|
@ -164,6 +163,7 @@ class OrderProcessor {
|
|||
}
|
||||
|
||||
$order = $this->patch_order( $wc_order, $order );
|
||||
|
||||
if ( $order->intent() === 'CAPTURE' ) {
|
||||
$order = $this->order_endpoint->capture( $order );
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
|
|||
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\State;
|
||||
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
||||
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
|
||||
|
@ -21,6 +22,13 @@ use function Brain\Monkey\Functions\when;
|
|||
|
||||
class WcGatewayTest extends TestCase
|
||||
{
|
||||
private $environment;
|
||||
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->environment = Mockery::mock(Environment::class);
|
||||
}
|
||||
|
||||
public function testProcessPaymentSuccess() {
|
||||
expect('is_admin')->andReturn(false);
|
||||
|
@ -69,7 +77,8 @@ class WcGatewayTest extends TestCase
|
|||
$state,
|
||||
$transactionUrlProvider,
|
||||
$subscriptionHelper,
|
||||
PayPalGateway::ID
|
||||
PayPalGateway::ID,
|
||||
$this->environment
|
||||
);
|
||||
|
||||
expect('wc_get_order')
|
||||
|
@ -118,7 +127,8 @@ class WcGatewayTest extends TestCase
|
|||
$state,
|
||||
$transactionUrlProvider,
|
||||
$subscriptionHelper,
|
||||
PayPalGateway::ID
|
||||
PayPalGateway::ID,
|
||||
$this->environment
|
||||
);
|
||||
|
||||
expect('wc_get_order')
|
||||
|
@ -184,7 +194,8 @@ class WcGatewayTest extends TestCase
|
|||
$state,
|
||||
$transactionUrlProvider,
|
||||
$subscriptionHelper,
|
||||
PayPalGateway::ID
|
||||
PayPalGateway::ID,
|
||||
$this->environment
|
||||
);
|
||||
|
||||
expect('wc_get_order')
|
||||
|
@ -255,7 +266,8 @@ class WcGatewayTest extends TestCase
|
|||
$state,
|
||||
$transactionUrlProvider,
|
||||
$subscriptionHelper,
|
||||
PayPalGateway::ID
|
||||
PayPalGateway::ID,
|
||||
$this->environment
|
||||
);
|
||||
|
||||
$this->assertTrue($testee->capture_authorized_payment($wcOrder));
|
||||
|
@ -310,7 +322,8 @@ class WcGatewayTest extends TestCase
|
|||
$state,
|
||||
$transactionUrlProvider,
|
||||
$subscriptionHelper,
|
||||
PayPalGateway::ID
|
||||
PayPalGateway::ID,
|
||||
$this->environment
|
||||
);
|
||||
|
||||
$this->assertTrue($testee->capture_authorized_payment($wcOrder));
|
||||
|
@ -359,7 +372,8 @@ class WcGatewayTest extends TestCase
|
|||
$state,
|
||||
$transactionUrlProvider,
|
||||
$subscriptionHelper,
|
||||
PayPalGateway::ID
|
||||
PayPalGateway::ID,
|
||||
$this->environment
|
||||
);
|
||||
|
||||
$this->assertFalse($testee->capture_authorized_payment($wcOrder));
|
||||
|
@ -399,7 +413,8 @@ class WcGatewayTest extends TestCase
|
|||
$onboardingState,
|
||||
$transactionUrlProvider,
|
||||
$subscriptionHelper,
|
||||
PayPalGateway::ID
|
||||
PayPalGateway::ID,
|
||||
$this->environment
|
||||
);
|
||||
|
||||
$this->assertSame($needSetup, $testee->needs_setup());
|
||||
|
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
namespace WooCommerce\PayPalCommerce\WcGateway\Processor;
|
||||
|
||||
|
||||
use Dhii\Container\Dictionary;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||
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\Factory\OrderFactory;
|
||||
use WooCommerce\PayPalCommerce\Button\Helper\ThreeDSecure;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
||||
use WooCommerce\PayPalCommerce\TestCase;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
|
@ -22,6 +24,13 @@ use function Brain\Monkey\Functions\when;
|
|||
|
||||
class OrderProcessorTest extends TestCase
|
||||
{
|
||||
private $environment;
|
||||
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->environment = new Environment(new Dictionary([]));
|
||||
}
|
||||
|
||||
public function testAuthorize() {
|
||||
$transactionId = 'ABC123';
|
||||
|
@ -112,7 +121,7 @@ class OrderProcessorTest extends TestCase
|
|||
$authorizedPaymentProcessor,
|
||||
$settings,
|
||||
$logger,
|
||||
false
|
||||
$this->environment
|
||||
);
|
||||
|
||||
$cart = Mockery::mock(\WC_Cart::class);
|
||||
|
@ -240,7 +249,7 @@ class OrderProcessorTest extends TestCase
|
|||
$authorizedPaymentProcessor,
|
||||
$settings,
|
||||
$logger,
|
||||
false
|
||||
$this->environment
|
||||
);
|
||||
|
||||
$cart = Mockery::mock(\WC_Cart::class);
|
||||
|
@ -340,7 +349,7 @@ class OrderProcessorTest extends TestCase
|
|||
$authorizedPaymentProcessor,
|
||||
$settings,
|
||||
$logger,
|
||||
false
|
||||
$this->environment
|
||||
);
|
||||
|
||||
$wcOrder
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue