|
1
|
|
|
<?php |
|
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
3
|
|
|
exit; // Exit if accessed directly |
|
4
|
|
|
} |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* WooCommerce Payment Token. |
|
8
|
|
|
* |
|
9
|
|
|
* Representation of a general payment token to be extended by individuals types of tokens |
|
10
|
|
|
* examples: Credit Card, eCheck. |
|
11
|
|
|
* |
|
12
|
|
|
* @class WC_Payment_Token |
|
13
|
|
|
* @since 2.6.0 |
|
14
|
|
|
* @package WooCommerce/Abstracts |
|
15
|
|
|
* @category Abstract Class |
|
16
|
|
|
* @author WooThemes |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class WC_Payment_Token extends WC_Data { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Token Data (stored in the payment_tokens table). |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $data = array( |
|
25
|
|
|
'gateway_id' => '', |
|
26
|
|
|
'token' => '', |
|
27
|
|
|
'is_default' => 0, |
|
28
|
|
|
'user_id' => 0, |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Meta type. Payment tokens are a new object type. |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $meta_type = 'payment_token'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Initialize a payment token. |
|
39
|
|
|
* |
|
40
|
|
|
* These fields are accepted by all payment tokens: |
|
41
|
|
|
* is_default - boolean Optional - Indicates this is the default payment token for a user |
|
42
|
|
|
* token - string Required - The actual token to store |
|
43
|
|
|
* gateway_id - string Required - Identifier for the gateway this token is associated with |
|
44
|
|
|
* user_id - int Optional - ID for the user this token is associated with. 0 if this token is not associated with a user |
|
45
|
|
|
* |
|
46
|
|
|
* @since 2.6.0 |
|
47
|
|
|
* @param mixed $token |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct( $token = '' ) { |
|
50
|
|
|
if ( is_numeric( $token ) ) { |
|
51
|
|
|
$this->read( $token ); |
|
52
|
|
|
} elseif ( is_object( $token ) ) { |
|
53
|
|
|
$token_id = $token->get_id(); |
|
54
|
|
|
if ( ! empty( $token_id ) ) { |
|
55
|
|
|
$this->read( $token->get_id() ); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
// Set token type (cc, echeck) |
|
59
|
|
|
if ( ! empty( $this->type ) ) { |
|
60
|
|
|
$this->data['type'] = $this->type; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returns the raw payment token. |
|
66
|
|
|
* @since 2.6.0 |
|
67
|
|
|
* @return string Raw token |
|
68
|
|
|
*/ |
|
69
|
|
|
public function get_token() { |
|
70
|
|
|
return $this->data['token']; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Set the raw payment token. |
|
75
|
|
|
* @since 2.6.0 |
|
76
|
|
|
* @param string $token |
|
77
|
|
|
*/ |
|
78
|
|
|
public function set_token( $token ) { |
|
79
|
|
|
$this->data['token'] = $token; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns the type of this payment token (CC, eCheck, or something else). |
|
84
|
|
|
* @since 2.6.0 |
|
85
|
|
|
* @return string Payment Token Type (CC, eCheck) |
|
86
|
|
|
*/ |
|
87
|
|
|
public function get_type() { |
|
88
|
|
|
return isset( $this->data['type'] ) ? $this->data['type'] : ''; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get type to display to user. |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
public function get_display_name() { |
|
96
|
|
|
return $this->get_type(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns the user ID associated with the token or false if this token is not associated. |
|
101
|
|
|
* @since 2.6.0 |
|
102
|
|
|
* @return int User ID if this token is associated with a user or 0 if no user is associated |
|
103
|
|
|
*/ |
|
104
|
|
|
public function get_user_id() { |
|
105
|
|
|
return ( isset( $this->data['user_id'] ) && $this->data['user_id'] > 0 ) ? absint( $this->data['user_id'] ) : 0; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Set the user ID for the user associated with this order. |
|
110
|
|
|
* @since 2.6.0 |
|
111
|
|
|
* @param int $user_id |
|
112
|
|
|
*/ |
|
113
|
|
|
public function set_user_id( $user_id ) { |
|
114
|
|
|
$this->data['user_id'] = absint( $user_id ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns the ID of the gateway associated with this payment token. |
|
119
|
|
|
* @since 2.6.0 |
|
120
|
|
|
* @return string Gateway ID |
|
121
|
|
|
*/ |
|
122
|
|
|
public function get_gateway_id() { |
|
123
|
|
|
return $this->data['gateway_id']; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Set the gateway ID. |
|
128
|
|
|
* @since 2.6.0 |
|
129
|
|
|
* @param string $gateway_id |
|
130
|
|
|
*/ |
|
131
|
|
|
public function set_gateway_id( $gateway_id ) { |
|
132
|
|
|
$this->data['gateway_id'] = $gateway_id; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Returns if the token is marked as default. |
|
137
|
|
|
* @since 2.6.0 |
|
138
|
|
|
* @return boolean True if the token is default |
|
139
|
|
|
*/ |
|
140
|
|
|
public function is_default() { |
|
141
|
|
|
return ! empty( $this->data['is_default'] ); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Marks the payment as default or non-default. |
|
146
|
|
|
* @since 2.6.0 |
|
147
|
|
|
* @param boolean $is_default True or false |
|
148
|
|
|
*/ |
|
149
|
|
|
public function set_default( $is_default ) { |
|
150
|
|
|
$this->data['is_default'] = (bool) $is_default; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Validate basic token info (token and type are required). |
|
155
|
|
|
* @since 2.6.0 |
|
156
|
|
|
* @return boolean True if the passed data is valid |
|
157
|
|
|
*/ |
|
158
|
|
|
public function validate() { |
|
159
|
|
|
if ( empty( $this->data['token'] ) ) { |
|
160
|
|
|
return false; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if ( empty( $this->data['type'] ) ) { |
|
164
|
|
|
return false; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return true; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Get a token from the database. |
|
172
|
|
|
* @since 2.6.0 |
|
173
|
|
|
* @param int $token_id Token ID |
|
174
|
|
|
*/ |
|
175
|
|
|
public function read( $token_id ) { |
|
176
|
|
|
global $wpdb; |
|
177
|
|
|
if ( $token = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_payment_tokens WHERE token_id = %d LIMIT 1;", $token_id ) ) ) { |
|
178
|
|
|
$token_id = $token->token_id; |
|
179
|
|
|
$token = (array) $token; |
|
180
|
|
|
unset( $token['token_id'] ); |
|
181
|
|
|
$this->data = $token; |
|
182
|
|
|
$this->set_id( $token_id ); |
|
183
|
|
|
$this->read_meta_data(); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Update a payment token. |
|
189
|
|
|
* @since 2.6.0 |
|
190
|
|
|
* @return boolean on success, false if validation failed and a payment token could not be updated |
|
191
|
|
|
*/ |
|
192
|
|
|
public function update() { |
|
193
|
|
|
if ( false === $this->validate() ) { |
|
194
|
|
|
return false; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
global $wpdb; |
|
198
|
|
|
|
|
199
|
|
|
$payment_token_data = array( |
|
200
|
|
|
'gateway_id' => $this->get_gateway_id(), |
|
201
|
|
|
'token' => $this->get_token(), |
|
202
|
|
|
'user_id' => $this->get_user_id(), |
|
203
|
|
|
'type' => $this->get_type(), |
|
204
|
|
|
); |
|
205
|
|
|
|
|
206
|
|
|
$wpdb->update( |
|
207
|
|
|
$wpdb->prefix . 'woocommerce_payment_tokens', |
|
208
|
|
|
$payment_token_data, |
|
209
|
|
|
array( 'token_id' => $this->get_id() ) |
|
210
|
|
|
); |
|
211
|
|
|
|
|
212
|
|
|
$this->save_meta_data(); |
|
213
|
|
|
|
|
214
|
|
|
// Make sure all other tokens are not set to default |
|
215
|
|
|
if ( $this->is_default() && $this->get_user_id() > 0 ) { |
|
216
|
|
|
WC_Payment_Tokens::set_users_default( $this->get_user_id(), $this->get_id() ); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
do_action( 'woocommerce_payment_token_updated', $this->get_id() ); |
|
220
|
|
|
return true; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Create a new payment token in the database. |
|
225
|
|
|
* @since 2.6.0 |
|
226
|
|
|
* @return boolean on success, false if validation failed and a payment token could not be created |
|
227
|
|
|
*/ |
|
228
|
|
|
public function create() { |
|
229
|
|
|
if ( false === $this->validate() ) { |
|
230
|
|
|
return false; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
global $wpdb; |
|
234
|
|
|
// Are there any other tokens? If not, set this token as default |
|
235
|
|
|
if ( ! $this->is_default() && $this->get_user_id() > 0 ) { |
|
236
|
|
|
$default_token = WC_Payment_Tokens::get_customer_default_token( $this->get_user_id() ); |
|
237
|
|
|
if ( is_null( $default_token ) ) { |
|
238
|
|
|
$this->set_default( true ); |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
$payment_token_data = array( |
|
243
|
|
|
'gateway_id' => $this->get_gateway_id(), |
|
244
|
|
|
'token' => $this->get_token(), |
|
245
|
|
|
'user_id' => $this->get_user_id(), |
|
246
|
|
|
'type' => $this->get_type(), |
|
247
|
|
|
); |
|
248
|
|
|
|
|
249
|
|
|
$wpdb->insert( $wpdb->prefix . 'woocommerce_payment_tokens', $payment_token_data ); |
|
250
|
|
|
$token_id = $wpdb->insert_id; |
|
251
|
|
|
$this->set_id( $token_id ); |
|
252
|
|
|
$this->save_meta_data(); |
|
253
|
|
|
|
|
254
|
|
|
// Make sure all other tokens are not set to default |
|
255
|
|
|
if ( $this->is_default() && $this->get_user_id() > 0 ) { |
|
256
|
|
|
WC_Payment_Tokens::set_users_default( $this->get_user_id(), $token_id ); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
do_action( 'woocommerce_payment_token_created', $token_id ); |
|
260
|
|
|
return true; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Saves a payment token to the database - does not require you to know if this is a new token or an update token. |
|
265
|
|
|
* @since 2.6.0 |
|
266
|
|
|
* @return boolean on success, false if validation failed and a payment token could not be saved |
|
267
|
|
|
*/ |
|
268
|
|
|
public function save() { |
|
269
|
|
|
if ( $this->get_id() > 0 ) { |
|
270
|
|
|
return $this->update(); |
|
271
|
|
|
} else { |
|
272
|
|
|
return $this->create(); |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Remove a payment token from the database. |
|
278
|
|
|
* @since 2.6.0 |
|
279
|
|
|
*/ |
|
280
|
|
|
public function delete() { |
|
281
|
|
|
global $wpdb; |
|
282
|
|
|
$this->read( $this->get_id() ); // Make sure we have a token to return after deletion |
|
283
|
|
|
$wpdb->delete( $wpdb->prefix . 'woocommerce_payment_tokens', array( 'token_id' => $this->get_id() ), array( '%d' ) ); |
|
284
|
|
|
$wpdb->delete( $wpdb->prefix . 'woocommerce_payment_tokenmeta', array( 'payment_token_id' => $this->get_id() ), array( '%d' ) ); |
|
285
|
|
|
do_action( 'woocommerce_payment_token_deleted', $this->get_id(), $this ); |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|