Skip to content

Commit 8c1bfaa

Browse files
jsacksickbojanz
authored andcommitted
Issue #2953940 by pslcbs, jsacksick, bojanz: Add an OrderPaymentGateway condition
1 parent ac9f16b commit 8c1bfaa

5 files changed

Lines changed: 188 additions & 0 deletions

File tree

modules/payment/commerce_payment.services.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ services:
1616
class: Drupal\commerce_payment\PaymentTypeManager
1717
parent: default_plugin_manager
1818

19+
commerce_payment.filter_conditions_subscriber:
20+
class: Drupal\commerce_payment\EventSubscriber\FilterConditionsEventSubscriber
21+
tags:
22+
- { name: event_subscriber }
23+
1924
commerce_payment.options_builder:
2025
class: Drupal\commerce_payment\PaymentOptionsBuilder
2126
arguments: ['@entity_type.manager']

modules/payment/config/schema/commerce_payment.schema.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
commerce.commerce_condition.plugin.order_payment_gateway:
2+
type: commerce_condition_configuration
3+
mapping:
4+
payment_gateways:
5+
type: sequence
6+
label: 'Payment gateways'
7+
orderby: value
8+
sequence:
9+
type: string
10+
label: 'Payment gateway'
11+
112
commerce_payment.commerce_payment_gateway.*:
213
type: config_entity
314
label: 'Payment gateway'
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Drupal\commerce_payment\EventSubscriber;
4+
5+
use Drupal\commerce\Event\FilterConditionsEvent;
6+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7+
8+
class FilterConditionsEventSubscriber implements EventSubscriberInterface {
9+
10+
/**
11+
* {@inheritdoc}
12+
*/
13+
public static function getSubscribedEvents() {
14+
$events = [
15+
'commerce.filter_conditions' => 'onFilterConditions',
16+
];
17+
return $events;
18+
}
19+
20+
/**
21+
* Removes the payment gateway condition on payment gateways.
22+
*
23+
* @param \Drupal\commerce\Event\FilterConditionsEvent $event
24+
* The event.
25+
*/
26+
public function onFilterConditions(FilterConditionsEvent $event) {
27+
if ($event->getParentEntityTypeId() == 'commerce_payment_gateway') {
28+
$definitions = $event->getDefinitions();
29+
unset($definitions['order_payment_gateway']);
30+
$event->setDefinitions($definitions);
31+
}
32+
}
33+
34+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Drupal\commerce_payment\Plugin\Commerce\Condition;
4+
5+
use Drupal\commerce\Plugin\Commerce\Condition\ConditionBase;
6+
use Drupal\Core\Entity\EntityInterface;
7+
use Drupal\Core\Form\FormStateInterface;
8+
9+
/**
10+
* Provides the payment gateway condition for orders.
11+
*
12+
* @CommerceCondition(
13+
* id = "order_payment_gateway",
14+
* label = @Translation("Payment gateway"),
15+
* display_label = @Translation("Limit by payment gateway"),
16+
* category = @Translation("Order"),
17+
* entity_type = "commerce_order",
18+
* )
19+
*/
20+
class OrderPaymentGateway extends ConditionBase {
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function defaultConfiguration() {
26+
return [
27+
'payment_gateways' => [],
28+
] + parent::defaultConfiguration();
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
35+
$form = parent::buildConfigurationForm($form, $form_state);
36+
37+
$form['payment_gateways'] = [
38+
'#type' => 'commerce_entity_select',
39+
'#title' => $this->t('Payment gateways'),
40+
'#default_value' => $this->configuration['payment_gateways'],
41+
'#target_type' => 'commerce_payment_gateway',
42+
'#hide_single_entity' => FALSE,
43+
'#multiple' => TRUE,
44+
'#required' => TRUE,
45+
];
46+
47+
return $form;
48+
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
54+
parent::submitConfigurationForm($form, $form_state);
55+
56+
$values = $form_state->getValue($form['#parents']);
57+
$this->configuration['payment_gateways'] = $values['payment_gateways'];
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function evaluate(EntityInterface $entity) {
64+
$this->assertEntity($entity);
65+
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
66+
$order = $entity;
67+
if ($order->get('payment_gateway')->isEmpty()) {
68+
// The payment gateway is not known yet, the condition cannot pass.
69+
return FALSE;
70+
}
71+
$payment_gateway_id = $order->get('payment_gateway')->get('target_id');
72+
73+
return in_array($payment_gateway_id, $this->configuration['payment_gateways']);
74+
}
75+
76+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Drupal\Tests\commerce_payment\Unit\Plugin\Commerce\Condition;
4+
5+
use Drupal\commerce_order\Entity\OrderInterface;
6+
use Drupal\commerce_payment\Plugin\Commerce\Condition\OrderPaymentGateway;
7+
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
8+
use Drupal\Tests\UnitTestCase;
9+
10+
/**
11+
* @coversDefaultClass \Drupal\commerce_payment\Plugin\Commerce\Condition\OrderPaymentGateway
12+
* @group commerce
13+
*/
14+
class OrderPaymentGatewayTest extends UnitTestCase {
15+
16+
/**
17+
* ::covers evaluate.
18+
*/
19+
public function testIncompleteOrder() {
20+
$condition = new OrderPaymentGateway([
21+
'payment_gateways' => ['test'],
22+
], 'order_payment_gateway', ['entity_type' => 'commerce_order']);
23+
$order = $this->prophesize(OrderInterface::class);
24+
25+
$entity_reference_item = $this->prophesize(EntityReferenceItem::class);
26+
$entity_reference_item->isEmpty()->willReturn(TRUE);
27+
$entity_reference_item = $entity_reference_item->reveal();
28+
29+
$order = $this->prophesize(OrderInterface::class);
30+
$order->getEntityTypeId()->willReturn('commerce_order');
31+
$order->get('payment_gateway')->willReturn($entity_reference_item);
32+
$order = $order->reveal();
33+
34+
$this->assertFalse($condition->evaluate($order));
35+
}
36+
37+
/**
38+
* Covers evaluate.
39+
*/
40+
public function testOrderPaymentGateway() {
41+
$entity_reference_item = $this->prophesize(EntityReferenceItem::class);
42+
$entity_reference_item->isEmpty()->willReturn(FALSE);
43+
$entity_reference_item->get('target_id')->willReturn('test');
44+
$entity_reference_item = $entity_reference_item->reveal();
45+
46+
$order = $this->prophesize(OrderInterface::class);
47+
$order->getEntityTypeId()->willReturn('commerce_order');
48+
$order->get('payment_gateway')->willReturn($entity_reference_item);
49+
$order = $order->reveal();
50+
51+
$condition = new OrderPaymentGateway([
52+
'payment_gateways' => ['cash_on_delivery'],
53+
], 'order_payment_gateway', ['entity_type' => 'commerce_order']);
54+
$this->assertFalse($condition->evaluate($order));
55+
56+
$condition = new OrderPaymentGateway([
57+
'payment_gateways' => ['test', 'cash_on_delivery'],
58+
], 'order_payment_gateway', ['entity_type' => 'commerce_order']);
59+
$this->assertTrue($condition->evaluate($order));
60+
}
61+
62+
}

0 commit comments

Comments
 (0)