woocommerce-square
/
includes
/
Framework
/
PaymentGateway
/
PaymentTokens
/
Payment_Gateway_Payment_Token.php
Payment_Gateway_Payment_Token.php
3 years ago
Payment_Gateway_Payment_Tokens_Handler.php
1 year ago
Square_Credit_Card_Payment_Token.php
3 years ago
Payment_Gateway_Payment_Token.php
386 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Payment Gateway Framework |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@skyverge.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * @since 3.0.0 |
| 14 | * @author WooCommerce / SkyVerge |
| 15 | * @copyright Copyright (c) 2013-2019, SkyVerge, Inc. |
| 16 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 17 | * |
| 18 | * Modified by WooCommerce on 01 December 2021. |
| 19 | */ |
| 20 | |
| 21 | namespace WooCommerce\Square\Framework\PaymentGateway\PaymentTokens; |
| 22 | |
| 23 | use WooCommerce\Square\Framework\PaymentGateway\Payment_Gateway_Helper; |
| 24 | |
| 25 | defined( 'ABSPATH' ) || exit; |
| 26 | |
| 27 | /** |
| 28 | * WooCommerce Payment Gateway Token |
| 29 | * |
| 30 | * Represents a credit card or check payment token |
| 31 | */ |
| 32 | class Payment_Gateway_Payment_Token { |
| 33 | |
| 34 | |
| 35 | /** @var string payment gateway token ID */ |
| 36 | protected $id; |
| 37 | |
| 38 | /** |
| 39 | * @var array associated token data |
| 40 | */ |
| 41 | protected $data; |
| 42 | |
| 43 | /** |
| 44 | * @var string payment type image url |
| 45 | */ |
| 46 | protected $img_url; |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * Initialize a payment token with associated $data which is expected to |
| 51 | * have the following members: |
| 52 | * |
| 53 | * default - boolean optional indicates this is the default payment token |
| 54 | * type - string one of 'credit_card' or 'echeck' ('check' for backwards compatibility) |
| 55 | * last_four - string last four digits of account number |
| 56 | * card_type - string credit card type: visa, mc, amex, disc, diners, jcb, etc (credit card only) |
| 57 | * exp_month - string optional expiration month MM (credit card only) |
| 58 | * exp_year - string optional expiration year YYYY (credit card only) |
| 59 | * account_type - string one of 'checking' or 'savings' (checking gateway only) |
| 60 | * |
| 61 | * @since 3.0.0 |
| 62 | * @param string $id the payment gateway token ID |
| 63 | * @param array $data associated data |
| 64 | */ |
| 65 | public function __construct( $id, $data ) { |
| 66 | |
| 67 | if ( isset( $data['type'] ) && 'credit_card' === $data['type'] ) { |
| 68 | |
| 69 | // normalize the provided card type to adjust for possible abbreviations if set |
| 70 | if ( isset( $data['card_type'] ) && $data['card_type'] ) { |
| 71 | |
| 72 | $data['card_type'] = Payment_Gateway_Helper::normalize_card_type( $data['card_type'] ); |
| 73 | |
| 74 | // otherwise, get the payment type from the account number |
| 75 | } elseif ( isset( $data['account_number'] ) ) { |
| 76 | |
| 77 | $data['card_type'] = Payment_Gateway_Helper::card_type_from_account_number( $data['account_number'] ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // remove account number so it's not saved to the token |
| 82 | unset( $data['account_number'] ); |
| 83 | |
| 84 | $this->id = $id; |
| 85 | $this->data = $data; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns the payment token string |
| 90 | * |
| 91 | * @since 3.0.0 |
| 92 | * @return string payment token string |
| 93 | */ |
| 94 | public function get_id() { |
| 95 | |
| 96 | return $this->id; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | /** |
| 101 | * Returns true if this payment token is default |
| 102 | * |
| 103 | * @since 3.0.0 |
| 104 | * @return boolean true if this payment token is default |
| 105 | */ |
| 106 | public function is_default() { |
| 107 | |
| 108 | return isset( $this->data['default'] ) && $this->data['default']; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * Makes this payment token the default or a non-default one |
| 114 | * |
| 115 | * @since 3.0.0 |
| 116 | * @param boolean $default true or false |
| 117 | */ |
| 118 | public function set_default( $default ) { |
| 119 | |
| 120 | $this->data['default'] = $default; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * Returns true if this payment token represents a credit card |
| 126 | * |
| 127 | * @since 3.0.0 |
| 128 | * @return boolean true if this payment token represents a credit card |
| 129 | */ |
| 130 | public function is_credit_card() { |
| 131 | |
| 132 | return 'credit_card' === $this->data['type']; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns the payment type, one of 'credit_card' or 'echeck' |
| 137 | * |
| 138 | * @since 3.0.0 |
| 139 | * @return string the payment type |
| 140 | */ |
| 141 | public function get_type() { |
| 142 | |
| 143 | return $this->data['type']; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | * Returns the card type ie visa, mc, amex, disc, diners, jcb, etc |
| 149 | * |
| 150 | * Credit card gateway only |
| 151 | * |
| 152 | * @since 3.0.0 |
| 153 | * @return string the payment type |
| 154 | */ |
| 155 | public function get_card_type() { |
| 156 | |
| 157 | return isset( $this->data['card_type'] ) ? $this->data['card_type'] : null; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Returns the bank account type, one of 'checking' or 'savings' |
| 162 | * |
| 163 | * eCheck gateway only |
| 164 | * |
| 165 | * @since 3.0.0 |
| 166 | * @return string the payment type |
| 167 | */ |
| 168 | public function get_account_type() { |
| 169 | |
| 170 | return isset( $this->data['account_type'] ) ? $this->data['account_type'] : null; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | /** |
| 175 | * Set the account type |
| 176 | * |
| 177 | * eCheck gateway only |
| 178 | * |
| 179 | * @since 3.0.0 |
| 180 | * @param string $account_type |
| 181 | */ |
| 182 | public function set_account_type( $account_type ) { |
| 183 | |
| 184 | $this->data['account_type'] = $account_type; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | /** |
| 189 | * Returns the full payment type, ie Visa, MasterCard, American Express, |
| 190 | * Discover, Diners, JCB, eCheck, etc |
| 191 | * |
| 192 | * @since 3.0.0 |
| 193 | * @return string the payment type |
| 194 | */ |
| 195 | public function get_type_full() { |
| 196 | |
| 197 | if ( $this->is_credit_card() ) { |
| 198 | $type = $this->get_card_type() ? $this->get_card_type() : 'card'; |
| 199 | } else { |
| 200 | $type = $this->get_account_type() ? $this->get_account_type() : 'bank'; |
| 201 | } |
| 202 | |
| 203 | return Payment_Gateway_Helper::payment_type_to_name( $type ); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | /** |
| 208 | * Returns the last four digits of the credit card or check account number |
| 209 | * |
| 210 | * @since 3.0.0 |
| 211 | * @return string last four of account |
| 212 | */ |
| 213 | public function get_last_four() { |
| 214 | |
| 215 | return isset( $this->data['last_four'] ) ? $this->data['last_four'] : null; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /** |
| 220 | * Set the account last four |
| 221 | * |
| 222 | * @since 3.0.0 |
| 223 | * @param string $last_four |
| 224 | */ |
| 225 | public function set_last_four( $last_four ) { |
| 226 | |
| 227 | $this->data['last_four'] = $last_four; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | /** |
| 232 | * Returns the expiration month of the credit card. This should only be |
| 233 | * called for credit card tokens |
| 234 | * |
| 235 | * @since 3.0.0 |
| 236 | * @return string expiration month as a two-digit number |
| 237 | */ |
| 238 | public function get_exp_month() { |
| 239 | |
| 240 | return isset( $this->data['exp_month'] ) ? $this->data['exp_month'] : null; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /** |
| 245 | * Set the expiration month |
| 246 | * |
| 247 | * @since 3.0.0 |
| 248 | * @param string $month |
| 249 | */ |
| 250 | public function set_exp_month( $month ) { |
| 251 | |
| 252 | $this->data['exp_month'] = $month; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | /** |
| 257 | * Returns the expiration year of the credit card. This should only be |
| 258 | * called for credit card tokens |
| 259 | * |
| 260 | * @since 3.0.0 |
| 261 | * @return string expiration year as a four-digit number |
| 262 | */ |
| 263 | public function get_exp_year() { |
| 264 | |
| 265 | return isset( $this->data['exp_year'] ) ? $this->data['exp_year'] : null; |
| 266 | } |
| 267 | |
| 268 | |
| 269 | /** |
| 270 | * Set the expiration year |
| 271 | * |
| 272 | * @since 3.0.0 |
| 273 | * @param string $year |
| 274 | */ |
| 275 | public function set_exp_year( $year ) { |
| 276 | |
| 277 | $this->data['exp_year'] = $year; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | /** |
| 282 | * Returns the expiration date in the format MM/YY, suitable for use |
| 283 | * in order notes or other customer-facing areas |
| 284 | * |
| 285 | * @since 3.0.0 |
| 286 | * @return string formatted expiration date |
| 287 | */ |
| 288 | public function get_exp_date() { |
| 289 | |
| 290 | return $this->get_exp_month() . '/' . substr( $this->get_exp_year(), -2 ); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | /** |
| 295 | * Set the full image URL based on the token payment type. Note that this |
| 296 | * is available for convenience during a single request and will not be |
| 297 | * included in persistent storage |
| 298 | * |
| 299 | * @see Payment_Gateway_Payment_Token::get_image_url() |
| 300 | * @since 3.0.0 |
| 301 | * @param string $url the full image URL |
| 302 | */ |
| 303 | public function set_image_url( $url ) { |
| 304 | |
| 305 | $this->img_url = $url; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | /** |
| 310 | * Get the full image URL based on teh token payment type. |
| 311 | * |
| 312 | * @see Payment_Gateway_Payment_Token::set_image_url() |
| 313 | * @since 3.0.0 |
| 314 | * @return string the full image URL |
| 315 | */ |
| 316 | public function get_image_url() { |
| 317 | |
| 318 | return $this->img_url; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | /** |
| 323 | * Gets the payment method nickname. |
| 324 | * |
| 325 | * @since 3.0.0 |
| 326 | * |
| 327 | * @return string |
| 328 | */ |
| 329 | public function get_nickname() { |
| 330 | |
| 331 | return isset( $this->data['nickname'] ) ? $this->data['nickname'] : ''; |
| 332 | } |
| 333 | |
| 334 | |
| 335 | /** |
| 336 | * Sets the payment method nickname. |
| 337 | * |
| 338 | * @since 3.0.0 |
| 339 | * |
| 340 | * @param string $value nickname value |
| 341 | */ |
| 342 | public function set_nickname( $value ) { |
| 343 | |
| 344 | $this->data['nickname'] = $value; |
| 345 | } |
| 346 | |
| 347 | |
| 348 | /** |
| 349 | * Gets the billing address hash. |
| 350 | * |
| 351 | * @since 3.0.0 |
| 352 | * |
| 353 | * @return string |
| 354 | */ |
| 355 | public function get_billing_hash() { |
| 356 | |
| 357 | return isset( $this->data['billing_hash'] ) ? $this->data['billing_hash'] : ''; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | /** |
| 362 | * Sets the billing hash. |
| 363 | * |
| 364 | * @since 3.0.0 |
| 365 | * |
| 366 | * @param string $value billing hash |
| 367 | */ |
| 368 | public function set_billing_hash( $value = '' ) { |
| 369 | |
| 370 | $this->data['billing_hash'] = $value; |
| 371 | } |
| 372 | |
| 373 | |
| 374 | /** |
| 375 | * Returns a representation of this token suitable for persisting to a |
| 376 | * datastore |
| 377 | * |
| 378 | * @since 3.0.0 |
| 379 | * @return mixed datastore representation of token |
| 380 | */ |
| 381 | public function to_datastore_format() { |
| 382 | |
| 383 | return $this->data; |
| 384 | } |
| 385 | } |
| 386 |