You are here

class CommerceCardOnFile in Commerce Card on File 7.2

Entity class representing the commerce_cardonfile entity type.

Hierarchy

Expanded class hierarchy of CommerceCardOnFile

1 string reference to 'CommerceCardOnFile'
commerce_cardonfile_entity_info in ./commerce_cardonfile.module
Implements of hook_entity_info().

File

includes/commerce_cardonfile.entity.inc, line 6

View source
class CommerceCardOnFile extends Entity {

  /**
   * The card id.
   *
   * @var integer
   */
  public $card_id;

  /**
   * The uid of the card owner.
   *
   * @var integer
   */
  public $uid;

  /**
   * The method_id of the payment method that stored the card.
   *
   * @var string
   */
  public $payment_method;

  /**
   * The instance_id of the payment method that stored the card.
   *
   * @var string
   */
  public $instance_id;

  /**
   * The id of the card at the payment gateway.
   *
   * @var string
   */
  public $remote_id;

  /**
   * The card type.
   *
   * @var string
   */
  public $card_type;

  /**
   * The name on the card.
   *
   * @var string
   */
  public $card_name;

  /**
   * Truncated card number (last 4 digits).
   *
   * @var string
   */
  public $card_number;

  /**
   * Expiration month.
   *
   * @var integer
   */
  public $card_exp_month;

  /**
   * Expiration year.
   *
   * @var integer
   */
  public $card_exp_year;

  /**
   * Whether this is the default card for this payment method instance..
   *
   * @var boolean
   */
  public $instance_default = 1;

  /**
   * Card status: inactive (0), active (1), not deletable (2), declined (3).
   *
   * @var integer
   */
  public $status = 1;

  /**
   * The Unix timestamp when the card was first stored.
   *
   * @var integer
   */
  public $created;

  /**
   * The Unix timestamp when the card was last updated.
   *
   * @var integer
   */
  public $changed;
  public function __construct($values = array()) {
    parent::__construct($values, 'commerce_cardonfile');
  }

  /**
   * Overrides Entity::defaultLabel().
   */
  protected function defaultLabel() {
    $card_type = t('Card');
    if (!empty($this->card_type)) {
      $card_types = commerce_cardonfile_credit_card_types();
      if (isset($card_types[$this->card_type])) {
        $card_type = $card_types[$this->card_type];
      }
    }
    $args = array(
      '@card_type' => $card_type,
      '@card_number' => $this->card_number,
    );
    return t('@card_type ending in @card_number', $args);
  }

