Moved code for payment process order to subscriptions module by adding a new filter

This commit is contained in:
Daniel Hüsken 2025-01-31 08:25:03 +01:00
parent b0a5d9b3c4
commit 99ca470d3f
No known key found for this signature in database
GPG key ID: 9F732DA37FA709E8
10 changed files with 127 additions and 34 deletions

View file

@ -180,11 +180,22 @@ class ApplePayGateway extends WC_Payment_Gateway {
); );
} }
do_action( 'woocommerce_paypal_payments_before_process_order', $wc_order ); do_action_deprecated('woocommerce_paypal_payments_before_process_order', [ $wc_order ], '2.9.7', 'woocommerce_paypal_payments_before_order_process', __( 'Usage of this action is deprecated. Please use the filter woocommerce_paypal_payments_before_order_process instead. ', 'woocommerce-paypal-payments' ) );
try { try {
try { try {
$this->order_processor->process( $wc_order ); /**
* This filter controls if the method 'precess()' from OrderProcessor will be called.
* So you can implement your own for example on subscriptions
*
* - true bool controls execution of 'OrderProcessor::precess()'
* @var $this \WC_Payment_Gateway
* @var $wc_order \WC_Order
*/
$process = apply_filters( 'woocommerce_paypal_payments_before_order_process', true, $this, $wc_order );
if ( $process ) {
$this->order_processor->process( $wc_order );
}
do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order ); do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order );

View file

@ -253,7 +253,18 @@ class AxoGateway extends WC_Payment_Gateway {
$order = $this->create_paypal_order( $wc_order, $token ); $order = $this->create_paypal_order( $wc_order, $token );
$this->order_processor->process_captured_and_authorized( $wc_order, $order ); /**
* This filter controls if the method 'precess()' from OrderProcessor will be called.
* So you can implement your own for example on subscriptions
*
* - true bool controls execution of 'OrderProcessor::precess()'
* @var $this \WC_Payment_Gateway
* @var $wc_order \WC_Order
*/
$process = apply_filters( 'woocommerce_paypal_payments_before_order_process', true, $this, $wc_order );
if ( $process ) {
$this->order_processor->process_captured_and_authorized( $wc_order, $order );
}
} catch ( Exception $exception ) { } catch ( Exception $exception ) {
return $this->handle_payment_failure( $wc_order, $exception ); return $this->handle_payment_failure( $wc_order, $exception );
} }

View file

@ -189,11 +189,22 @@ class GooglePayGateway extends WC_Payment_Gateway {
} }
//phpcs:enable WordPress.Security.NonceVerification.Recommended //phpcs:enable WordPress.Security.NonceVerification.Recommended
do_action( 'woocommerce_paypal_payments_before_process_order', $wc_order ); do_action_deprecated('woocommerce_paypal_payments_before_process_order', [ $wc_order ], '2.9.7', 'woocommerce_paypal_payments_before_order_process', __( 'Usage of this action is deprecated. Please use the filter woocommerce_paypal_payments_before_order_process instead. ', 'woocommerce-paypal-payments' ) );
try { try {
try { try {
$this->order_processor->process( $wc_order ); /**
* This filter controls if the method 'precess()' from OrderProcessor will be called.
* So you can implement your own for example on subscriptions
*
* - true bool controls execution of 'OrderProcessor::precess()'
* @var $this \WC_Payment_Gateway
* @var $wc_order \WC_Order
*/
$process = apply_filters( 'woocommerce_paypal_payments_before_order_process', true, $this, $wc_order );
if ( $process ) {
$this->order_processor->process( $wc_order );
}
do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order ); do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order );

View file

@ -28,6 +28,7 @@ use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ModuleClassNameI
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ServiceModule; use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ServiceModule;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException; use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper; use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
use WP_Post; use WP_Post;
@ -56,6 +57,43 @@ class PayPalSubscriptionsModule implements ServiceModule, ExtendingModule, Execu
* {@inheritDoc} * {@inheritDoc}
*/ */
public function run( ContainerInterface $c ): bool { public function run( ContainerInterface $c ): bool {
add_filter(
'woocommerce_paypal_payments_before_order_process',
function ( bool $process, \WC_Payment_Gateway $gateway, \WC_Order $wc_order ) use ( $c ) {
if ( ! $gateway instanceof PayPalGateway || $gateway::ID !== 'ppcp-gateway' ) {
return $process;
}
$paypal_subscription_id = \WC()->session->get( 'ppcp_subscription_id' ) ?? '';
if ( ! $paypal_subscription_id ) {
return $process;
}
$order = $c->get( 'session.handler' )->order();
$gateway->add_paypal_meta( $wc_order, $order, $c->get( 'onboarding.environment' ) );
$subscriptions = function_exists( 'wcs_get_subscriptions_for_order' ) ? wcs_get_subscriptions_for_order( $wc_order ) : array();
foreach ( $subscriptions as $subscription ) {
$subscription->update_meta_data( 'ppcp_subscription', $paypal_subscription_id );
$subscription->save();
$subscription->add_order_note( "PayPal subscription {$paypal_subscription_id} added." );
}
$transaction_id = $gateway->get_paypal_order_transaction_id( $order );
if ( $transaction_id ) {
$gateway->update_transaction_id( $transaction_id, $wc_order, $c->get( 'woocommerce.logger.woocommerce' ) );
}
$wc_order->payment_complete();
return false;
},
10,
3
);
add_action( add_action(
'save_post', 'save_post',
/** /**

View file

@ -295,7 +295,18 @@ class CardButtonGateway extends \WC_Payment_Gateway {
try { try {
try { try {
$this->order_processor->process( $wc_order ); /**
* This filter controls if the method 'precess()' from OrderProcessor will be called.
* So you can implement your own for example on subscriptions
*
* - true bool controls execution of 'OrderProcessor::precess()'
* @var $this \WC_Payment_Gateway
* @var $wc_order \WC_Order
*/
$process = apply_filters( 'woocommerce_paypal_payments_before_order_process', true, $this, $wc_order );
if ( $process ) {
$this->order_processor->process( $wc_order );
}
do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order ); do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order );

View file

@ -523,7 +523,18 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
//phpcs:enable WordPress.Security.NonceVerification.Recommended //phpcs:enable WordPress.Security.NonceVerification.Recommended
try { try {
$this->order_processor->process( $wc_order ); /**
* This filter controls if the method 'precess()' from OrderProcessor will be called.
* So you can implement your own for example on subscriptions
*
* - true bool controls execution of 'OrderProcessor::precess()'
* @var $this \WC_Payment_Gateway
* @var $wc_order \WC_Order
*/
$process = apply_filters( 'woocommerce_paypal_payments_before_order_process', true, $this, $wc_order );
if ( $process ) {
$this->order_processor->process( $wc_order );
}
do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order ); do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order );

View file

@ -625,30 +625,19 @@ class PayPalGateway extends \WC_Payment_Gateway {
//phpcs:enable WordPress.Security.NonceVerification.Recommended //phpcs:enable WordPress.Security.NonceVerification.Recommended
try { try {
$paypal_subscription_id = WC()->session->get( 'ppcp_subscription_id' ) ?? '';
if ( $paypal_subscription_id ) {
$order = $this->session_handler->order();
$this->add_paypal_meta( $wc_order, $order, $this->environment );
$subscriptions = function_exists( 'wcs_get_subscriptions_for_order' ) ? wcs_get_subscriptions_for_order( $order_id ) : array();
foreach ( $subscriptions as $subscription ) {
$subscription->update_meta_data( 'ppcp_subscription', $paypal_subscription_id );
$subscription->save();
$subscription->add_order_note( "PayPal subscription {$paypal_subscription_id} added." );
}
$transaction_id = $this->get_paypal_order_transaction_id( $order );
if ( $transaction_id ) {
$this->update_transaction_id( $transaction_id, $wc_order );
}
$wc_order->payment_complete();
return $this->handle_payment_success( $wc_order );
}
try { try {
$this->order_processor->process( $wc_order ); /**
* This filter controls if the method 'precess()' from OrderProcessor will be called.
* So you can implement your own for example on subscriptions
*
* - true bool controls execution of 'OrderProcessor::precess()'
* @var $this \WC_Payment_Gateway
* @var $wc_order \WC_Order
*/
$process = apply_filters( 'woocommerce_paypal_payments_before_order_process', true, $this, $wc_order );
if ( $process ) {
$this->order_processor->process( $wc_order );
}
do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order ); do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order );

View file

@ -28,7 +28,7 @@ trait OrderMetaTrait {
* @param Environment $environment The environment. * @param Environment $environment The environment.
* @param OrderTransient|null $order_transient The order transient helper. * @param OrderTransient|null $order_transient The order transient helper.
*/ */
protected function add_paypal_meta( public function add_paypal_meta(
WC_Order $wc_order, WC_Order $wc_order,
Order $order, Order $order,
Environment $environment, Environment $environment,

View file

@ -28,7 +28,7 @@ trait TransactionIdHandlingTrait {
* *
* @return bool * @return bool
*/ */
protected function update_transaction_id( public function update_transaction_id(
string $transaction_id, string $transaction_id,
WC_Order $wc_order, WC_Order $wc_order,
LoggerInterface $logger = null LoggerInterface $logger = null
@ -67,7 +67,7 @@ trait TransactionIdHandlingTrait {
* *
* @return string|null * @return string|null
*/ */
protected function get_paypal_order_transaction_id( Order $order ): ?string { public function get_paypal_order_transaction_id( Order $order ): ?string {
$purchase_unit = $order->purchase_units()[0] ?? null; $purchase_unit = $order->purchase_units()[0] ?? null;
if ( ! $purchase_unit ) { if ( ! $purchase_unit ) {
return null; return null;

View file

@ -230,7 +230,18 @@ class CheckoutOrderApproved implements RequestHandler {
} }
try { try {
$this->order_processor->process( $wc_order ); /**
* This filter controls if the method 'precess()' from OrderProcessor will be called.
* So you can implement your own for example on subscriptions
*
* - true bool controls execution of 'OrderProcessor::precess()'
* @var $this \WC_Payment_Gateway
* @var $wc_order \WC_Order
*/
$process = apply_filters( 'woocommerce_paypal_payments_before_order_process', true, $this, $wc_order );
if ( $process ) {
$this->order_processor->process( $wc_order );
}
} catch ( RuntimeException $exception ) { } catch ( RuntimeException $exception ) {
return $this->failure_response( return $this->failure_response(
sprintf( sprintf(