Skip to content

Commit

Permalink
PLGWOOS-654: Add gateway_id to the properties set for each token (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcivit authored May 18, 2021
1 parent 57654df commit 80daa73
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 19 deletions.
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<properties>
<property name="maxNestingLevel" value="3"/>
</properties>
<exclude-pattern>*/src/PaymentMethods/TokenizationMethodsController.php</exclude-pattern>
</rule>

<!-- Minimal length for variables names -->
Expand Down
6 changes: 3 additions & 3 deletions src/PaymentMethods/Base/BaseTokenizationPaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@

namespace MultiSafepay\WooCommerce\PaymentMethods\Base;

use MultiSafepay\WooCommerce\PaymentMethods\Base\BasePaymentMethod;
use MultiSafepay\WooCommerce\Services\CustomerService;
use MultiSafepay\WooCommerce\Services\OrderService;
use MultiSafepay\WooCommerce\Services\SdkService;
use WC_Payment_Tokens;

abstract class BaseTokenizationPaymentMethod extends BasePaymentMethod {


/**
* TokenizationPaymentMethod constructor.
*/
public function __construct() {
parent::__construct();
if ( is_user_logged_in() && (bool) get_option( 'multisafepay_tokenization', false ) ) {
$this->supports[] = 'tokenization';
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}

/**
Expand Down Expand Up @@ -124,7 +124,7 @@ public function payment_fields() {
$this->save_payment_method_checkbox();
}

if ( wc_get_customer_saved_methods_list( get_current_user_id() ) && is_checkout() ) {
if ( ! empty( $this->get_tokens() ) && is_checkout() ) {
$this->saved_payment_methods();
}

Expand Down
97 changes: 81 additions & 16 deletions src/PaymentMethods/TokenizationMethodsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use MultiSafepay\WooCommerce\Utils\Logger;
use WC_Payment_Token;
use WC_Payment_Token_CC;
use WC_Payment_Tokens;

class TokenizationMethodsController {

Expand All @@ -40,48 +41,113 @@ class TokenizationMethodsController {
* @param string $gateway_id
*
* @return WC_Payment_Token_CC[]
* @throws \Psr\Http\Client\ClientExceptionInterface
*/
public function multisafepay_get_customer_payment_tokens( array $tokens, int $customer_id, string $gateway_id ): array {
if ( is_user_logged_in() && class_exists( 'WC_Payment_Token_CC' ) ) {
$stored_tokens = array();
foreach ( $tokens as $token ) {
$stored_tokens[] = $token->get_token();
$stored_tokens[ $token->get_token() ] = $token;
}

// Account payment method page templates use wc_get_customer_saved_methods_list at the header
// which generates a loop in combination with the method $token->save(), used in this filter: woocommerce_get_customer_payment_tokens
if ( is_account_page() ) {
remove_filter( 'woocommerce_get_customer_payment_tokens', array( $this, 'multisafepay_get_customer_payment_tokens' ), 10 );
}
$multisafepay_tokens = $this->get_multisafepay_tokens_by_customer_id();
foreach ( $multisafepay_tokens as $multisafepay_token ) {
if ( ! in_array( $multisafepay_token->getToken(), $stored_tokens, true ) ) {
$wc_payment_token_cc = $this->create_wc_payment_token_cc( $multisafepay_token, $gateway_id );
$tokens[ $wc_payment_token_cc->get_id() ] = $wc_payment_token_cc;

if ( is_checkout() && 'multisafepay_creditcard' === $gateway_id || is_account_page() ) {
$multisafepay_tokens = $this->get_multisafepay_tokens_by_customer_id();
foreach ( $multisafepay_tokens as $multisafepay_token ) {
// If the token has not been registered
if ( ! isset( $stored_tokens[ $multisafepay_token->getToken() ] ) ) {
$token = $this->save_wc_payment_token_cc( $multisafepay_token, $customer_id );
$tokens[ $token->get_id() ] = $token;
}
// Since 4.1.0, the tokens has been saving without register the gateway_id
if ( isset( $stored_tokens[ $multisafepay_token->getToken() ] ) ) {
$token = $this->update_wc_payment_token_cc( $multisafepay_token, $stored_tokens[ $multisafepay_token->getToken() ]->get_id() );
$tokens[ $token->get_id() ] = $token;
}
}
}
return $tokens;
}

// Account payment method page calls wc_get_customer_saved_methods_list
// Which generated a loop over the hook used in this filter: woocommerce_get_customer_payment_tokens
if ( is_account_page() ) {
add_filter( 'woocommerce_get_customer_payment_tokens', array( $this, 'multisafepay_get_customer_payment_tokens' ), 10, 3 );
}
}
return $tokens;
}

/**
* Returns a WC_Payment_Token_CC object for the given MultiSafepay Token object
*
* @see https://woocommerce.github.io/code-reference/classes/WC-Payment-Token-CC.html
*
* @param Token $multisafepay_token
* @param string $gateway_id
* @param Token $multisafepay_token
* @param int $customer_id
*
* @return WC_Payment_Token_CC
*/
private function create_wc_payment_token_cc( Token $multisafepay_token, string $gateway_id ): WC_Payment_Token_CC {
private function save_wc_payment_token_cc( Token $multisafepay_token, int $customer_id ): WC_Payment_Token_CC {
$token = new WC_Payment_Token_CC();
$token->set_user_id( get_current_user_id() );
$token->set_user_id( $customer_id );
$token->set_token( $multisafepay_token->getToken() );
$token->set_gateway_id( $gateway_id );
$token->set_card_type( strtolower( $multisafepay_token->getGatewayCode() ) );
$token->set_gateway_id( 'multisafepay_creditcard' );
$token->set_card_type( $this->get_wc_payment_token_allowed_card_type( $multisafepay_token->getGatewayCode() ) );
$token->set_last4( $multisafepay_token->getLastFour() );
$token->set_expiry_month( $multisafepay_token->getExpiryMonth() );
$token->set_expiry_year( '20' . $multisafepay_token->getExpiryYear() );
$token->save();
return $token;
}

/**
* Returns an updated WC_Payment_Token_CC object with the given MultiSafepay Token object
*
* @see https://woocommerce.github.io/code-reference/classes/WC-Payment-Token-CC.html
*
* @param Token $multisafepay_token
* @param int $token_id
*
* @return WC_Payment_Token_CC
*/
private function update_wc_payment_token_cc( Token $multisafepay_token, int $token_id ): WC_Payment_Token_CC {
$token = WC_Payment_Tokens::get( $token_id );
$token->set_gateway_id( 'multisafepay_creditcard' );
$card_type = $this->get_wc_payment_token_allowed_card_type( $multisafepay_token->getGatewayCode() );
if ( ! empty( $card_type ) ) {
$token->set_card_type( $card_type );
}
$token->save();
return $token;
}

/**
* Return the allowed string to define the card_type of a WC_Payment_Token_CC object
*
* @see https://github.com/woocommerce/woocommerce/wiki/Payment-Token-API#set_card_type-type-
*
* @param string $multisafepay_token_gateway_code
*
* @return string
*/
private function get_wc_payment_token_allowed_card_type( string $multisafepay_token_gateway_code ): string {
$allowed_card_types_by_woocommerce = array(
'MASTERCARD' => 'mastercard',
'VISA' => 'visa',
'AMEX' => 'american express',
);

if ( ! isset( $allowed_card_types_by_woocommerce[ $multisafepay_token_gateway_code ] ) ) {
return '';
}

return $allowed_card_types_by_woocommerce[ $multisafepay_token_gateway_code ];
}


/**
* Return MultiSafepay tokens by customer id
*
Expand Down Expand Up @@ -122,7 +188,6 @@ public function woocommerce_payment_token_deleted( int $token_id, WC_Payment_Tok
$sdk = new SdkService();
try {
$remove = $sdk->get_sdk()->getTokenManager()->delete( $token->get_token(), (string) $token->get_user_id() );
$token->delete( true );
} catch ( ApiException $api_exception ) {
Logger::log_error( $api_exception->getMessage() );
}
Expand Down

0 comments on commit 80daa73

Please sign in to comment.