/
IbanRules.drl
130 lines (114 loc) · 3.7 KB
/
IbanRules.drl
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
package sctrcd.payments.rules
import com.sctrcd.payments.facts.*
import com.sctrcd.payments.validation.iban.IbanMod97Check
global java.util.List countryList;
rule "Trigger IBAN validation."
when
$req: PaymentValidationRequest($payment:payment, payment.iban != null)
not IbanValidationRequest(iban == $payment.iban)
then
insert(
new IbanValidationRequest($payment.getIban())
);
end
rule "IBAN failed the Mod-97 checksum test."
salience 100
when
$req: IbanValidationRequest($iban:iban)
not PaymentValidationAnnotation(
attribute == PaymentAttribute.iban,
level == AnnotationLevel.REJECT,
ruleName == "IBAN failed the Mod-97 checksum test."
)
eval(!IbanMod97Check.isValid($iban))
then
insert(
new PaymentValidationAnnotation(
drools.getRule().getName(),
AnnotationLevel.REJECT,
"The IBAN is not valid.",
PaymentAttribute.iban
)
);
end
rule "IBAN doesn't begin with a valid country ISO code."
salience 90
when
$req: IbanValidationRequest($iban:iban, $country:iban.substring(0, 2))
not PaymentValidationAnnotation(
attribute == PaymentAttribute.iban,
level == AnnotationLevel.REJECT,
ruleName == "IBAN doesn't begin with a valid country ISO code."
)
not Country(isoCode == $country) from countryList
then
insert(
new PaymentValidationAnnotation(
drools.getRule().getName(),
AnnotationLevel.REJECT,
"The IBAN does not begin with a valid 2-character country code. '" + $country + "' is not a country.",
PaymentAttribute.iban
)
);
end
// A UK IBAN will look something like the following:
// GB19 LOYD 3096 1700 7099 43
// 2-letter country code "GB"
// 4-letter bank code
// 6-digit sort code
// 8-digit account number
rule "IBAN is for UK, but doesn't have BBAN structure."
salience 80 // Check for correct structure first.
when
$req: IbanValidationRequest(
$country:iban.substring(0, 2) == "GB",
$iban:iban not matches "GB[0-9]{2}[a-zA-Z]{4}[0-9]{14}"
)
not PaymentValidationAnnotation(
attribute == PaymentAttribute.iban,
level == AnnotationLevel.REJECT,
ruleName == "IBAN is for UK, but doesn't have BBAN structure."
)
then
insert(
new PaymentValidationAnnotation(
drools.getRule().getName(),
AnnotationLevel.REJECT,
"The IBAN is for the UK, but doesn't follow the standard UK structure.",
PaymentAttribute.iban
)
);
end
rule "Add annotations to IBAN request"
salience -1 // Activate at end of validations to accumulate results.
no-loop
when
$req: IbanValidationRequest()
$ann: PaymentValidationAnnotation(attribute == PaymentAttribute.iban)
then
$req.addAnnotation($ann);
// Note that we don't update the request. If we do, then we trigger
// re-evaluation of the fact, and create an infinite loop.
end
rule "Derive most severe IBAN annotation"
salience -1 // Activate at end of validations to accumulate results.
when
$req: IbanValidationRequest()
$ann: PaymentValidationAnnotation(
attribute == PaymentAttribute.iban,
$lvl: level,
$req.mostSevereAnnotation == null || level.ordinal() > $req.mostSevereAnnotation.ordinal()
)
then
$req.setMostSevereAnnotation($lvl);
update( $req );
end
rule "Reject IBAN request if there is an IBAN rejection annotation"
salience -1 // Activate at end of validations to accumulate results.
no-loop
when
$req: IbanValidationRequest(valid == true, mostSevereAnnotation == AnnotationLevel.REJECT)
then
$req.setValid(false);
update( $req );
end