Merx

Merx

This class is the heart and the soul of the plugin.

Merx:: __construct()

Creates Cart and merges default Gateways with custom gateways.

$merx = new Merx();

Source Code

Merx::formatIBAN()

Helper method to get a formatted IBAN.

Merx::formatIBAN(string $iban): string

There is a formatIBAN shortcut.

Return Type

string

Example

echo formatIBAN('DE89370400440532013000'); // DE89 3704 0044 0532 0130 00

Source Code

Merx::formatPrice()

Localizes the price. Currency symbol is shown before or after price based on the locale information.

Merx::formatPrice(float $price): string

There is a formatPrice shortcut.

Return Type

string

Example

echo formatPrice(9.41); // € 9.41

// if language is set to German
echo formatPrice(1029.41); // 1.029,41 €

Source Code

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 excluding tax. E.g. 99.99
tax float In percent. E.g. 19

Return Type

float

Example

echo calculateTax(99.99, 19); // 15.964789915966

Source Code

$merx->cart()

Returns visitors cart.

$merx->cart(?array $data = null): Cart
$merx->cart(): Cart

Return Type

Wagnerwagner\Merx\Cart

Example

$cart = $merx->cart();
echo $cart->getSum(); // 49.95

Source Code

$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.

Source Code

$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

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.

Source Code

Merx::setMessage()

Stores a message in the visitor’s session.

Merx::setMessage($message)

Source Code

Merx::getMessage()

Returns and removes message stored by Merx::setMessage().

Merx::getMessage()

Return Type

null|mixed

Source Code

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

Source Code