woocommerce/includes/abstracts/abstract-wc-payment-token.php

234 lines
5.5 KiB
PHP
Raw Normal View History

2016-02-02 17:08:32 +00:00
<?php
2016-11-14 14:20:41 +00:00
include_once( 'abstract-wc-legacy-payment-token.php' );
2016-02-02 17:08:32 +00:00
if ( ! defined( 'ABSPATH' ) ) {
2016-11-14 14:20:41 +00:00
exit;
2016-02-02 17:08:32 +00:00
}
/**
* WooCommerce Payment Token.
2016-02-02 17:08:32 +00:00
*
* Representation of a general payment token to be extended by individuals types of tokens
* examples: Credit Card, eCheck.
*
* @class WC_Payment_Token
2016-11-14 14:20:41 +00:00
* @version 2.7.0
2016-02-02 17:08:32 +00:00
* @since 2.6.0
* @package WooCommerce/Abstracts
* @category Abstract Class
* @author WooThemes
*/
2016-11-14 14:20:41 +00:00
abstract class WC_Payment_Token extends WC_Legacy_Payment_Token {
2016-02-02 17:08:32 +00:00
2016-06-06 19:04:45 +00:00
/**
* Token Data (stored in the payment_tokens table).
* @var array
*/
protected $data = array(
2016-06-06 19:04:45 +00:00
'gateway_id' => '',
'token' => '',
2016-11-14 14:20:41 +00:00
'is_default' => false,
2016-06-06 19:04:45 +00:00
'user_id' => 0,
2016-11-14 14:20:41 +00:00
'type' => '',
2016-06-06 19:04:45 +00:00
);
2016-11-14 14:20:41 +00:00
/**
* Initialize a payment token.
*
* These fields are accepted by all payment tokens:
* is_default - boolean Optional - Indicates this is the default payment token for a user
* token - string Required - The actual token to store
* gateway_id - string Required - Identifier for the gateway this token is associated with
* user_id - int Optional - ID for the user this token is associated with. 0 if this token is not associated with a user
*
* @since 2.6.0
* @param mixed $token
*/
public function __construct( $token = '' ) {
2016-11-14 14:20:41 +00:00
// Set token type (cc, echeck)
if ( ! empty( $this->type ) ) {
2016-11-14 17:08:46 +00:00
$this->set_type( $this->type );
2016-11-14 14:20:41 +00:00
}
if ( is_numeric( $token ) ) {
2016-11-14 14:20:41 +00:00
$this->set_id( $token );
} elseif ( is_object( $token ) ) {
$token_id = $token->get_id();
if ( ! empty( $token_id ) ) {
2016-11-14 14:20:41 +00:00
$this->set_id( $token->get_id() );
}
2016-11-14 14:20:41 +00:00
} else {
$this->set_object_read( true );
}
2016-11-14 14:20:41 +00:00
$this->data_store = WC_Data_Store::load( 'payment-token' );
if ( $this->get_id() > 0 ) {
$this->data_store->read( $this );
}
2016-02-02 17:08:32 +00:00
}
2016-11-14 14:20:41 +00:00
/*
|--------------------------------------------------------------------------
| Getters
|--------------------------------------------------------------------------
2016-02-02 17:08:32 +00:00
*/
/**
2016-11-14 14:20:41 +00:00
* Returns the raw payment token.
*
* @since 2.6.0
* @param string $context
* @return string Raw token
2016-02-02 17:08:32 +00:00
*/
2016-11-14 14:20:41 +00:00
public function get_token( $context = 'view' ) {
return $this->get_prop( 'token', $context );
2016-02-02 17:08:32 +00:00
}
/**
* Returns the type of this payment token (CC, eCheck, or something else).
2016-11-14 14:20:41 +00:00
*
* @since 2.6.0
* @param string $context
* @return string Payment Token Type (CC, eCheck)
2016-02-02 17:08:32 +00:00
*/
2016-11-14 14:20:41 +00:00
public function get_type( $context = 'view' ) {
return $this->get_prop( 'type', $context );
2016-02-02 17:08:32 +00:00
}
2016-05-26 10:55:11 +00:00
/**
* Get type to display to user.
2016-11-14 14:20:41 +00:00
* Get's overwritten by child classes.
*
* @since 2.6.0
* @param string $context
2016-05-26 10:55:11 +00:00
* @return string
*/
2016-11-14 14:20:41 +00:00
public function get_display_name( $context = 'view' ) {
return $this->get_type( $context );
2016-05-26 10:55:11 +00:00
}
2016-02-02 17:08:32 +00:00
/**
* Returns the user ID associated with the token or false if this token is not associated.
2016-11-14 14:20:41 +00:00
*
2016-02-02 17:08:32 +00:00
* @since 2.6.0
2016-11-14 14:20:41 +00:00
* @param string $context
* @return int User ID if this token is associated with a user or 0 if no user is associated
2016-02-02 17:08:32 +00:00
*/
2016-11-14 14:20:41 +00:00
public function get_user_id( $context = 'view' ) {
return $this->get_prop( 'user_id', $context );
2016-02-02 17:08:32 +00:00
}
/**
2016-11-14 14:20:41 +00:00
* Returns the ID of the gateway associated with this payment token.
*
2016-02-02 17:08:32 +00:00
* @since 2.6.0
2016-11-14 14:20:41 +00:00
* @param string $context
* @return string Gateway ID
2016-02-02 17:08:32 +00:00
*/
2016-11-14 14:20:41 +00:00
public function get_gateway_id( $context = 'view' ) {
return $this->get_prop( 'gateway_id', $context );
2016-02-02 17:08:32 +00:00
}
/**
* Returns the ID of the gateway associated with this payment token.
2016-11-14 14:20:41 +00:00
*
2016-02-02 17:08:32 +00:00
* @since 2.6.0
2016-11-14 14:20:41 +00:00
* @param string $context
2016-02-02 17:08:32 +00:00
* @return string Gateway ID
*/
2016-11-14 14:20:41 +00:00
public function get_is_default( $context = 'view' ) {
return $this->get_prop( 'is_default', $context );
2016-02-02 17:08:32 +00:00
}
2016-11-14 14:20:41 +00:00
/*
|--------------------------------------------------------------------------
| Setters
|--------------------------------------------------------------------------
*/
2016-02-02 17:08:32 +00:00
/**
2016-11-14 14:20:41 +00:00
* Set the raw payment token.
*
2016-02-02 17:08:32 +00:00
* @since 2.6.0
2016-11-14 14:20:41 +00:00
* @param string $token
2016-02-02 17:08:32 +00:00
*/
2016-11-14 14:20:41 +00:00
public function set_token( $token ) {
$this->set_prop( 'token', $token );
2016-02-02 17:08:32 +00:00
}
/**
2016-11-14 14:20:41 +00:00
* Sets the type of this payment token (CC, eCheck, or something else).
*
* @since 2.7.0
* @param string Payment Token Type (CC, eCheck)
2016-02-02 17:08:32 +00:00
*/
2016-11-14 14:20:41 +00:00
public function set_type( $type ) {
2016-11-14 17:08:46 +00:00
return $this->set_prop( 'type', $type );
2016-02-02 17:08:32 +00:00
}
/**
2016-11-14 14:20:41 +00:00
* Set the user ID for the user associated with this order.
*
2016-02-02 17:08:32 +00:00
* @since 2.6.0
2016-11-14 14:20:41 +00:00
* @param int $user_id
2016-02-02 17:08:32 +00:00
*/
2016-11-14 14:20:41 +00:00
public function set_user_id( $user_id ) {
$this->set_prop( 'user_id', absint( $user_id ) );
2016-02-02 17:08:32 +00:00
}
/**
2016-11-14 14:20:41 +00:00
* Set the gateway ID.
*
* @since 2.6.0
2016-11-14 14:20:41 +00:00
* @param string $gateway_id
*/
2016-11-14 14:20:41 +00:00
public function set_gateway_id( $gateway_id ) {
2016-11-17 20:07:58 +00:00
$this->set_prop( 'gateway_id', $gateway_id );
}
/**
2016-11-14 14:20:41 +00:00
* Marks the payment as default or non-default.
* @since 2.6.0
2016-11-14 14:20:41 +00:00
* @param boolean $is_default True or false
*/
2016-11-14 14:20:41 +00:00
public function set_default( $is_default ) {
$this->set_prop( 'is_default', (bool) $is_default );
}
2016-11-14 14:20:41 +00:00
/*
|--------------------------------------------------------------------------
| Other Methods
|--------------------------------------------------------------------------
*/
/**
2016-11-14 14:20:41 +00:00
* Returns if the token is marked as default.
*
* @since 2.6.0
2016-11-14 14:20:41 +00:00
* @return boolean True if the token is default
*/
2016-11-14 14:20:41 +00:00
public function is_default() {
return (bool) $this->get_prop( 'is_default', 'view' );
}
/**
2016-11-14 14:20:41 +00:00
* Validate basic token info (token and type are required).
* @since 2.6.0
2016-11-14 14:20:41 +00:00
* @return boolean True if the passed data is valid
*/
2016-11-14 14:20:41 +00:00
public function validate() {
$token = $this->get_prop( 'token', 'edit' );
if ( empty( $token ) ) {
return false;
}
2016-11-14 14:20:41 +00:00
$type = $this->get_prop( 'type', 'edit' );
if ( empty( $type ) ) {
return false;
}
return true;
}
2016-02-02 17:08:32 +00:00
}