Purpose:
The getAddresses function is used to retrieve a customer’s address(es). Using this, the customer is not required to enter an address – only confirm the one presented to him/her.
The getAddresses function can also be used for companies. If the customer enters an organisation number, it will return all the addresses where the company is registered at.
The get_addresses function is ONLY allowed to be used for Swedish persons with the following conditions:
- The function can only be used in the checkout, never with registering customers or on any other page in the shop.
- The function can be only used if Klarna is the default payment method when the function is used before choosing the payment method.
- The function has to disappear if the customer chooses another payment method.
- The button is not allowed to be called get address (hämta adress), but continue (fortsätt) or the address can be picked up automatically when all the numbers have been typed in.
In the other countries you will have to have the required input fields so that the customer can enter this information by himself.
Return value:
An array of arrays containing the addresses (one array per address). The array contains strings.
Example when using GA_GIVEN or GA_OLD
0 = First name (not returned if company)
1 = Last name or company name
2 = Address
3 = Postno
4 = City
5 = Country
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.
$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
);
$k->setCountry('se');
try {
//Attempt to get the address(es) associated with the SSN/PNO.
$addrs = $k->getAddresses('410321-9202');
/* If there exists several addresses you would want to output a list in
which the customer could choose the address which suits him/her.
*/
// Print them if available:
foreach ($addrs as $key => $addr) {
echo "<table>\n";
// This only works if the right getAddresses type is used.
if ($addr->isCompany) {
echo "\t<tr><td>Company</td><td> {$addr->getCompanyName()} </td></tr>\n";
} else {
echo "\t<tr><td>First name</td><td>{$addr->getFirstName()}</td></tr>\n";
echo "\t<tr><td>Last name</td><td>{$addr->getLastName()}</td></tr>\n";
}
echo "\t<tr><td>Street</td><td>{$addr->getStreet()}</td></tr>\n";
echo "\t<tr><td>Zip code</td><td>{$addr->getZipCode()}</td></tr>\n";
echo "\t<tr><td>City</td><td>{$addr->getCity()}</td></tr>\n";
echo "\t<tr><td>Country</td><td>{$addr->getCountryCode()}</td></tr>\n";
echo "</table>\n";
}
} catch(Exception $e) {
//Something went wrong
echo "{$e->getMessage()} (#{$e->getCode()})\n";
}
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"
});
// It's important that we set the end customers IP
klarna.ClientIP = "192.0.2.9";
try {
List addresses = klarna.GetAddresses (
"410321-9202",
API.Encoding.Swedish,
API.GetAddressFlag.GA_Given //Could alternativly be API.GetAddressFlag.GA_Last or API.GetAddressFlag.GA_All
);
//If there exists several addresses you would want to output a list in which the customer
//could choose the address which suits him/her
foreach (Address address in addresses) {
//This only works if the right GetAddresses type is used.
if (address.IsCompany) {
Console.WriteLine ("Company name: " + address.Company);
} else {
Console.WriteLine ("First name: " + address.Firstname);
Console.WriteLine ("Last name: " + address.Lastname);
}
Console.WriteLine ("Street address: " + address.Street);
Console.WriteLine ("Zip code: " + address.ZipCode);
Console.WriteLine ("City: " + address.City);
API.Country country = (API.Country)Convert.ToInt32 (address.Country);
Console.WriteLine ("Country: " + country);
}
} catch (Exception e) {
//Something went wrong, print the error:
Console.WriteLine ("Error: " + e.Message);
}
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"
* }
*
*/
k.setCountry(KlarnaCountry.SE);
try {
k.setClientIP(request.getRemoteAddr()); // Set the IP address of the customer.
// Attempts to get the address(es) associated with the SSN/PNO.
List<KlarnaAddr> addrs = k.getAddresses("19410321-9202");
/* If there exists several addresses you would want to output a list in which the customer
* could choose the address which suits him/her */
for (KlarnaAddr ka : addrs) {
out.println("<table>");
//This only works if the right getAddresses type is used.
if (ka.isCompany()) {
out.println("<tr><td>Company</td><td>" + ka.getCompanyName() + "</td></tr>");
} else {
out.println("<tr><td>First name</td><td>" + ka.getFirstName() + "</td></tr>");
out.println("<tr><td>Last name</td><td>" + ka.getLastName() + "</td></tr>");
}
out.println("<tr><td>Street</td><td>" + ka.getStreet() + "</td></tr>");
out.println("<tr><td>Zip code</td><td>" + ka.getZip() + "</td></tr>");
out.println("<tr><td>City</td><td>" + ka.getCity() + "</td></tr>");
out.println("<tr><td>Country</td><td>" + ka.getCountry() + "</td></tr>");
out.println("</table>");
}
} catch (Exception e) {
// Something went wrong, print the message.
out.println(e.getMessage());
}
<%
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"))
%>
<%
On Error Resume Next
kAPI.SetCountry("se")
' Attempt to get the address(es) associated with the SSN/PNO.
Dim pno
pno = "430415-8399"
Dim result
result = kAPI.GetAddresses(pno, null, GA_GIVEN)
If Err.number = 0 Then
' Success
' If there exists several addresses you would want to output a list in which the customer
' could choose the address which suits him/her.
' Print them if available:
Dim address
Dim i
For i = 0 To UBound(result)
Response.Write("<table>")
Set address = result(i)
' This only works if the right getAddresses type is used.
If address.isCompany Then
Response.Write("<tr><td>Company</td><td>"&address.getCompanyName()&"</td></tr>")
Else
Response.Write("<tr><td>First name</td><td>"&address.getFirstName()&"</td></tr>")
Response.Write("<tr><td>Last name</td><td>"&address.getLastName()&"</td></tr>")
End If
Response.Write("<tr><td>Street</td><td>"&address.getStreet()&"</td></tr>")
Response.Write("<tr><td>Zip code</td><td>"&address.getZipCode()&"</td></tr>")
Response.Write("<tr><td>City</td><td>"&address.getCity()&"</td></tr>")
Response.Write("<tr><td>Country</td><td>"&address.getCountryCode()&"</td></tr>")
Response.Write("</table>")
Next
Else
' Something went wrong
Response.Write("Error occured: " & Err.number & " - " & Err.Description & "</br>" & Err.Source & "</br>")
End If
%>
