def post(self, request, *args, **kwargs): context = super().get_context_data(**kwargs) invoice = get_purchase_invoice(request.user, get_site_from_request(request)) if not invoice.order_items.count( ) or invoice.status == Invoice.InvoiceStatus.CART: messages.info(request, _("Cart changed while in checkout process")) return redirect('vendor:cart') credit_card_form = CreditCardForm(request.POST) if request.POST.get('same_as_shipping') == 'on': billing_address_form = BillingAddressForm( instance=invoice.shipping_address) billing_address_form.data = billing_address_form.initial billing_address_form.is_bound = True else: billing_address_form = BillingAddressForm(request.POST) if not (billing_address_form.is_valid() and credit_card_form.is_valid()): processor = payment_processor(invoice) context['billing_address_form'] = billing_address_form context['credit_card_form'] = credit_card_form return render(request, self.template_name, processor.get_checkout_context(context=context)) else: billing_address_form.full_clean() credit_card_form.full_clean() request.session[ 'billing_address_form'] = billing_address_form.cleaned_data request.session['credit_card_form'] = credit_card_form.cleaned_data return redirect('vendor:checkout-review')
def get_checkout_context(self, request=None, context={}): context = super().get_checkout_context(context=context) if 'credit_card_form' not in context: context['credit_card_form'] = CreditCardForm(initial={'payment_type': PaymentTypes.CREDIT_CARD}) if 'billing_address_form' not in context: context['billing_address_form'] = BillingAddressForm() return context
def test_view_missing_data(self): session = self.client.session session['billing_address_form'] = BillingAddressForm().initial session['credit_card_form'] = CreditCardForm().initial session.save() response = self.client.post(self.view_url) self.assertRedirects(response, reverse('vendor:checkout-account'))
def get(self, request, *args, **kwargs): invoice = get_purchase_invoice(request.user) if not invoice.order_items.count(): return redirect('vendor:cart') context = super().get_context_data() processor = payment_processor(invoice) if 'billing_address_form' in request.session: context['billing_address_form'] = BillingAddressForm( request.session['billing_address_form']) if 'credit_card_form' in request.session: context['credit_card_form'] = CreditCardForm( request.session['credit_card_form']) context = processor.get_checkout_context(context=context) return render(request, self.template_name, context)
def test_view_payment_success(self): self.invoice.status = Invoice.InvoiceStatus.CHECKOUT self.invoice.save() Payment.objects.all().delete() form_data = { 'billing_address_form': { 'billing-name': 'Home', 'billing-company': 'Whitemoon Dreams', 'billing-country': 840, 'billing-address_1': '221B Baker Street', 'billing-address_2': '', 'billing-locality': 'Marylebone', 'billing-state': 'California', 'billing-postal_code': '90292' }, 'credit_card_form': { 'full_name': 'Bob Ross', 'card_number': '5424000000000015', 'expire_month': '12', 'expire_year': '2030', 'cvv_number': '900', 'payment_type': '10' } } billing_address = BillingAddressForm(form_data['billing_address_form']) billing_address.is_bound = True billing_address.is_valid() payment_info = CreditCardForm(form_data['credit_card_form']) payment_info.is_bound = True payment_info.is_valid() session = self.client.session session['billing_address_form'] = { f'billing-{key}': value for key, value in billing_address.cleaned_data.items() } session['credit_card_form'] = payment_info.cleaned_data session.save() response = self.client.post(self.view_url) self.assertEquals(self.invoice.payments.all().count(), Payment.objects.filter(invoice=self.invoice).count())
def test_view_payment_success(self): Payment.objects.all().delete() form_data = { 'billing_address_form': {'name':'Home','company':'Whitemoon Dreams','country':'581','address_1':'221B Baker Street','address_2':'','locality':'Marylebone','state':'California','postal_code':'90292'}, 'credit_card_form': {'full_name':'Bob Ross','card_number':'5424000000000015','expire_month':'12','expire_year':'2030','cvv_number':'900','payment_type':'10'} } billing_address = BillingAddressForm(form_data['billing_address_form']) billing_address.is_bound = True billing_address.is_valid() payment_info = CreditCardForm(form_data['credit_card_form']) payment_info.is_bound = True payment_info.is_valid() session = self.client.session session['billing_address_form'] = billing_address.cleaned_data session['credit_card_form'] = payment_info.cleaned_data session.save() response = self.client.post(self.view_url) self.assertRedirects(response, reverse('vendor:purchase-summary', kwargs={'pk': 1}))