Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #2898217 by tilenav: Delete payment methods when their payment gateway is deleted #818

Open
wants to merge 7 commits into
base: 8.x-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion modules/payment/src/Entity/PaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Drupal\commerce\ConditionGroup;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;

/**
* Defines the payment gateway entity class.
Expand Down Expand Up @@ -253,9 +254,43 @@ public function set($property_name, $value) {
protected function getPluginCollection() {
if (!$this->pluginCollection) {
$plugin_manager = \Drupal::service('plugin.manager.commerce_payment_gateway');
$this->pluginCollection = new CommerceSinglePluginCollection($plugin_manager, $this->plugin, $this->configuration, $this->id);
$this->pluginCollection = new CommerceSinglePluginCollection(
$plugin_manager,
$this->plugin,
$this->configuration,
$this->id
);
}
return $this->pluginCollection;
}

/**
* {@inheritdoc}
*/
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);

/** @var \Drupal\commerce_payment\PaymentMethodStorageInterface $payment_method_storage */
$payment_method_storage = \Drupal::service('entity_type.manager')
->getStorage('commerce_payment_method');

foreach ($entities as $payment_gateway) {
$payment_methods = $payment_method_storage->loadByPaymentGateway($payment_gateway);
if (empty($payment_methods)) {
continue;
}
if (count($payment_methods) < 50) {
// If there is less than 50 payment methods, we delete them straight away.
$payment_method_storage->delete($payment_methods);
}
else {
// Else we queue them for deletion.
$queue = \Drupal::queue('payment_methods_delete_queue');
foreach (array_chunk($payment_methods, 50) as $payment_methods_chunk) {
$queue->createItem($payment_methods_chunk);
}
}
}
}

}
16 changes: 16 additions & 0 deletions modules/payment/src/PaymentMethodStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,20 @@ protected function doCreate(array $values) {
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);
}

}
11 changes: 11 additions & 0 deletions modules/payment/src/PaymentMethodStorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,15 @@ interface PaymentMethodStorageInterface extends ContentEntityStorageInterface {
*/
public function loadReusable(UserInterface $account, PaymentGatewayInterface $payment_gateway, array $billing_countries = []);

/**
* Loads the payment methods for the given payment gateway.
*
* @param \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway
* The payment gateway.
*
* @return \Drupal\commerce_payment\Entity\PaymentMethodInterface[]
* The payment methods.
*/
public function loadByPaymentGateway(PaymentGatewayInterface $payment_gateway);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Drupal\commerce_payment\Plugin\QueueWorker;

use Drupal\Core\Queue\QueueWorkerBase;

/**
* Deletes payment methods in groups of 50.
*
* @QueueWorker(
* id = "payment_methods_delete_queue",
* title = @Translation("Queue for deletion of payment methods"),
* cron = {"time" = 60}
* )
*/
class PaymentMethodDeleteQueue extends QueueWorkerBase {

/**
* {@inheritdoc}
*/
public function processItem($data) {
/** @var \Drupal\commerce_payment\PaymentMethodStorageInterface $payment_method_storage */
$payment_method_storage = \Drupal::service('entity_type.manager')
->getStorage('commerce_payment_method');
$payment_method_storage->delete($data);
}

}