calcMonthlyCost

Purpose:

To calculate the monthly cost for goods for the part payment service. The same function is used for account and account campaigns.

Please note! To obtain pclasses use the function fetchPClasses which will return the pclasses in a file or database.

Return value:

An amount to be displayed to the customer. Round this up to the nearest crown or to one tenth euro, e.g. 14,56 rounds up to 14,6 EUR.

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. Calculate the monthly cost for the product page
$sum = 149.99; // Let's assume this is the product cost.
$flag = KlarnaFlags::PRODUCT_PAGE; // or KlarnaFlags::CHECKOUT_PAGE, if you want
                                   // to do it for the whole order.
$pclass = $k->getCheapestPClass($sum, $flag);

// Did we get a PClass? (it is false if we didn't)
if ($pclass) {
    // Here we reuse the same values as above:
    $value = KlarnaCalc::calc_monthly_cost(
        $sum,
        $pclass,
        $flag
    );

    echo "Value: {$value}\n";
    /*
      $value is now a rounded monthly cost amount to be displayed to the
      customer.
     */
}
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. Calculate the monthly cost for the product page
            // Create a new Calc object
            Calc calc = new Calc ();

            // Get the sum to calculate with.
            double sum = 149.99;
            PClass pclass = klarna.GetCheapestPClass (sum, API.CalculateOn.ProductPage);
            double value = calc.CalcMonthlyCost (sum, pclass, API.CalculateOn.ProductPage);

            Console.WriteLine ("value: " + value);
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. Calculate the monthly cost for the product page
   double sum = 149.99;    // Let's assume this is the products cost.
    
    KlarnaPClass pclass = k.getCheapestPClass(sum, KlarnaFlags.DisplayPage.PRODUCT_PAGE);  // or KlarnaFlags.CHECKOUT_PAGE if you want to do it for the whole order

    // Did we get a PClass? (it is null if we didn't)
    double value = 0d;
    if (pclass != null) {
        value = KlarnaCalc.calc_monthly_cost(
                sum,
                pclass,
                KlarnaFlags.DisplayPage.PRODUCT_PAGE.intVal()); // Using the int value of the flag.
        // value is now a rounded monthly cost amount to be displayed to the customer. 
    }
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. Calculate the monthly cost for the product page
<%

    On Error Resume Next

    Dim sum
    sum = 149.99 ' Let's assume this is the product cost.

    Dim flag
    flag = PRODUCT_PAGE ' or CHECKOUT_PAGE, if you want to do it for the whole order.

    Dim pclass
    Set pclass = kAPI.GetCheapestPClass(sum, flag)

    If Err.number = 0 Then
        ' Success

        ' Did we get a PClass?
        If TypeName(pclass) = "KlarnaPClass" Then
            Dim calc
            Set calc = New KlarnaCalc

            ' Here we reuse the same values as above
            Dim value
            value = calc.calc_monthly_cost(sum, pclass, flag)
    
            ' value is now a rounded monthly cost amount to be displayed to the customer
            Response.Write("Monthly cost is: " & value)
        Else
            Response.Write("Could not get a PClass")
        End If
    Else 
        ' Something went wrong
        Response.Write("Error occured: " & Err.number & " - " & Err.Description & "
" & Err.Source & "
") End If %>

Input data:

Variable Type Description
Required eidintegerAn e-store ID which refers to your store in Klarna's database
Required currencyintegerCurrency 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)
Required secretstringA shared secret which is used to secure all traffic between Klarna and your store
Required countryintegerCode 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)

Required languageintegerLanguage 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)

Required sumfloat/doubleAmount to be calculated, ex. 9.95(€) or 12O(SEK). Value can be sent with or without VAT.
Required pclassobjectThis argument designates campaign code and information to be used by the function.
Required flagsintegerOne of two flags designating if the amount is to be shown with fees (to be used in checkout) or without fees (to be used on product page).

PRODUCT_PAGE (Value: 1)
Amount is without fees, to be used for productpage and / or product gallery. Fees are based on the total purchase amount and therefore they are added in the checkout and not on product pages.

CHECKOUT_PAGE (Value: 0) Amount is with fees, to be used in checkout in order to present the customer with an appropriate monthly cost of the completed purchase.