Conditionals
Conditionals allow us to write configuration that changes based on various factors.
For example, show different amounts based on the currency.
To achieve something like that, you have to write a conditional.
There are many different config keys that support conditionals.
A conditional can either be a value of any kind – a simple value without a condition,
or the same value wrapped into a if, then
structure – a value inside a condition.
As an example, amounts without a condition:
As an example, with conditionals you can change the amounts depending on the currency like this.
A condition usually consists of operators and methods. In the example above,
currency()
would be a method and ==
an operator
Operators
You might write conditions either in traditional or functional format, for example,
traditional: 1 < 2 and 3 > 2
,
functional: and(lt(1, 2), gt(3, 2))
.
List of available operators:
Operator | Description |
---|---|
and, && | Logical and operator |
or, || | Logical or operator |
> | Greater then comparison |
>= | Greater then or equal to comparison |
< | Lesser then comparison |
<= | Lesser then or equal to Ocomparison |
== | Equal to comparison |
=== | Strict equal to comparison |
!= | Not equal to comparison |
!== | Strict not equal to comparison |
+, -, *, / | Mathematical operators |
% | Modulo operator |
!, not, unless | Inversion operator |
in | Check if in array |
nin | Check if not in array |
Methods
List of available methods:
Method | Example | Description |
---|---|---|
and | and(true, true) | Logical and comparison |
or | or(true, false) | Logical or comparison |
gt | gt(2, 1) | Greater then comparison |
gte | gte(2, 2) | Greater then equal to comparison |
lt | lt(1, 2) | Lesser then equal to comparison |
lte | lte(2, 2) | Lesser then equal to comparison |
eq | eq(1, 1) | Equal to comparison |
eqs | eqs(1, '1') | Strict equal to comparison |
neq | neq(1, 2) | Not equal to comparison |
neqs | neqs(1, '1') | Strict not equal to comparison |
add | add(1, 2) | Mathematical + operator |
sub | sub(2, 1) | Mathematical - operator |
mul | mul(2, 3) | Mathematical * operator |
div | div(6, 2) | Mathematical / operator |
mod | mod(10, 3) | Mathematical % operator |
arr | arr(1, 2, 3) | Create an array from inputs |
round | round(1.5) | Regular rounding |
ceil | ceil(1.8) | Round to next highest |
floor | floor(1.2) | Round to next lowest |
nan | nan('1') | Check that value is a number, not a string |
in | in(1, arr(1, 2, 3)) | Check that value is part of array |
nin | in(4, arr(1, 2, 3)) | Check that value is not part of array |
neg | neg(true) | Negate a value to the opposite |
val | val(1) | Return given value, used internally |
purpose | purpose() | Get payment form purpose |
paymentType | paymentType() | Get payment form payment type |
recurringInterval | recurringInterval() | Get payment form payment interval |
currency | currency() | Get payment form currency |
amount | amount() | Get payment form amount |
feeAmount | feeAmount() | Get payment form fee amount |
formattedAmount | formattedAmount() | Get payment form formatted amount |
formattedTotalAmount | formattedTotalAmount() | Get payment form formatted total amount |
formattedFeeAmount | formattedFeeAmount() | Get payment form formatted fee amount |
paymentMethod | paymentMethod() | Get payment form payment method |
isCard | isCard('vis') | Is given payment method a card payment method |
paymentForm | paymentForm('stored_customer_firstname') | Get payment form field value by name |
showPaymentAddress | showPaymentAddress() | Returns true if payment address block is mandatory |
isPostFinanceIban | isPostFinanceIban() | Returns true if iban, filled in payment form, is PostFinance iban |
trans | trans('blocks.payment_purposes.title') | Get translation string by path (escaped) |
transUnescaped | transUnescaped('blocks.payment_purposes.title') | Get translation string by path (not escaped) |
config | config('epms.accountUuid') | Get raw config option value by option name (path) |
resolveConfig | resolveConfig('amounts') | Get resolved config option values by option name (path) |
resolveConfigWithSingleResult | resolveConfigWithSingleResult('allowCustomAmount') | Get first resolved config option values by option name (path) |
transactionInfo | transactionInfo('payment_method') | Get transactionInfo field value by name |
subscriptionInfo | subscriptionInfo('payment_method') | Get subscriptionInfo field value by name |
transactionFinalStatus | transactionFinalStatus() | Get final status of transaction |
abTestVariant | abTestVariant() | Get current variant of AB testing |
Adding custom methods
Methods are simple functions that may, or may not, take arguments and return a value. New methods can easily be added like this:
window.rnw.tamaro.events.afterCreate.subscribe((event) => {
const tamaro = event.data.api;
// Adding a new operator `todaysDay`
tamaro.parser.addMethods({
todaysDay: () => new Date().getDay(),
});
// On every second day we change the amounts
tamaro.extendConfig({
amounts: [
{
if: 'todaysDay() % 2 === 0',
then: [1, 2, 3, 4],
},
{
if: 'todaysDay() % 2 === 1',
then: [5, 6, 7, 8],
},
],
});
});
window.rnw.tamaro.runWidget('.rnw-tamaro-widget');