<?php

require_once '../Klarna.php';

// Dependencies from http://phpxmlrpc.sourceforge.net/
require_once dirname(dirname(__FILE__)) .
    '/transport/xmlrpc-3.0.0.beta/lib/xmlrpc.inc';
require_once dirname(dirname(__FILE__)) .
    '/transport/xmlrpc-3.0.0.beta/lib/xmlrpc_wrappers.inc';

/**
 * 1. Initialize and setup the Klarna instance.
 */

$k = new Klarna();

$k->config(
    123456,               // Merchant ID
    'sharedSecret',       // Shared Secret
    KlarnaCountry::SE,    // Country
    KlarnaLanguage::SV,   // Language
    KlarnaCurrency::SEK,  // Currency
    Klarna::BETA,         // Server
    'json',               // PClass Storage
    '/srv/pclasses.json', // PClass Storage URI path
    true,                 // SSL
    true                  // Remote logging of response times of xmlrpc calls
);

// OR you can set the config to loads from a file, for example /srv/klarna.json:
// $k->setConfig(new KlarnaConfig('/srv/klarna.json'));

/**
 * 2. Add the article(s), shipping and/or handling fee. (OPTIONAL)
 */

// Here we add a normal product to our goods list.
$k->addArticle(
    4,                      // Quantity
    "MG200MMS",             // Article number
    "Matrox G200 MMS",      // Article name/title
    299.99,                 // Price
    25,                     // 25% VAT
    0,                      // Discount
    KlarnaFlags::INC_VAT    // Price is including VAT.
);

// Next we might want to add a shipment fee for the product
$k->addArticle(
    1,
    "",
    "Shipping fee",
    14.5,
    25,
    0,
    // Price is including VAT and is shipment fee
    KlarnaFlags::INC_VAT | KlarnaFlags::IS_SHIPMENT
);

// Lastly, we want to use an invoice/handling fee as well
$k->addArticle(
    1,
    "",
    "Handling fee",
    11.5,
    25,
    0,
    // Price is including VAT and is handling/invoice fee
    KlarnaFlags::INC_VAT | KlarnaFlags::IS_HANDLING
);

/**
 * 3. Create and set the address(es). (OPTIONAL)
 */

// Create the address object and specify the values.
$addr = new KlarnaAddr(
    'always_approved@klarna.com', // email
    '',                           // Telno, only one phone number is needed.
    '0762560000',                 // Cellno
    'Testperson-se',              // Firstname
    'Approved',                   // Lastname
    '',                           // No care of, C/O.
    'Stårgatan 1',                // Street
    '12345',                      // Zip Code
    'Ankeborg',                   // City
    KlarnaCountry::SE,            // Country
    null,                         // HouseNo for German and Dutch customers.
    null                          // House Extension. Dutch customers only.
);

// Next we tell the Klarna instance to use the address in the next order.
$k->setAddress(KlarnaFlags::IS_BILLING, $addr);  // Billing / invoice address
$k->setAddress(KlarnaFlags::IS_SHIPPING, $addr); // Shipping / delivery address

/**
 * 4. Specify relevant information from your store. (OPTIONAL)
 */

// Set store specific information so you can e.g. search and associate invoices
// with order numbers.
$k->setEstoreInfo(
    '175012',       // Order ID 1
    '1999110234',   // Order ID 2
    ''              // Optional username, email or identifier
);

/**
 * 5. Make the call to Klarna
 */

// Reservation number
$rno = '123456';

try {
    $result = $k->update('1402740');

    if ($result) {
        echo "Update successful\n";
    }
} catch(KlarnaException $e) {
    echo "{$e->getMessage()} (#{$e->getCode()})\n";
}
