mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
Allow WC credit card token deletion
This commit is contained in:
parent
e64acceea7
commit
941f2f303a
2 changed files with 58 additions and 38 deletions
|
@ -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,15 +159,20 @@ class VaultingModule implements ModuleInterface {
|
|||
|
||||
$this->filterFailedVaultingEmailsForSubscriptionOrders( $container );
|
||||
|
||||
add_filter('woocommerce_payment_token_class', function ($type) {
|
||||
if($type === 'WC_Payment_Token_PayPal') {
|
||||
add_filter(
|
||||
'woocommerce_payment_token_class',
|
||||
function ( $type ) {
|
||||
if ( $type === 'WC_Payment_Token_PayPal' ) {
|
||||
return PaymentTokenPayPal::class;
|
||||
}
|
||||
|
||||
return $type;
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
add_filter( 'woocommerce_payment_methods_list_item', function($item, $payment_token) {
|
||||
add_filter(
|
||||
'woocommerce_payment_methods_list_item',
|
||||
function( $item, $payment_token ) {
|
||||
if ( strtolower( $payment_token->get_type() ) !== 'paypal' ) {
|
||||
return $item;
|
||||
}
|
||||
|
@ -173,37 +180,47 @@ class VaultingModule implements ModuleInterface {
|
|||
$item['method']['brand'] = 'PayPal';
|
||||
|
||||
return $item;
|
||||
}, 10, 2 );
|
||||
},
|
||||
10,
|
||||
2
|
||||
);
|
||||
|
||||
add_action('wp', function() use ( $container ) {
|
||||
add_action(
|
||||
'wp',
|
||||
function() use ( $container ) {
|
||||
global $wp;
|
||||
|
||||
if ( isset( $wp->query_vars['delete-payment-method'] ) ) {
|
||||
$token_id = absint( $wp->query_vars['delete-payment-method'] );
|
||||
$token = WC_Payment_Tokens::get( $token_id );
|
||||
|
||||
if(is_null( $token ) || $token->get_type() !== 'PayPal') {
|
||||
if (
|
||||
is_null( $token )
|
||||
|| ( $token->get_gateway_id() !== PayPalGateway::ID && $token->get_gateway_id() !== CreditCardGateway::ID )
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(
|
||||
$wpnonce = wc_clean( wp_unslash( $_REQUEST['_wpnonce'] ?? '' ) );
|
||||
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 )
|
||||
|| ! isset( $wpnonce ) || wp_verify_nonce( $wpnonce, 'delete-payment-method-' . $token_id ) === false
|
||||
) {
|
||||
wc_add_notice( __( 'Invalid payment method.', 'woocommerce' ), 'error' );
|
||||
wc_add_notice( __( 'Invalid payment method.', 'woocommerce-paypal-payments' ), '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' );
|
||||
$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;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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() );
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue