# 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. import klarnacheckout eid = '0' shared_secret = 'shared_secret' connector = klarnacheckout.create_connector(shared_secret, klarnacheckout.BASE_TEST_URL) order = None merchant = { 'id': eid, 'terms_uri': 'http://example.com/terms.html', 'checkout_uri': 'http://example.com/checkout', 'confirmation_uri': ('http://example.com/thank-you' + '?klarna_order_id={checkout.order.id}'), # You can not receive push notification on # a non publicly available uri 'push_uri': ('http://example.com/push' + '?klarna_order_id={checkout.order.id}') }
'tax_rate': 2500 } ) # Merchant ID eid = "0" # Shared Secret shared_secret = 'shared_secret' klarnacheckout.Order.content_type = \ 'application/vnd.klarna.checkout.aggregated-order-v2+json' klarnacheckout.Order.base_uri = \ 'https://checkout.testdrive.klarna.com/checkout/orders' connector = klarnacheckout.create_connector(shared_secret) resource_location = \ 'https://checkout.testdrive.klarna.com/checkout/orders/ABC123' order = klarnacheckout.Order(connector, resource_location) # Reset cart data = {'cart': []} data["cart"] = {"items": []} for item in cart: data["cart"]["items"].append(item) order.update(data) # [[examples-update]]
def __call__(self, **kw): uid = self.request['uid'] base_url = self.context.absolute_url() registry = getUtility(IRegistry) settings = registry.forInterface(IKlarnaPaymentSettings) data = IPaymentData(self.context).data(uid) #amount = data['amount'] #description = data['description'] #ordernumber = data['ordernumber'] currency = data['currency'] #get items for klarna order_data = OrderData(self.context, uid) order = dict(order_data.order.attrs) # Merchant ID eid = settings.klarna_eid # Shared Secret shared_secret = settings.klarna_secret #other settings from control panel terms_uri = settings.klarna_terms_uri checkout_uri = settings.klarna_checkout_uri confirmation_uri = settings.klarna_confirmation_uri push_uri = settings.klarna_push_uri #Add the cart items cart = list() for booking in order_data.bookings: cart.append({ 'quantity': int(booking.attrs['buyable_count']), 'reference': uuidToURL(booking.attrs['buyable_uid']), 'name': booking.attrs['title'], 'unit_price': int((booking.attrs.get('net', 0.0)*100)+(booking.attrs.get('net', 0.0)*booking.attrs.get('vat', 0.0))), 'discount_rate': int((booking.attrs['discount_net'])*100), 'tax_rate': int(booking.attrs.get('vat', 0.0)*100), }) create_data = {} create_data["cart"] = {"items": []} for item in cart: create_data["cart"]["items"].append(item) #Configure the checkout order #import pdb; pdb.set_trace() create_data['purchase_country'] = 'NO' create_data['purchase_currency'] = currency create_data['locale'] = 'nb-no' create_data['shipping_address'] = { 'email' : order['personal_data.email'], 'postal_code' : order['billing_address.zip'], } create_data['merchant'] = { 'id' : eid, 'terms_uri' : terms_uri, 'checkout_uri' : checkout_uri, 'confirmation_uri' : confirmation_uri, 'push_uri' : push_uri, } # Create a checkout order 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 = klarnacheckout.Order(connector) order.create(create_data) # Render the checkout snippet order.fetch() # Store location of checkout session #session["klarna_checkout"] = order.location # Display checkout return "<div>%s</div>" % (order["gui"]["snippet"])
# # 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]]
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"]