fetchPClasses

Purpose:

To obtain pclass values from Klarna which are necessary to calculate monthly costs on the product and checkout pages.

These values also have to be sent in the pclass argument when doing an addTransaction, activateInvoice, reserveAmount and activateReservation request which is required for part payments and the mobile payment service. 

Please note!
This function is only to be used to obtain pclass values for stores one time only. It is not allowed to use this function for continuous calculation of monthly costs or with every purchase in the checkout. The pclass values do not change unless you get a new type of part payment or special campaign.

The API will try to create a file, what sort depends on the config but it can be for example the pclasses.json, and then populate it with the fetched pclasses. There are some things to keep in mind here; If the process lacks writing permissions no file can be created. The file can then be added manually, if so please make sure that it has writing permissions set. Whether the API sets up the file or you do it, one still needs to define a path to it in the config.

Return value:

An array of arrays containing the pclass values

0 = Pclass id number
1 = Description
2 = Amount of months for part payment
3 = Start fee
4 = Invoice fee
5 = Interest rate
6 = Minimum purchase amount for pclass
7 = Country
8 = Type (This is used to determine what type the pclass id is and which calculation method is to be used)
9 = Pclass expiry date in the format YYYY-MM-DD. This is used for Buy now, pay in X month campaigns.

Type no Description
0 Campaign
1 Account
2 Buy now, pay in X month e.g. christmas campaign campaign)
3 Fixed price
4 Pay in X months
5 Klarna mobile


or

Throws an exception with error code and error message.

Code examples

Please note that you can find more code examples in our API-files which you will find under Download API in the left menu.

The code examples have been divided into several steps. Click on the step you would like to see the code example.
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
);
2. Retrieve the PClasses from Klarna
try {
    $k->fetchPClasses(); // You can specify country (and language, currency if
                         // you wish) if you don't want to use the configured
                         // country.

    /* PClasses successfully fetched, now you can use getPClasses() to load them
       locally or getPClass to load a specific PClass locally.
     */
    echo "Fetched " . count($k->getAllPClasses()) . " pclasses.\n";

} catch(Exception $e) {
    // Something went wrong, print the message:
    echo "{$e->getMessage()} (#{$e->getCode()})\n";
}
The code examples have been divided into several steps. Click on the step you would like to see the code example.
1. Initialize and setup the Klarna instance
            API klarna = new API ();

            /** Configure the Klarna object **/
            klarna.Config (new KlarnaConfig () {
                EID = 123456,
                Secret = "sharedsecret",
                Country = API.Country.Sweden,
                Language = API.Language.Swedish,
                Currency = API.Currency.SEK,
                Encoding = API.Encoding.Swedish,
            // API.KlarnaServer.Beta or API.KlarnaServer.Live, depending
            // on which server your eid is associated with
                Mode = API.KlarnaServer.Beta,
                PCStorage = "xml",
                PCURI = @"/tmp/pclasses.xml"
            });
2. Retrieve the PClasses from Klarna
            try {
                klarna.FetchPClasses (); // Fetch pclasses for the configured country
                klarna.FetchPClasses (API.Country.Finland); // Fetch pclasses for the given country

                //PClasses successfully fetched, now you can use GetPClasses() to load them locally or GetPClass to load a specific PClass instead.
            } catch (Exception e) {
                //Something went wrong, print the error:
                Console.WriteLine ("Error: " + e.Message);
            }
The code examples have been divided into several steps. Click on the step you would like to see the code example.
1. Initialize and setup the Klarna instance
    Klarna k = new Klarna();

    /*
     *  Configure the Klarna object using the config() method. (Alternative 1)
     */

    /* KlarnaConfig conf = new KlarnaConfig();
    
    // Define values:
    conf.setEid(0);                         // Merchant ID or Estore ID, an Integer above 0.
    conf.setSecret("sharedSecret");         // The shared secret which accompanied your eid.
    conf.setCountry(KlarnaCountry.DE);      // The country of your store.
    conf.setLanguage(KlarnaLanguage.DE);    // The language of your store.
    conf.setCurrency(KlarnaCurrency.EUR);   // The currency of your store.
    conf.setMode(Klarna.BETA);              // or Klarna.LIVE when you are ready to go live.
    
    // Define pclass settings:
    conf.setPcStorage("json");              // Storage module. Currently only json is supported.
    conf.setPcURI("/srv/pclasses.json");    // Where the json file for the pclasses are stored.
    
    // Should we use SSL?
    conf.setSsl(false);
    
    // Should we error report/status report to klarna.
    conf.setCandice(true);                  // set to false if your server doesn't support UDP
    
    // Do we want to see normal debug information?
    conf.setDebug(null);                    // true to debug, null or false not to debug
    // Set the config object.
    k.config(conf);
    
     */


    /* 
     *  Configure the Klarna object using the config() method (Alternative 2)
     */

    /*
     *   k.config(0,                     // e-store ID
     *           "sharedSecret",         // shared secret
     *           KlarnaCountry.DE,       // Country constant
     *           KlarnaLanguage.DE,      // Language constant
     *           KlarnaCurrency.EUR,     // Currency constant
     *           Klarna.BETA,            // Mode, BETA or LIVE
     *           "json",                 // Storage module
     *           "/srv/pclasses.json",   // Storage location
     *           true,                   // SSL
     *           true                    // Candice
     *           );
     * 
     *  k.debug = false;
     */

    /* 
     *  Configure the Klarna object using the config() method (Alternative 3)
     */
    
    k.config(KlarnaConfig.fromJson("/srv/klarna.json"));

    /*  the file would contain the following data to set the same information as above alternatives:
     * {
     *      "eid":0,
     *      "secret":"sharedSecret",
     *      "country":"DE",
     *      "language":"DE",
     *      "currency":"EUR",
     *      "mode":1,
     *      "ssl":false,
     *      "candice":true,
     *      "pcStorage":"json",
     *      "pcURI":"/srv/pclasses.json"
     *  }
     *
     */
