Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix subscription card deletion #174

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
22 changes: 13 additions & 9 deletions src/Controllers/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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
Expand Down Expand Up @@ -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');
}
}

}
9 changes: 7 additions & 2 deletions src/View/SavedPaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -56,15 +58,18 @@ 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 = <?php echo json_encode($is_subscription_page); ?>;

jQuery.ajax({
type: 'POST',
contentType: 'application/json',
url: '<?php echo esc_url_raw($delete_card_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');
}
}
})
Expand Down
Loading