Move payment checker scheduler to module

This commit is contained in:
Emili Castells Guasch 2023-09-19 15:16:22 +02:00
parent 7d17e4a892
commit 5f3dc3a647
16 changed files with 202 additions and 224 deletions

View file

@ -1,14 +0,0 @@
<?php
/**
* The SavedPaymentChecker module services.
*
* @package WooCommerce\PayPalCommerce\SavedPaymentChecker
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\SavedPaymentChecker;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
return array();

View file

@ -1,55 +0,0 @@
<?php
/**
* The SavedPaymentChecker module.
*
* @package WooCommerce\PayPalCommerce\SavedPaymentChecker
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\SavedPaymentChecker;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
/**
* Class SavedPaymentCheckerModule
*/
class SavedPaymentCheckerModule implements ModuleInterface {
/**
* {@inheritDoc}
*/
public function setup(): ServiceProviderInterface {
return new ServiceProvider(
require __DIR__ . '/../services.php',
require __DIR__ . '/../extensions.php'
);
}
/**
* {@inheritDoc}
*/
public function run( ContainerInterface $c ): void {
/**
* Set authorize intent for vaulted subscriptions, so we can void if payment not saved.
*/
add_filter(
'woocommerce_paypal_payments_saved_payment_subscription_intent',
function( string $intent ) use ( $c ) {
$subscription_helper = $c->get( 'subscription.helper' );
assert( $subscription_helper instanceof SubscriptionHelper );
if ( $subscription_helper->cart_contains_subscription() || $subscription_helper->current_product_is_subscription() ) {
return 'AUTHORIZE';
}
return $intent;
}
);
}
}

View file

