PaymentMethod.php 9.02 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
 *     "permission_provider" = "Drupal\commerce_payment\PaymentMethodPermissionProvider",
32
 *     "list_builder" = "Drupal\commerce_payment\PaymentMethodListBuilder",
33
 *     "storage" = "Drupal\commerce_payment\PaymentMethodStorage",
34
 *     "views_data" = "Drupal\commerce\CommerceEntityViewsData",
35
 *     "storage_schema" = "Drupal\commerce\CommerceContentEntityStorageSchema",
36
37
38
39
40
41
42
 *     "form" = {
 *       "edit" = "Drupal\commerce_payment\Form\PaymentMethodEditForm",
 *       "delete" = "Drupal\commerce_payment\Form\PaymentMethodDeleteForm"
 *     },
 *     "route_provider" = {
 *       "default" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
 *     },
43
44
 *   },
 *   base_table = "commerce_payment_method",
45
 *   admin_permission = "administer commerce_payment_method",
46
47
48
 *   field_indexes = {
 *     "remote_id"
 *   },
49
50
51
 *   entity_keys = {
 *     "id" = "method_id",
 *     "uuid" = "uuid",
52
53
54
 *     "bundle" = "type",
 *     "owner" = "uid",
 *     "uid" = "uid",
55
 *   },
56
57
58
59
60
 *   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",
 *   },
61
62
63
64
65
 * )
 */
class PaymentMethod extends ContentEntityBase implements PaymentMethodInterface {

  use EntityChangedTrait;
66
  use EntityOwnerTrait;
67

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

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
105
  /**
   * {@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;
  }

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

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
138
  /**
   * {@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) {
139
    $this->set('billing_profile', $profile);
140
141
142
143
144
145
146
    return $this;
  }

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

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

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

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

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

211
212
213
214
215
216
217
218
219
220
221
222
223
224
  /**
   * {@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());
    }
225
226
227
228
229
230
    // 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();
    }
231
232
  }

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

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

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

251
    $fields['uid']
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
      ->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);

267
    $fields['billing_profile'] = BaseFieldDefinition::create('entity_reference_revisions')
268
269
270
271
      ->setLabel(t('Billing profile'))
      ->setDescription(t('Billing profile'))
      ->setSetting('target_type', 'profile')
      ->setSetting('handler', 'default')
272
      ->setSetting('handler_settings', ['target_bundles' => ['customer' => 'customer']])
273
274
275
276
277
      ->setDisplayOptions('form', [
        'type' => 'options_select',
        'weight' => 0,
        'settings' => [],
      ])
278
279
      ->setDisplayOptions('view', [
        'label' => 'hidden',
280
        'type' => 'commerce_payment_method_profile',
281
282
        'weight' => 2,
      ])
283
284
285
286
287
288
289
290
291
292
293
      ->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'))
294
295
      ->setDescription(t("Whether this is the user's default payment method."))
      ->setDefaultValue(FALSE);
296
297
298

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

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

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

    return $fields;
  }

}