Handle refunds


In this section, you will learn how to implement and use full, partial and good will refunds.

Prerequisites


Partial refund


In this tutorial, you will learn how to process a partial refund. This functionality will give the consumer a refund for a returned item.

Use case


A consumer purchases a number of items from your web store. The consumer then decides to return some of the items. You will need to process a refund.
 

1. Initialize the Klarna object


​Each Klarna library function requires a basic setup of the Klarna object. This configuration specifies how the library communicates with Klarna’s servers.

Follow the code examples below to see how to do this with our different libraries:

 

use Klarna\XMLRPC\Klarna;
use Klarna\XMLRPC\Country;
use Klarna\XMLRPC\Language;
use Klarna\XMLRPC\Currency;

$k = new Klarna();

$k->config(
    0,              // Merchant ID
    'sharedSecret', // Shared secret
    Country::SE,    // Purchase country
    Language::SV,   // Purchase language
    Currency::SEK,  // Purchase currency
    Klarna::BETA,   // Server
);
Klarna k = new Klarna(
        0,
        "sharedSecret",
        KlarnaCountry.SE,
        KlarnaCurrency.SEK,
        new JsonStorage("/srv/data/pclasses.json"),
        Klarna.BETA
);
config = klarna.Config(
    eid=0,
    secret='sharedSecret',
    country='DE',
    language='DE',
    currency='EUR',
    mode='beta',
    pcstorage='json',
    pcuri='/srv/pclasses.json',
    scheme='https',
    candice=True)

k = klarna.Klarna(config)
k.init()
Configuration configuration = new Configuration(
    Country.Code.SE, Language.Code.SV, Currency.Code.SEK,
    Encoding.Sweden)
{
    Eid = 0,
    Secret = "sharedsecret",
    IsLiveMode = false
};

Api api = new Api(configuration);
Dim kAPI
Set kAPI = GetKlarna(0, "sharedsecret", "SE", "SV", "SEK")
kAPI.SetPort(HTTPS_PORT) ' or HTTP_PORT
kAPI.SetHost(BETA_HOST) ' or LIVE_HOST
kAPI.SetPClassesStorageUri(Server.MapPath("pclasses.xml"))

2. Add call data


The creditPart call requires you to send in the list of items for which you wish to issue a refund.

This is facilitated by a helper function within the Klarna library:

$k->addArtNo(1, 'MG200MMS');
k.addArtNo(2, "1234e");
k.add_art_no(1, 'MG200MMS')
api.AddArticleNumber(2, "1234e");
Call kAPI.AddArticleNumber(1, "MG200MMS")

After adding the articles you wish to refund, make the call to Klarna.

 

3. Make the API call

 

Please initiate the creditPart API call to Klarna using the invoice number to which you want to apply the refund.

$k->creditPart("123456");
k.creditPart("123456");
k.credit_part("123456")
api.CreditPart("123456");
Call kAPI.CreditPart("123456", Null)

Klarna will respond with a string with the invoice number of the invoice to which the refund is applied, or if an error occurred an exception containing an error code and message will be thrown.

You can now apply the refund to the invoice in your system.


Good-will refund

 

In this tutorial, you will learn how to process a good-will refund. This functionality will give the consumer a discount on the invoice.

Use case

 

A consumer purchases a number of items from your web store. Due to an issue with the logistics company the package arrives late. Since this is a loyal customer and customer satisfaction is important to you, you decide to give the consumer a good-will refund in the form of a discount on the invoice.

1. Initialize the Klarna object


​Each Klarna library function requires a basic setup of the Klarna object. This configuration specifies how the library communicates with Klarna’s servers.

Follow the code examples below to see how to do this with our different libraries:

 

use Klarna\XMLRPC\Klarna;
use Klarna\XMLRPC\Country;
use Klarna\XMLRPC\Language;
use Klarna\XMLRPC\Currency;

$k = new Klarna();

$k->config(
    0,              // Merchant ID
    'sharedSecret', // Shared secret
    Country::SE,    // Purchase country
    Language::SV,   // Purchase language
    Currency::SEK,  // Purchase currency
    Klarna::BETA,   // Server
);
Klarna k = new Klarna(
        0,
        "sharedSecret",
        KlarnaCountry.SE,
        KlarnaCurrency.SEK,
        new JsonStorage("/srv/data/pclasses.json"),
        Klarna.BETA
);
config = klarna.Config(
    eid=0,
    secret='sharedSecret',
    country='DE',
    language='DE',
    currency='EUR',
    mode='beta',
    pcstorage='json',
    pcuri='/srv/pclasses.json',
    scheme='https',
    candice=True)

