PluginProbe ʕ •ᴥ•ʔ
GlobalPayments Gateway Provider for WooCommerce / 1.5.4
GlobalPayments Gateway Provider for WooCommerce v1.5.4
1.20.2 1.20.1 1.19.3 1.19.2 1.19.1 1.19.0 1.18.4 1.18.3 trunk 1.0.0 1.0.0-b2 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.6 1.10.7 1.10.8 1.11.0 1.12.0 1.12.1 1.13.0 1.13.1 1.13.2 1.13.3 1.13.4 1.13.7 1.13.8 1.14.0 1.14.1 1.14.2 1.14.3 1.14.4 1.14.5 1.14.6 1.14.7 1.14.8 1.14.9 1.15.0 1.15.2 1.15.4 1.15.5 1.15.6 1.15.8 1.15.9 1.16.0 1.16.1 1.16.2 1.16.3 1.16.4 1.16.5 1.16.6 1.17.0 1.17.1 1.18.0 1.18.1 1.18.2 1.2.0 1.2.1 1.2.2 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.6.0 1.7.0 1.8.0 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5
global-payments-woocommerce / src / Data / PaymentTokenData.php
global-payments-woocommerce / src / Data Last commit date
PaymentTokenData.php 3 years ago
PaymentTokenData.php
165 lines
1 <?php
2
3 namespace GlobalPayments\WooCommercePaymentGatewayProvider\Data;
4
5 use GlobalPayments\WooCommercePaymentGatewayProvider\Gateways\AbstractGateway;
6 use GlobalPayments\WooCommercePaymentGatewayProvider\Gateways\Requests\RequestInterface;
7 use WC_Payment_Tokens;
8 use WC_Payment_Token_CC;
9
10 class PaymentTokenData {
11 const KEY_SHOULD_SAVE_TOKEN = 'should_save_for_later';
12
13 protected $card_type_map = array(
14 'mastercard' => 'mastercard',
15 'visa' => 'visa',
16 'discover' => 'discover',
17 'amex' => 'american express',
18 'diners' => 'diners',
19 'jcb' => 'jcb',
20 );
21
22 /**
23 * Current request
24 *
25 * @var RequestInterface
26 */
27 protected $request;
28
29 /**
30 * Used w/TransIT gateway
31 *
32 * @var string
33 */
34 public static $tsepCvv = null;
35
36 /**
37 * Standardize getting single- and multi-use token data
38 *
39 * @param RequestInterface $request
40 *
41 * @return
42 */
43 public function __construct( RequestInterface $request = null ) {
44 $this->request = $request;
45 }
46
47 public function get_token() {
48 $token = $this->get_multi_use_token();
49
50 if ( null === $token ) {
51 $token = $this->get_single_use_token();
52 }
53
54 return $token;
55 }
56
57 public function save_new_token( $multi_use_token, $card_brand_txn_id = null ) {
58 $user_id = get_current_user_id();
59 $current_tokens = WC_Payment_Tokens::get_customer_tokens( $user_id, $this->request->gateway_id );
60
61 $token = $this->get_single_use_token();
62
63 if ( !empty( $token ) ) {
64 // a card number should only have a single token stored
65 foreach ( $current_tokens as $t ) {
66 if ( $t->get_token() === $multi_use_token ) {
67 $t->delete( true );
68 }
69 }
70
71 if ( ! $token->get_meta( self::KEY_SHOULD_SAVE_TOKEN, true ) ) {
72 return;
73 }
74
75 $token->set_token( $multi_use_token );
76 $token->add_meta_data( 'card_brand_txn_id', $card_brand_txn_id );
77 $token->set_user_id( $user_id );
78 $token->set_gateway_id( $this->request->gateway_id );
79 $token->add_meta_data( self::KEY_SHOULD_SAVE_TOKEN, false, true );
80 $token->save();
81 }
82 }
83
84 public function get_single_use_token() {
85 if ( null === $this->request ) {
86 return null;
87 }
88
89 $gateway = $this->request->get_request_data( 'payment_method' );
90 $request_data = $this->request->get_request_data( $gateway );
91 if ( ! isset( $request_data['token_response'] ) ) {
92 return null;
93 }
94
95 $data = json_decode( stripslashes( $request_data['token_response'] ) );
96
97 if ( empty( $data ) ) {
98 return null;
99 }
100
101 $token = new WC_Payment_Token_CC();
102
103 // phpcs:disable WordPress.NamingConventions.ValidVariableName
104 $token->add_meta_data( self::KEY_SHOULD_SAVE_TOKEN, $this->get_should_save_for_later(), true );
105 $token->set_token( $data->paymentReference );
106
107 if ( isset( $data->details->cardLast4 ) ) {
108 $token->set_last4( $data->details->cardLast4 );
109 }
110
111 if ( isset( $data->details->expiryYear ) ) {
112 $token->set_expiry_year( $data->details->expiryYear );
113 }
114
115 if ( isset( $data->details->expiryMonth ) ) {
116 $token->set_expiry_month( $data->details->expiryMonth );
117 }
118
119 static::$tsepCvv = isset( $data->details->cardSecurityCode ) ? $data->details->cardSecurityCode : null;
120
121 if ( isset( $data->details->cardType ) && isset( $this->card_type_map[ $data->details->cardType ] ) ) {
122 $token->set_card_type( $this->card_type_map[ $data->details->cardType ] );
123 }
124 // phpcs:enable WordPress.NamingConventions.ValidVariableName
125
126 return $token;
127 }
128
129 public function get_multi_use_token() {
130 if ( null === $this->request ) {
131 return null;
132 }
133
134 $gateway = $this->request->get_request_data( 'payment_method' );
135 $token_id = $this->request->get_request_data( sprintf( 'wc-%s-payment-token', $gateway ) );
136
137 if ( 'new' === $token_id ) {
138 return null;
139 }
140
141 $token = WC_Payment_Tokens::get( $token_id );
142 if ( ! isset( $token ) ) {
143 return null;
144 }
145
146 if ( $token->get_user_id() !== get_current_user_id() && ! wc_current_user_has_role( 'administrator' ) ) {
147 return null;
148 }
149
150 if ( $token->get_meta( self::KEY_SHOULD_SAVE_TOKEN, true ) ) {
151 $token->add_meta_data( self::KEY_SHOULD_SAVE_TOKEN, false, true );
152 }
153
154 return $token;
155 }
156
157 protected function get_should_save_for_later() {
158 $gateway = $this->request->get_request_data( 'payment_method' );
159 return // Verify transactions always mean we're storing a token
160 $this->request->get_transaction_type() === AbstractGateway::TXN_TYPE_VERIFY ||
161 // Merchant has enabled card storage. Customer has elected to store card.
162 $this->request->get_request_data( sprintf( 'wc-%s-new-payment-method', $gateway ) ) === 'true';
163 }
164 }
165