Extract logic from gateway to modules

This commit is contained in:
Emili Castells Guasch 2024-07-16 17:51:02 +02:00
parent 74f319dd82
commit 335f680344
4 changed files with 33 additions and 35 deletions

View file

@ -941,7 +941,6 @@ return array(
'googlepay.wc-gateway' => static function ( ContainerInterface $container ): GooglePayGateway { 'googlepay.wc-gateway' => static function ( ContainerInterface $container ): GooglePayGateway {
return new GooglePayGateway( return new GooglePayGateway(
$container->get( 'wcgateway.order-processor' ), $container->get( 'wcgateway.order-processor' ),
$container->get( 'wc-subscriptions.helper' ),
$container->get( 'api.factory.paypal-checkout-url' ), $container->get( 'api.factory.paypal-checkout-url' ),
$container->get( 'wcgateway.processor.refunds' ), $container->get( 'wcgateway.processor.refunds' ),
$container->get( 'wcgateway.transaction-url-provider' ), $container->get( 'wcgateway.transaction-url-provider' ),

View file

@ -21,7 +21,6 @@ use WooCommerce\PayPalCommerce\WcGateway\Gateway\ProcessPaymentTrait;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\TransactionUrlProvider; use WooCommerce\PayPalCommerce\WcGateway\Gateway\TransactionUrlProvider;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor; use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor; use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
/** /**
* Class GooglePayGateway * Class GooglePayGateway
@ -38,13 +37,6 @@ class GooglePayGateway extends WC_Payment_Gateway {
*/ */
protected $order_processor; protected $order_processor;
/**
* The subscription helper.
*
* @var SubscriptionHelper
*/
protected $subscription_helper;
/** /**
* The function return the PayPal checkout URL for the given order ID. * The function return the PayPal checkout URL for the given order ID.
* *
@ -77,7 +69,6 @@ class GooglePayGateway extends WC_Payment_Gateway {
* GooglePayGateway constructor. * GooglePayGateway constructor.
* *
* @param OrderProcessor $order_processor The Order Processor. * @param OrderProcessor $order_processor The Order Processor.
* @param SubscriptionHelper $subscription_helper The subscription helper.
* @param callable(string):string $paypal_checkout_url_factory The function return the PayPal checkout URL for the given order ID. * @param callable(string):string $paypal_checkout_url_factory The function return the PayPal checkout URL for the given order ID.
* @param RefundProcessor $refund_processor The Refund Processor. * @param RefundProcessor $refund_processor The Refund Processor.
* @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.
@ -85,7 +76,6 @@ class GooglePayGateway extends WC_Payment_Gateway {
*/ */
public function __construct( public function __construct(
OrderProcessor $order_processor, OrderProcessor $order_processor,
SubscriptionHelper $subscription_helper,
callable $paypal_checkout_url_factory, callable $paypal_checkout_url_factory,
RefundProcessor $refund_processor, RefundProcessor $refund_processor,
TransactionUrlProvider $transaction_url_provider, TransactionUrlProvider $transaction_url_provider,
@ -102,7 +92,6 @@ class GooglePayGateway extends WC_Payment_Gateway {
$this->init_form_fields(); $this->init_form_fields();
$this->init_settings(); $this->init_settings();
$this->order_processor = $order_processor; $this->order_processor = $order_processor;
$this->subscription_helper = $subscription_helper;
$this->paypal_checkout_url_factory = $paypal_checkout_url_factory; $this->paypal_checkout_url_factory = $paypal_checkout_url_factory;
$this->refund_processor = $refund_processor; $this->refund_processor = $refund_processor;
$this->transaction_url_provider = $transaction_url_provider; $this->transaction_url_provider = $transaction_url_provider;
@ -155,28 +144,7 @@ class GooglePayGateway extends WC_Payment_Gateway {
); );
} }
/** do_action( 'woocommerce_paypal_payments_before_process_order', $wc_order );
* If customer has chosen change Subscription payment.
*/
if ( $this->subscription_helper->has_subscription( $order_id ) && $this->subscription_helper->is_subscription_change_payment() ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$saved_paypal_payment = wc_clean( wp_unslash( $_POST['saved_paypal_payment'] ?? '' ) );
if ( $saved_paypal_payment ) {
$wc_order->update_meta_data( 'payment_token_id', $saved_paypal_payment );
$wc_order->save();
return $this->handle_payment_success( $wc_order );
}
}
/**
* If the WC_Order is paid through the approved webhook.
*/
//phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( isset( $_REQUEST['ppcp-resume-order'] ) && $wc_order->has_status( 'processing' ) ) {
return $this->handle_payment_success( $wc_order );
}
//phpcs:enable WordPress.Security.NonceVerification.Recommended
try { try {
try { try {

View file

@ -23,6 +23,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\ProcessPaymentTrait;
use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait; use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcSubscriptions\Endpoint\SubscriptionChangePaymentMethod; use WooCommerce\PayPalCommerce\WcSubscriptions\Endpoint\SubscriptionChangePaymentMethod;
@ -33,7 +34,7 @@ use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
*/ */
class WcSubscriptionsModule implements ModuleInterface { class WcSubscriptionsModule implements ModuleInterface {
use TransactionIdHandlingTrait; use TransactionIdHandlingTrait, ProcessPaymentTrait;
/** /**
* {@inheritDoc} * {@inheritDoc}
@ -255,6 +256,23 @@ class WcSubscriptionsModule implements ModuleInterface {
10, 10,
3 3
); );
add_action( 'woocommerce_paypal_payments_before_process_order', function(WC_Order $wc_order) use ( $c ) {
$subscriptions_helper = $c->get( 'wc-subscriptions.helper' );
assert( $subscriptions_helper instanceof SubscriptionHelper );
// If the customer has chosen to change the subscription payment.
if ( $subscriptions_helper->has_subscription( $wc_order->get_id() ) && $subscriptions_helper->is_subscription_change_payment() ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$saved_paypal_payment = wc_clean( wp_unslash( $_POST['saved_paypal_payment'] ?? '' ) );
if ( $saved_paypal_payment ) {
$wc_order->update_meta_data( 'payment_token_id', $saved_paypal_payment );
$wc_order->save();
return $this->handle_payment_success( $wc_order );
}
}
});
} }
/** /**

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Webhooks; namespace WooCommerce\PayPalCommerce\Webhooks;
use WC_Order;
use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider; use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface; use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
@ -16,6 +17,7 @@ use Exception;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface; use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\ProcessPaymentTrait;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\Webhooks\Endpoint\ResubscribeEndpoint; use WooCommerce\PayPalCommerce\Webhooks\Endpoint\ResubscribeEndpoint;
use WooCommerce\PayPalCommerce\Webhooks\Endpoint\SimulateEndpoint; use WooCommerce\PayPalCommerce\Webhooks\Endpoint\SimulateEndpoint;
@ -27,6 +29,8 @@ use WooCommerce\PayPalCommerce\Webhooks\Status\Assets\WebhooksStatusPageAssets;
*/ */
class WebhookModule implements ModuleInterface { class WebhookModule implements ModuleInterface {
use ProcessPaymentTrait;
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -158,6 +162,15 @@ class WebhookModule implements ModuleInterface {
); );
} }
); );
// If the WC_Order is paid through the approved webhook.
add_action( 'woocommerce_paypal_payments_before_process_order', function(WC_Order $wc_order) {
//phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( isset( $_REQUEST['ppcp-resume-order'] ) && $wc_order->has_status( 'processing' ) ) {
return $this->handle_payment_success( $wc_order );
}
//phpcs:enable
});
} }
/** /**