diff --git a/src/Controllers/Card.php b/src/Controllers/Card.php index b03b5fd2..5c0dad33 100644 --- a/src/Controllers/Card.php +++ b/src/Controllers/Card.php @@ -7,11 +7,10 @@ use Paytrail\WooCommercePaymentGateway\Gateway; use Paytrail\WooCommercePaymentGateway\Plugin; -use Paytrail\WooCommercePaymentGateway\Exception; +//use Paytrail\WooCommercePaymentGateway\Exception; use WC_Payment_Tokens; use WP_Error; use WP_HTTP_Response; -use WP_REST_Request; class Card extends AbstractController { @@ -37,7 +36,7 @@ protected function delete() { $data = json_decode($body, true); if (!is_array($data)) { - throw new Exception('Failed to decode JSON object'); + throw new \Exception('Failed to decode JSON object'); } // @var \WP_User $current_user @@ -69,16 +68,21 @@ protected function delete() { } private function validate_request() { - if (empty($_SERVER['REQUEST_METHOD'])) { + $request_method = isset($_SERVER['REQUEST_METHOD']) ? sanitize_text_field($_SERVER['REQUEST_METHOD']) : ''; + + if (empty($request_method)) { return; } - $request = new WP_REST_Request(); - if ( $request->get_method() != 'POST' ) { - throw new Exception('Only POST requests are allowed'); + + if ('POST' !== $request_method) { + throw new \Exception('Only POST requests are allowed'); } - $content_type = WP_REST_Request::get_content_type(); + + $content_type = isset($_SERVER['CONTENT_TYPE']) ? sanitize_text_field($_SERVER['CONTENT_TYPE']) : ''; + if (stripos($content_type, 'application/json') === false) { - throw new Exception('Content-Type must be application/json'); + throw new \Exception('Content-Type must be application/json'); } } + } diff --git a/src/View/SavedPaymentMethods.php b/src/View/SavedPaymentMethods.php index 31bc7f0b..117800b6 100644 --- a/src/View/SavedPaymentMethods.php +++ b/src/View/SavedPaymentMethods.php @@ -14,8 +14,10 @@ if (\Paytrail\WooCommercePaymentGateway\Helper::getIsChangeSubscriptionPaymentMethod()) { $add_card_form_url = Router::get_url(Plugin::CARD_ENDPOINT, 'add') . '?change_payment_method=1'; + $is_subscription_page = true; } else { $add_card_form_url = Router::get_url(Plugin::CARD_ENDPOINT, 'add'); + $is_subscription_page = false; } $delete_card_url = Router::get_url(Plugin::CARD_ENDPOINT, 'delete'); @@ -56,6 +58,7 @@ function openTokenizedCardProviderGroupSelection() { jQuery(".paytrail-for-woocommerce-tokenized-payment-method-links.delete-card-button").click(function (evt) { evt.preventDefault(); let cardTokenId = jQuery("input[name='wc-paytrail-payment-token']:checked").val(); + const isSubscriptionPage = ; jQuery.ajax({ type: 'POST', @@ -63,8 +66,10 @@ function openTokenizedCardProviderGroupSelection() { url: '', data: JSON.stringify({token_id: cardTokenId}), success: function (response) { - if (response.success) { - jQuery('body').trigger('update_checkout') + if (response.success && isSubscriptionPage) { + location.reload() + } else if (response.success) { + jQuery('body').trigger('update_checkout'); } } })