Example #1
0
    def get_shipping_methods(self,
                             user,
                             basket,
                             shipping_addr=None,
                             request=None,
                             **kwargs):
        """
        Return a list of all applicable shipping method objects
        for a given basket, address etc.

        """
        if not basket.is_shipping_required():
            return [NoShippingRequired()]
        # We need to instantiate each method class to avoid thread-safety
        # issues
        self.prepare_methods(basket, shipping_addr, **kwargs)
        # 1. return preliminary methods
        if 'preliminary' in kwargs:
            return self.prime_methods(basket, self.preliminary_methods)
        # 3. return final (not preliminary) methods
        if 'preliminary_code' in kwargs:
            method = self.methods_dict.get(kwargs['preliminary_code'], None)
            if method is not None:
                if method in self.sufficient_methods:
                    return (self.prime_method(basket, method), )
                else:
                    if len(self.final_methods):
                        return self.prime_methods(basket, self.final_methods)
        # 2. return sufficient methods (default)
        return self.prime_methods(basket, self.sufficient_methods)
Example #2
0
    def get_shipping_method(self, basket, shipping_address=None, **kwargs):
        """
        Return the shipping method used
        """
        if not basket.is_shipping_required():
            return NoShippingRequired()

        # Instantiate a new FixedPrice shipping method instance
        charge_incl_tax = D(self.txn.value('PAYMENTREQUEST_0_SHIPPINGAMT'))

        # Assume no tax for now
        charge_excl_tax = charge_incl_tax
        name = self.txn.value('SHIPPINGOPTIONNAME')

        session_method = super(SuccessResponseView, self).get_shipping_method(
            basket, shipping_address, **kwargs)
        if not session_method or (name and name != session_method.name):
            if name:
                method = self._get_shipping_method_by_name(name, basket, shipping_address)
            else:
                method = None
            if not method:
                method = FixedPrice(charge_excl_tax, charge_incl_tax)
                if session_method:
                    method.name = session_method.name
                    method.code = session_method.code
        else:
            method = session_method
        return method
Example #3
0
    def get(self, request, *args, **kwargs):
        # Check that the user's basket is not empty
        if request.basket.is_empty:
            messages.error(request, _("You need to add some items to your basket to checkout"))
            return HttpResponseRedirect(reverse('basket:summary'))

        # Check that shipping is required at all
        if not request.basket.is_shipping_required():
            self.checkout_session.use_shipping_method(NoShippingRequired().code)
            return self.get_success_response()

        # Check that shipping address has been completed
        if not self.checkout_session.is_shipping_address_set():
            messages.error(request, _("Please choose a shipping address"))
            return HttpResponseRedirect(reverse('checkout:shipping-address'))

        # Save shipping methods as instance var as we need them both here
        # and when setting the context vars.
        self._methods = self.get_available_shipping_methods()
        if len(self._methods) == 0:
            # No shipping methods available for given address
            messages.warning(request, _("Shipping is not available for your chosen address - please choose another"))
            return HttpResponseRedirect(reverse('checkout:shipping-address'))
        elif len(self._methods) == 1:
            # Only one shipping method - set this and redirect onto the next step
            self.checkout_session.use_shipping_method(self._methods[0].code)
            return self.get_success_response()

        # Must be more than one available shipping method, we present them to
        # the user to make a choice.
        return super(ShippingMethodView, self).get(request, *args, **kwargs)
Example #4
0
    def get(self, request, *args, **kwargs):
        # These pre-conditions can't easily be factored out into the normal
        # pre-conditions as they do more than run a test and then raise an
        # exception if it fails.

        # Check that shipping is required at all
        if not request.basket.is_shipping_required():
            # No shipping required - we store a special code to indicate so.
            self.checkout_session.use_shipping_method(
                NoShippingRequired().code)
            return self.get_success_response()

        # Check that shipping address has been completed
        if not self.checkout_session.is_shipping_address_set():
            messages.error(request, _("Please choose a shipping address"))
            return redirect('checkout:shipping-address')

        # Save shipping methods as instance var as we need them both here
        # and when setting the context vars.
        self._methods = self.get_available_shipping_methods()
        if len(self._methods) == 0:
            # No shipping methods available for given address
            messages.warning(request, _(
                "Shipping is unavailable for your chosen address - please "
                "choose another"))
            return redirect('checkout:shipping-address')
        elif len(self._methods) == 1:
            # Only one shipping method - set this and redirect onto the next
            # step
            self.checkout_session.use_shipping_method(self._methods[0].code)
            return self.get_success_response()

        # Must be more than one available shipping method, we present them to
        # the user to make a choice.
        return super(ShippingMethodView, self).get(request, *args, **kwargs)
