This class is the heart and the soul of the plugin.
Merx
Merx:: __construct()
Creates Cart and merges default Gateways with custom gateways.
$merx = new Merx();
Merx::formatIBAN()
Helper method to get a formatted IBAN.
Merx::formatIBAN(string $iban): string
There is a formatIBAN
shortcut.
Parameters
Name | Type | Description |
---|---|---|
iban |
string |
E.g. DE0000000000000000 |
Return Type
string
Example
echo formatIBAN('DE89370400440532013000'); // DE89 3704 0044 0532 0130 00
Merx::formatPrice()
Localizes the price. Currency symbol is shown before or after price based on arguments, options or localeconv
.
Merx::formatPrice(float $price, bool $currencyPositionPrecedes = null, bool $currencySeparateBySpace = null): string
There is a formatPrice
shortcut.
You can set global options for currencyPositionPrecedes
and currencySeparateBySpace
in your config file to overwrite the localeconv
information. The method arguments overwrite the global options.
Parameters
Name | Type | Description |
---|---|---|
price |
float |
|
currencyPositionPrecedes |
bool |
true if currency symbol precedes, false if it succeeds one |
currencySeparateBySpace |
bool |
true if a space separates currency_symbol, false otherwise |
Return Type
string
Example
echo formatPrice(9.41); // € 9.41
// if language is set to German
echo formatPrice(1029.41); // 1.029,41 €
// using additional arguments
echo formatPrice(49.95, true, true); // € 49.95
Merx::calculateTax()
Helper method to calculate tax
Merx::calculateTax(float $grossPrice, float $tax): float
There is a calculateTax
shortcut.
Parameters
Name | Type | Description |
---|---|---|
grossPrice |
float |
Price including tax. E.g. 99.99 |
tax |
float |
In percent. E.g. 19 |
Return Type
float
Example
echo calculateTax(99.99, 19); // 15.964789915966
Merx::calculateNet()
Helper method to calculate net price
Merx::calculateNet(float $grossPrice, float $tax): float
There is a calculateNet
shortcut.
Parameters
Name | Type | Description |
---|---|---|
grossPrice |
float |
Price including tax. E.g. 99.99 |
tax |
float |
In percent. E.g. 19 |
Return Type
float
$merx->cart()
Returns visitors cart.
$merx->cart(?array $data = null): Cart
Parameters
Name | Type | Description |
---|---|---|
data |
array |
Optional data to create a new cart. |
Return Type
Wagnerwagner\Merx\Cart
Example
$cart = $merx->cart();
echo $cart->getSum(); // 49.95
$merx->initializePayment()
Creates virtual OrderPage and validates it. Runs payment gateway’s initializePayment function. Saves virtual OrderPage in user session.
$merx->initializePayment(array $data): string
Parameters
Name | Type | Description |
---|---|---|
data |
array |
Content of OrderPage . Must contain paymentMethod . |
Return Type
string
option('ww.merx.successPage')
or result of initializePayment()
of paymentMethod
gateway.
Example
try {
$merx = merx();
$data = [
'paymentMethod' => 'paypal',
'name' => 'Dagobert Duck',
'email' => 'dagobert@entenhausen.de',
];
$url = $merx->initializePayment($data); // https://www.paypal.com/cgi-bin/webscr…
go($url);
} catch (Exception $ex) {
// handle Exception
}
Exceptions
If the validation fails or something went wrong a Kirby\Exception\Exception
is thrown. You should take care of this.
Key | httpCode | Description |
---|---|---|
checkout.emptycart | 500 |
Cart contains zero items. |
merx.noPaymentMethod | 400 |
$data has no paymentMethod key. |
checkout.fieldsvalidation | 400 |
OrderPage validation fails. Read more about the checkout validation. |
merx.initializePayment | 500 |
Basic error with initialize payment. Use $ex->getDetails() and $ex->getData() for further information. |
$merx->completePayment()
Runs payment gateway’s completePayment function
$merx->completePayment(array $data = []): OrderPage
Parameters
Name | Type | Description |
---|---|---|
data |
array |
Data required for payment gateway’s completePayment() |
Return Type
OrderPage
Additional Information
The required $data
depends on your paymentMethod
. See Payment Methods for more information.
Exceptions
This method could throw Kirby\Exception\Exception
you should handle.
Key | httpCode | Description |
---|---|---|
checkout.paymentMethod | 500 |
Payment method’s completePayment key has to be a function. |
completePayment | 500 |
|
paymentCanceled | 400 |
User canceled the payment. |
Merx::setMessage()
Stores a message in the visitor’s session.
Merx::getMessage()
Returns and removes message stored by Merx::setMessage()
.
Merx::getFieldError()
Returns field error for a given field.
Merx::getFieldError(\Field $field, array $rules): array
See Conditional Errors for more information.
Return Type
array