-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathPaymentMethodStorage.php
More file actions
144 lines (128 loc) · 4.86 KB
/
PaymentMethodStorage.php
File metadata and controls
144 lines (128 loc) · 4.86 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
<?php
namespace Drupal\commerce_payment;
use Drupal\commerce\CommerceContentEntityStorage;
use Drupal\commerce_payment\Entity\PaymentGatewayInterface;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\SupportsStoredPaymentMethodsInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Defines the payment method storage.
*/
class PaymentMethodStorage extends CommerceContentEntityStorage implements PaymentMethodStorageInterface {
/**
* The time.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* Constructs a new PaymentMethodStorage object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Database\Connection $database
* The database connection to be used.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* The cache backend to be used.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time.
*/
public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, EventDispatcherInterface $event_dispatcher, TimeInterface $time) {
parent::__construct($entity_type, $database, $entity_manager, $cache, $language_manager, $event_dispatcher);
$this->time = $time;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('database'),
$container->get('entity.manager'),
$container->get('cache.entity'),
$container->get('language_manager'),
$container->get('event_dispatcher'),
$container->get('datetime.time')
);
}
/**
* {@inheritdoc}
*/
public function loadReusable(UserInterface $account, PaymentGatewayInterface $payment_gateway, array $billing_countries = []) {
// Anonymous users cannot have reusable payment methods.
if ($account->isAnonymous()) {
return [];
}
if (!($payment_gateway->getPlugin() instanceof SupportsStoredPaymentMethodsInterface)) {
return [];
}
$query = $this->getQuery();
$query
->condition('uid', $account->id())
->condition('payment_gateway', $payment_gateway->id())
->condition('reusable', TRUE)
->condition($query->orConditionGroup()
->condition('expires', $this->time->getRequestTime(), '>')
->condition('expires', 0))
->sort('created', 'DESC');
$result = $query->execute();
if (empty($result)) {
return [];
}
/** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface[] $payment_methods */
$payment_methods = $this->loadMultiple($result);
if (!empty($billing_countries)) {
// Filter out payment methods that don't match the billing countries.
// Payment methods without a billing profile should also be filtered out.
// @todo Use a query condition once #2822359 is fixed.
foreach ($payment_methods as $id => $payment_method) {
$country_code = 'ZZ';
if ($billing_profile = $payment_method->getBillingProfile()) {
$country_code = $billing_profile->address->first()->getCountryCode();
}
if (!in_array($country_code, $billing_countries)) {
unset($payment_methods[$id]);
}
}
}
return $payment_methods;
}
/**
* {@inheritdoc}
*/
protected function doCreate(array $values) {
if (!isset($values['payment_gateway'])) {
throw new EntityStorageException('Missing "payment_gateway" property when creating a payment method.');
}
return parent::doCreate($values);
}
/**
* {@inheritdoc}
*/
public function loadByPaymentGateway(PaymentGatewayInterface $payment_gateway) {
if (!($payment_gateway->getPlugin() instanceof SupportsStoredPaymentMethodsInterface)) {
return [];
}
$query = $this->getQuery()
->condition('payment_gateway', $payment_gateway->id());
$result = $query->execute();
if (empty($result)) {
return [];
}
return $this->loadMultiple($result);
}
}