PaymentMethod.php 8.93 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\commerce_payment\Entity;

5
use Drupal\commerce\EntityOwnerTrait;
6 7
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
8 9
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityStorageInterface;
10 11 12 13 14 15 16 17 18 19
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\profile\Entity\ProfileInterface;

/**
 * Defines the payment method entity class.
 *
 * @ContentEntityType(
 *   id = "commerce_payment_method",
 *   label = @Translation("Payment method"),
20
 *   label_collection = @Translation("Payment methods"),
21 22
 *   label_singular = @Translation("payment method"),
 *   label_plural = @Translation("payment methods"),
23 24 25 26 27 28 29
 *   label_count = @PluralTranslation(
 *     singular = "@count payment method",
 *     plural = "@count payment methods",
 *   ),
 *   bundle_label = @Translation("Payment method type"),
 *   bundle_plugin_type = "commerce_payment_method_type",
 *   handlers = {
30
 *     "access" = "Drupal\commerce_payment\PaymentMethodAccessControlHandler",
31
 *     "list_builder" = "Drupal\commerce_payment\PaymentMethodListBuilder",
32
 *     "storage" = "Drupal\commerce_payment\PaymentMethodStorage",
33
 *     "views_data" = "Drupal\commerce\CommerceEntityViewsData",
34
 *     "storage_schema" = "Drupal\commerce\CommerceContentEntityStorageSchema",
35 36 37 38 39 40 41
 *     "form" = {
 *       "edit" = "Drupal\commerce_payment\Form\PaymentMethodEditForm",
 *       "delete" = "Drupal\commerce_payment\Form\PaymentMethodDeleteForm"
 *     },
 *     "route_provider" = {
 *       "default" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
 *     },
42 43
 *   },
 *   base_table = "commerce_payment_method",
44
 *   admin_permission = "administer commerce_payment_method",
45 46 47
 *   field_indexes = {
 *     "remote_id"
 *   },
48 49 50
 *   entity_keys = {
 *     "id" = "method_id",
 *     "uuid" = "uuid",
51 52 53
 *     "bundle" = "type",
 *     "owner" = "uid",
 *     "uid" = "uid",
54
 *   },
55 56 57 58 59
 *   links = {
 *     "collection" = "/user/{user}/payment-methods",
 *     "edit-form" = "/user/{user}/payment-methods/{commerce_payment_method}/edit",
 *     "delete-form" = "/user/{user}/payment-methods/{commerce_payment_method}/delete",
 *   },
60 61 62 63 64
 * )
 */
class PaymentMethod extends ContentEntityBase implements PaymentMethodInterface {

  use EntityChangedTrait;
65
  use EntityOwnerTrait;
66

67 68 69 70 71 72 73 74 75
  /**
   * {@inheritdoc}
   */
  protected function urlRouteParameters($rel) {
    $uri_route_parameters = parent::urlRouteParameters($rel);
    $uri_route_parameters['user'] = $this->getOwnerId();
    return $uri_route_parameters;
  }

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  /**
   * {@inheritdoc}
   */
  public function label() {
    return $this->getType()->buildLabel($this);
  }

  /**
   * {@inheritdoc}
   */
  public function getType() {
    $payment_method_type_manager = \Drupal::service('plugin.manager.commerce_payment_method_type');
    return $payment_method_type_manager->createInstance($this->bundle());
  }

  /**
   * {@inheritdoc}
   */
  public function getPaymentGateway() {
    return $this->get('payment_gateway')->entity;
  }

