django paypal with multiple products returns paypal test store

130 Views Asked by At

I'm using django-paypal and PayPalEncryptedPaymentsForm as

def payment_process(request):
    host = request.get_host()
    paypal_dict = {
        "business": settings.PAYPAL_RECEIVER_EMAIL,
        "currency_code": "USD",
        "notify_url": f"http://{host}{reverse('paypal-ipn')}",
        "return_url": f"http://{host}{reverse('payment:success')}",
        "cancel_return": f"http://{host}{reverse('payment:cancel')}",
    }
    i = 1
    for x in Order.objects.filter(user=request.user):
        paypal_dict.update({f"item_name_{i}": str(x.product.name)})
        paypal_dict.update({f"amount_{i}": x.product.price})
        i += 1
    form = PayPalEncryptedPaymentsForm(initial=paypal_dict)
    return render(request, "payment/payment_process.html", {"form": form})

and in my templates that's {{ form.render }} but when i click to Buy it button, it loads a page like this

enter image description here

Then what ever price i enter and continue.. paypal wants me to pay that much price. (Why it just do like this) :\

1

There are 1 best solutions below

0
Tohid Patel On

This is an example, I think it will help you

def process_payment(request):
order_id = request.session.get('order_id')
order = get_object_or_404(Order, id=order_id)
host = request.get_host()

paypal_dict = {
    'business': settings.PAYPAL_RECEIVER_EMAIL,
    'amount': '%.2f' % order.total_cost().quantize(
        Decimal('.01')),
    'item_name': 'Order {}'.format(order.id),
    'invoice': str(order.id),
    'currency_code': 'USD',
    'notify_url': 'http://{}{}'.format(host,
                                       reverse('paypal-ipn')),
    'return_url': 'http://{}{}'.format(host,
                                       reverse('payment_done')),
    'cancel_return': 'http://{}{}'.format(host,
                                          reverse('payment_cancelled')),
}

form = PayPalPaymentsForm(initial=paypal_dict)
return render(request, 'ecommerce_app/process_payment.html', {'order': order, 'form': form})

One more thing if you want set the Order paid use signals.py and inform to the app config

from django.shortcuts import get_object_or_404
from .models import Order
from paypal.standard.ipn.signals import valid_ipn_received
from django.dispatch import receiver


@receiver(valid_ipn_received)
def payment_notification(sender, **kwargs):
    ipn = sender
    if ipn.payment_status == 'Completed':
        # payment was successful
        order = get_object_or_404(Order, id=ipn.invoice)

        if order.total_cost() == ipn.mc_gross:
            # mark the order as paid
            order.paid = True
            order.save()

In your apps.py

from django.apps import AppConfig


class EcommerceAppConfig(AppConfig):
    name = 'ecommerce_app'

    def ready(self):
        # import signal handlers
        import ecommerce_app.signals

This full documentation

https://overiq.com/django-paypal-integration-with-django-paypal/

and This is Video link

https://youtu.be/Ftz3DG9Sq50?si=SVrEmZqYyd-HePdI

I think this is gonna help You.

I am too late