Example #5
0
    def get_shipping_method(self, basket, shipping_address=None, **kwargs):
        """
        Return the shipping method used
        """
        if not basket.is_shipping_required():
            return NoShippingRequired()

        code = self.checkout_session.shipping_method_code(basket)
        shipping_method = self.get_current_shipping_method()

        allowed_countries = [country.pk for country in \
                                        shipping_method.countries.all()]
        if shipping_address.country.pk not in allowed_countries:
            countries = ", ".join(allowed_countries)
            message = _(
                "We do not yet ship to countries outside of {}.".format(
                    countries))
            messages.error(self.request, _(message))
            return None

        charge_incl_tax = D(self.txn.value('PAYMENTREQUEST_0_SHIPPINGAMT'))
        # Assume no tax for now
        charge_excl_tax = charge_incl_tax
        method = shipping_method

        name = self.txn.value('SHIPPINGOPTIONNAME')
        if shipping_method:
            method.name = shipping_method.name
        else:
            method.name = name
        return method
    def get_shipping_method(self, basket, shipping_address=None, **kwargs):
        """
        Return the shipping method used
        """
        if not basket.is_shipping_required():
            return NoShippingRequired()

        return super().get_shipping_method(basket, shipping_address, **kwargs)
Example #7
0
 def find_by_code(self, code, basket, shipping_addr=None):
     """
     Return the appropriate Method object for the given code
     """
     self.prepare_methods(basket, shipping_addr)
     # Check for NoShippingRequired as that is a special case
     if not code in self.methods_dict or code == NoShippingRequired.code:
         return self.prime_method(basket, NoShippingRequired())
     return self.prime_method(basket, self.methods_dict[code])
Example #8
0
class CheckoutSerializer(serializers.Serializer):
    system = serializers.CharField(max_length=4, min_length=4)
    card_method = serializers.ChoiceField(choices=['preauth', 'payment'],
                                          default='payment')
    shipping_method = serializers.CharField(default=NoShippingRequired().code)
    basket_owner = serializers.IntegerField(required=False, default=None)
    template = serializers.CharField(required=False, default=None)
    fallback_url = serializers.URLField()
    return_preload_url = serializers.URLField(required=False, default=None)
    return_url = serializers.URLField()
    associate_invoice_with_token = serializers.BooleanField(default=False)
    force_redirect = serializers.BooleanField(default=False)
    send_email = serializers.BooleanField(default=False)
    proxy = serializers.BooleanField(default=False)
    checkout_token = serializers.BooleanField(default=False)
    bpay_format = serializers.ChoiceField(choices=['crn', 'icrn'],
                                          default='crn')
    icrn_format = serializers.ChoiceField(
        choices=['ICRNAMT', 'ICRNDATE', 'ICRNAMTDATE'], default='ICRNAMT')
    icrn_date = serializers.DateField(required=False, default=None)
    invoice_text = serializers.CharField(required=False, default=None)
    check_url = serializers.URLField(required=False, default=None)
    amount_override = serializers.FloatField(required=False, default=None)

    def validate(self, data):
        if data['proxy'] and not data['basket_owner']:
            raise serializers.ValidationError(
                'A proxy payment requires the basket_owner to be set.')
        return data

    def validate_system(self, value):
        if not value:
            raise serializers.ValidationError('No system ID provided')
        elif not len(value) == 4:
            raise serializers.ValidationError(
                'The system ID should be 4 characters long')
        if not is_valid_system(value):
            raise serializers.ValidationError('The system ID is not valid')

        return value

    def validate_template(self, value):
        if value is not None:
            try:
                get_template(value)
            except TemplateDoesNotExist as e:
                raise serializers.ValidationError(str(e))
        return value

    def validate_basket_owner(self, value):
        if value is not None:
            try:
                EmailUser.objects.get(id=value)
            except EmailUser.DoesNotExist as e:
                raise serializers.ValidationError(str(e))
        return value
Example #9
0
    def find_by_code(self, code, basket):
        """
        Return the appropriate Method object for the given code
        """
        for method in self.methods:
            if method.code == code:
                return self.prime_method(basket, method)

        # Check for NoShippingRequired as that is a special case
        if code == NoShippingRequired.code:
            return self.prime_method(basket, NoShippingRequired())
 def find_by_code(self, code, basket):
     if code == NoShippingRequired.code:
         method = NoShippingRequired()
     else:
         method = None
         for method_ in METHODS:
             if method_.code == code:
                 method = method_
         if method is None:
             raise ValueError(
                 "No shipping method found with code '%s'" % code)
     return self.prime_method(basket, method)
Example #11
0
    def get_shipping_method(self, basket, shipping_address=None, **kwargs):
        """
        Return the shipping method used
        """
        if not basket.is_shipping_required():
            return NoShippingRequired()

        # Instantiate a new FixedPrice shipping method instance
        charge_incl_tax = D(self.txn.value('PAYMENTREQUEST_0_SHIPPINGAMT'))
        # Assume no tax for now
        charge_excl_tax = charge_incl_tax
        method = FixedPrice(charge_excl_tax, charge_incl_tax)
        method.set_basket(basket)
        name = self.txn.value('SHIPPINGOPTIONNAME')
        if not name:
            # Look to see if there is a method in the session
            session_method = self.checkout_session.shipping_method_code(basket)
            if session_method:
                method.name = session_method  #.name
        else:
            method.name = name
        return method
Example #12
0
 def find_by_code(self, code, basket):
     if code == NoShippingRequired.code:
         method = NoShippingRequired()
     else:
         method = self.methods.get(code, None)
     return self.prime_method(basket, method)
Example #13
0
 def find_by_code(self, code):
     if code == NoShippingRequired.code:
         return NoShippingRequired()
     return self.methods.get(code, None)