This repository has been archived by the owner on Jun 2, 2020. It is now read-only.
/
CustomerTokenApi.cs
97 lines (94 loc) · 3.56 KB
/
CustomerTokenApi.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Collections.Generic;
using Klarna.Rest.Core.Common;
using Klarna.Rest.Core.Communication;
using Klarna.Rest.Core.Model.Enum;
using Klarna.Rest.Core.Model;
namespace Klarna.Rest.Core.Examples
{
public class CustomerTokenApi
{
/// <summary>
/// Gets the customer token details.
/// </summary>
public void GetCustomerTokenDetails()
{
var username = "0_abc";
var password = "sharedsecret";
var klarna = new Klarna(username, password, KlarnaEnvironment.TestingEurope);
var customerToken = "234534...345345....345";
try
{
var session = klarna.CustomerToken.GetCustomerTokenDetails(customerToken).Result;
Console.WriteLine("payment method: " + session.PaymentMethodType);
Console.WriteLine("Status: " + session.Status);
}
catch (AggregateException ae)
{
foreach (var e in ae.InnerExceptions) {
if (e is ApiException)
{
var apiException = (ApiException) e;
Console.WriteLine("Status code: " + apiException.StatusCode);
Console.WriteLine("Error: " + string.Join("; ", apiException.ErrorMessage.ErrorMessages));
}
else {
// Rethrow any other exception or process it
Console.WriteLine(e.Message);
throw;
}
}
}
}
public void CreateOrderOnCustomerToken()
{
var username = "0_abc";
var password = "sharedsecret";
var klarna = new Klarna(username, password, KlarnaEnvironment.TestingEurope);
var customerToken = "234534...345345....345";
var orderData = new CustomerTokenOrder
{
PurchaseCurrency = "SEK",
OrderAmount = 1000,
OrderTaxAmount = 2000,
MerchantReference1 = "StoreOrderId",
AutoCapture = true,
OrderLines = new List<OrderLine>()
{
new OrderLine
{
Type = OrderLineType.physical,
Name = "Foo",
Quantity = 1,
UnitPrice = 10000,
TaxRate = 2500,
TotalAmount = 10000,
TotalTaxAmount = 2000,
TotalDiscountAmount = 0,
}
},
};
try
{
var order = klarna.CustomerToken.CreateOrder(customerToken, orderData).Result;
Console.WriteLine("OrderId: " + order.OrderId);
}
catch (AggregateException ae)
{
foreach (var e in ae.InnerExceptions) {
if (e is ApiException)
{
var apiException = (ApiException) e;
Console.WriteLine("Status code: " + apiException.StatusCode);
Console.WriteLine("Error: " + string.Join("; ", apiException.ErrorMessage.ErrorMessages));
}
else {
// Rethrow any other exception or process it
Console.WriteLine(e.Message);
throw;
}
}
}
}
}
}