-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathPayment.php
More file actions
417 lines (367 loc) · 11.4 KB
/
Payment.php
File metadata and controls
417 lines (367 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
105
106
107
108
109
110
111
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
namespace Drupal\commerce_payment\Entity;
use Drupal\commerce_price\Price;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Defines the payment entity class.
*
* @ContentEntityType(
* id = "commerce_payment",
* label = @Translation("Payment"),
* label_collection = @Translation("Payments"),
* label_singular = @Translation("payment"),
* label_plural = @Translation("payments"),
* label_count = @PluralTranslation(
* singular = "@count payment",
* plural = "@count payments",
* ),
* bundle_label = @Translation("Payment type"),
* bundle_plugin_type = "commerce_payment_type",
* handlers = {
* "access" = "Drupal\commerce_payment\PaymentAccessControlHandler",
* "event" = "Drupal\commerce_payment\Event\PaymentEvent",
* "list_builder" = "Drupal\commerce_payment\PaymentListBuilder",
* "storage" = "Drupal\commerce_payment\PaymentStorage",
* "form" = {
* "operation" = "Drupal\commerce_payment\Form\PaymentOperationForm",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
* },
* "views_data" = "Drupal\views\EntityViewsData",
* "route_provider" = {
* "default" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
* },
* },
* base_table = "commerce_payment",
* admin_permission = "administer commerce_payment",
* fieldable = TRUE,
* entity_keys = {
* "id" = "payment_id",
* "bundle" = "type",
* "uuid" = "uuid",
* },
* links = {
* "collection" = "/admin/commerce/orders/{commerce_order}/payments",
* "edit-form" = "/admin/commerce/orders/{commerce_order}/payments/commerce_payment/edit",
* "delete-form" = "/admin/commerce/orders/{commerce_order}/payments/{commerce_payment}/delete",
* },
* )
*/
class Payment extends ContentEntityBase implements PaymentInterface {
/**
* {@inheritdoc}
*/
protected function urlRouteParameters($rel) {
$uri_route_parameters = parent::urlRouteParameters($rel);
$uri_route_parameters['commerce_order'] = $this->getOrderId();
return $uri_route_parameters;
}
/**
* {@inheritdoc}
*/
public function label() {
// UIs should use the number formatter to show a more user-readable version.
return $this->getAmount()->__toString();
}
/**
* {@inheritdoc}
*/
public function getType() {
$payment_type_manager = \Drupal::service('plugin.manager.commerce_payment_type');
return $payment_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;
}
/**
* {@inheritdoc}
*/
public function getPaymentGatewayMode() {
return $this->get('payment_gateway_mode')->value;
}
/**
* {@inheritdoc}
*/
public function getPaymentMethod() {
return $this->get('payment_method')->entity;
}
/**
* {@inheritdoc}
*/
public function getPaymentMethodId() {
return $this->get('payment_method')->target_id;
}
/**
* {@inheritdoc}
*/
public function getOrder() {
return $this->get('order_id')->entity;
}
/**
* {@inheritdoc}
*/
public function getOrderId() {
return $this->get('order_id')->target_id;
}
/**
* {@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 getRemoteState() {
return $this->get('remote_state')->value;
}
/**
* {@inheritdoc}
*/
public function setRemoteState($remote_state) {
$this->set('remote_state', $remote_state);
return $this;
}
/**
* {@inheritdoc}
*/
public function getBalance() {
if ($amount = $this->getAmount()) {
$refunded_amount = $this->getRefundedAmount();
return $amount->subtract($refunded_amount);
}
}
/**
* {@inheritdoc}
*/
public function getAmount() {
if (!$this->get('amount')->isEmpty()) {
return $this->get('amount')->first()->toPrice();
}
}
/**
* {@inheritdoc}
*/
public function setAmount(Price $amount) {
$this->set('amount', $amount);
return $this;
}
/**
* {@inheritdoc}
*/
public function getRefundedAmount() {
if (!$this->get('refunded_amount')->isEmpty()) {
return $this->get('refunded_amount')->first()->toPrice();
}
}
/**
* {@inheritdoc}
*/
public function setRefundedAmount(Price $refunded_amount) {
$this->set('refunded_amount', $refunded_amount);
return $this;
}
/**
* {@inheritdoc}
*/
public function getState() {
return $this->get('state')->first();
}
/**
* {@inheritdoc}
*/
public function setState($state_id) {
$this->set('state', $state_id);
return $this;
}
/**
* {@inheritdoc}
*/
public function getAuthorizedTime() {
return $this->get('authorized')->value;
}
/**
* {@inheritdoc}
*/
public function setAuthorizedTime($timestamp) {
$this->set('authorized', $timestamp);
return $this;
}
/**
* {@inheritdoc}
*/
public function isExpired() {
$expires = $this->getExpiresTime();
return $expires > 0 && $expires <= \Drupal::time()->getRequestTime();
}
/**
* {@inheritdoc}
*/
public function getExpiresTime() {
return $this->get('expires')->value;
}
/**
* {@inheritdoc}
*/
public function setExpiresTime($timestamp) {
$this->set('expires', $timestamp);
return $this;
}
/**
* {@inheritdoc}
*/
public function getCompletedTime() {
return $this->get('completed')->value;
}
/**
* {@inheritdoc}
*/
public function setCompletedTime($timestamp) {
$this->set('completed', $timestamp);
return $this;
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
$payment_gateway = $this->getPaymentGateway();
if (!$payment_gateway) {
throw new EntityMalformedException(sprintf('Required payment 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());
}
// Initialize the refunded amount.
$refunded_amount = $this->getRefundedAmount();
if (!$refunded_amount) {
$refunded_amount = new Price('0', $this->getAmount()->getCurrencyCode());
$this->setRefundedAmount($refunded_amount);
}
// Maintain the authorized completed timestamps.
$state = $this->getState()->value;
$original_state = isset($this->original) ? $this->original->getState()->value : '';
if ($state == 'authorized' && $original_state != 'authorized') {
if (empty($this->getAuthorizedTime())) {
$this->setAuthorizedTime(\Drupal::time()->getRequestTime());
}
}
if ($state == 'completed' && $original_state != 'completed') {
if (empty($this->getCompletedTime())) {
$this->setCompletedTime(\Drupal::time()->getRequestTime());
}
}
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['payment_gateway'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Payment gateway'))
->setDescription(t('The payment gateway.'))
->setRequired(TRUE)
->setSetting('target_type', 'commerce_payment_gateway');
$fields['payment_gateway_mode'] = BaseFieldDefinition::create('string')
->setLabel(t('Payment gateway mode'))
->setDescription(t('The payment gateway mode.'))
->setRequired(TRUE);
$fields['payment_method'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Payment method'))
->setDescription(t('The payment method.'))
->setSetting('target_type', 'commerce_payment_method')
->setReadOnly(TRUE);
$fields['order_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Order'))
->setDescription(t('The parent order.'))
->setSetting('target_type', 'commerce_order')
->setReadOnly(TRUE);
$fields['remote_id'] = BaseFieldDefinition::create('string')
->setLabel(t('Remote ID'))
->setDescription(t('The remote payment ID.'))
->setSetting('max_length', 255)
->setDisplayConfigurable('view', TRUE);
$fields['remote_state'] = BaseFieldDefinition::create('string')
->setLabel(t('Remote State'))
->setDescription(t('The remote payment state.'))
->setSetting('max_length', 255)
->setDisplayConfigurable('view', TRUE);
$fields['amount'] = BaseFieldDefinition::create('commerce_price')
->setLabel(t('Amount'))
->setDescription(t('The payment amount.'))
->setRequired(TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['refunded_amount'] = BaseFieldDefinition::create('commerce_price')
->setLabel(t('Refunded amount'))
->setDescription(t('The refunded payment amount.'))
->setDisplayConfigurable('view', TRUE);
$fields['state'] = BaseFieldDefinition::create('state')
->setLabel(t('State'))
->setDescription(t('The payment state.'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'list_default',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE)
->setSetting('workflow_callback', ['\Drupal\commerce_payment\Entity\Payment', 'getWorkflowId']);
$fields['authorized'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Authorized'))
->setDescription(t('The time when the payment was authorized.'))
->setDisplayConfigurable('view', TRUE);
$fields['expires'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Expires'))
->setDescription(t('The time when the payment expires. 0 for never.'))
->setDisplayConfigurable('view', TRUE)
->setDefaultValue(0);
$fields['completed'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Completed'))
->setDescription(t('The time when the payment was completed.'))
->setDisplayConfigurable('view', TRUE);
// These fields have been replaced by payment_gateway_mode and completed.
// They have been temporarily kept for commerce_payment_post_update_2().
// They are no longer used and will be removed in Commerce 2.0.
$fields['test'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Test'))
->setDescription(t('Whether this is a test payment.'));
$fields['captured'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Captured'))
->setDescription(t('The time when the payment was captured.'))
->setDisplayConfigurable('view', TRUE);
return $fields;
}
/**
* Gets the workflow ID for the state field.
*
* @param \Drupal\commerce_payment\Entity\PaymentInterface $payment
* The payment.
*
* @return string
* The workflow ID.
*/
public static function getWorkflowId(PaymentInterface $payment) {
return $payment->getType()->getWorkflowId();
}
}