mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
Delete WC card payment token if it does not exist on PayPal
This commit is contained in:
parent
ad7e7cbe55
commit
8ea790fedd
2 changed files with 29 additions and 1 deletions
|
@ -135,6 +135,7 @@ return array(
|
||||||
$container->get( 'api.endpoint.order' ),
|
$container->get( 'api.endpoint.order' ),
|
||||||
$container->get( 'wcgateway.endpoint.capture-card-payment' ),
|
$container->get( 'wcgateway.endpoint.capture-card-payment' ),
|
||||||
$container->get( 'api.prefix' ),
|
$container->get( 'api.prefix' ),
|
||||||
|
$container->get( 'api.endpoint.payment-tokens' ),
|
||||||
$logger
|
$logger
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
|
@ -15,6 +15,7 @@ use WC_Order;
|
||||||
use WC_Payment_Tokens;
|
use WC_Payment_Tokens;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentTokensEndpoint;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||||
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||||
|
@ -154,6 +155,13 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
|
||||||
*/
|
*/
|
||||||
private $prefix;
|
private $prefix;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Payment tokens endpoint.
|
||||||
|
*
|
||||||
|
* @var PaymentTokensEndpoint
|
||||||
|
*/
|
||||||
|
private $payment_tokens_endpoint;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The logger.
|
* The logger.
|
||||||
*
|
*
|
||||||
|
@ -179,6 +187,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
|
||||||
* @param OrderEndpoint $order_endpoint The order endpoint.
|
* @param OrderEndpoint $order_endpoint The order endpoint.
|
||||||
* @param CaptureCardPayment $capture_card_payment Capture card payment.
|
* @param CaptureCardPayment $capture_card_payment Capture card payment.
|
||||||
* @param string $prefix The prefix.
|
* @param string $prefix The prefix.
|
||||||
|
* @param PaymentTokensEndpoint $payment_tokens_endpoint Payment tokens endpoint.
|
||||||
* @param LoggerInterface $logger The logger.
|
* @param LoggerInterface $logger The logger.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
@ -197,6 +206,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
|
||||||
OrderEndpoint $order_endpoint,
|
OrderEndpoint $order_endpoint,
|
||||||
CaptureCardPayment $capture_card_payment,
|
CaptureCardPayment $capture_card_payment,
|
||||||
string $prefix,
|
string $prefix,
|
||||||
|
PaymentTokensEndpoint $payment_tokens_endpoint,
|
||||||
LoggerInterface $logger
|
LoggerInterface $logger
|
||||||
) {
|
) {
|
||||||
$this->id = self::ID;
|
$this->id = self::ID;
|
||||||
|
@ -215,6 +225,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
|
||||||
$this->order_endpoint = $order_endpoint;
|
$this->order_endpoint = $order_endpoint;
|
||||||
$this->capture_card_payment = $capture_card_payment;
|
$this->capture_card_payment = $capture_card_payment;
|
||||||
$this->prefix = $prefix;
|
$this->prefix = $prefix;
|
||||||
|
$this->payment_tokens_endpoint = $payment_tokens_endpoint;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
|
|
||||||
if ( $state->current_state() === State::STATE_ONBOARDED ) {
|
if ( $state->current_state() === State::STATE_ONBOARDED ) {
|
||||||
|
@ -414,9 +425,25 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
|
||||||
// phpcs:ignore WordPress.Security.NonceVerification.Missing
|
// phpcs:ignore WordPress.Security.NonceVerification.Missing
|
||||||
$card_payment_token_id = wc_clean( wp_unslash( $_POST['wc-ppcp-credit-card-gateway-payment-token'] ?? '' ) );
|
$card_payment_token_id = wc_clean( wp_unslash( $_POST['wc-ppcp-credit-card-gateway-payment-token'] ?? '' ) );
|
||||||
if ( $card_payment_token_id ) {
|
if ( $card_payment_token_id ) {
|
||||||
|
$customer_id = get_user_meta( get_current_user_id(), '_ppcp_target_customer_id', true );
|
||||||
|
if ( ! $customer_id ) {
|
||||||
|
$customer_id = get_user_meta( get_current_user_id(), 'ppcp_customer_id', true );
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$customer_tokens = $this->payment_tokens_endpoint->payment_tokens_for_customer( $customer_id );
|
||||||
|
} catch ( RuntimeException $exception ) {
|
||||||
|
$customer_tokens = array();
|
||||||
|
}
|
||||||
|
|
||||||
$tokens = WC_Payment_Tokens::get_customer_tokens( get_current_user_id() );
|
$tokens = WC_Payment_Tokens::get_customer_tokens( get_current_user_id() );
|
||||||
foreach ( $tokens as $token ) {
|
foreach ( $tokens as $token ) {
|
||||||
if ( $token->get_id() === (int) $card_payment_token_id ) {
|
if ( $token->get_id() === (int) $card_payment_token_id ) {
|
||||||
|
if ( ! in_array( $token->get_token(), $customer_tokens, true ) ) {
|
||||||
|
$token->delete();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$custom_id = $wc_order->get_order_number();
|
$custom_id = $wc_order->get_order_number();
|
||||||
$invoice_id = $this->prefix . $wc_order->get_order_number();
|
$invoice_id = $this->prefix . $wc_order->get_order_number();
|
||||||
$create_order = $this->capture_card_payment->create_order( $token->get_token(), $custom_id, $invoice_id );
|
$create_order = $this->capture_card_payment->create_order( $token->get_token(), $custom_id, $invoice_id );
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue