mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +08:00
Moved code for payment process order to subscriptions module by adding a new filter
This commit is contained in:
parent
b0a5d9b3c4
commit
99ca470d3f
10 changed files with 127 additions and 34 deletions
|
@ -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 {
|
||||
$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 );
|
||||
|
||||
|
|
|
@ -253,7 +253,18 @@ class AxoGateway extends WC_Payment_Gateway {
|
|||
|
||||
$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 ) {
|
||||
return $this->handle_payment_failure( $wc_order, $exception );
|
||||
}
|
||||
|
|
|
@ -189,11 +189,22 @@ class GooglePayGateway extends WC_Payment_Gateway {
|
|||
}
|
||||
//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 {
|
||||
$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 );
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@ use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ModuleClassNameI
|
|||
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ServiceModule;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
||||
use WP_Post;
|
||||
|
@ -56,6 +57,43 @@ class PayPalSubscriptionsModule implements ServiceModule, ExtendingModule, Execu
|
|||
* {@inheritDoc}
|
||||
*/
|
||||
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(
|
||||
'save_post',
|
||||
/**
|
||||
|
|
|
@ -295,7 +295,18 @@ class CardButtonGateway extends \WC_Payment_Gateway {
|
|||
|
||||
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 );
|
||||
|
||||
|
|
|
@ -523,7 +523,18 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
|
|||
//phpcs:enable WordPress.Security.NonceVerification.Recommended
|
||||
|
||||
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 );
|
||||
|
||||
|
|
|
@ -625,30 +625,19 @@ class PayPalGateway extends \WC_Payment_Gateway {
|
|||
//phpcs:enable WordPress.Security.NonceVerification.Recommended
|
||||
|
||||
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 {
|
||||
$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 );
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ trait OrderMetaTrait {
|
|||
* @param Environment $environment The environment.
|
||||
* @param OrderTransient|null $order_transient The order transient helper.
|
||||
*/
|
||||
protected function add_paypal_meta(
|
||||
public function add_paypal_meta(
|
||||
WC_Order $wc_order,
|
||||
Order $order,
|
||||
Environment $environment,
|
||||
|
|
|
@ -28,7 +28,7 @@ trait TransactionIdHandlingTrait {
|
|||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function update_transaction_id(
|
||||
public function update_transaction_id(
|
||||
string $transaction_id,
|
||||
WC_Order $wc_order,
|
||||
LoggerInterface $logger = null
|
||||
|
@ -67,7 +67,7 @@ trait TransactionIdHandlingTrait {
|
|||
*
|
||||
* @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;
|
||||
if ( ! $purchase_unit ) {
|
||||
return null;
|
||||
|
|
|
@ -230,7 +230,18 @@ class CheckoutOrderApproved implements RequestHandler {
|
|||
}
|
||||
|
||||
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 ) {
|
||||
return $this->failure_response(
|
||||
sprintf(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue