# Rules

> Custom rules, the blocklist and the allowlist, and which one wins.

The score decides on its own most of the time. On top of it sit three deterministic overrides, for
the cases where you know something the score cannot: a customer you trust, a number you have seen
defraud you, a bracket of payments you want challenged whatever they score.

All three are Shield Advanced, with one exception: a customer you allowed from the review queue
stays allowed on every plan. None of it runs unless your own Shield configuration is on.

## Which one wins

Your rules are read in priority order, and the first matching live rule decides. A blocklist or
sanctions match always refuses the payment, so no allow rule can release a blocklisted customer.

## What a rule is made of

- `name` (string, required): Up to 120 characters. It is what appears on the reasons of a payment this rule decided, so name it after what it catches.
- `conditions` (array, required): One to ten conditions, each a field, an operator and a value. Every one of them must hold: there is no OR, and an empty list matches nothing.
- `action` (enum, required): block, review, challenge_3ds or allow. See the table below for what each does.
- `mode` (enum, required, default `live`): live rules decide. test rules are evaluated and counted, and never change anything.
- `priority` (integer, default `ten above your highest`): Lower runs first. Rules sharing a priority are walked newest first, so the most recent one wins a tie.
- `enabled` (boolean, default `true`): A disabled rule is not evaluated and does not count hits.

Every rule also carries a hit counter and the time it last matched, both updated on every payment
it hits, test rules included. That counter is the honest measure of whether a rule is doing
anything.

## The eight fields

The builder is a closed catalogue. There is no expression language and no access to anything not
on this list, including the risk score itself.

| Field                                | Type    | Operators                  |
| ------------------------------------ | ------- | -------------------------- |
| Transaction amount                   | number  | `gt` `gte` `lt` `lte` `eq` |
| Currency                             | list    | `in` `not.in`              |
| Payment method                       | list    | `in` `not.in`              |
| Customer email                       | text    | `eq` `contains` `matches`  |
| Customer phone                       | list    | `in` `not.in`              |
| IP country                           | list    | `in` `not.in`              |
| First transaction from this customer | boolean | `is_true` `is_false`       |
| Matches an entry in the blocklist    | boolean | `is_true`                  |

A few behaviours are worth knowing before you write anything.

**The amount is in major units**, the same units as the payment, so `50000` is fifty thousand XAF.

**Payment method is the channel's type**, not its slug: `card`, `mobile_money`, `wallet`, `crypto`,
`ussd`, `voucher`, `bnpl`, `redirect`, and three bank types, `bank.transfer`, `bank.debit` and
`bank.redirect`. There is no plain `bank`: a rule on bank payments lists the bank types it means.

**Phone numbers are compared on their digits alone**, so `+237 6 70 00 00 00` and `237670000000`
are the same value. Email comparison is case-insensitive, and so is `matches`, where `*` is the
only wildcard and it is anchored at both ends.

**First transaction** means this customer has never had a live payment succeed with you before. A
payment with no customer attached counts as a first.

## The four actions

What an action does depends on what the score already decided, which is the part that catches
people out.

| Action          | Effect                                                                                                         |
| --------------- | -------------------------------------------------------------------------------------------------------------- |
| `block`         | The payment is refused, whatever it scored                                                                     |
| `review`        | The payment is flagged, unless the score already blocked it. A review never rescues a block                    |
| `challenge_3ds` | 3D Secure is requested on a card payment. The allow, review or block decision is unchanged                     |
| `allow`         | The score decision is dropped and the payment goes through, except against your blocklist or a sanctions match |

> [!WARNING]
> **Allow is the only action that can undo a decision**
>
> And it deliberately cannot undo all of them. A payment matching your blocklist, or a sanctions
> watchlist, keeps its refusal no matter how many allow rules point at it. An explicit deny always
> wins over an explicit allow, which is the only arrangement that makes a blocklist worth anything.

`challenge_3ds` only reaches the issuer through providers that support it. Everywhere else the
provider applies its own 3D Secure policy and the rule changes nothing.

## Test mode

A rule in test mode is evaluated on every live payment, the ones a live rule already decided
included, counts its hits, and is recorded on the payment as having matched, without ever deciding
anything. It is the closest thing Shield has to a
dry run, and it is the only one: there is still no sandbox.

