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')
class SubscriptionUpdatePaymentView(LoginRequiredMixin, FormView): form_class = CreditCardForm() success_url = reverse_lazy('vendor:customer-subscriptions') def post(self, request, *args, **kwargs): receipt = Receipt.objects.get(uuid=self.kwargs["uuid"]) payment_form = CreditCardForm(request.POST) if not payment_form.is_valid(): messages.info(request, _("Invalid Card")) return redirect(request.META.get('HTTP_REFERER', self.success_url)) processor = PaymentProcessor(receipt.order_item.invoice) processor.set_payment_info_form_data(request.POST, CreditCardForm) processor.subscription_update_payment(receipt) if not processor.transaction_submitted: messages.info( request, _(f"Payment gateway error: {processor.transaction_message.get('message', '')}" )) return redirect(request.META.get('HTTP_REFERER', self.success_url)) messages.info(request, _(f"Success: Payment Updated")) return redirect(request.META.get('HTTP_REFERER', self.success_url))
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 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 get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) payment = Payment.objects.get( transaction=context['object'].transaction) context['payment'] = payment context['payment_form'] = CreditCardForm( initial={'payment_type': PaymentTypes.CREDIT_CARD}) context['billing_form'] = AddressForm(instance=payment.billing_address) return context
def post(self, request, *args, **kwargs): receipt = Receipt.objects.get(uuid=self.kwargs["uuid"]) payment_form = CreditCardForm(request.POST) if not payment_form.is_valid(): messages.info(request, _("Invalid Card")) return redirect(request.META.get('HTTP_REFERER', self.success_url)) processor = PaymentProcessor(receipt.order_item.invoice) processor.get_payment_info_form_data(request.POST, CreditCardForm) processor.subscription_update_payment(receipt) if not processor.transaction_submitted: messages.info( request, _(f"Payment gateway error: {processor.transaction_message.get('message', '')}" )) return redirect(request.META.get('HTTP_REFERER', self.success_url)) messages.info(request, _(f"Success: Payment Updated")) return redirect(request.META.get('HTTP_REFERER', self.success_url))
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}))
def get(self, request, *args, **kwargs): context = super().get_context_data(**kwargs) customer_profile, created = request.user.customer_profile.get_or_create(site=Site.objects.get_current()) subscriptions = customer_profile.get_recurring_receipts() context['payments'] = customer_profile.payments.filter(success=True) context["offers"] = Offer.on_site.filter(available=True).order_by('terms') if subscriptions: context['subscription'] = subscriptions.first() context['payment'] = subscriptions.first().order_item.invoice.payments.filter(success=True).first() context['payment_form'] = CreditCardForm(initial={'payment_type': PaymentTypes.CREDIT_CARD}) return render(request, self.template_name, context)
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)