Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent payment method deletion in local environment when using Stripe production account #9566

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Prevented detaching payment methods from live Stripe accounts when working in non-production environments.
34 changes: 25 additions & 9 deletions includes/class-wc-payments-token-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,18 +280,34 @@ private function is_payment_method_enabled( $payment_method ) {
* Delete token from Stripe.
*
* @param string $token_id Token ID.
* @param WC_Payment_Token $token Token object.
* @param WC_Payment_Token $token Token object.
*
* @throws Exception
*/
public function woocommerce_payment_token_deleted( $token_id, $token ) {

if ( in_array( $token->get_gateway_id(), self::REUSABLE_GATEWAYS_BY_PAYMENT_METHOD, true ) ) {
try {
$this->payments_api_client->detach_payment_method( $token->get_token() );
// Clear cached payment methods.
$this->customer_service->clear_cached_payment_methods_for_user( $token->get_user_id() );
} catch ( Exception $e ) {
Logger::log( 'Error detaching payment method:' . $e->getMessage() );
}
// If it's not reusable payment method, we don't need to perform any additional checks.
if ( ! in_array( $token->get_gateway_id(), self::REUSABLE_GATEWAYS_BY_PAYMENT_METHOD, true ) ) {
return;
}
// First check if it's live mode.
// Second check if it's admin.
// Third check if it's not production environment.
// When all conditions are met, we don't want to delete the payment method from Stripe.
// This is to avoid detaching the payment method from the live stripe account on non production environments.
if (
WC_Payments::mode()->is_live() &&
is_admin() &&
'production' !== wp_get_environment_type()
) {
return;
}
try {
$this->payments_api_client->detach_payment_method( $token->get_token() );
// Clear cached payment methods.
$this->customer_service->clear_cached_payment_methods_for_user( $token->get_user_id() );
} catch ( Exception $e ) {
Logger::log( 'Error detaching payment method:' . $e->getMessage() );
}
}

Expand Down
Loading