public function PaymentMethodStorage::loadReusable in Commerce Core 8.2
Loads the user's reusable payment methods for the given payment gateway.
Parameters
\Drupal\user\UserInterface $account: The user account.
\Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway: The payment gateway.
array $billing_countries: (Optional) A list of billing countries to filter by. For example, if ['US', 'FR'] is given, only payment methods with billing profiles from those countries will be returned. Filtering is skipped if the payment gateway doesn't collect billing information.
Return value
\Drupal\commerce_payment\Entity\PaymentMethodInterface[] The reusable payment methods.
Overrides PaymentMethodStorageInterface::loadReusable
File
- modules/
payment/ src/ PaymentMethodStorage.php, line 85
Class
- PaymentMethodStorage
- Defines the payment method storage.
Namespace
Drupal\commerce_paymentCode
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('payment_gateway_mode', $payment_gateway
->getPlugin()
->getMode())
->condition('reusable', TRUE)
->condition($query
->orConditionGroup()
->condition('expires', $this->time
->getRequestTime(), '>')
->condition('expires', 0))
->accessCheck(FALSE)
->sort('method_id', '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) && $payment_gateway
->getPlugin()
->collectsBillingInformation()) {
// Filter out payment methods that don't match the billing countries.
// @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;
}