Refactor payment checker (WIP)

This commit is contained in:
dinamiko 2022-03-01 15:12:54 +01:00
parent 5ffc792ce0
commit b5f74f1207
3 changed files with 183 additions and 120 deletions

View file

@ -41,4 +41,14 @@ return array(
$container->get( 'woocommerce.logger.woocommerce' )
);
},
'vaulting.payment-token-checker' => function(ContainerInterface $container) : PaymentTokenChecker {
return new PaymentTokenChecker(
$container->get('vaulting.repository.payment-token'),
$container->get( 'wcgateway.settings' ),
$container->get( 'wcgateway.processor.authorized-payments' ),
$container->get( 'api.endpoint.order' ),
$container->get( 'api.endpoint.payments' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
);

View file

@ -0,0 +1,171 @@
<?php
/**
* Check if payment token is saved and updates order accordingly.
*
* @package WooCommerce\PayPalCommerce\Vaulting
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Vaulting;
use Exception;
use Psr\Log\LoggerInterface;
use RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
class PaymentTokenChecker {
/**
* @var PaymentTokenRepository
*/
protected $payment_token_repository;
/**
* @var Settings
*/
protected $settings;
/**
* @var AuthorizedPaymentsProcessor
*/
protected $authorized_payments_processor;
/**
* @var OrderEndpoint
*/
protected $order_endpoint;
/**
* @var PaymentsEndpoint
*/
protected $payments_endpoint;
/**
* @var LoggerInterface
*/
protected $logger;
public function __construct(
PaymentTokenRepository $payment_token_repository,
Settings $settings,
AuthorizedPaymentsProcessor $authorized_payments_processor,
OrderEndpoint $order_endpoint,
PaymentsEndpoint $payments_endpoint,
LoggerInterface $logger
)
{
$this->payment_token_repository = $payment_token_repository;
$this->settings = $settings;
$this->authorized_payments_processor = $authorized_payments_processor;
$this->order_endpoint = $order_endpoint;
$this->logger = $logger;
$this->payments_endpoint = $payments_endpoint;
}
public function checkAndUpdate($order_id, $customer_id) {
$tokens = $this->payment_token_repository->all_for_user_id( $customer_id );
if ( $tokens ) {
$this->capture_authorized_payment(
$order_id,
$customer_id
);
}
$this->logger->error( "Payment for subscription parent order #{$order_id} was not saved on PayPal." );
$wc_order = wc_get_order( $order_id );
$order = $this->getOrder( $wc_order );
try {
$this->void_authorizations( $order);
} catch ( RuntimeException $exception ) {
$this->logger->warning($exception->getMessage());
}
$this->updateFailedStatus( $wc_order, $order_id );
}
/**
* @param $order_id
* @param $customer_id
* @throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException
*/
private function capture_authorized_payment($order_id, $customer_id): void {
if ( $this->settings->has( 'intent' ) && strtoupper( (string) $this->settings->get( 'intent' ) ) === 'CAPTURE' ) {
$wc_order = wc_get_order( $order_id );
$this->authorized_payments_processor->capture_authorized_payment( $wc_order );
$this->logger->info( "Order: #{$order_id} for user: {$customer_id} captured successfully." );
}
}
/**
* @param $order
* @param $payments_endpoint
* @throws RuntimeException
*/
protected function void_authorizations( $order ): void {
$purchase_units = $order->purchase_units();
if ( ! $purchase_units ) {
throw new RuntimeException( 'No purchase units.' );
}
$payments = $purchase_units[0]->payments();
if ( ! $payments ) {
throw new RuntimeException( 'No payments.' );
}
$voidable_authorizations = array_filter(
$payments->authorizations(),
function ( Authorization $authorization ): bool {
return $authorization->is_voidable();
}
);
if ( ! $voidable_authorizations ) {
throw new RuntimeException( 'No voidable authorizations.' );
}
foreach ( $voidable_authorizations as $authorization ) {
$this->payments_endpoint->void( $authorization );
}
}
/**
* @param $wc_order
* @return mixed
*/
protected function getOrder( $wc_order ) {
$paypal_order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY );
if ( ! $paypal_order_id ) {
throw new RuntimeException( 'PayPal order ID not found in meta.' );
}
return $this->order_endpoint->order( $paypal_order_id );
}
/**
* @param $wc_order
* @param $order_id
*/
protected function updateFailedStatus( $wc_order, $order_id ): void {
$error_message = __( 'Could not process order because it was not possible to save the payment on PayPal.', 'woocommerce-paypal-payments' );
$wc_order->update_status( 'failed', $error_message );
$subscriptions = wcs_get_subscriptions_for_order( $order_id );
foreach ( $subscriptions as $key => $subscription ) {
if ( $subscription->get_parent_id() === $order_id ) {
try {
$subscription->update_status( 'cancelled' );
break;
} catch ( Exception $exception ) {
$this->logger->error( "Could not update cancelled status on subscription #{$subscription->get_id()} " . $exception->getMessage() );
}
}
}
}
}

