Get wc customer id by removing prefix

This commit is contained in:
dinamiko 2022-03-02 17:02:37 +01:00
parent 78286b0542
commit 6ebe8d3171

View file

@ -12,8 +12,8 @@ namespace WooCommerce\PayPalCommerce\Webhooks\Handler;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
class VaultPaymentTokenCreated implements RequestHandler
{
class VaultPaymentTokenCreated implements RequestHandler {
/**
* @var LoggerInterface
*/
@ -29,43 +29,37 @@ class VaultPaymentTokenCreated implements RequestHandler
*/
protected $authorized_payments_processor;
public function __construct(LoggerInterface $logger, string $prefix, AuthorizedPaymentsProcessor $authorized_payments_processor)
{
$this->logger = $logger;
$this->prefix = $prefix;
public function __construct( LoggerInterface $logger, string $prefix, AuthorizedPaymentsProcessor $authorized_payments_processor ) {
$this->logger = $logger;
$this->prefix = $prefix;
$this->authorized_payments_processor = $authorized_payments_processor;
}
public function event_types(): array
{
public function event_types(): array {
return array(
'VAULT.PAYMENT-TOKEN.CREATED',
);
}
public function responsible_for_request(\WP_REST_Request $request): 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
{
$response = array( 'success' => false );
$webhook_id = (string) ( $request['id'] ?? '' );
public function handle_request( \WP_REST_Request $request ): \WP_REST_Response {
$response = array( 'success' => false );
$customer_id = $request['resource']['customer_id'] ?? '';
if(!$customer_id) {
$message = sprintf( 'No customer id for webhook event %s was found.', $webhook_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 );
}
$customer_id_parts = explode('-', $customer_id);
$wc_customer_id = (int) end($customer_id_parts);
$this->authorized_payments_processor->capture_authorized_payments_for_customer($wc_customer_id);
$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 rest_ensure_response( $response );
}
}