Skip to content

Commit

Permalink
Token lookup (#21)
Browse files Browse the repository at this point in the history
- Enhance getting the saved token for the user
- Add note to the Order if the Subscription failed
- Methods only available in checkout page
- Subscription always use the tokenization
- Refactoring
  • Loading branch information
wajihkm authored Sep 22, 2021
1 parent 67ce1b0 commit 07e2cac
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 19 deletions.
89 changes: 72 additions & 17 deletions includes/paytabs_payment_methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
**/
Expand All @@ -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();
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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
*/
Expand Down
4 changes: 2 additions & 2 deletions paytabs-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Plugin Name: PayTabs - WooCommerce Payment Gateway
* Plugin URI: https://paytabs.com/
* Description: PayTabs is a <strong>3rd party payment gateway</strong>. Ideal payment solutions for your internet business.
* Version: 4.5.3
* Version: 4.5.4
* Requires PHP: 7.0
* Author: PayTabs
* Author URI: [email protected]
Expand All @@ -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");
Expand Down

0 comments on commit 07e2cac

Please sign in to comment.