The way to use it is to write the rule you are considering in test mode, leave it for a week, and
read its hit counter. A rule that would have blocked four hundred payments is a rule you want to
know about before it is live.

## The blocklist

An entry on the blocklist refuses the payment, whatever your thresholds and your rules. It is the
bluntest thing in Shield and the only one that needs no rule.

- `type` (enum, required): One of email, phone, ip, country or card_bin.
- `value` (string, required): Checked against its type and stored in one form: an email is lower-cased, a phone becomes `+` and its 6 to 15 digits (so `+237 670 00 00 00` and `237670000000` are one entry), an IP is one IPv4 or IPv6 address (no range), a country is an ISO 3166-1 alpha-2 code, upper-cased, and a card BIN is 6 to 8 digits, matched against the start of the card. A value that cannot be one of its type is refused with a 422.
- `reason` (string): Up to 500 characters, for you. It is what the decision records as the reason.

A `country` entry is matched against the country of the payer's IP address, not their phone number
and not the currency. Blocking `NG` stops every payment made from a Nigerian IP, including your own
Nigerian customers paying while they are there.

`ip` and `country` entries are matched against the payer's own address. If you charge from your
server with a secret key, see
[If you run your own checkout](https://docs.wajub.com/risk/shield/quickstart#if-you-run-your-own-checkout).

> [!NOTE]
> **The list only applies while it is switched on**
>
> A blocklist entry does nothing unless Shield and `blocklist_enabled` are both on in your
> settings, the entries Shield adds after a lost dispute included. Adding an entry from the
> Dashboard switches the list on for you, adding one through the API does not.

A `card_bin` entry matches every card whose number starts with those digits. A match is refused,
or sent to your review queue, where you can refund it and block the payer.

The blocklist is the one part of this page with an API.

`POST /shield/blocklist`

The type and the value are required, the reason is yours to fill in and is what the decision
records when the entry matches.

```bash title="Block a number"
curl https://api.wajub.com/shield/blocklist \
  -H "Authorization: sk.xxxxxxxxxxxxxxxx…" \
  -H "Content-Type: application/json" \
  -d '{
    "type": "phone",
    "value": "+237690000000",
    "reason": "confirmed fraud, chargeback March"
  }'
```

The full list comes back on every write, so you never need a second call to see the result.

## The allowlist

Allowing a customer creates an allow rule that runs before your other rules. The Dashboard writes
one for you when you close a review with **Always allow customer**: one rule per identifier, matching
the email exactly or the phone digit for digit.

That rule is the one part of this page that works on every plan. Without Shield Advanced it is
still read on each payment while every other rule stays dormant, and **Shield**, lists shows the
customers you allowed and removes one in a click. It is how you release a payer Shield refused by
mistake.

What counts is narrow on purpose: a live `allow` rule with a single condition, the email with `eq`
or the phone with `in`. With Shield Advanced you can write the same thing by hand, and you should
keep it just as narrow. An allow rule on `contains` rather than `eq` would let
`x-attacker@example.com` through along with `attacker@example.com`, and it is a custom rule like any
other: it stops being read if you leave Shield Advanced.

## What a rule cannot do

**It cannot see the score.** There is no risk score field in the catalogue, so you cannot write
"block anything over 60 that is also a first payment". Thresholds are the only lever on the score.

**It cannot combine conditions with OR.** Every condition in a rule must hold. Two alternatives are
two rules.

**It cannot read your metadata.** Nothing you send on the payment, beyond the amount, the currency,
the channel and the customer's email and phone, is visible to a rule.

**It cannot release a blocklisted payment.** A blocklist or sanctions match refuses the payment
whatever your rules say.

- [Shield](https://docs.wajub.com/risk/shield): Where the score sits in a payment, and what the plans differ on.
- [Quickstart](https://docs.wajub.com/risk/shield/quickstart): Turn it on, set thresholds that hold, and work the queue.
- [Risk Scoring](https://docs.wajub.com/risk/shield/risk-scoring): What the score means, and the levers that are yours.
- [Shield API](https://docs.wajub.com/api/shield): The settings, stats and blocklist endpoints.