@ -1,7 +1,7 @@
{
"name": "woocommerce/ppcp-saved-payment-checked",
"type": "dhii-mod",
"description": "Saved payment checker module for PPCP",
"description": "Saved payments checker module",
"license": "GPL-2.0",
"require": {
"php": "^7.2 | ^8.0",

View file

@ -9,6 +9,4 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\SavedPaymentChecker;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
return array();

View file

@ -0,0 +1,26 @@
<?php
/**
* The SavedPaymentChecker module services.
*
* @package WooCommerce\PayPalCommerce\SavedPaymentChecker
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\SavedPaymentChecker;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
return array(
'saved-payment-checker.payment-token-checker' => function( ContainerInterface $container ) : PaymentTokenChecker {
return new PaymentTokenChecker(
$container->get( 'vaulting.repository.payment-token' ),
$container->get( 'api.repository.order' ),
$container->get( 'wcgateway.settings' ),
$container->get( 'wcgateway.processor.authorized-payments' ),
$container->get( 'api.endpoint.payments' ),
$container->get( 'api.endpoint.payment-token' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
);

View file

@ -7,17 +7,17 @@
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Vaulting;
namespace WooCommerce\PayPalCommerce\SavedPaymentChecker;
use Exception;
use Psr\Log\LoggerInterface;
use RuntimeException;
use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentTokenEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Repository\OrderRepository;
use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
@ -203,6 +203,32 @@ class PaymentTokenChecker {
}
}
/**
* Schedules the vaulted payment check.
*
* @param int $wc_order_id The WC order ID.
* @param int $customer_id The customer ID.
*/
public function schedule_saved_payment_check( int $wc_order_id, int $customer_id ): void {
$timestamp = 3 * MINUTE_IN_SECONDS;
if (
$this->settings->has( 'subscription_behavior_when_vault_fails' )
&& $this->settings->get( 'subscription_behavior_when_vault_fails' ) === 'capture_auth'
) {
$timestamp = 0;
}
as_schedule_single_action(
time() + $timestamp,
'woocommerce_paypal_payments_check_saved_payment',
array(
'order_id' => $wc_order_id,
'customer_id' => $customer_id,
'intent' => $this->settings->has( 'intent' ) ? $this->settings->get( 'intent' ) : '',
)
);
}
/**
* Captures authorized payments for the given WC order.
*

View file

@ -0,0 +1,142 @@
<?php
/**
* The SavedPaymentChecker module.
*
* @package WooCommerce\PayPalCommerce\SavedPaymentChecker
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\SavedPaymentChecker;
use Psr\Log\LoggerInterface;
use WC_Order;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
/**
* Class SavedPaymentCheckerModule
*/
class SavedPaymentCheckerModule implements ModuleInterface {
/**
* {@inheritDoc}
*/
public function setup(): ServiceProviderInterface {
return new ServiceProvider(
require __DIR__ . '/../services.php',
require __DIR__ . '/../extensions.php'
);
}
/**
* {@inheritDoc}
*/
public function run( ContainerInterface $c ): void {
/**
* Set authorize intent for vaulted subscriptions, so we can void if payment not saved.
*/
add_filter(
'woocommerce_paypal_payments_saved_payment_subscription_intent',
function( string $intent ) use ( $c ) {
$subscription_helper = $c->get( 'subscription.helper' );
assert( $subscription_helper instanceof SubscriptionHelper );
if ( $subscription_helper->cart_contains_subscription() || $subscription_helper->current_product_is_subscription() ) {
return 'AUTHORIZE';
}
return $intent;
}
);
/**
* Schedules saved payment checker before payment success handler.
*/
add_action(
'woocommerce_paypal_payments_before_handle_payment_success',
function( WC_Order $wc_order ) use ( $c ) {
$subscription_helper = $c->get( 'subscription.helper' );
assert( $subscription_helper instanceof SubscriptionHelper );
if ( $subscription_helper->has_subscription( $wc_order->get_id() ) ) {
$payment_token_checker = $c->get( 'saved-payment-checker.payment-token-checker' );
assert( $payment_token_checker instanceof PaymentTokenChecker );
$payment_token_checker->schedule_saved_payment_check( $wc_order->get_id(), $wc_order->get_customer_id() );
}
}
);
/**
* Triggers a payment token check for the given order and customer id.
*/
add_action(
'woocommerce_paypal_payments_check_saved_payment',
function ( int $order_id, int $customer_id, string $intent ) use ( $c ) {
$payment_token_checker = $c->get( 'vaulting.payment-token-checker' );
assert( $payment_token_checker instanceof PaymentTokenChecker );
$payment_token_checker->check_and_update( $order_id, $customer_id, $intent );
},
10,
3
);
/**
* Adds email content for vaulting failure.
*/
add_action(
'woocommerce_email_before_order_table',
function( WC_Order $order ) use ( $c ) {
$subscription_helper = $c->get( 'subscription.helper' );
assert( $subscription_helper instanceof SubscriptionHelper );
$logger = $c->get( 'woocommerce.logger.woocommerce' );
assert( $logger instanceof LoggerInterface );
$vault_failed = $order->get_meta( PaymentTokenChecker::VAULTING_FAILED_META_KEY );
if ( $subscription_helper->has_subscription( $order->get_id() ) && ! empty( $vault_failed ) ) {
$logger->info( "Adding vaulting failure info to email for order #{$order->get_id()}." );
if ( $vault_failed === 'void_auth' ) {
echo wp_kses_post( '<p>' . __( 'The subscription payment failed because the payment method could not be saved. Please try again with a different payment method.', 'woocommerce-paypal-payments' ) . '</p>' );
}
if ( $vault_failed === 'capture_auth' ) {
echo wp_kses_post( '<p>' . __( 'The subscription has been activated, but the payment method could not be saved. Please contact the merchant to save a payment method for automatic subscription renewal payments.', 'woocommerce-paypal-payments' ) . '</p>' );
}
}
}
);
/**
* Adds email content for vaulting changing manual renewal order.
*/
add_action(
'woocommerce_email_after_order_table',
function( WC_Order $order ) use ( $c ) {
$subscription_helper = $c->get( 'subscription.helper' );
assert( $subscription_helper instanceof SubscriptionHelper );
$logger = $c->get( 'woocommerce.logger.woocommerce' );
assert( $logger instanceof LoggerInterface );
$vault_failed = $order->get_meta( PaymentTokenChecker::VAULTING_FAILED_META_KEY );
if ( $subscription_helper->has_subscription( $order->get_id() ) && ! empty( $vault_failed ) ) {
$logger->info( "Changing subscription auto-renewal status for order #{$order->get_id()}." );
if ( $vault_failed === 'capture_auth' ) {
$subscriptions = function_exists( 'wcs_get_subscriptions_for_order' ) ? wcs_get_subscriptions_for_order( $order->get_id() ) : array();
foreach ( $subscriptions as $subscription ) {
$subscription->set_requires_manual_renewal( true );
$subscription->save();
}
}
}
}
);
}
}

View file

@ -23,17 +23,6 @@ return array(
$endpoint = $container->get( 'api.endpoint.payment-token' );
return new PaymentTokenRepository( $factory, $endpoint );
},
'vaulting.payment-token-checker' => function( ContainerInterface $container ) : PaymentTokenChecker {
return new PaymentTokenChecker(
$container->get( 'vaulting.repository.payment-token' ),
$container->get( 'api.repository.order' ),
$container->get( 'wcgateway.settings' ),
$container->get( 'wcgateway.processor.authorized-payments' ),
$container->get( 'api.endpoint.payments' ),
$container->get( 'api.endpoint.payment-token' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
'vaulting.customer-approval-listener' => function( ContainerInterface $container ) : CustomerApprovalListener {
return new CustomerApprovalListener(
$container->get( 'api.endpoint.payment-token' ),

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Vaulting;
use Psr\Log\LoggerInterface;
use RuntimeException;
use WC_Payment_Token;
use WC_Payment_Tokens;
@ -16,9 +17,6 @@ use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use WC_Order;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
@ -68,19 +66,6 @@ class VaultingModule implements ModuleInterface {
}
);
add_action(
'woocommerce_paypal_payments_check_saved_payment',
function ( int $order_id, int $customer_id, string $intent ) use ( $container ) {
$payment_token_checker = $container->get( 'vaulting.payment-token-checker' );
assert( $payment_token_checker instanceof PaymentTokenChecker );
$payment_token_checker->check_and_update( $order_id, $customer_id, $intent );
},
10,
3
);
$this->filterFailedVaultingEmailsForSubscriptionOrders( $container );
add_filter(
'woocommerce_payment_token_class',
/**
@ -271,95 +256,8 @@ class VaultingModule implements ModuleInterface {
}
}
/**
* Filters the emails when vaulting is failed for subscription orders.
*
* @param ContainerInterface $container A services container instance.
* @throws NotFoundException When service could not be found.
*/
protected function filterFailedVaultingEmailsForSubscriptionOrders( ContainerInterface $container ):void {
add_action(
'woocommerce_email_before_order_table',
function( WC_Order $order ) use ( $container ) {
/**
* The SubscriptionHelper.
*
* @var SubscriptionHelper $subscription_helper
*/
$subscription_helper = $container->get( 'subscription.helper' );
/**
* The logger.
*
* @var LoggerInterface $logger
*/
$logger = $container->get( 'woocommerce.logger.woocommerce' );
$vault_failed = $order->get_meta( PaymentTokenChecker::VAULTING_FAILED_META_KEY );
if ( $subscription_helper->has_subscription( $order->get_id() ) && ! empty( $vault_failed ) ) {
$logger->info( "Adding vaulting failure info to email for order #{$order->get_id()}." );
if ( $vault_failed === 'void_auth' ) {
echo wp_kses_post( '<p>' . __( 'The subscription payment failed because the payment method could not be saved. Please try again with a different payment method.', 'woocommerce-paypal-payments' ) . '</p>' );
}
if ( $vault_failed === 'capture_auth' ) {
echo wp_kses_post( '<p>' . __( 'The subscription has been activated, but the payment method could not be saved. Please contact the merchant to save a payment method for automatic subscription renewal payments.', 'woocommerce-paypal-payments' ) . '</p>' );
}
}
}
);
add_action(
'woocommerce_email_after_order_table',
function( WC_Order $order ) use ( $container ) {
/**
* The SubscriptionHelper.
*
* @var SubscriptionHelper $subscription_helper
*/
$subscription_helper = $container->get( 'subscription.helper' );
/**
* The logger.
*
* @var LoggerInterface $logger
*/
$logger = $container->get( 'woocommerce.logger.woocommerce' );
$vault_failed = $order->get_meta( PaymentTokenChecker::VAULTING_FAILED_META_KEY );
if ( $subscription_helper->has_subscription( $order->get_id() ) && ! empty( $vault_failed ) ) {
$logger->info( "Changing subscription auto-renewal status for order #{$order->get_id()}." );
if ( $vault_failed === 'capture_auth' ) {
$subscriptions = function_exists( 'wcs_get_subscriptions_for_order' ) ? wcs_get_subscriptions_for_order( $order->get_id() ) : array();
foreach ( $subscriptions as $subscription ) {
$subscription->set_requires_manual_renewal( true );
$subscription->save();
}
}
}
}
);
}
/**
* {@inheritDoc}
*/
public function getKey() { }
/**
* Check if is payments page.
*
* @return bool Whethen page is payments or not.
*/
private function is_payments_page(): bool {
global $wp;
$request = explode( '/', wp_parse_url( $wp->request, PHP_URL_PATH ) );
if ( end( $request ) === 'ppcp-paypal-payment-tokens' ) {
return true;
}
return false;
}
}

View file

@ -299,9 +299,7 @@ class CardButtonGateway extends \WC_Payment_Gateway {
);
}
if ( $this->subscription_helper->has_subscription( $order_id ) ) {
$this->schedule_saved_payment_check( $order_id, $wc_order->get_customer_id() );
}
do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order );
return $this->handle_payment_success( $wc_order );
} catch ( PayPalApiException $error ) {

View file

@ -396,9 +396,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
);
}
if ( $this->subscription_helper->has_subscription( $order_id ) ) {
$this->schedule_saved_payment_check( $order_id, $wc_order->get_customer_id() );
}
do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order );
return $this->handle_payment_success( $wc_order );
} catch ( PayPalApiException $error ) {

View file

@ -556,9 +556,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
);
}
if ( $this->subscription_helper->has_subscription( $order_id ) ) {
$this->schedule_saved_payment_check( $order_id, $wc_order->get_customer_id() );
}
do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order );
return $this->handle_payment_success( $wc_order );
} catch ( PayPalApiException $error ) {

View file

@ -33,32 +33,6 @@ trait ProcessPaymentTrait {
return false;
}
/**
* Scheduled the vaulted payment check.
*
* @param int $wc_order_id The WC order ID.
* @param int $customer_id The customer ID.
*/
protected function schedule_saved_payment_check( int $wc_order_id, int $customer_id ): void {
$timestamp = 3 * MINUTE_IN_SECONDS;
if (
$this->config->has( 'subscription_behavior_when_vault_fails' )
&& $this->config->get( 'subscription_behavior_when_vault_fails' ) === 'capture_auth'
) {
$timestamp = 0;
}
as_schedule_single_action(
time() + $timestamp,
'woocommerce_paypal_payments_check_saved_payment',
array(
'order_id' => $wc_order_id,
'customer_id' => $customer_id,
'intent' => $this->config->has( 'intent' ) ? $this->config->get( 'intent' ) : '',
)
);
}
/**
* Handles the payment failure.
*