# Order API

## Example implementation:

```php
<?php
    namespace Tussendoor\Billink;

    // The easiest way is to resolve the customer from a WooCommerce order.
    // Give the WC_Customer instance to the customer converter to easily 
    // create a Tussendoor\Billink\Customer\Customer instance. 
    $wcOrder = wc_get_order(123);
    $customerId = $wcOrder->get_customer_id();
    $wcCustomer = new \WC_Customer($customerId);

    $order = (new Converters\Order($wcOrder))->parse();
    $customer = (new Converters\Customer($wcCustomer))->parse();

    // Don't forget to set the customer UUID. Either resolve it from 
    // storage or grab it from the Check API response.
    $customer->checkUuid = $checkResponse->getUuid();
    // $customer->checkUuid = 'SOME-RANDOM-UUID-HERE';

    // Use the Customer and Order instance on the Order endpoint class
    // and inject the AuthenticationHeader into it.
    $orderEndpoint = new Endpoint\Order($customer, $order);
    $orderEndpoint->setAuthenticationHeader(App::get('auth.header'));

    // Build the HTTP request through our HTTP client and serialize the 
    // Endpoint/Order instance into XML by calling the serialize() method.
    try {
        $response = App::get('http.client')
            ->post($orderEndpoint->getUrlEndpoint(), $orderEndpoint->serialize())
            ->send();

        $orderResponse = App::get('serializer')
            ->unserialize($response->getBody(), Endpoint\Order::class, 'xml');
    } catch (\Tussendoor\Http\RequestException $e) {
        // When the HTTP request fails, a RequestException is thrown.
        var_dump($e->getErrors());
        exit();
    } catch (\Exception $e) {
        var_dump($e);
        exit();
    }

    // The response will be invalid if a validation error occured over at Billink.
    if ($orderResponse->isInvalid()) {
        var_dump(
            $orderResponse->isValid(),
            $orderResponse->getCode(),
            $orderResponse->getError(),
            $orderResponse->getErrorLabel(),
            $orderResponse
        );

        exit('Order is invalid');
    }

    // This is a OrderResponse specific method. If this method returns false,
    // the order was NOT created by Billink.
    if ($orderResponse->orderWasCreated() === false) {
        exit('Order creation failed');
    }

    // By now the order was succesfully created by Billink.
    // By default, the order ID in Billink is the same as
    // the order ID within WooCommerce.
```
