def clean(self): """ Pre save validation: - A payment product and all its variants must exist before creating a LicenceType. - Check for senior voucher if applicable. :return: raise an exception if error """ variant_codes = payment_utils.generate_product_title_variants(self) missing_product_variants = [] for variant_code in variant_codes: if payment_utils.get_product(variant_code) is None: missing_product_variants.append(variant_code) if missing_product_variants: msg = mark_safe( "The payments products with titles matching the below list of product codes were not " "found. Note: You must create a payment product(s) for a new licence type and all its " "variants, even if the licence has no fee. <ul><li>{}</li></ul>" .format('</li><li>'.join(missing_product_variants))) raise ValidationError(msg) if self.senior_applicable and payment_utils.get_voucher( settings.WL_SENIOR_VOUCHER_CODE) is None: msg = mark_safe( "The senior voucher with code={} cannot be found. It must be created before setting a " "licence type to be senior applicable.<br>" "Note: the senior voucher code can be changed in the settings of the application." .format(settings.WL_SENIOR_VOUCHER_CODE)) raise ValidationError(msg)
def clean(self): """ Pre save validation: - A payment product and all its variants must exist before creating a LicenceType. - Check for senior voucher if applicable. :return: raise an exception if error """ variant_codes = payment_utils.generate_product_code_variants(self) missing_product_variants = [] for variant_code in variant_codes: if payment_utils.get_product(variant_code) is None: missing_product_variants.append(variant_code) if missing_product_variants: msg = mark_safe("The payments products with titles matching the below list of product codes were not " "found. Note: You must create a payment product(s) for a new licence type and all its " "variants, even if the licence is free. <ul><li>{}</li></ul>". format('</li><li>'.join(missing_product_variants))) raise ValidationError(msg) if self.senior_applicable and payment_utils.get_voucher(settings.WL_SENIOR_VOUCHER_CODE) is None: msg = mark_safe("The senior voucher with code={} cannot be found. It must be created before setting a " "licence type to be senior applicable.<br>" "Note: the senior voucher code can be changed in the settings of the application." .format(settings.WL_SENIOR_VOUCHER_CODE)) raise ValidationError(msg)
def clean(self): """ Checks if there are any licence types with this group or one it this group's parents set and makes sure there's products for every variant of those licence types. type. :return: """ if self.instance is not None: related_variant_groups = [] def __get_group_and_parent_groups(current_variant_group, groups): groups.append(current_variant_group) for group in VariantGroup.objects.filter( child=current_variant_group): __get_group_and_parent_groups(group, groups) __get_group_and_parent_groups(self.instance, related_variant_groups) # keep previous variants in case the validation fails if hasattr(self.instance, 'variants'): previous_variants = list(self.instance.variants.all()) self.instance.variants = self.cleaned_data['variants'] missing_product_variants = [] for licence_type in WildlifeLicenceType.objects.filter( variant_group__in=related_variant_groups): variant_codes = payment_utils.generate_product_title_variants( licence_type) for variant_code in variant_codes: if payment_utils.get_product(variant_code) is None: missing_product_variants.append(variant_code) if missing_product_variants: msg = mark_safe( "The payments products with titles matching the below list of product codes were not " "found. Note: You must create a payment product(s) for variants of each licence type linked " "to this variant group, even if the licence has no fee. <ul><li>{}</li></ul>" .format('</li><li>'.join(missing_product_variants))) if hasattr(self.instance, 'variants'): # revert back to previous variants list self.instance.variants = previous_variants raise ValidationError(msg)
def clean(self): """ Checks if there are any licence types with this group or one it this group's parents set and makes sure there's products for every variant of those licence types. type. :return: """ if self.instance is not None: related_variant_groups = [] def __get_group_and_parent_groups(current_variant_group, groups): groups.append(current_variant_group) for group in VariantGroup.objects.filter(child=current_variant_group): __get_group_and_parent_groups(group, groups) __get_group_and_parent_groups(self.instance, related_variant_groups) # keep previous variants in case the validation fails if hasattr(self.instance, "variants"): previous_variants = list(self.instance.variants.all()) self.instance.variants = self.cleaned_data["variants"] missing_product_variants = [] for licence_type in WildlifeLicenceType.objects.filter(variant_group__in=related_variant_groups): variant_codes = payment_utils.generate_product_code_variants(licence_type) for variant_code in variant_codes: if payment_utils.get_product(variant_code) is None: missing_product_variants.append(variant_code) if missing_product_variants: msg = mark_safe( "The payments products with titles matching the below list of product codes were not " "found. Note: You must create a payment product(s) for variants of each licence type linked " "to this variant group, even if the licence is free. <ul><li>{}</li></ul>".format( "</li><li>".join(missing_product_variants) ) ) if hasattr(self.instance, "variants"): # revert back to previous variants list self.instance.variants = previous_variants raise ValidationError(msg)
def get(self, request, *args, **kwargs): application = get_object_or_404(Application, pk=args[0]) product = get_product(generate_product_title(application)) user = application.applicant.id error_url = request.build_absolute_uri( reverse('wl_applications:preview')) success_url = request.build_absolute_uri( reverse('wl_applications:complete')) parameters = { 'system': PAYMENT_SYSTEM_ID, 'basket_owner': user, 'associateInvoiceWithToken': True, 'checkoutWithToken': True, 'fallback_url': error_url, 'return_url': success_url, 'forceRedirect': True, 'template': 'wl/payment_information.html', 'proxy': is_officer(request.user), "products": [{ "id": product.id if product is not None else None }], "vouchers": [] } headers = { 'X-CSRFToken': request.COOKIES.get('csrftoken'), 'Referer': request.META.get('HTTP_REFERER'), } headers.update(JSON_REQUEST_HEADER_PARAMS) # senior discount if application.is_senior_offer_applicable: parameters['vouchers'].append({'code': SENIOR_VOUCHER_CODE}) url = request.build_absolute_uri( reverse('payments:ledger-initial-checkout')) response = requests.post(url, headers=headers, cookies=request.COOKIES, data=json.dumps(parameters)) return HttpResponse(response.content)
def get(self, request, *args, **kwargs): application = get_object_or_404(Application, pk=args[0]) product = get_product(generate_product_title(application)) user = application.applicant.id error_url = request.build_absolute_uri( reverse('wl_applications:preview')) success_url = request.build_absolute_uri( reverse('wl_applications:complete')) basket_params = { 'products': [{ 'id': product.id if product is not None else None }], 'vouchers': [], 'system': PAYMENT_SYSTEM_ID } # senior discount if application.is_senior_offer_applicable: basket_params['vouchers'].append({'code': SENIOR_VOUCHER_CODE}) basket, basket_hash = create_basket_session(request, basket_params) checkout_params = { 'system': PAYMENT_SYSTEM_ID, 'basket_owner': user, 'associate_invoice_with_token': True, 'fallback_url': error_url, 'return_url': success_url, 'force_redirect': True, 'template': 'wl/payment_information.html', 'proxy': is_officer(request.user), } create_checkout_session(request, checkout_params) if checkout_params['proxy']: response = place_order_submission(request) else: response = HttpResponseRedirect(reverse('checkout:index')) return response
def get(self, request, *args, **kwargs): application = get_object_or_404(Application, pk=args[0]) product = get_product(generate_product_title(application)) user = application.applicant.id error_url = request.build_absolute_uri(reverse('wl_applications:preview')) success_url = request.build_absolute_uri(reverse('wl_applications:complete')) basket_params = { 'products': [ {'id': product.id if product is not None else None} ], 'vouchers': [], 'system': PAYMENT_SYSTEM_ID } # senior discount if application.is_senior_offer_applicable: basket_params['vouchers'].append({'code': SENIOR_VOUCHER_CODE}) basket, basket_hash = create_basket_session(request, basket_params) checkout_params = { 'system': PAYMENT_SYSTEM_ID, 'basket_owner': user, 'associate_invoice_with_token': True, 'fallback_url': error_url, 'return_url': success_url, 'force_redirect': True, 'template': 'wl/payment_information.html', 'proxy': is_officer(request.user), } create_checkout_session(request, checkout_params) if checkout_params['proxy']: response = place_order_submission(request) else: response = HttpResponseRedirect(reverse('checkout:index')) return response
def get(self, request, *args, **kwargs): application = get_object_or_404(Application, pk=args[0]) product = get_product(generate_product_code(application)) user = application.applicant.id error_url = request.build_absolute_uri(reverse('wl_applications:preview')) success_url = request.build_absolute_uri(reverse('wl_applications:complete')) parameters = { 'system': PAYMENT_SYSTEM_ID, 'basket_owner': user, 'associateInvoiceWithToken': True, 'checkoutWithToken': True, 'fallback_url': error_url, 'return_url': success_url, 'forceRedirect': True, 'template': 'wl/payment_information.html', 'proxy': is_officer(request.user), "products": [ {"id": product.id if product is not None else None} ], "vouchers": [] } # senior discount if application.is_senior_offer_applicable: parameters['vouchers'].append({'code': SENIOR_VOUCHER_CODE}) url = request.build_absolute_uri( reverse('payments:ledger-initial-checkout') ) response = requests.post(url, headers=JSON_REQUEST_HEADER_PARAMS, cookies=request.COOKIES, data=json.dumps(parameters)) return HttpResponse(response.content)