覆盖Django-奥斯卡付款

时间:2020-04-02 17:48:50

标签: django django-oscar

我想将付款数据保存到Django oscar中BankCard的admin部分中。

我像下面那样覆盖了贝宝代码,但出现错误,说: / checkout / preview /

处的ValueError

由于数据未通过验证,因此无法创建银行卡。

请求方法:POST 无法创建银行卡,因为数据未通过验证。

如何覆盖验证?

from oscar.apps.checkout.views import PaymentDetailsView as CorePaymentDetailsView
#from .models import PaymentCard as Bankcard
from django.shortcuts import redirect
from django.shortcuts import render
from oscar.apps.payment.forms import BankcardForm
from oscar.apps.checkout import views
from oscar.apps.payment import forms, models
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.urls import reverse



class PaymentDetailsView(CorePaymentDetailsView):
    template_name = 'paymen_details.html'


    def get_context_data(self, **kwargs):
        """
        Add data for Paypal Express flow.

        """
        # Override method so the bankcard and billing address forms can be
        # added to the context.
        template_name = 'checkout/payment_details.html'

        ctx = super(PaymentDetailsView, self).get_context_data(**kwargs)
        ctx['bankcard_form'] = kwargs.get(
            'bankcard_form', forms.BankcardForm())
        """
        ctx['billing_address_form'] = kwargs.get(
            'billing_address_form', forms.BillingAddressForm())
        """
        return ctx

    def post(self, request, *args, **kwargs):
        # Override so we can validate the bankcard/billingaddress submission.
        # If it is valid, we render the preview screen with the forms hidden
        # within it.  When the preview is submitted, we pick up the 'action'
        # parameters and actually place the order.
        if request.POST.get('action', '') == 'place_order':
            bankcard_form = forms.BankcardForm(request.POST)
            if bankcard_form.is_valid():
                bankcard_form.save()
            return self.do_place_order(request)

        bankcard_form = forms.BankcardForm(request.POST)
        #billing_address_form = forms.BillingAddressForm(request.POST)
        if not all([bankcard_form.is_valid()]):
                    #billing_address_form.is_valid()]):
            # Form validation failed, render page again with errors
            self.preview = False
            ctx = self.get_context_data(
                bankcard_form=bankcard_form)
             #  " billing_address_form=billing_address_form)
            return self.render_to_response(ctx)

        #bankcard_form.save()

        #billing_address_form = forms.BillingAddressForm(request.POST)


        # Render preview with bankcard and billing address details hidden

        return self.render_preview(request,
                                   bankcard_form=bankcard_form,
                                  )

    def do_place_order(self, request):
        # Helper method to check that the hidden forms wasn't tinkered
        # with.
        bankcard_form = forms.BankcardForm(request.POST)
        #billing_address_form = forms.BillingAddressForm(request.POST)
        bankcard_form.save()
        """
        if not all([bankcard_form.is_valid(),
                    billing_address_form.is_valid()]):
            messages.error(request, "Invalid submission")
            return HttpResponseRedirect(reverse('checkout:payment-details'))

        # Attempt to submit the order, passing the bankcard object so that it
        # gets passed back to the 'handle_payment' method below.
        submission = self.build_submission()
        submission['payment_kwargs']['bankcard'] = bankcard_form.bankcard
        submission['payment_kwargs']['billing_address'] = billing_address_form.cleaned_data
        return self.submit(**submission)
        """

    def handle_payment(self, order_number, total, **kwargs):
        """
        Make submission to PayPal
        """
        # Using authorization here (two-stage model).  You could use sale to
        # perform the auth and capture in one step.  The choice is dependent
        # on your business model.

0 个答案:

没有答案
相关问题