  /**
   * Overrides Entity::save().
   */
  public function save() {
    $this->changed = REQUEST_TIME;

    // Set the created timestamp during initial save.
    if (!$this->card_id) {
      $this->created = REQUEST_TIME;
    }
    if ($this->card_id) {
      $this->original = $original = entity_load_unchanged('commerce_cardonfile', $this->card_id);

      // Reactivate a declined card after its expiration date has been modified.
      if ($this->status == 3) {
        $exp_month_changed = $this->original->card_exp_month != $this->card_exp_month;
        $exp_year_changed = $this->original->card_exp_year != $this->card_exp_year;
        if ($exp_month_changed || $exp_year_changed) {
          $this->status = 1;
        }
      }
    }

    // Perform the save.
    $save_result = parent::save();
    if ($save_result !== FALSE) {

      // We cannot consider operation as update when original card has uid 0.
      // If we would, one user would end up with multiple default cards.
      $is_update = isset($original) && $original->uid != 0;
      $value_changed = $is_update && $this->instance_default != $original->instance_default;

      // If the card is now instance_default, remove the flag from other cards.
      if ($this->instance_default && (!$is_update || $value_changed)) {
        $query = new EntityFieldQuery();
        $query
          ->entityCondition('entity_type', 'commerce_cardonfile');
        $query
          ->entityCondition('entity_id', $this->card_id, '<>');
        $query
          ->propertyCondition('instance_id', $this->instance_id);
        $query
          ->propertyCondition('uid', $this->uid);
        $query
          ->propertyCondition('instance_default', TRUE);
        $result = $query
          ->execute();
        if (isset($result['commerce_cardonfile'])) {
          $card_ids = array_keys($result['commerce_cardonfile']);
          $other_cards = commerce_cardonfile_load_multiple($card_ids);
          foreach ($other_cards as $other_card) {
            $other_card->instance_default = 0;
            commerce_cardonfile_save($other_card);
          }
        }
      }
    }
    return $save_result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceCardOnFile::$card_exp_month public property Expiration month.
CommerceCardOnFile::$card_exp_year public property Expiration year.
CommerceCardOnFile::$card_id public property The card id.
CommerceCardOnFile::$card_name public property The name on the card.
CommerceCardOnFile::$card_number public property Truncated card number (last 4 digits).
CommerceCardOnFile::$card_type public property The card type.
CommerceCardOnFile::$changed public property The Unix timestamp when the card was last updated.
CommerceCardOnFile::$created public property The Unix timestamp when the card was first stored.
CommerceCardOnFile::$instance_default public property Whether this is the default card for this payment method instance..
CommerceCardOnFile::$instance_id public property The instance_id of the payment method that stored the card.
CommerceCardOnFile::$payment_method public property The method_id of the payment method that stored the card.
CommerceCardOnFile::$remote_id public property The id of the card at the payment gateway.
CommerceCardOnFile::$status public property Card status: inactive (0), active (1), not deletable (2), declined (3).
CommerceCardOnFile::$uid public property The uid of the card owner.
CommerceCardOnFile::defaultLabel protected function Overrides Entity::defaultLabel(). Overrides Entity::defaultLabel
CommerceCardOnFile::save public function Overrides Entity::save(). Overrides Entity::save
CommerceCardOnFile::__construct public function Overrides Entity::__construct
Entity::$defaultLabel protected property 1
Entity::$entityInfo protected property
Entity::$entityType protected property
Entity::$idKey protected property
Entity::$wrapper protected property
Entity::buildContent public function Builds a structured array representing the entity's content. Overrides EntityInterface::buildContent 1
Entity::bundle public function Returns the bundle of the entity. Overrides EntityInterface::bundle
Entity::defaultUri protected function Override this in order to implement a custom default URI and specify 'entity_class_uri' as 'uri callback' hook_entity_info().
Entity::delete public function Permanently deletes the entity. Overrides EntityInterface::delete
Entity::entityInfo public function Returns the info of the type of the entity. Overrides EntityInterface::entityInfo
Entity::entityType public function Returns the type of the entity. Overrides EntityInterface::entityType
Entity::export public function Exports the entity. Overrides EntityInterface::export
Entity::getTranslation public function Gets the raw, translated value of a property or field. Overrides EntityInterface::getTranslation
Entity::hasStatus public function Checks if the entity has a certain exportable status. Overrides EntityInterface::hasStatus
Entity::identifier public function Returns the entity identifier, i.e. the entities name or numeric id. Overrides EntityInterface::identifier
Entity::internalIdentifier public function Returns the internal, numeric identifier. Overrides EntityInterface::internalIdentifier
Entity::isDefaultRevision public function Checks whether the entity is the default revision. Overrides EntityInterface::isDefaultRevision
Entity::label public function Returns the label of the entity. Overrides EntityInterface::label
Entity::setUp protected function Set up the object instance on construction or unserializiation.
Entity::uri public function Returns the uri of the entity just as entity_uri(). Overrides EntityInterface::uri
Entity::view public function Generate an array for rendering the entity. Overrides EntityInterface::view
Entity::wrapper public function Returns the EntityMetadataWrapper of the entity. Overrides EntityInterface::wrapper
Entity::__sleep public function Magic method to only serialize what's necessary.
Entity::__wakeup public function Magic method to invoke setUp() on unserialization.