Home > Blockchain >  TypeError: PaystackPayment() missing 1 positional argument: "request"
TypeError: PaystackPayment() missing 1 positional argument: "request"

Time:12-25

I'm getting the error

PaystackPayment() missing 1 required positional argument: 'request'

at line 246 of my core.views of my Ecommerce project when trying to access the function PaystackPayment(). I don't know what is wrong.

views:

def PaystackPayment(request):
    try:
        order = Order.objects.get(user=request.user, ordered=False)
        secret_key = settings.PAYSTACK_SECRET_KEY
        ref = create_ref_code()
        amount = int(order.get_total() * 100)
        client_credentials = request.user.email
        paystack_call = TransactionResource(
            secret_key, ref)
        response = paystack_call.initialize(
            amount,
            client_credentials
        )

        authorization_url = response['data']['authorization_url']
        # create payment
        payment = PaystackPayment()
        payment.user = request.user
        payment.ref = ref
        payment.email = client_credentials
        payment.amount = int(order.get_total())
        payment.save()

       # assign the payment to the order
        order.ordered = True
        order.email = client_credentials
        order.ref_code = ref
        order.paystack_payment = payment.amount
        order.save()
        # send confirmation order email to the user
        subject = f'New Order Made by {request.user.username} today totaled {int(order.get_total())} '

        message = 'Hello there '   \
            ', Thanks for your order. Your order has been received and it is being processed as we shall get back to you shortly. Thank you once again!'
        email_from = settings.EMAIL_HOST_USER
        recipient_list = [request.user.email, ]
        send_mail(subject, message, email_from, recipient_list)

        messages.success(request, 'Your order was successful')
        return redirect(authorization_url)
    except ObjectDoesNotExist:
        messages.info(
            request, """
                    Their was an error when you where possibly entering the checkout or payment fields.
                    You were not charged, try again!
                    """)
    return redirect('core:checkout')

urls:

from django.urls import path
from .views import (PurchaseSearch, PaystackPayment, Checkout)

app_name = 'core'

urlpatterns = [
path('checkout/', Checkout.as_view(), name='checkout'),
path('make-payment/', PaystackPayment, name='payment')

model:

class PaystackPayment(models.Model):
    user = models.ForeignKey(User,
                             on_delete=models.SET_NULL, blank=True, null=True)
    amount = models.PositiveIntegerField()
    time_stamp = models.DateTimeField(auto_now_add=True)
    ref = models.CharField(max_length=20)
    email = models.EmailField()
    payment_verified = models.BooleanField(default=False)

    def __str__(self) -> str:
        return f'PaystackPayment: {self.amount}'

CodePudding user response:

Your view name (PaystackPayment) is the same as your model class. The following line

payment = PaystackPayment()

is trying to instantiate your view, not your model.

Following the PEP-8 recommendation of using snake_case for function names would have prevented this specific error (although it's not a silver bullet, e.g. class based views):

def paystack_payment(request):
    ...
  • Related