def shipping_rate(context, **kwargs): """Return the shipping rate for a country & shipping option name. """ settings = LongclawSettings.for_site(context["request"].site) code = kwargs.get('code', None) name = kwargs.get('name', None) return get_shipping_cost(settings, code, name)
def shipping_cost(request): ''' Returns the shipping cost for a given country If the shipping cost for the given country has not been set, it will fallback to the default shipping cost if it has been enabled in the app settings ''' try: code = request.query_params.get('country_code') except AttributeError: return Response(data={"message": "No country code supplied"}, status=status.HTTP_400_BAD_REQUEST) option = request.query_params.get('shipping_rate_name', 'standard') try: settings = LongclawSettings.for_site(request.site) data = utils.get_shipping_cost(code, option, settings) response = Response(data=data, status=status.HTTP_200_OK) except utils.InvalidShippingRate: response = Response(data={"message": "Shipping option {} is invalid".format(option)}, status=status.HTTP_400_BAD_REQUEST) except utils.InvalidShippingCountry: response = Response(data={"message": "Shipping to {} is not available".format(code)}, status=status.HTTP_400_BAD_REQUEST) return response
def create_payment(self, request, amount, description=''): try: currency = LongclawSettings.for_site(request.site).currency charge = stripe.Charge.create( amount=int(math.ceil(amount * 100)), # Amount in pence currency=currency.lower(), source=request.data['token'], description=description) return charge.id except stripe.error.CardError as error: raise PaymentError(error)
def get_context(self): settings = LongclawSettings.for_site(self.request.site) sales = stats.sales_for_time_period(*stats.current_month()) return { 'total': "{}{}".format(settings.currency_html_code, sum(order.total for order in sales)), 'text': 'In sales this month', 'url': '/admin/longclaworders/order/', 'icon': 'icon-tick' }
def __init__(self, *args, **kwargs): site = kwargs.pop('site', None) super(AddressForm, self).__init__(*args, **kwargs) # Edit the country field to only contain # countries specified for shipping all_countries = True if site: settings = LongclawSettings.for_site(site) all_countries = settings.default_shipping_enabled if all_countries: queryset = Country.objects.all() else: queryset = Country.objects.exclude(shippingrate=None) self.fields['country'] = ModelChoiceField(queryset)
def create_payment(self, request, amount, description=''): longclaw_settings = LongclawSettings.for_site(request.site) nonce = request.POST.get('payment_method_nonce') result = self.gateway.transaction.sale({ "amount": str(amount), "payment_method_nonce": nonce, "merchant_account_id": longclaw_settings.currency, "options": { "paypal": { "description": description } } }) if not result.is_success: raise PaymentError(result.message) return result.transaction.order_id
def create_order(email, request, addresses=None, shipping_address=None, billing_address=None, shipping_option=None, capture_payment=False): ''' Create an order from a basket and customer infomation ''' basket_items, _ = get_basket_items(request) if addresses: # Longclaw < 0.2 used 'shipping_name', longclaw > 0.2 uses a consistent # prefix (shipping_address_xxxx) try: shipping_name = addresses['shipping_name'] except KeyError: shipping_name = addresses['shipping_address_name'] shipping_country = addresses['shipping_address_country'] if not shipping_country: shipping_country = None shipping_address, _ = Address.objects.get_or_create(name=shipping_name, line_1=addresses[ 'shipping_address_line1'], city=addresses[ 'shipping_address_city'], postcode=addresses[ 'shipping_address_zip'], country=shipping_country) shipping_address.save() try: billing_name = addresses['billing_name'] except KeyError: billing_name = addresses['billing_address_name'] billing_country = addresses['shipping_address_country'] if not billing_country: billing_country = None billing_address, _ = Address.objects.get_or_create(name=billing_name, line_1=addresses[ 'billing_address_line1'], city=addresses[ 'billing_address_city'], postcode=addresses[ 'billing_address_zip'], country=billing_country) billing_address.save() else: shipping_country = shipping_address.country ip_address = get_real_ip(request) if shipping_country and shipping_option: site_settings = LongclawSettings.for_site(request.site) shipping_rate = get_shipping_cost( shipping_address.country.pk, shipping_option, site_settings) else: shipping_rate = 0 order = Order( email=email, ip_address=ip_address, shipping_address=shipping_address, billing_address=billing_address, shipping_rate=shipping_rate ) order.save() # Create the order items & compute total total = 0 for item in basket_items: total += item.total() order_item = OrderItem( product=item.variant, quantity=item.quantity, order=order ) order_item.save() if capture_payment: desc = 'Payment from {} for order id #{}'.format(email, order.id) transaction_id = GATEWAY.create_payment(request, float(total) + shipping_rate, description=desc) order.payment_date = timezone.now() order.transaction_id = transaction_id # Once the order has been successfully taken, we can empty the basket destroy_basket(request) return order
def currency(request): settings = LongclawSettings.for_site(request.site) return { 'currency_html_code': settings.currency_html_code, 'currency': settings.currency }
def __init__(self, **kwargs): request = kwargs.get('request', None) self._all_countries = True if request: settings = LongclawSettings.for_site(request.site) self._all_countries = settings.default_shipping_enabled