View file

@ -137,38 +137,8 @@ class VaultingModule implements ModuleInterface {
add_action(
'woocommerce_paypal_payments_check_saved_payment',
function ( $order_id, $customer_id ) use ( $container ) {
$payment_token_repository = $container->get( 'vaulting.repository.payment-token' );
$settings = $container->get( 'wcgateway.settings' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
$authorized_payments_processor = $container->get( 'wcgateway.processor.authorized-payments' );
$order_endpoint = $container->get( 'api.endpoint.order' );
$payments_endpoint = $container->get( 'api.endpoint.payments' );
$tokens = $payment_token_repository->all_for_user_id( $customer_id );
if ( $tokens ) {
$this->capture_authorized_payment(
$settings,
$order_id,
$authorized_payments_processor,
$logger,
$customer_id
);
return;
}
$logger->error( "Payment for subscription parent order #{$order_id} was not saved on PayPal." );
$wc_order = wc_get_order( $order_id );
$order = $this->getOrder( $wc_order, $order_endpoint );
try {
$this->void_authorizations( $order, $payments_endpoint );
} catch ( RuntimeException $exception ) {
$logger->warning($exception->getMessage());
}
$this->updateFailedStatus( $wc_order, $order_id, $logger );
$payment_token_checker = $container->get('vaulting.payment-token-checker');
$payment_token_checker->checkAndUpdate($order_id, $customer_id);
},
10,
2
@ -194,92 +164,4 @@ class VaultingModule implements ModuleInterface {
return false;
}
/**
* @param $settings
* @param $order_id
* @param $authorized_payments_processor
* @param $logger
* @param $customer_id
*/
protected function capture_authorized_payment(
$settings,
$order_id,
$authorized_payments_processor,
$logger,
$customer_id
): void {
if ( $settings->has( 'intent' ) && strtoupper( (string) $settings->get( 'intent' ) ) === 'CAPTURE' ) {
$wc_order = wc_get_order( $order_id );
$authorized_payments_processor->capture_authorized_payment( $wc_order );
$logger->info( "Order: #{$order_id} for user: {$customer_id} captured successfully." );
}
}
/**
* @param $order
* @param $payments_endpoint
* @throws RuntimeException
*/
protected function void_authorizations( $order, $payments_endpoint ): void {
$purchase_units = $order->purchase_units();
if ( ! $purchase_units ) {
throw new RuntimeException( 'No purchase units.' );
}
$payments = $purchase_units[0]->payments();
if ( ! $payments ) {
throw new RuntimeException( 'No payments.' );
}
$voidable_authorizations = array_filter(
$payments->authorizations(),
function ( Authorization $authorization ): bool {
return $authorization->is_voidable();
}
);
if ( ! $voidable_authorizations ) {
throw new RuntimeException( 'No voidable authorizations.' );
}
foreach ( $voidable_authorizations as $authorization ) {
$payments_endpoint->void( $authorization );
}
}
/**
* @param $wc_order
* @param $order_endpoint
* @return mixed
*/
protected function getOrder( $wc_order, $order_endpoint ) {
$paypal_order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY );
if ( ! $paypal_order_id ) {
throw new RuntimeException( 'PayPal order ID not found in meta.' );
}
return $order_endpoint->order( $paypal_order_id );
}
/**
* @param $wc_order
* @param $order_id
* @param $logger
*/
protected function updateFailedStatus( $wc_order, $order_id, $logger ): void {
$error_message = __( 'Could not process order because it was not possible to save the payment on PayPal.', 'woocommerce-paypal-payments' );
$wc_order->update_status( 'failed', $error_message );
$subscriptions = wcs_get_subscriptions_for_order( $order_id );
foreach ( $subscriptions as $key => $subscription ) {
if ( $subscription->get_parent_id() === $order_id ) {
try {
$subscription->update_status( 'cancelled' );
break;
} catch ( Exception $exception ) {
$logger->error( "Could not update cancelled status on subscription #{$subscription->get_id()} " . $exception->getMessage() );
}
}
}
}
}