-
Notifications
You must be signed in to change notification settings - Fork 0
/
commerce_fraud-AVS_Response_Code-3172294-3.patch
220 lines (218 loc) · 6.79 KB
/
commerce_fraud-AVS_Response_Code-3172294-3.patch
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
diff --git a/src/EventSubscriber/CommerceFraudSubscriber.php b/src/EventSubscriber/CommerceFraudSubscriber.php
index 1238b01..0c18c32 100644
--- a/src/EventSubscriber/CommerceFraudSubscriber.php
+++ b/src/EventSubscriber/CommerceFraudSubscriber.php
@@ -14,6 +14,7 @@ use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\commerce_fraud\Entity\SuspectedOrder;
+use Drupal\commerce_payment\Event\PaymentEvent;
/**
* Event subscriber, that acts on the place transition of commerce order.
@@ -137,10 +138,59 @@ class CommerceFraudSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
$events = [
'commerce_order.place.pre_transition' => ['setFraudScore'],
+ 'commerce_payment.commerce_payment.insert' => ['setPaymentScore'],
];
return $events;
}
+ /**
+ * Sets Fraud Score based upon payment AVS response codes.
+ *
+ * @param \Drupal\commerce_payment\Event\PaymentEvent $event
+ * The transition event.
+ */
+ public function setPaymentScore(PaymentEvent $event) {
+
+ $payment = $event->getPayment();
+
+ $order = $payment->getOrder();
+
+ $this->suspectedOrder = $this->suspectedOrderStorage->loadByProperties(['order_id' => $order->id()]);
+ $this->suspectedOrder = reset($this->suspectedOrder);
+
+ if (empty($this->suspectedOrder)) {
+ $this->suspectedOrder = SuspectedOrder::create([
+ 'order_id' => $order->id(),
+ 'rules' => [],
+ ]);
+ }
+ // Get rules.
+ $rules = $this->entityTypeManager->getStorage('rules')->loadMultiple();
+
+ // Apply rules to payment.
+ foreach ($rules as $rule) {
+ // Only payment rules to be checked.
+ if ($rule->getPlugin()->getLabel() != 'Compare order AVS code') {
+ continue;
+ }
+
+ if (!$rule->getPlugin()->applyPaymentRule($payment)) {
+ continue;
+ }
+
+ // Get the name set in the entity.
+ $rule_name = $rule->getPlugin()->getLabel();
+
+ // Add a log to order activity.
+ $this->logStorage->generate($order, 'fraud_rule_name', ['rule_name' => $rule_name])->save();
+
+ $this->suspectedOrder->addRule($rule);
+ }
+
+ $this->suspectedOrder->save();
+
+ }
+
/**
* Sets the Fraud score on placing the order.
*
diff --git a/src/Plugin/Commerce/FraudRule/CheckAddressVerificationSystemFraudRule.php b/src/Plugin/Commerce/FraudRule/CheckAddressVerificationSystemFraudRule.php
new file mode 100644
index 0000000..d2f3c61
--- /dev/null
+++ b/src/Plugin/Commerce/FraudRule/CheckAddressVerificationSystemFraudRule.php
@@ -0,0 +1,142 @@
+<?php
+
+namespace Drupal\commerce_fraud\Plugin\Commerce\FraudRule;
+
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\commerce_payment\CreditCard;
+use Drupal\commerce_payment\Entity\PaymentInterface;
+use Drupal\commerce_order\Entity\OrderInterface;
+
+/**
+ * Provides the fraud rule.
+ *
+ * @CommerceFraudRule(
+ * id = "check_avs",
+ * label = @Translation("Compare order AVS code"),
+ * description = @Translation("Compares order AVS code"),
+ * )
+ */
+class CheckAddressVerificationSystemFraudRule extends FraudRuleBase {
+
+ /**
+ * {@inheritdoc}
+ */
+ public function defaultConfiguration() {
+ return [
+ 'avs_codes' => [
+ 'visa' => [],
+ 'mastercard' => [],
+ 'amex' => [],
+ 'discover' => [],
+ 'maestro' => [],
+ ],
+ ] + parent::defaultConfiguration();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
+ $form += parent::buildConfigurationForm($form, $form_state);
+
+ $form['#type'] = 'container';
+ $form['#title'] = $this->t('AVS Code');
+ $form['#collapsible'] = FALSE;
+
+ $form['avs_code'] = [
+ '#type' => 'fieldset',
+ '#title' => $this->t('AVS Code'),
+ '#collapsible' => FALSE,
+ ];
+
+ $avs_code_meanings = CreditCard::getAvsResponseCodeMeanings();
+
+ $form['avs_code']['visa_codes'] = [
+ '#type' => 'select',
+ '#title' => $this->t('Visa Codes'),
+ '#options' => $avs_code_meanings['visa'],
+ '#multiple' => 1,
+ '#required' => 1,
+ '#default' => $this->configuration['avs_codes']['visa'],
+ ];
+ $form['avs_code']['mastercard_codes'] = [
+ '#type' => 'select',
+ '#title' => $this->t('Mastercard Codes'),
+ '#options' => $avs_code_meanings['mastercard'],
+ '#multiple' => 1,
+ '#required' => 1,
+ '#default' => $this->configuration['avs_codes']['mastercard'],
+ ];
+ $form['avs_code']['amex_codes'] = [
+ '#type' => 'select',
+ '#title' => $this->t('Amex Codes'),
+ '#options' => $avs_code_meanings['amex'],
+ '#multiple' => 1,
+ '#required' => 1,
+ '#default' => $this->configuration['avs_codes']['amex'],
+ ];
+ $form['avs_code']['discover_codes'] = [
+ '#type' => 'select',
+ '#title' => $this->t('Discover Codes'),
+ '#options' => $avs_code_meanings['discover'],
+ '#multiple' => 1,
+ '#required' => 1,
+ '#default' => $this->configuration['avs_codes']['discover'],
+ ];
+ $form['avs_code']['maestro_codes'] = [
+ '#type' => 'select',
+ '#title' => $this->t('Maestro Codes'),
+ '#options' => $avs_code_meanings['maestro'],
+ '#multiple' => 1,
+ '#required' => 1,
+ '#default' => $this->configuration['avs_codes']['maestro'],
+ ];
+
+ return $form;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
+ parent::submitConfigurationForm($form, $form_state);
+
+ if (!$form_state->getErrors()) {
+ $values = $form_state->getValue($form['#parents']);
+ $this->configuration['avs_codes']['visa'] = $values['avs_code']['visa_codes'];
+ $this->configuration['avs_codes']['mastercard'] = $values['avs_code']['mastercard_codes'];
+ $this->configuration['avs_codes']['amex'] = $values['avs_code']['amex_codes'];
+ $this->configuration['avs_codes']['discover'] = $values['avs_code']['discover_codes'];
+ $this->configuration['avs_codes']['maestro'] = $values['avs_code']['maestro_codes'];
+ }
+
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function apply(OrderInterface $order) {
+ // Invalid application of rule.
+ return FALSE;
+
+ }
+
+ /**
+ * Checks payment AVS code response.
+ */
+ public function applyPaymentRule(PaymentInterface $payment) {
+ if (!$payment->getPaymentMethod() || !$payment->getAvsResponseCode()) {
+ return FALSE;
+ }
+ $card_type = $payment->getPaymentMethod()->getType();
+
+ $avs_response_code = $payment->getAvsResponseCode();
+
+ if (isset($this->configuration['avs_codes'][$card_type][$avs_response_code])) {
+ return TRUE;
+ }
+
+ return FALSE;
+ }
+
+}