# Miscellaneous code examples

## Parse a WC_Order to an array or XML
```php
use WC_Customer;
use BillinkGateway\App;
use BillinkGateway\Converters\Order;
use BillinkGateway\Converters\Customer;
use BillinkGateway\Endpoint\Order as OrderEndpoint;

add_action('init', function () {
    $wc_order = wc_get_order($someOrderId);
    $wc_customer = new WC_Customer($wc_order->get_customer_id());

    $order = (new Order($wc_order))->parse();
    $customer = (new Customer($wc_customer))->parse();

    $endpoint = new OrderEndpoint($customer, $order);
    $endpoint->setAuthenticationHeader(App::get('auth.header'));

    // As array (might still contain normalizable elements)
    var_dump($endpoint->normalize());

    // As XML
    var_dump($endpoint->serialize());
});
```

## Debug mode
By default, the plugin will listen to the BILLINK_GATEWAY_PLUGIN_DEBUG constant to determine if it has to go into debug mode. If the constant is not found, a fallback to WP_DEBUG is used.

When the plugin is in debug mode, additional data will be logged. The minimum loglevel will be set to 'debug' (from 'notice') and stacktraces will be provided.

To enable debug mode, simply define the BILLINK_GATEWAY_PLUGIN_DEBUG constant and set it to true:
```php
define('BILLINK_GATEWAY_PLUGIN_DEBUG', true);
```

## Default loglevel
To overwrite the default loglevel ('notice' by default or 'debug' in debugmode), use the 'billink_gateway_min_loglevel' filter:
```php
add_filter('billink_gateway_min_loglevel', function($level) {
    return 'warning';
});
```
The loglevel should adhere to [PSR-3](https://www.php-fig.org/psr/psr-3/#5-psrlogloglevel) LoggerInterface.

## Triggers
The plugin uses triggers for certain events in the plugin. By default, the following triggers are defined:

- order.credit
- order.workflow.start
- order.credit.failed
- midpage.routing.failed
- order.startworkflow.failed

For every trigger a method is bound to it. Whenever a trigger is called, that method is executed. 

To overwrite the default implementation of a trigger, use the `billink_gateway_triggers` filter:
```php
add_filter('billink_gateway_triggers', function($triggers) {
    $triggers['order.credit'] = 'MyClass@MyMethod'

    return $triggers;
});
```

