public ActionResult Checkout(Cart cart) { var cartItems = new List<Dictionary<string, object>>(); foreach (Cart.CartLine item in GetCart().Lines) { cartItems.Add(new Dictionary<string, object> { { "reference", item.Product.ArticleNumber.ToString() }, { "name", item.Product.Name }, { "quantity", item.Quantity }, { "unit_price", (int)item.Product.Price * 100}, { "tax_rate", 2500 } }); } var cart_info = new Dictionary<string, object> { { "items", cartItems } }; var data = new Dictionary<string, object> { { "cart", cart_info } }; var merchant = new Dictionary<string, object> { { "id", "5214" }, { "back_to_store_uri", "https://localhost:44300/" }, { "terms_uri", "https://localhost:44300/Cart/Terms" }, { "checkout_uri", "https://localhost:44300/Cart/Checkout" }, { "confirmation_uri", "https://localhost:44300/Cart/ThankYou" + "?klarna_order_id={checkout.order.id}" }, { "push_uri", "https://localhost:44300/Cart/push" + "?klarna_order_id={checkout.order.id}" } }; data.Add("purchase_country", "SE"); data.Add("purchase_currency", "SEK"); data.Add("locale", "sv-se"); data.Add("merchant", merchant); var connector = Connector.Create("Y7mNXnxIdYxXU6c", Connector.TestBaseUri); Order order = new Order(connector); order.Create(data); order.Fetch(); // Store order id of checkout in session object. // session["klarna_order_id"] = order.GetValue("id") as string; // Display checkout var gui = order.GetValue("gui") as JObject; var snippet = gui["snippet"]; return View(snippet); }
public override string ProcessRequest(Order order, HttpRequest request, IDictionary <string, string> settings) { string response = ""; try { order.MustNotBeNull("order"); settings.MustNotBeNull("settings"); settings.MustContainKey("sharedSecret", "settings"); string communicationType = request["communicationType"]; KlarnaOrder klarnaOrder = null; IConnector connector = Connector.Create(settings["sharedSecret"]); if (communicationType == "checkout") { settings.MustContainKey("merchant.id", "settings"); settings.MustContainKey("merchant.terms_uri", "settings"); settings.MustContainKey("locale", "settings"); //Cart information List <Dictionary <string, object> > cartItems = new List <Dictionary <string, object> > { new Dictionary <string, object> { { "reference", settings.ContainsKey("totalSku") ? settings["totalSku"] : "0001" }, { "name", settings.ContainsKey("totalName") ? settings["totalName"] : "Total" }, { "quantity", 1 }, { "unit_price", (int)(order.TotalPrice.Value.WithVat * 100M) }, { "tax_rate", 0 } } }; Dictionary <string, object> data = new Dictionary <string, object> { { "cart", new Dictionary <string, object> { { "items", cartItems } } } }; string klarnaLocation = order.Properties["klarnaLocation"]; string merchantTermsUri = settings["merchant.terms_uri"]; if (!merchantTermsUri.StartsWith("http")) { Uri baseUrl = new UriBuilder(HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Host, HttpContext.Current.Request.Url.Port).Uri; merchantTermsUri = new Uri(baseUrl, merchantTermsUri).AbsoluteUri; } //Merchant information data["merchant"] = new Dictionary <string, object> { { "id", settings["merchant.id"] }, { "terms_uri", merchantTermsUri }, { "checkout_uri", request.UrlReferrer.ToString() }, { "confirmation_uri", order.Properties["teaCommerceContinueUrl"] }, { "push_uri", order.Properties["teaCommerceCallbackUrl"] } }; data["merchant_reference"] = new Dictionary <string, object>() { { "orderid1", order.CartNumber } }; //Combined data Currency currency = CurrencyService.Instance.Get(order.StoreId, order.CurrencyId); //If the currency is not a valid iso4217 currency then throw an error if (!Iso4217CurrencyCodes.ContainsKey(currency.IsoCode)) { throw new Exception("You must specify an ISO 4217 currency code for the " + currency.Name + " currency"); } data["purchase_country"] = CountryService.Instance.Get(order.StoreId, order.PaymentInformation.CountryId).RegionCode; data["purchase_currency"] = currency.IsoCode; data["locale"] = settings["locale"]; //Check if the order has a Klarna location URI property - then we try and update the order if (!string.IsNullOrEmpty(klarnaLocation)) { try { klarnaOrder = new KlarnaOrder(connector, new Uri(klarnaLocation)) { ContentType = KlarnaApiRequestContentType }; klarnaOrder.Fetch(); klarnaOrder.Update(data); } catch (Exception) { //Klarna cart session has expired and we make sure to remove the Klarna location URI property klarnaOrder = null; } } //If no Klarna order was found to update or the session expired - then create new Klarna order if (klarnaOrder == null) { klarnaOrder = new KlarnaOrder(connector) { BaseUri = settings.ContainsKey("testMode") && settings["testMode"] == "1" ? new Uri("https://checkout.testdrive.klarna.com/checkout/orders") : new Uri("https://checkout.klarna.com/checkout/orders"), ContentType = KlarnaApiRequestContentType }; //Create new order klarnaOrder.Create(data); klarnaOrder.Fetch(); order.Properties.AddOrUpdate(new CustomProperty("klarnaLocation", klarnaOrder.Location.ToString()) { ServerSideOnly = true }); order.Save(); } } else if (communicationType == "confirmation") { //get confirmation response string klarnaLocation = order.Properties["klarnaLocation"]; if (!string.IsNullOrEmpty(klarnaLocation)) { //Fetch and show confirmation page if status is not checkout_incomplete klarnaOrder = new KlarnaOrder(connector, new Uri(klarnaLocation)) { ContentType = KlarnaApiRequestContentType }; klarnaOrder.Fetch(); if ((string)klarnaOrder.GetValue("status") == "checkout_incomplete") { throw new Exception("Confirmation page reached without a Klarna order that is finished"); } } } //Get the JavaScript snippet from the Klarna order if (klarnaOrder != null) { JObject guiElement = klarnaOrder.GetValue("gui") as JObject; if (guiElement != null) { response = guiElement["snippet"].ToString(); } } } catch (Exception exp) { LoggingService.Instance.Error <Klarna>("Klarna(" + order.CartNumber + ") - ProcessRequest", exp); } return(response); }
public override string ProcessRequest( Order order, HttpRequest request, IDictionary<string, string> settings ) { string response = ""; try { order.MustNotBeNull( "order" ); settings.MustNotBeNull( "settings" ); settings.MustContainKey( "sharedSecret", "settings" ); string communicationType = request[ "communicationType" ]; KlarnaOrder klarnaOrder = null; IConnector connector = Connector.Create( settings[ "sharedSecret" ] ); if ( communicationType == "checkout" ) { settings.MustContainKey( "merchant.id", "settings" ); settings.MustContainKey( "merchant.terms_uri", "settings" ); settings.MustContainKey( "locale", "settings" ); //Cart information List<Dictionary<string, object>> cartItems = new List<Dictionary<string, object>> { new Dictionary<string, object> { {"reference", settings.ContainsKey( "totalSku" ) ? settings[ "totalSku" ] : "0001"}, {"name", settings.ContainsKey( "totalName" ) ? settings[ "totalName" ] : "Total"}, {"quantity", 1}, {"unit_price", (int) ( order.TotalPrice.Value.WithVat*100M )}, {"tax_rate", 0} } }; Dictionary<string, object> data = new Dictionary<string, object> { { "cart", new Dictionary<string, object> { { "items", cartItems } } } }; string klarnaLocation = order.Properties[ "klarnaLocation" ]; string merchantTermsUri = settings[ "merchant.terms_uri" ]; if ( !merchantTermsUri.StartsWith( "http" ) ) { Uri baseUrl = new UriBuilder( HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Host, HttpContext.Current.Request.Url.Port ).Uri; merchantTermsUri = new Uri( baseUrl, merchantTermsUri ).AbsoluteUri; } //Merchant information data[ "merchant" ] = new Dictionary<string, object> { {"id", settings[ "merchant.id" ]}, {"terms_uri", merchantTermsUri}, {"checkout_uri", request.UrlReferrer.ToString()}, {"confirmation_uri", order.Properties[ "teaCommerceContinueUrl" ]}, {"push_uri", order.Properties[ "teaCommerceCallbackUrl" ]} }; data[ "merchant_reference" ] = new Dictionary<string, object>() { {"orderid1", order.CartNumber} }; //Combined data Currency currency = CurrencyService.Instance.Get( order.StoreId, order.CurrencyId ); //If the currency is not a valid iso4217 currency then throw an error if ( !Iso4217CurrencyCodes.ContainsKey( currency.IsoCode ) ) { throw new Exception( "You must specify an ISO 4217 currency code for the " + currency.Name + " currency" ); } data[ "purchase_country" ] = CountryService.Instance.Get( order.StoreId, order.PaymentInformation.CountryId ).RegionCode; data[ "purchase_currency" ] = currency.IsoCode; data[ "locale" ] = settings[ "locale" ]; //Check if the order has a Klarna location URI property - then we try and update the order if ( !string.IsNullOrEmpty( klarnaLocation ) ) { try { klarnaOrder = new KlarnaOrder( connector, new Uri( klarnaLocation ) ) { ContentType = KlarnaApiRequestContentType }; klarnaOrder.Fetch(); klarnaOrder.Update( data ); } catch ( Exception ) { //Klarna cart session has expired and we make sure to remove the Klarna location URI property klarnaOrder = null; } } //If no Klarna order was found to update or the session expired - then create new Klarna order if ( klarnaOrder == null ) { klarnaOrder = new KlarnaOrder( connector ) { BaseUri = settings.ContainsKey( "testMode" ) && settings[ "testMode" ] == "1" ? new Uri( "https://checkout.testdrive.klarna.com/checkout/orders" ) : new Uri( "https://checkout.klarna.com/checkout/orders" ), ContentType = KlarnaApiRequestContentType }; //Create new order klarnaOrder.Create( data ); klarnaOrder.Fetch(); order.Properties.AddOrUpdate( new CustomProperty( "klarnaLocation", klarnaOrder.Location.ToString() ) { ServerSideOnly = true } ); order.Save(); } } else if ( communicationType == "confirmation" ) { //get confirmation response string klarnaLocation = order.Properties[ "klarnaLocation" ]; if ( !string.IsNullOrEmpty( klarnaLocation ) ) { //Fetch and show confirmation page if status is not checkout_incomplete klarnaOrder = new KlarnaOrder( connector, new Uri( klarnaLocation ) ) { ContentType = KlarnaApiRequestContentType }; klarnaOrder.Fetch(); if ( (string)klarnaOrder.GetValue( "status" ) == "checkout_incomplete" ) { throw new Exception( "Confirmation page reached without a Klarna order that is finished" ); } } } //Get the JavaScript snippet from the Klarna order if ( klarnaOrder != null ) { JObject guiElement = klarnaOrder.GetValue( "gui" ) as JObject; if ( guiElement != null ) { response = guiElement[ "snippet" ].ToString(); } } } catch ( Exception exp ) { LoggingService.Instance.Error<Klarna>( "Klarna(" + order.CartNumber + ") - ProcessRequest", exp ); } return response; }
public Uri Create() { var cart = _klarnaCheckoutUtils.GetCart(); var merchant = _klarnaCheckoutUtils.GetMerchant(); var supportedLocale = _klarnaCheckoutUtils.GetSupportedLocale(); var gui = _klarnaCheckoutUtils.GetGui(); var options = _klarnaCheckoutUtils.GetOptions(); var klarnaOrder = new KlarnaOrder { Cart = cart, Merchant = merchant, Gui = gui, Options = options, Locale = supportedLocale.Locale, PurchaseCountry = supportedLocale.PurchaseCountry, PurchaseCurrency = supportedLocale.PurchaseCurrency }; var dictData = klarnaOrder.ToDictionary(); var connector = Connector.Create(_klarnaSettings.SharedSecret); var order = new Klarna.Checkout.Order(connector) { BaseUri = new Uri(BaseUri), ContentType = ContentType }; order.Create(dictData); var location = order.Location; var kcoOrderRequest = GetKcoOrderRequest(_workContext.CurrentCustomer, location); _klarnaRepository.Insert(kcoOrderRequest); return location; }
private ProcessPaymentResult ProcessKlarnaOrder(KlarnaLocalization localization, ProcessPaymentEvaluationContext context) { var retVal = new ProcessPaymentResult(); var cartItems = CreateKlarnaCartItems(context.Order); //Create cart var cart = new Dictionary<string, object> { { "items", cartItems } }; var data = new Dictionary<string, object> { { "cart", cart } }; //Create klarna order "http://example.com" context.Store.Url var connector = Connector.Create(AppSecret); Order order = null; var merchant = new Dictionary<string, object> { { "id", AppKey }, { "terms_uri", string.Format("{0}/{1}", context.Store.Url, TermsUrl) }, { "checkout_uri", string.Format("{0}/{1}", context.Store.Url, CheckoutUrl) }, { "confirmation_uri", string.Format("{0}/{1}?sid=123&orderId={2}&", context.Store.Url, ConfirmationUrl, context.Order.Id) + "klarna_order={checkout.order.uri}" }, { "push_uri", string.Format("{0}/{1}?sid=123&orderId={2}&", context.Store.Url, "admin/api/paymentcallback", context.Order.Id) + "klarna_order={checkout.order.uri}" }, { "back_to_store_uri", context.Store.Url } }; var layout = new Dictionary<string, object> { { "layout", "desktop" } }; data.Add("purchase_country", localization.CountryName); data.Add("purchase_currency", localization.Currency); data.Add("locale", localization.Locale); data.Add("merchant", merchant); data.Add("gui", layout); order = new Order(connector) { BaseUri = new Uri(GetCheckoutBaseUrl(localization.Currency)), ContentType = _contentType }; order.Create(data); order.Fetch(); //Gets snippet var gui = order.GetValue("gui") as JObject; var html = gui["snippet"].Value<string>(); retVal.IsSuccess = true; retVal.NewPaymentStatus = context.Payment.PaymentStatus = PaymentStatus.Pending; retVal.HtmlForm = html; retVal.OuterId = context.Payment.OuterId = order.GetValue("id") as string; return retVal; }
public ViewResult Checkout(Cart cart) { var cartItems = new List<Dictionary<string, object>>(); foreach(CartLine item in cart.Lines) { cartItems.Add(new Dictionary<string, object> { { "reference", item.Product.ArticleNumber.ToString() }, { "name", item.Product.Name }, { "quantity", item.Quantity }, { "unit_price", (int)item.Product.Price * 100 }, { "tax_rate", 2500 } }); } cartItems.Add( new Dictionary<string, object> { { "type", "shipping_fee" }, { "reference", "SHIPPING" }, { "name", "Shipping Fee" }, { "quantity", 1 }, { "unit_price", 4900 }, { "tax_rate", 2500 } }); var cart_info = new Dictionary<string, object> { { "items", cartItems } }; var data = new Dictionary<string, object> { { "cart", cart_info } }; var merchant = new Dictionary<string, object> { { "id", "5160" }, { "back_to_store_uri", "http://127.0.0.1:53284/" }, { "terms_uri", "http://127.0.0.1:53284/Cart/Terms" }, { "checkout_uri", "http://127.0.0.1:53284/sv/Cart/Checkout" }, { "confirmation_uri", "http://127.0.0.1:53284/sv/Cart/OrderConfirmed" + "?klarna_order_id={checkout.order.id}" }, { "push_uri", "https://snowroller.azurewebsites.net/Home/Callback" + "?secret="+Shared_Secret+"&klarna_order_id={checkout.order.id}" } }; data.Add("purchase_country", "SE"); data.Add("purchase_currency", "SEK"); data.Add("locale", "sv-se"); data.Add("merchant", merchant); var connector = Connector.Create(Shared_Secret, Connector.TestBaseUri); Order order = new Order(connector); order.Create(data); order.Fetch(); // Store order id of checkout in session object. Session.Add("klarna_order_id", order.GetValue("id")); // Display checkout var gui = order.GetValue("gui") as JObject; var snippet = gui["snippet"]; return View(snippet); }