woocommerce-paypal-payments/modules/ppcp-vaulting/src/PaymentTokenChecker.php

228 lines
6.6 KiB
PHP
Raw Normal View History

2022-03-01 15:12:54 +01:00
<?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;
2022-03-01 16:10:46 +01:00
use WC_Order;
2022-03-01 15:12:54 +01:00
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
2022-03-01 16:10:46 +01:00
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
2022-03-01 15:12:54 +01:00
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
2022-03-01 16:10:46 +01:00
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
2022-03-01 15:12:54 +01:00
2022-03-01 16:10:46 +01:00
/**
* Class PaymentTokenChecker
*/
2022-03-01 15:12:54 +01:00
class PaymentTokenChecker {
/**
2022-03-01 16:10:46 +01:00
* The payment token repository.
*
2022-03-01 15:12:54 +01:00
* @var PaymentTokenRepository
*/
protected $payment_token_repository;
/**
2022-03-01 16:10:46 +01:00
* The settings.
*
2022-03-01 15:12:54 +01:00
* @var Settings
*/
protected $settings;
/**
2022-03-01 16:10:46 +01:00
* The authorized payments processor.
*
2022-03-01 15:12:54 +01:00
* @var AuthorizedPaymentsProcessor
*/
protected $authorized_payments_processor;
/**
2022-03-01 16:10:46 +01:00
* The order endpoint.
*
2022-03-01 15:12:54 +01:00
* @var OrderEndpoint
*/
protected $order_endpoint;
/**
2022-03-01 16:10:46 +01:00
* The payments endpoint.
*
2022-03-01 15:12:54 +01:00
* @var PaymentsEndpoint
*/
protected $payments_endpoint;
/**
2022-03-01 16:10:46 +01:00
* The logger.
*
2022-03-01 15:12:54 +01:00
* @var LoggerInterface
*/
protected $logger;
2022-03-01 16:10:46 +01:00
/**
* PaymentTokenChecker constructor.
*
* @param PaymentTokenRepository $payment_token_repository The payment token repository.
* @param Settings $settings The settings.
* @param AuthorizedPaymentsProcessor $authorized_payments_processor The authorized payments processor.
* @param OrderEndpoint $order_endpoint The order endpoint.
* @param PaymentsEndpoint $payments_endpoint The payments endpoint.
* @param LoggerInterface $logger The logger.
*/
2022-03-01 15:12:54 +01:00
public function __construct(
PaymentTokenRepository $payment_token_repository,
Settings $settings,
AuthorizedPaymentsProcessor $authorized_payments_processor,
OrderEndpoint $order_endpoint,
PaymentsEndpoint $payments_endpoint,
LoggerInterface $logger
2022-03-01 16:10:46 +01:00
) {
$this->payment_token_repository = $payment_token_repository;
$this->settings = $settings;
2022-03-01 15:12:54 +01:00
$this->authorized_payments_processor = $authorized_payments_processor;
2022-03-01 16:10:46 +01:00
$this->order_endpoint = $order_endpoint;
$this->payments_endpoint = $payments_endpoint;
$this->logger = $logger;
2022-03-01 15:12:54 +01:00
}
2022-03-01 16:10:46 +01:00
/**
* Check if payment token exist and updates order accordingly.
*
* @param int $order_id The order ID.
* @param int $customer_id The customer ID.
* @return void
*/
public function check_and_update( int $order_id, int $customer_id ):void {
$wc_order = wc_get_order( $order_id );
2022-03-04 12:02:43 +01:00
if ( ! is_a( $wc_order, WC_Order::class ) ) {
return;
}
if ( $wc_order->get_status() === 'processing' ) {
return;
}
2022-03-01 15:12:54 +01:00
$tokens = $this->payment_token_repository->all_for_user_id( $customer_id );
if ( $tokens ) {
2022-03-01 16:10:46 +01:00
try {
2022-03-04 12:02:43 +01:00
$this->capture_authorized_payment( $wc_order );
} catch ( Exception $exception ) {
$this->logger->error( $exception->getMessage() );
2022-03-01 16:10:46 +01:00
}
return;
2022-03-01 15:12:54 +01:00
}
$this->logger->error( "Payment for subscription parent order #{$order_id} was not saved on PayPal." );
try {
2022-03-04 12:02:43 +01:00
$order = $this->get_order( $wc_order );
2022-03-01 16:10:46 +01:00
$this->void_authorizations( $order );
2022-03-01 15:12:54 +01:00
} catch ( RuntimeException $exception ) {
2022-03-01 16:10:46 +01:00
$this->logger->warning( $exception->getMessage() );
2022-03-01 15:12:54 +01:00
}
2022-03-01 16:10:46 +01:00
$this->update_failed_status( $wc_order );
2022-03-01 15:12:54 +01:00
}
/**
2022-03-04 12:02:43 +01:00
* Captures authorized payments for the given WC order.
2022-03-01 16:10:46 +01:00
*
2022-03-04 12:02:43 +01:00
* @param WC_Order $wc_order The WC order.
* @throws Exception When there is a problem capturing the payment.
2022-03-01 15:12:54 +01:00
*/
2022-03-04 12:02:43 +01:00
private function capture_authorized_payment( WC_Order $wc_order ): void {
2022-03-01 15:12:54 +01:00
if ( $this->settings->has( 'intent' ) && strtoupper( (string) $this->settings->get( 'intent' ) ) === 'CAPTURE' ) {
if ( ! $this->authorized_payments_processor->capture_authorized_payment( $wc_order ) ) {
throw new Exception( "Could not capture payment for order: #{$wc_order->get_id()}" );
}
2022-03-01 15:12:54 +01:00
}
}
/**
2022-03-01 16:10:46 +01:00
* Voids authorizations for the given PayPal order.
*
* @param Order $order The PayPal order.
* @return void
* @throws RuntimeException When there is a problem voiding authorizations.
2022-03-01 15:12:54 +01:00
*/
2022-03-01 16:10:46 +01:00
private function void_authorizations( Order $order ): void {
2022-03-01 15:12:54 +01:00
$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 );
}
}
/**
2022-03-01 16:10:46 +01:00
* Gets a PayPal order from the given WooCommerce order.
*
* @param WC_Order $wc_order The WooCommerce order.
* @return Order The PayPal order.
* @throws RuntimeException When there is a problem getting the PayPal order.
2022-03-01 15:12:54 +01:00
*/
2022-03-01 16:10:46 +01:00
private function get_order( WC_Order $wc_order ): Order {
2022-03-01 15:12:54 +01:00
$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 );
}
/**
2022-03-01 16:10:46 +01:00
* Updates WC order and subscription status to failed and canceled respectively.
*
* @param WC_Order $wc_order The WC order.
2022-03-01 15:12:54 +01:00
*/
2022-03-01 16:10:46 +01:00
private function update_failed_status( WC_Order $wc_order ): void {
2022-03-01 15:12:54 +01:00
$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 );
2022-03-04 12:02:43 +01:00
/**
* Function already exist in Subscription plugin
*
* @psalm-suppress UndefinedFunction
*/
2022-03-01 16:10:46 +01:00
$subscriptions = wcs_get_subscriptions_for_order( $wc_order->get_id() );
2022-03-01 15:12:54 +01:00
foreach ( $subscriptions as $key => $subscription ) {
2022-03-01 16:10:46 +01:00
if ( $subscription->get_parent_id() === $wc_order->get_id() ) {
2022-03-01 15:12:54 +01:00
try {
$subscription->update_status( 'cancelled' );
break;
} catch ( Exception $exception ) {
$this->logger->error( "Could not update cancelled status on subscription #{$subscription->get_id()} " . $exception->getMessage() );
}
}
}
}
}