| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hyperpay\Gateways\Helpers; |
| 4 |
|
| 5 |
use WC_Payment_Token_CC; |
| 6 |
use WC_Payment_tokens; |
| 7 |
|
| 8 |
class TokenManager |
| 9 |
{ |
| 10 |
const TOKENIZATION_AMOUNT = "0.01"; |
| 11 |
|
| 12 |
private static function is_exists($registration_id) |
| 13 |
{ |
| 14 |
if (empty($registration_id) || !get_current_user_id()) { |
| 15 |
return false; |
| 16 |
} |
| 17 |
|
| 18 |
$token_manager = null; |
| 19 |
|
| 20 |
foreach (WC_Payment_tokens::get_customer_tokens(get_current_user_id()) ?? [] as $token) { |
| 21 |
if ($token->get_token() === $registration_id) { |
| 22 |
$token_manager = $token; |
| 23 |
break; |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
return $token_manager; |
| 28 |
} |
| 29 |
|
| 30 |
public static function save($registration_id, $details, $gateway_id) |
| 31 |
{ |
| 32 |
|
| 33 |
if ($token_manager = self::is_exists($registration_id)) { |
| 34 |
return $token_manager; |
| 35 |
} |
| 36 |
$token_manager = new WC_Payment_Token_CC(); |
| 37 |
$token_manager->set_token($registration_id); |
| 38 |
$token_manager->set_last4($details['last4Digits'] ?? '****'); |
| 39 |
$token_manager->set_expiry_year($details['expiryYear'] ?? '****'); |
| 40 |
$token_manager->set_expiry_month($details['expiryMonth'] ?? '****'); |
| 41 |
$token_manager->set_card_type($details['paymentBrand'] ?? 'unknown'); |
| 42 |
$token_manager->set_gateway_id($gateway_id); |
| 43 |
$token_manager->set_user_id(get_current_user_id()); |
| 44 |
$token_manager->add_meta_data('agreement_id', $details['recurringPaymentAgreement'] ?? '', true); |
| 45 |
$token_manager->add_meta_data('initial_transaction_id', $details['initial_tx_id'] ?? '', true); |
| 46 |
$token_manager->save(); |
| 47 |
|
| 48 |
return $token_manager; |
| 49 |
} |
| 50 |
|
| 51 |
} |
| 52 |
|