1 | <?php |
||
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 ) { |
||
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() { |
||
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 ) { |
||
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() { |
||
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 ) { |
||
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() { |
||
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() { |
||
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() { |
||
275 | |||
276 | /** |
||
277 | * Remove a payment token from the database. |
||
278 | * @since 2.6.0 |
||
279 | */ |
||
280 | public function delete() { |
||
287 | } |
||
288 |