mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-03 08:37:53 +08:00
Fix psalm errors
This commit is contained in:
parent
8bbd50bbe6
commit
dcbac3cae3
6 changed files with 140 additions and 49 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue