Fix psalm errors

This commit is contained in:
dinamiko 2022-03-04 12:02:43 +01:00
parent 8bbd50bbe6
commit dcbac3cae3
6 changed files with 140 additions and 49 deletions

View file

@ -104,6 +104,10 @@ class PaymentTokenChecker {
*/
public function check_and_update( int $order_id, int $customer_id ):void {
$wc_order = wc_get_order( $order_id );
if ( ! is_a( $wc_order, WC_Order::class ) ) {
return;
}
if ( $wc_order->get_status() === 'processing' ) {
return;
}
@ -111,7 +115,7 @@ class PaymentTokenChecker {
$tokens = $this->payment_token_repository->all_for_user_id( $customer_id );
if ( $tokens ) {
try {
$this->capture_authorized_payment( $order_id );
$this->capture_authorized_payment( $wc_order );
} catch ( NotFoundException $exception ) {
$this->logger->warning( "It was not possible to capture the payment for order: #{$order_id}" );
}
@ -122,7 +126,7 @@ class PaymentTokenChecker {
$this->logger->error( "Payment for subscription parent order #{$order_id} was not saved on PayPal." );
try {
$order = $this->get_order( $wc_order );
$order = $this->get_order( $wc_order );
$this->void_authorizations( $order );
} catch ( RuntimeException $exception ) {
$this->logger->warning( $exception->getMessage() );
@ -132,15 +136,13 @@ class PaymentTokenChecker {
}
/**
* Captures authorized payments for the given order id.
* Captures authorized payments for the given WC order.
*
* @param int $order_id The WC order ID.
* @return void
* @param WC_Order $wc_order The WC order.
* @throws NotFoundException When there is a problem capturing the payment.
*/
private function capture_authorized_payment( int $order_id ): void {
private function capture_authorized_payment( WC_Order $wc_order ): 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 );
}
}
@ -203,6 +205,11 @@ class PaymentTokenChecker {
$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 );
/**
* Function already exist in Subscription plugin
*
* @psalm-suppress UndefinedFunction
*/
$subscriptions = wcs_get_subscriptions_for_order( $wc_order->get_id() );
foreach ( $subscriptions as $key => $subscription ) {
if ( $subscription->get_parent_id() === $wc_order->get_id() ) {

View file

@ -132,7 +132,7 @@ class VaultingModule implements ModuleInterface {
add_action(
'woocommerce_paypal_payments_check_saved_payment',
function ( $order_id, $customer_id ) use ( $container ) {
function ( int $order_id, int $customer_id ) use ( $container ) {
$payment_token_checker = $container->get( 'vaulting.payment-token-checker' );
$payment_token_checker->check_and_update( $order_id, $customer_id );
},

View file

@ -84,6 +84,8 @@ class AuthorizedPaymentsProcessor {
private $config;
/**
* The subscription helper.
*
* @var SubscriptionHelper
*/
private $subscription_helper;
@ -95,7 +97,8 @@ class AuthorizedPaymentsProcessor {
* @param PaymentsEndpoint $payments_endpoint The Payments endpoint.
* @param LoggerInterface $logger The logger.
* @param AuthorizeOrderActionNotice $notice The notice.
* @param ContainerInterface $config The settings.
* @param ContainerInterface $config The settings.
* @param SubscriptionHelper $subscription_helper The subscription helper.
*/
public function __construct(
OrderEndpoint $order_endpoint,
@ -106,11 +109,11 @@ class AuthorizedPaymentsProcessor {
SubscriptionHelper $subscription_helper
) {
$this->order_endpoint = $order_endpoint;
$this->payments_endpoint = $payments_endpoint;
$this->logger = $logger;
$this->notice = $notice;
$this->config = $config;
$this->order_endpoint = $order_endpoint;
$this->payments_endpoint = $payments_endpoint;
$this->logger = $logger;
$this->notice = $notice;
$this->config = $config;
$this->subscription_helper = $subscription_helper;
}
@ -209,22 +212,29 @@ class AuthorizedPaymentsProcessor {
return false;
}
public function capture_authorized_payments_for_customer(int $customer_id) {
/**
* Captures the authorized payments for the given customer.
*
* @param int $customer_id The customer id.
*/
public function capture_authorized_payments_for_customer( int $customer_id ): void {
$wc_orders = wc_get_orders(array(
'customer_id' => $customer_id,
'status' => array('wc-on-hold'),
'limit' => -1,
));
$wc_orders = wc_get_orders(
array(
'customer_id' => $customer_id,
'status' => array( 'wc-on-hold' ),
'limit' => -1,
)
);
if ($this->config->has('intent') && strtoupper((string)$this->config->get('intent')) === 'CAPTURE') {
foreach ($wc_orders as $wc_order) {
if(
if ( $this->config->has( 'intent' ) && strtoupper( (string) $this->config->get( 'intent' ) ) === 'CAPTURE' ) {
foreach ( $wc_orders as $wc_order ) {
if (
$this->subscription_helper->has_subscription( $wc_order->get_id() )
&& $wc_order->get_meta('_ppcp_captured_vault_webhook') === 'false'
&& $wc_order->get_meta( '_ppcp_captured_vault_webhook' ) === 'false'
) {
$this->capture_authorized_payment($wc_order);
$wc_order->update_meta_data('_ppcp_captured_vault_webhook', 'true');
$this->capture_authorized_payment( $wc_order );
$wc_order->update_meta_data( '_ppcp_captured_vault_webhook', 'true' );
}
}
}

View file

@ -9,6 +9,9 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Webhooks\Handler;
use WP_REST_Request;
use WP_REST_Response;
/**
* Interface RequestHandler
*/
@ -24,18 +27,18 @@ interface RequestHandler {
/**
* Whether a handler is responsible for a given request or not.
*
* @param \WP_REST_Request $request The request.
* @param WP_REST_Request $request The request.
*
* @return bool
*/
public function responsible_for_request( \WP_REST_Request $request): bool;
public function responsible_for_request( WP_REST_Request $request): bool;
/**
* Responsible for handling the request.
*
* @param \WP_REST_Request $request The request.
* @param WP_REST_Request $request The request.
*
* @return \WP_REST_Response
* @return WP_REST_Response
*/
public function handle_request( \WP_REST_Request $request): \WP_REST_Response;
public function handle_request( WP_REST_Request $request): WP_REST_Response;
}

View file

@ -1,6 +1,6 @@
<?php
/**
* Handles the Webhook VAULT.PAYMENT-TOKEN.CREATED
* Handles the Webhook VAULT.CREDIT-CARD.CREATED
*
* @package WooCommerce\PayPalCommerce\Webhooks\Handler
*/
@ -10,46 +10,78 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Webhooks\Handler;
use Psr\Log\LoggerInterface;
use WP_REST_Request;
use WP_REST_Response;
/**
* Class VaultCreditCardCreated
*/
class VaultCreditCardCreated implements RequestHandler {
class VaultCreditCardCreated implements RequestHandler
{
/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;
/**
* The prefix.
*
* @var string
*/
protected $prefix;
public function __construct(LoggerInterface $logger, string $prefix)
{
/**
* VaultCreditCardCreated constructor.
*
* @param LoggerInterface $logger The logger.
* @param string $prefix The prefix.
*/
public function __construct( LoggerInterface $logger, string $prefix ) {
$this->logger = $logger;
$this->prefix = $prefix;
}
public function event_types(): array
{
/**
* The event types a handler handles.
*
* @return string[]
*/
public function event_types(): array {
return array(
'VAULT.CREDIT-CARD.CREATED',
);
}
public function responsible_for_request(\WP_REST_Request $request): bool
{
/**
* Whether a handler is responsible for a given request or not.
*
* @param WP_REST_Request $request The request.
*
* @return bool
*/
public function responsible_for_request( WP_REST_Request $request ): bool {
return in_array( $request['event_type'], $this->event_types(), true );
}
public function handle_request(\WP_REST_Request $request): \WP_REST_Response
{
/**
* Responsible for handling the request.
*
* @param WP_REST_Request $request The request.
*
* @return WP_REST_Response
*/
public function handle_request( WP_REST_Request $request ): WP_REST_Response {
// TODO currently this webhook is not triggered from PayPal, implement it once is available.
$message = 'VAULT.CREDIT-CARD.CREATED received.';
$this->logger->log('info', $message);
$this->logger->log( 'info', $message );
$response = array(
'success' => true,
'message' => $message,
);
return rest_ensure_response($response);
return new WP_REST_Response( $response );
}
}

View file

@ -11,55 +11,94 @@ namespace WooCommerce\PayPalCommerce\Webhooks\Handler;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WP_REST_Request;
use WP_REST_Response;
/**
* Class VaultPaymentTokenCreated
*/
class VaultPaymentTokenCreated implements RequestHandler {
/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;
/**
* The prefix.
*
* @var string
*/
protected $prefix;
/**
* The authorized payment processor.
*
* @var AuthorizedPaymentsProcessor
*/
protected $authorized_payments_processor;
/**
* VaultPaymentTokenCreated constructor.
*
* @param LoggerInterface $logger The logger.
* @param string $prefix The prefix.
* @param AuthorizedPaymentsProcessor $authorized_payments_processor The authorized payment processor.
*/
public function __construct( LoggerInterface $logger, string $prefix, AuthorizedPaymentsProcessor $authorized_payments_processor ) {
$this->logger = $logger;
$this->prefix = $prefix;
$this->authorized_payments_processor = $authorized_payments_processor;
}
/**
* The event types a handler handles.
*
* @return string[]
*/
public function event_types(): array {
return array(
'VAULT.PAYMENT-TOKEN.CREATED',
);
}
public function responsible_for_request( \WP_REST_Request $request ): bool {
/**
* Whether a handler is responsible for a given request or not.
*
* @param WP_REST_Request $request The request.
*
* @return bool
*/
public function responsible_for_request( WP_REST_Request $request ): bool {
return in_array( $request['event_type'], $this->event_types(), true );
}
public function handle_request( \WP_REST_Request $request ): \WP_REST_Response {
/**
* Responsible for handling the request.
*
* @param WP_REST_Request $request The request.
*
* @return WP_REST_Response
*/
public function handle_request( WP_REST_Request $request ): WP_REST_Response {
$response = array( 'success' => false );
$customer_id = $request['resource']['customer_id'] ?? '';
$customer_id = null !== $request['resource'] && isset( $request['resource']['customer_id'] )
? $request['resource']['customer_id']
: '';
if ( ! $customer_id ) {
$message = 'No customer id was found.';
$this->logger->warning( $message, array( 'request' => $request ) );
$response['message'] = $message;
return new \WP_REST_Response( $response );
return new WP_REST_Response( $response );
}
$wc_customer_id = (int) str_replace( $this->prefix, '', $customer_id );
$this->authorized_payments_processor->capture_authorized_payments_for_customer( $wc_customer_id );
$response['success'] = true;
return rest_ensure_response( $response );
return new WP_REST_Response( $response );
}
}