def add_extra_cart_row(self, cart, request): if not self.is_active(cart.extra.get('payment_modifier')) and len(cart_modifiers_pool.get_payment_modifiers()) > 1: return False amount = Money('30') instance = {'label': _("Extra charge by post"), 'amount': amount} cart.extra_rows[self.identifier] = ExtraCartRow(instance) cart.total += amount
def purchase(self, request): """ This is the final step on converting a cart into an order object. It normally is used in combination with the AngularJS directive `shop-dialog-proceed` used in combination with `proceedWith("PURCHASE_NOW")`. Use the plugin `shop.cascade.checkout.ProceedButtonPlugin` to render such a button into any placeholder field. """ cart = self.get_queryset() if cart.pk is None: raise ValidationError("Can not purchase without a cart") cart.update(request) cart.save() response = self.list(request) # Iterate over the registered modifiers, and search for the active payment service provider for modifier in cart_modifiers_pool.get_payment_modifiers(): if modifier.is_active(cart): payment_provider = getattr(modifier, 'payment_provider', None) if payment_provider: expression = payment_provider.get_payment_request( cart, request) response.data.update(expression=expression) break return response
def get_formset(self, request, obj=None, **kwargs): """ Convert the field `payment_method` into a select box with all possible payment methods. """ choices = [pm.get_choice() for pm in cart_modifiers_pool.get_payment_modifiers()] kwargs.update(widgets={'payment_method': widgets.Select(choices=choices)}) formset = super(OrderPaymentInline, self).get_formset(request, obj, **kwargs) return formset
def get_formset(self, request, obj=None, **kwargs): """ Convert the field `payment_method` into a select box with all possible payment methods. """ choices = [pm.get_choice() for pm in cart_modifiers_pool.get_payment_modifiers()] kwargs.update(widgets={'payment_method': widgets.Select(choices=choices)}) formset = super(OrderPaymentInline, self).get_formset(request, obj, **kwargs) return formset
def render(self, context, instance, placeholder): self.super(PaymentMethodFormPlugin, self).render(context, instance, placeholder) for payment_modifier in cart_modifiers_pool.get_payment_modifiers(): payment_modifier.update_render_context(context) context['show_additional_charge'] = instance.glossary.get( 'show_additional_charge', False) return context
def __init__(self, *args, **kwargs): super(PaymentMethodForm, self).__init__(*args, **kwargs) choices = [ x.get_choice() for x in cart_modifiers_pool.get_payment_modifiers() if not x.is_disabled(self.cart) ] self.fields['payment_modifier'].choices = choices if len(choices) == 1: self.fields['payment_modifier'].initial = choices[0][0]
def __init__(self, *args, **kwargs): choices = [m.get_choice() for m in cart_modifiers_pool.get_payment_modifiers() if not m.is_disabled(kwargs['cart'])] self.base_fields['payment_modifier'].choices = choices if len(choices) == 1: # if there is only one shipping method available, always set it as default try: kwargs['initial']['payment_modifier'] = choices[0][0] except KeyError: pass super(PaymentMethodForm, self).__init__(*args, **kwargs)
def __init__(self, *args, **kwargs): choices = [m.get_choice() for m in cart_modifiers_pool.get_payment_modifiers() if not m.is_disabled(kwargs['cart'])] self.base_fields['payment_modifier'].choices = choices if len(choices) == 1: # if there is only one shipping method available, always set it as default try: kwargs['initial']['payment_modifier'] = choices[0][0] except KeyError: pass super(PaymentMethodForm, self).__init__(*args, **kwargs)
def purchase(self, request): """ This is the final step on converting a cart into an order object. It normally is used in combination with the plugin :class:`shop.cascade.checkout.ProceedButtonPlugin` to render a button labeled "Purchase Now". """ cart = CartModel.objects.get_from_request(request) try: cart.update(request, raise_exception=True) except ProductNotAvailable as exc: message = _("The product '{product_name}' ({product_code}) suddenly became unavailable, "\ "presumably because someone else has been faster purchasing it.\n Please "\ "recheck the cart or add an alternative product and proceed with the checkout.").\ format(product_name=exc.product.product_name, product_code=exc.product.product_code) messages.error(request, message, title=_("Product Disappeared"), delay=10) message = _("The product '{product_name}' ({product_code}) suddenly became unavailable.").\ format(product_name=exc.product.product_name, product_code=exc.product.product_code) response_data = {'purchasing_error_message': message} return Response(data=response_data, status=status.HTTP_422_UNPROCESSABLE_ENTITY) cart.save() response_data = {} try: # Iterate over the registered modifiers, and search for the active payment service provider for modifier in cart_modifiers_pool.get_payment_modifiers(): if modifier.is_active(cart.extra.get('payment_modifier')): expression = modifier.payment_provider.get_payment_request( cart, request) response_data.update(expression=expression) break except ValidationError as err: message = _("Please select a valid payment method.") messages.warning(request, message, title=_("Choose Payment Method"), delay=5) response_data = {'purchasing_error_message': '. '.join(err.detail)} return Response(data=response_data, status=status.HTTP_422_UNPROCESSABLE_ENTITY) return Response(data=response_data)
def forms_valid(self, **forms): """ All the forms are valid, make the purchase happen. Return's JSON. """ forms['customer_form'].save() self.cart.shipping_address = forms['shipping_form'].save() self.cart.billing_address = forms['billing_form'].save() self.cart.extra.update(forms['payment_form'].cleaned_data) self.cart.extra.update(forms['delivery_form'].cleaned_data) self.cart.extra.update(forms['extra_form'].cleaned_data) self.update_cart() self.cart.save() for modifier in cart_modifiers_pool.get_payment_modifiers(): if modifier.is_active(self.cart): payment_provider = getattr(modifier, 'payment_provider', None) if payment_provider: expression = payment_provider.get_payment_request(self.cart, self.request) return JsonResponse({'expression': expression}) return HttpResponseBadRequest()
def purchase(self, request): """ This is the final step on converting a cart into an order object. It normally is used in combination with the plugin :class:`shop.cascade.checkout.ProceedButtonPlugin` to render a button labeled "Purchase Now". """ cart = CartModel.objects.get_from_request(request) cart.update(request) cart.save() response_data = {} # Iterate over the registered modifiers, and search for the active payment service provider for modifier in cart_modifiers_pool.get_payment_modifiers(): if modifier.is_active(cart): payment_provider = getattr(modifier, 'payment_provider', None) if payment_provider: expression = payment_provider.get_payment_request( cart, request) response_data.update(expression=expression) break return Response(data=response_data)
def purchase(self, request): """ This is the final step on converting a cart into an order object. It normally is used in combination with the plugin :class:`shop.cascade.checkout.ProceedButtonPlugin` to render a button labeled "Purchase Now". """ cart = CartModel.objects.get_from_request(request) cart.update(request) cart.save() response_data = {} try: # Iterate over the registered modifiers, and search for the active payment service provider for modifier in cart_modifiers_pool.get_payment_modifiers(): if modifier.is_active(cart.extra.get('payment_modifier')): expression = modifier.payment_provider.get_payment_request(cart, request) response_data.update(expression=expression) break except ValidationError as err: response_data = {'purchasing_error_message': '. '.join(err.detail)} return Response(data=response_data, status=status.HTTP_422_UNPROCESSABLE_ENTITY) return Response(data=response_data)
def purchase(self, request): """ This is the final step on converting a cart into an order object. It normally is used in combination with the plugin :class:`shop.cascade.checkout.ProceedButtonPlugin` to render a button labeled "Purchase Now". """ cart = CartModel.objects.get_from_request(request) cart.update(request) cart.save() response_data = {} try: # Iterate over the registered modifiers, and search for the active payment service provider for modifier in cart_modifiers_pool.get_payment_modifiers(): if modifier.is_active(cart.extra.get('payment_modifier')): expression = modifier.payment_provider.get_payment_request( cart, request) response_data.update(expression=expression) break except ValidationError as err: response_data = {'purchasing_error_message': '. '.join(err.detail)} return Response(data=response_data, status=status.HTTP_422_UNPROCESSABLE_ENTITY) return Response(data=response_data)
def purchase(self, request): """ This is the final step on converting a cart into an order object. It normally is used in combination with the AngularJS directive `shop-dialog-proceed` used in combination with `proceedWith("PURCHASE_NOW")`. Use the plugin `shop.cascade.checkout.ProceedButtonPlugin` to render such a button into any placeholder field. """ cart = self.get_queryset() if cart is None: raise ValidationError("Can not purchase without a cart") cart.update(request) cart.save() response = self.list(request) # Iterate over the registered modifiers, and search for the active payment service provider for modifier in cart_modifiers_pool.get_payment_modifiers(): if modifier.is_active(cart): payment_provider = getattr(modifier, 'payment_provider', None) if payment_provider: expression = payment_provider.get_payment_request(cart, request) response.data.update(expression=expression) break return response
# -*- coding: utf-8 -*- from __future__ import unicode_literals import warnings from django.conf.urls import patterns, include from shop.modifiers.pool import cart_modifiers_pool urlpatterns = patterns('') # For every payment modifier, load the URLs from the associated `payment_provider`. for modifier in cart_modifiers_pool.get_payment_modifiers(): try: namespace = modifier.payment_provider.namespace regexp = r'^{}/'.format(namespace) urls = modifier.payment_provider.get_urls() pattern = patterns('', (regexp, include(urls, namespace=namespace))) urlpatterns = pattern + urlpatterns except AttributeError as err: warnings.warn(err.message)
def render(self, context, instance, placeholder): super(PaymentMethodFormPlugin, self).render(context, instance, placeholder) for payment_modifier in cart_modifiers_pool.get_payment_modifiers(): payment_modifier.update_render_context(context) return context
if cart: form_data.update( initial={ 'payment_modifier': cart.extra.get('payment_modifier') }) return form_data def render(self, context, instance, placeholder): super(PaymentMethodFormPlugin, self).render(context, instance, placeholder) for payment_modifier in cart_modifiers_pool.get_payment_modifiers(): payment_modifier.update_render_context(context) return context if cart_modifiers_pool.get_payment_modifiers(): # Plugin is registered only if at least one payment modifier exists DialogFormPluginBase.register_plugin(PaymentMethodFormPlugin) class ShippingMethodFormPlugin(DialogFormPluginBase): name = _("Shipping Method Form") form_class = 'shop.forms.checkout.ShippingMethodForm' template_leaf_name = 'shipping-method-{}.html' def get_form_data(self, context, instance, placeholder): form_data = super(ShippingMethodFormPlugin, self).get_form_data(context, instance, placeholder) cart = form_data.get('cart') if cart: form_data.update(
def test_payment_modifiers_with_same_processors(api_rf, filled_cart): for modifier_to_test in cart_modifiers_pool.get_payment_modifiers(): for modifier_for_id in cart_modifiers_pool.get_payment_modifiers(): if modifier_to_test.is_active(modifier_for_id.identifier): assert modifier_for_id.identifier == modifier_to_test.identifier
def render(self, context, instance, placeholder): self.super(PaymentMethodFormPlugin, self).render(context, instance, placeholder) for payment_modifier in cart_modifiers_pool.get_payment_modifiers(): payment_modifier.update_render_context(context) return context
template_leaf_name = 'payment-method-{}.html' def get_form_data(self, context, instance, placeholder): form_data = self.super(PaymentMethodFormPlugin, self).get_form_data(context, instance, placeholder) cart = form_data.get('cart') if cart: form_data.update(initial={'payment_modifier': cart.extra.get('payment_modifier')}) return form_data def render(self, context, instance, placeholder): self.super(PaymentMethodFormPlugin, self).render(context, instance, placeholder) for payment_modifier in cart_modifiers_pool.get_payment_modifiers(): payment_modifier.update_render_context(context) return context if cart_modifiers_pool.get_payment_modifiers(): # Plugin is registered only if at least one payment modifier exists DialogFormPluginBase.register_plugin(PaymentMethodFormPlugin) class ShippingMethodFormPlugin(DialogFormPluginBase): name = _("Shipping Method Form") form_class = 'shop.forms.checkout.ShippingMethodForm' template_leaf_name = 'shipping-method-{}.html' def get_form_data(self, context, instance, placeholder): form_data = self.super(ShippingMethodFormPlugin, self).get_form_data(context, instance, placeholder) cart = form_data.get('cart') if cart: form_data.update(initial={'shipping_modifier': cart.extra.get('shipping_modifier')}) return form_data
# -*- coding: utf-8 -*- from __future__ import unicode_literals import warnings from django.conf.urls import patterns, include from shop.modifiers.pool import cart_modifiers_pool urlpatterns = patterns('') # For every payment modifier, load the URLs from the associated `payment_provider`. for modifier in cart_modifiers_pool.get_payment_modifiers(): try: namespace = modifier.payment_provider.namespace regexp = r'^{}/'.format(namespace) urls = modifier.payment_provider.get_urls() pattern = patterns('', (regexp, include(urls, namespace=namespace)) ) urlpatterns = pattern + urlpatterns except AttributeError as err: warnings.warn(err.message)
def render(self, context, instance, placeholder): self.super(PaymentMethodFormPlugin, self).render(context, instance, placeholder) for payment_modifier in cart_modifiers_pool.get_payment_modifiers(): payment_modifier.update_render_context(context) context['show_additional_charge'] = instance.glossary.get('show_additional_charge', False) return context