diff --git a/includes/paytabs_payment_methods.php b/includes/paytabs_payment_methods.php index f88dd01..991fc87 100755 --- a/includes/paytabs_payment_methods.php +++ b/includes/paytabs_payment_methods.php @@ -247,6 +247,18 @@ public function init_form_fields() } + public function is_available() + { + if (is_add_payment_method_page()) { + if (!$this->supports('add_payment_method')) { + return false; + } + } + + return parent::is_available(); + } + + /** * There are no payment fields for paytabs, but we want to show the description if set. **/ @@ -258,9 +270,19 @@ function payment_fields() return; } + if (!is_checkout()) { + return; + } + $this->tokenization_script(); $this->saved_payment_methods(); - $this->save_payment_method_checkbox(); + + $has_subscription = WC_Subscriptions_Cart::cart_contains_subscription(); + if ($has_subscription) { + echo wpautop('Will Save to Account'); + } else { + $this->save_payment_method_checkbox(); + } // $this->form(); } @@ -360,14 +382,43 @@ public function process_payment($order_id) } } + /** + * return the last saved Token for the selected payment method + */ + private function get_user_token($user_id) + { + /* + $token_default = WC_Payment_Tokens::get_customer_default_token($user_id); + if ($token_default) { + return $token_default; + } + */ + + $tokens = WC_Payment_Tokens::get_customer_tokens($user_id, $this->id); + if ($tokens && count($tokens) > 0) { + return end($tokens); + } + + /* + $tokens = WC_Payment_Tokens::get_customer_tokens($user_id); + if ($tokens && count($tokens) > 0) { + return end($tokens); + } + */ + + // $tokens1 = WC_Payment_Tokens::get_order_tokens(); + + return false; + } public function scheduled_subscription_payment($amount_to_charge, $renewal_order) { $user_id = $renewal_order->get_user_id(); - $tokenObj = WC_Payment_Tokens::get_customer_default_token($user_id); + $tokenObj = $this->get_user_token($user_id); + if (!$tokenObj) { - // ToDo: Try to fetch User's Tokens - paytabs_error_log("Subscription renewal error: The User {$user_id} does not have saved Token."); + $renewal_order->add_order_note("Renewal failed [No Saved payment token found]"); + paytabs_error_log("Subscription renewal error: The User {$user_id} does not have saved Tokens."); return false; } $values = $this->prepareOrder_Tokenised($renewal_order, $tokenObj, $amount_to_charge); @@ -539,19 +590,7 @@ private function orderSuccess($order, $transaction_id, $transaction_type, $token // wc_add_notice(__('Thank you for shopping with us. Your account has been charged and your transaction is successful. We will be shipping your order to you soon.', 'woocommerce'), 'success'); if ($token_str) { - $token = new WC_Payment_Token_Paytabs(); - - $token->set_token($token_str); - $token->set_tran_ref($transaction_id); - - $token->set_gateway_id($this->id); - $user_id = $order->get_user_id(); - $token->set_user_id($user_id); - - $tokeId = $token->save(); - - $order->add_payment_token($token); - $order->save(); + $this->saveToken($order, $token_str, $transaction_id); } $redirect_url = $this->get_return_url($order); @@ -567,6 +606,22 @@ private function orderSuccess($order, $transaction_id, $transaction_type, $token } + private function saveToken($order, $token_str, $transaction_id) + { + $user_id = $order->get_user_id(); + + $token = new WC_Payment_Token_Paytabs(); + $token->set_token($token_str); + $token->set_tran_ref($transaction_id); + $token->set_gateway_id($this->id); + $token->set_user_id($user_id); + $tokeId = $token->save(); + + $order->add_payment_token($token); + $order->save(); + } + + /** * Payment failed => Order status change to failed */ diff --git a/paytabs-woocommerce.php b/paytabs-woocommerce.php index d9a16de..7263359 100644 --- a/paytabs-woocommerce.php +++ b/paytabs-woocommerce.php @@ -8,7 +8,7 @@ * Plugin Name: PayTabs - WooCommerce Payment Gateway * Plugin URI: https://paytabs.com/ * Description: PayTabs is a 3rd party payment gateway. Ideal payment solutions for your internet business. - * Version: 4.5.3 + * Version: 4.5.4 * Requires PHP: 7.0 * Author: PayTabs * Author URI: w.kammoun@paytabs.com @@ -20,7 +20,7 @@ } -define('PAYTABS_PAYPAGE_VERSION', '4.5.3'); +define('PAYTABS_PAYPAGE_VERSION', '4.5.4'); define('PAYTABS_PAYPAGE_DIR', plugin_dir_path(__FILE__)); define('PAYTABS_PAYPAGE_ICONS_URL', plugins_url("icons/", __FILE__)); define('PAYTABS_DEBUG_FILE', WP_CONTENT_DIR . "/debug_paytabs.log");