2. Retrieve the PClasses from Klarna
    try {
        k.fetchPClasses(); // You can specify country (and language, currency) if you don't want to use the configured country.
        k.fetchPClasses(KlarnaCountry.SE, KlarnaLanguage.SV, KlarnaCurrency.SEK);
        
        // PClasses successfully fetched, now you can use getPClasses() to load the locally or getPClass to load a specific PClass locally.
        
    } catch (Exception e) {
        // Something went wrong, print the message.
        out.println(e.getMessage());
    }
The code examples have been divided into several steps. Click on the step you would like to see the code example.
1. Initialize and setup the Klarna instance
<%
    Option Explicit
%>



<%

    ' Grab the API object
    ' -Merchant ID or Estore ID, an integer above 0.
    ' -The shared secret which accompanied your eid.
    ' -Country, language and currency.
    Dim kAPI
    Set kAPI = GetKlarna(0, "sharedsecret", "SE", "SV", "SEK")

    ' Do we want to see normal debug information?
    kAPI.DebugInformation = True

    ' Set the address and port to Klarna server.
    ' Use LIVE or BETA depending on which server your eid is associated with.
    kAPI.SetPort(HTTPS_PORT) ' or HTTP_PORT
    kAPI.SetHost(BETA_HOST) ' or LIVE_HOST

    ' Where the XML for the PClasses are stored, e.g. "pclasses.xml"
    kAPI.SetPClassesStorageUri(Server.MapPath("pclasses.xml"))

%>
2. Retrieve the PClasses from Klarna
<%

    On Error Resume Next

    Dim result
    result = kAPI.FetchPClasses()

    If Err.number = 0 Then
        ' Success
    
        ' PClasses successfully fetched, now you can use getPClasses() to load them locally
        ' or getPClass to load a specific PClass locally.
    
        ' Result also contains the PClasses.
        Response.Write("Fetched following pclasses:

") Dim pClass For Each pClass in result Response.Write("Id: " & pClass.GetId() & " and description: " & pClass.GetDescription() & "
") Next Else ' Something went wrong Response.Write("Error occured: " & Err.number & " - " & Err.Description & "
" & Err.Source & "
") End If %>

Input data:

Variable Type Description
Required eid integer An e-store ID which refers to your store in Klarna's database
Required currency integer Currency code to be used for the invoices.

Swedish krona: KRED_SEK (value: 0)
Euro: KRED_EUR (value: 2)
Danish krona: KRED_DKK (value: 3)
Norwegian krona: KRED_NOK (value: 1)

Please note!
Currency, country and language must be the same as where the customer is registered in. E.g. Swedish customer, SEK, Sweden and Swedish.
Required secret string A shared secret which is used to secure all traffic between Klarna and your store
Required country integer Code for the country where your sales will be in:

Sweden: KRED_ISO3166_SE (value: 209)
Finland: KRED_ISO3166_FI (value: 73)
Denmark: KRED_ISO3166_DK (value: 59)
Norway: KRED_ISO3166_NO (value: 164)
Germany: KRED_ISO3166_DE (value: 81)
Netherlands: KRED_ISO3166_NL (value: 154)

Please note!
Currency, country and language must be the same as where the customer is registered in. E.g. Swedish customer, SEK, Sweden and Swedish.
Required language integer Language code for the language used on the invoice.


Swedish: KRED_ISO639_SV (value: 138)
Norwegian: KRED_ISO639_NB (value: 97)
Finnish : KRED_ISO639_FI (value: 37)
Danish: KRED_ISO639_DA (value: 27)
German: KRED_ISO639_DE (value: 28)
Dutch: KRED_ISO639_NL (value: 101)

Please note!
Currency, country and language must be the same as where the customer is registered in. E.g. Swedish customer, SEK, Sweden and Swedish.