{ 'quantity': 1, 'reference': '123456789', 'name': 'Klarna t-shirt', 'unit_price': 12300, 'discount_rate': 1000, 'tax_rate': 2500 }, { 'quantity': 1, 'type': 'shipping_fee', 'reference': 'SHIPPING', 'name': 'Shipping Fee', 'unit_price': 4900, 'tax_rate': 2500 } ) for item in cart: data['cart']['items'].append(item) try: order = klarnacheckout.Order(connector) order.create(data) order.fetch() order_id = order['id'] print('Order ID: %s' % order_id) except klarnacheckout.HTTPResponseException as e: print(e.json.get('http_status_message')) print(e.json.get('internal_message'))
# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # [[examples-fetch]] import klarnacheckout # Shared Secret shared_secret = 'shared_secret' klarnacheckout.Order.content_type = \ 'application/vnd.klarna.checkout.aggregated-order-v2+json' resource_location = \ 'https://checkout.testdrive.klarna.com/checkout/orders/ABC123' connector = klarnacheckout.create_connector(shared_secret) order = klarnacheckout.Order(connector, resource_location) order.fetch() # [[examples-fetch]]
'name': 'Klarna t-shirt', 'unit_price': 12300, 'discount_rate': 1000, 'tax_rate': 2500 }, { 'quantity': 1, 'type': 'shipping_fee', 'reference': 'SHIPPING', 'name': 'Shipping Fee', 'unit_price': 4900, 'tax_rate': 2500 }) if 'klarna_order_id' in session: # Resume session order = klarnacheckout.Order(connector, session['klarna_order_id']) try: order.fetch() update_data = {} update_data['cart'] = {} # Reset cart update_data['cart']['items'] = [] for item in cart: update_data['cart']['items'].append(item) order.update(update_data) except klarnacheckout.HTTPResponseException as e: # Reset session order = None
# Shared Secret shared_secret = 'shared_secret' klarnacheckout.Order.base_uri = \ 'https://checkout.testdrive.klarna.com/checkout/orders' klarnacheckout.Order.content_type = \ 'application/vnd.klarna.checkout.aggregated-order-v2+json' connector = klarnacheckout.create_connector(shared_secret) order = None if 'klarna_checkout' in session: # Resume session order = klarnacheckout.Order(connector, session["klarna_checkout"]) try: order.fetch() update_data = {} update_data["cart"] = {} # Reset cart update_data["cart"]["items"] = [] for item in cart: update_data["cart"]["items"].append(item) order.update(update_data) except: # Reset session order = None
def connect_to_klarna(self, request): """ Connects to klarnas API and make an order object at klarna with products from basket. Cart example below: cart = [( { 'quantity': 1, 'reference': '123456789', 'name': 'Klarna t-shirt', 'unit_price': 12300, 'discount_rate': 1000, 'tax_rate': 2500 }, { 'quantity': 1, 'type': 'shipping_fee', 'reference': 'SHIPPING', 'name': 'Shipping Fee', 'unit_price': 4900, 'tax_rate': 2500 } )] """ cart = [] basket = self.request.basket for line in basket.all_lines(): # Adding each line from basket to klarna cart # klarna requires list of tuples where shippment # price is included for each product line_json = ({ 'quantity' : line.quantity, 'reference' : str(line.product.pk), 'name' : str(line.product.get_title()), 'unit_price' : decimal_to_longint(line.unit_price_incl_tax), 'discount_rate' : decimal_to_longint(line.discount_value), 'tax_rate' : decimal_to_longint(line._tax_ratio), },{ 'quantity': 1, 'type': 'shipping_fee', 'reference': 'SHIPPING', 'name': 'Shipping Fee', 'unit_price': 0, 'discount_rate' : 0, 'tax_rate': 0, }) cart.append(line_json) # Creating data for klarna create_data = {} create_data["cart"] = {"items": []} for obj in cart: for item in obj: create_data["cart"]["items"].append(item) create_data['purchase_country'] = 'SE' create_data['purchase_currency'] = 'SEK' create_data['locale'] = 'sv-se' create_data['merchant'] = { 'id': '7485', 'back_to_store_uri': 'http://109.124.136.183:8000', 'terms_uri': 'http://109.124.136.183:8000/terms', 'checkout_uri': 'http://109.124.136.183:8000/checkout', 'confirmation_uri': ('http://109.124.136.183:8000/thank-you' + '?klarna_order_id={checkout.order.id}'), 'push_uri': ('http://109.124.136.183:8000/push' + '?klarna_order_id={checkout.order.id}') } connector = klarnacheckout.create_connector('axxB71XSbDuTyox', klarnacheckout.BASE_TEST_URL) order = klarnacheckout.Order(connector) order.create(create_data) order.fetch() # Store order id of checkout session request.session['klarna_order_id'] = order['id'] return order["gui"]["snippet"]