Allow WC credit card token deletion

This commit is contained in:
emilicastells 2022-12-05 15:38:43 +01:00
parent e64acceea7
commit 941f2f303a
No known key found for this signature in database
GPG key ID: 1520C07081754570
2 changed files with 58 additions and 38 deletions

View file

@ -20,6 +20,8 @@ use WC_Order;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\Vaulting\Endpoint\DeletePaymentTokenEndpoint;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
/**
* Class StatusReportModule
@ -157,53 +159,68 @@ class VaultingModule implements ModuleInterface {
$this->filterFailedVaultingEmailsForSubscriptionOrders( $container );
add_filter('woocommerce_payment_token_class', function ($type) {
if($type === 'WC_Payment_Token_PayPal') {
return PaymentTokenPayPal::class;
add_filter(
'woocommerce_payment_token_class',
function ( $type ) {
if ( $type === 'WC_Payment_Token_PayPal' ) {
return PaymentTokenPayPal::class;
}
return $type;
}
);
return $type;
});
add_filter(
'woocommerce_payment_methods_list_item',
function( $item, $payment_token ) {
if ( strtolower( $payment_token->get_type() ) !== 'paypal' ) {
return $item;
}
$item['method']['brand'] = 'PayPal';
add_filter( 'woocommerce_payment_methods_list_item', function($item, $payment_token) {
if ( strtolower( $payment_token->get_type() ) !== 'paypal' ) {
return $item;
}
},
10,
2
);
$item['method']['brand'] = 'PayPal';
add_action(
'wp',
function() use ( $container ) {
global $wp;
return $item;
}, 10, 2 );
if ( isset( $wp->query_vars['delete-payment-method'] ) ) {
$token_id = absint( $wp->query_vars['delete-payment-method'] );
$token = WC_Payment_Tokens::get( $token_id );
add_action('wp', function() use ( $container ) {
global $wp;
if (
is_null( $token )
|| ( $token->get_gateway_id() !== PayPalGateway::ID && $token->get_gateway_id() !== CreditCardGateway::ID )
) {
return;
}
if ( isset( $wp->query_vars['delete-payment-method'] ) ) {
$token_id = absint( $wp->query_vars['delete-payment-method'] );
$token = WC_Payment_Tokens::get( $token_id );
$wpnonce = wc_clean( wp_unslash( $_REQUEST['_wpnonce'] ?? '' ) );
if (
$token->get_user_id() !== get_current_user_id()
|| ! isset( $wpnonce ) || wp_verify_nonce( $wpnonce, 'delete-payment-method-' . $token_id ) === false
) {
wc_add_notice( __( 'Invalid payment method.', 'woocommerce-paypal-payments' ), 'error' );
wp_safe_redirect( wc_get_account_endpoint_url( 'payment-methods' ) );
exit();
}
if(is_null( $token ) || $token->get_type() !== 'PayPal') {
return;
}
if(
$token->get_user_id() !== get_current_user_id()
|| ! isset( $_REQUEST['_wpnonce'] ) || false === wp_verify_nonce( wp_unslash( $_REQUEST['_wpnonce'] ), 'delete-payment-method-' . $token_id )
) {
wc_add_notice( __( 'Invalid payment method.', 'woocommerce' ), 'error' );
wp_safe_redirect( wc_get_account_endpoint_url( 'payment-methods' ) );
exit();
}
try {
$payment_token_endpoint = $container->get('api.endpoint.payment-token');
$payment_token_endpoint->delete_token_by_id($token->get_token());
} catch (RuntimeException $exception) {
wc_add_notice( __( 'Could not delete payment token. ' . $exception->getMessage(), 'woocommerce-paypal-payments' ), 'error' );
return;
try {
$payment_token_endpoint = $container->get( 'api.endpoint.payment-token' );
$payment_token_endpoint->delete_token_by_id( $token->get_token() );
} catch ( RuntimeException $exception ) {
wc_add_notice( __( 'Could not delete payment token. ', 'woocommerce-paypal-payments' ) . $exception->getMessage(), 'error' );
return;
}
}
}
});
);
}
/**

View file

@ -13,6 +13,8 @@ use Psr\Log\LoggerInterface;
use WC_Payment_Token_CC;
use WC_Payment_Tokens;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenPayPal;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WP_REST_Request;
use WP_REST_Response;
@ -100,8 +102,6 @@ class VaultPaymentTokenCreated implements RequestHandler {
* @return WP_REST_Response
*/
public function handle_request( WP_REST_Request $request ): WP_REST_Response {
$this->logger->info( wc_print_r( $request['resource'], true ) );
$response = array( 'success' => false );
$customer_id = null !== $request['resource'] && isset( $request['resource']['customer_id'] )
@ -122,6 +122,8 @@ class VaultPaymentTokenCreated implements RequestHandler {
$token = new WC_Payment_Token_CC();
$token->set_token( $request['resource']['id'] );
$token->set_user_id( $wc_customer_id );
$token->set_gateway_id( CreditCardGateway::ID );
$token->set_last4( $request['resource']['source']['card']['last_digits'] ?? '' );
$expiry = explode( '-', $request['resource']['source']['card']['expiry'] ?? '' );
$token->set_expiry_year( $expiry[0] ?? '' );
@ -132,6 +134,7 @@ class VaultPaymentTokenCreated implements RequestHandler {
} elseif ( isset( $request['resource']['source']['paypal'] ) ) {
$this->payment_token_paypal->set_token( $request['resource']['id'] );
$this->payment_token_paypal->set_user_id( $wc_customer_id );
$this->payment_token_paypal->set_gateway_id( PayPalGateway::ID );
$this->payment_token_paypal->save();
WC_Payment_Tokens::set_users_default( $wc_customer_id, $this->payment_token_paypal->get_id() );
}