  /**
   * {@inheritdoc}
   */
  public function getPaymentGatewayId() {
    return $this->get('payment_gateway')->target_id;
  }

105 106 107 108 109 110 111
  /**
   * {@inheritdoc}
   */
  public function getPaymentGatewayMode() {
    return $this->get('payment_gateway_mode')->value;
  }

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  /**
   * {@inheritdoc}
   */
  public function getRemoteId() {
    return $this->get('remote_id')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setRemoteId($remote_id) {
    $this->set('remote_id', $remote_id);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getBillingProfile() {
    return $this->get('billing_profile')->entity;
  }

  /**
   * {@inheritdoc}
   */
  public function setBillingProfile(ProfileInterface $profile) {
138
    $this->set('billing_profile', $profile);
139 140 141 142 143 144 145
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function isReusable() {
146
    return (bool) $this->get('reusable')->value;
147 148 149 150 151 152 153 154 155 156 157 158 159 160
  }

  /**
   * {@inheritdoc}
   */
  public function setReusable($reusable) {
    $this->set('reusable', $reusable);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function isDefault() {
161
    return (bool) $this->get('is_default')->value;
162 163 164 165 166 167 168 169 170 171
  }

  /**
   * {@inheritdoc}
   */
  public function setDefault($default) {
    $this->set('is_default', $default);
    return $this;
  }

172 173 174 175
  /**
   * {@inheritdoc}
   */
  public function isExpired() {
176
    $expires = $this->getExpiresTime();
177
    return $expires > 0 && $expires <= \Drupal::time()->getRequestTime();
178 179
  }

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
  /**
   * {@inheritdoc}
   */
  public function getExpiresTime() {
    return $this->get('expires')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setExpiresTime($timestamp) {
    $this->set('expires', $timestamp);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getCreatedTime() {
    return $this->get('created')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setCreatedTime($timestamp) {
    $this->set('created', $timestamp);
    return $this;
  }

210 211 212 213 214 215 216 217 218 219 220 221 222 223
  /**
   * {@inheritdoc}
   */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);

    $payment_gateway = $this->getPaymentGateway();
    if (!$payment_gateway) {
      throw new EntityMalformedException(sprintf('Required payment method field "payment_gateway" is empty.'));
    }
    // Populate the payment_gateway_mode automatically.
    if ($this->get('payment_gateway_mode')->isEmpty()) {
      $this->set('payment_gateway_mode', $payment_gateway->getPlugin()->getMode());
    }
224 225 226 227 228 229
    // Make sure the billing profile is owned by the payment method.
    $billing_profile = $this->getBillingProfile();
    if ($billing_profile && $billing_profile->getOwnerId()) {
      $billing_profile->setOwnerId(0);
      $billing_profile->save();
    }
230 231
  }

232 233 234 235 236
  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);
237
    $fields += static::ownerBaseFieldDefinitions($entity_type);
238 239 240 241 242 243 244

    $fields['payment_gateway'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Payment gateway'))
      ->setDescription(t('The payment gateway.'))
      ->setRequired(TRUE)
      ->setSetting('target_type', 'commerce_payment_gateway');

245 246 247 248 249
    $fields['payment_gateway_mode'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Payment gateway mode'))
      ->setDescription(t('The payment gateway mode.'))
      ->setRequired(TRUE);

250
    $fields['uid']
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
      ->setLabel(t('Owner'))
      ->setDescription(t('The payment method owner.'))
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'author',
        'weight' => 0,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['remote_id'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Remote ID'))
      ->setDescription(t('The payment method remote ID.'))
      ->setSetting('max_length', 255)
      ->setDisplayConfigurable('view', TRUE);

266
    $fields['billing_profile'] = BaseFieldDefinition::create('entity_reference_revisions')
267 268 269 270
      ->setLabel(t('Billing profile'))
      ->setDescription(t('Billing profile'))
      ->setSetting('target_type', 'profile')
      ->setSetting('handler', 'default')
271
      ->setSetting('handler_settings', ['target_bundles' => ['customer' => 'customer']])
272 273 274 275 276
      ->setDisplayOptions('form', [
        'type' => 'options_select',
        'weight' => 0,
        'settings' => [],
      ])
277 278
      ->setDisplayOptions('view', [
        'label' => 'hidden',
279
        'type' => 'commerce_payment_method_profile',
280 281
        'weight' => 2,
      ])
282 283 284 285 286 287 288 289 290 291 292
      ->setDisplayConfigurable('view', TRUE);

    $fields['reusable'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Reusable'))
      ->setDescription(t('Whether the payment method is reusable.'))
      ->setDefaultValue(TRUE)
      ->setDisplayConfigurable('view', TRUE);

    // 'default' is a reserved SQL word, hence the 'is_' prefix.
    $fields['is_default'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Default'))
293 294
      ->setDescription(t("Whether this is the user's default payment method."))
      ->setDefaultValue(FALSE);
295 296 297

    $fields['expires'] = BaseFieldDefinition::create('timestamp')
      ->setLabel(t('Expires'))
298
      ->setDescription(t('The time when the payment method expires. 0 for never.'))
299 300 301 302 303 304 305 306 307
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'timestamp',
        'weight' => 1,
        'settings' => [
          'date_format' => 'custom',
          'custom_date_format' => 'n/Y',
        ],
      ])
308 309
      ->setDisplayConfigurable('view', TRUE)
      ->setDefaultValue(0);
310 311 312

    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Created'))
313
      ->setDescription(t('The time when the payment method was created.'));
314 315 316

    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
317
      ->setDescription(t('The time when the payment method was last edited.'));
318 319 320 321 322

    return $fields;
  }

}