Dhruv Narang
Dhruv Narang

Reputation: 161

Update db on successful payment django

I have a model named 'Exhibition', where i enable users to create a new exhibition/event after filling out a form they are asked to pay some amount and are redirected to a payment gateway (paypal as per django-paypal), after successful payment i want to update my 'Exhibition' table only for those who have paid, users are requested to login before creating an event and i am also saving the user with the form in my db.

This is my view for saving the form:

def new_event(request):
    form = NewEventForm(request.POST or None)
    if form.is_valid():
        save_it = form.save(commit=False)
        save_it.user = request.user
        save_it.save()

    return render_to_response('new_event.html',locals(), context_instance=RequestContext(request))        

and my model:

class Exhibition(models.Model):
    user = models.ForeignKey(User, blank=True, null = True)
    Type = models.ForeignKey('home.Type')
    Category = models.ForeignKey('home.Category')
    Country = models.ForeignKey('home.Country')
    City = models.ForeignKey('home.City')
    Event_name = models.CharField(max_length = 250)
    Video_url = models.URLField(blank = True)
    Event_start_date = models.DateField(blank = False)
    Event_end_date = models.DateField(blank = False)
    Discription = RichTextField(blank = False)
    Contact_person = models.CharField(max_length=50,blank = True )
    Company = models.CharField(max_length = 100,blank = True)
    Company_website = models.URLField(blank = True)
    Email  = models.EmailField(blank = True)
    Event_website = models.URLField(blank = True)
    Event_email = models.EmailField(blank = True)
    Venue = models.CharField(max_length = 250, blank = False)
    Address = models.CharField(max_length = 250, blank = False)
    Paid = models.IntegerField(max_length=2, default='0')

    def __unicode__(self):
        return '%s' % self.Event_name

    @permalink
    def get_absolute_url(self):
        return ('view_category', None, { 'slug': self.Event_name })

If i use django-paypal view, i would be able to process payment but i don't know how to get the correct 'Exhibition - id' to update the db, shall i store it in cookies and how to do it?

Upvotes: 1

Views: 1374

Answers (2)

djangonaut
djangonaut

Reputation: 7778

I would try adding a post_save signal handler to django-paypal's model PayPalStandardBase and then use the transaction record's fields to identify your exhibition record. You could use the primary key field of your model and tell paypal that it is your item number for the transaction, or just add any other unique field you want to use as common identifier.

Check out the model here:

https://github.com/spookylukey/django-paypal/blob/master/paypal/standard/models.py

The signal handler would look like:

@receiver(post_save, sender=PayPalStandardBase)
def mark_as_paid(sender, instance=None, created=False, **kwargs):
    if created:
        e = Exhibition.objects.get(pk=instance.item_number)
        e.Paid = ...
        e.save()

You can add it to a models.py of yours or a signals.py which you then import elsewhere.

Upvotes: 1

l a a b i
l a a b i

Reputation: 23

I would suggest adding a custom field containing the user id , and recover it when paypal notify you back.

See custom below : https://developer.paypal.com/webapps/developer/docs/classic/ipn/integration-guide/IPNandPDTVariables/

Upvotes: 0

Related Questions