k = klarna.Klarna(config)
k.init()
Configuration configuration = new Configuration(
    Country.Code.SE, Language.Code.SV, Currency.Code.SEK,
    Encoding.Sweden)
{
    Eid = 0,
    Secret = "sharedsecret",
    IsLiveMode = false
};

Api api = new Api(configuration);
Dim kAPI
Set kAPI = GetKlarna(0, "sharedsecret", "SE", "SV", "SEK")
kAPI.SetPort(HTTPS_PORT) ' or HTTP_PORT
kAPI.SetHost(BETA_HOST) ' or LIVE_HOST
kAPI.SetPClassesStorageUri(Server.MapPath("pclasses.xml"))

2. Make the API call


Please initiate the returnAmount API call to Klarna using the invoice number to which you want to apply the refund, the amount to be refunded and the VAT rate.

You can also add a description, detailing the reason for the refund.

use Klarna\XMLRPC\Flags;

$k->returnAmount(
    $invNo,           // Invoice number
    19.99,            // Amount given as a discount.
    25,               // 25% VAT
    Flags::INC_VAT,   // Amount including VAT.
    "Family discount" // Description
);
k.returnAmount(
        "123456789",        // Invoice Number
        50d,                // Amount to return
        17d,                // VAT %
        Goods.INC_VAT,      // Amount is including VAT
        "Employee Discount" // Description of the return
);
k.return_amount(
    "123456789",
    amount=19.99,
    vat=19,
    description="Employee Discount")
api.ReturnAmount(
    "123456789", 19.99, 25.0, GoodsFlags.IncVAT, "Employee Discount");
' -Invoice number
' -Amount, give 19.99 as a discount
' -VAT, 25% VAT
' -Flags, price including VAT
Call kAPI.ReturnAmount("123456", 19.99, 25, INC_VAT)

Klarna will respond with a string with the invoice number of the invoice to which the refund is applied, or if an error occurred an exception containing an error code and message will be thrown.

You can now apply the refund to the invoice in your system.


Full refund


Full refund will give the consumer a complete refund on a purchase.

Use case


A consumer purchases a number of items from your web store. The consumer then decides to return all of the items in the order. You will need to process a full refund.

1. Initialize the Klarna object


​Each Klarna library function requires a basic setup of the Klarna object. This configuration specifies how the library communicates with Klarna’s servers.

Follow the code examples below to see how to do this with our different libraries:

 

use Klarna\XMLRPC\Klarna;
use Klarna\XMLRPC\Country;
use Klarna\XMLRPC\Language;
use Klarna\XMLRPC\Currency;

$k = new Klarna();

$k->config(
    0,              // Merchant ID
    'sharedSecret', // Shared secret
    Country::SE,    // Purchase country
    Language::SV,   // Purchase language
    Currency::SEK,  // Purchase currency
    Klarna::BETA,   // Server
);
Klarna k = new Klarna(
        0,
        "sharedSecret",
        KlarnaCountry.SE,
        KlarnaCurrency.SEK,
        new JsonStorage("/srv/data/pclasses.json"),
        Klarna.BETA
);
config = klarna.Config(
    eid=0,
    secret='sharedSecret',
    country='DE',
    language='DE',
    currency='EUR',
    mode='beta',
    pcstorage='json',
    pcuri='/srv/pclasses.json',
    scheme='https',
    candice=True)

k = klarna.Klarna(config)
k.init()
Configuration configuration = new Configuration(
    Country.Code.SE, Language.Code.SV, Currency.Code.SEK,
    Encoding.Sweden)
{
    Eid = 0,
    Secret = "sharedsecret",
    IsLiveMode = false
};

Api api = new Api(configuration);
Dim kAPI
Set kAPI = GetKlarna(0, "sharedsecret", "SE", "SV", "SEK")
kAPI.SetPort(HTTPS_PORT) ' or HTTP_PORT
kAPI.SetHost(BETA_HOST) ' or LIVE_HOST
kAPI.SetPClassesStorageUri(Server.MapPath("pclasses.xml"))

2. Make the API call

Please initiate the creditInvoice API call to Klarna using the invoice number to which you want to apply the refund.

$k->creditInvoice("123456");
k.creditInvoice("123456");
k.credit_invoice("123456")
api.CreditInvoice("123456");
Call kAPI.CreditInvoice("123456", Null)

Klarna will respond with a string with the invoice number of the invoice to which the refund is applied, or if an error occurred an exception containing an error code and message will be thrown.

You can now apply the refund to the invoice in your system.