Search code examples
pythondjangopaypaldjango-paypal

How to use django-paypal to update a users account balance


I'm using the standard django-paypal module found here: https://github.com/spookylukey/django-paypal

What I am trying to achieve is a person can add funds to their account on my website without requiring them to have a paypal account on file (explained below). Currently the system I have in place is

def paypal_payment_successful(sender, **kwargs):
    #Called when the payment is sucessful
    ipn_obj = sender


    if ipn_obj.payment_status == "Completed":
        try:
            user_profile = models.UserProfile.objects.get(paypal_account=sender.payer_email)
            user_profile.account_balance += float(ipn_obj.mc_gross)
            user_profile.save()

        except models.UserProfile.DoesNotExist:
            pass # TODO email admin

paypal_signals.payment_was_successful.connect(paypal_payment_successful)

@csrf_exempt
def account_paypal_return(request):
    if request.REQUEST.get('payment_status') == 'Completed':
        messages.add_message(request, messages.INFO,
                message=_('The amount of %(amount)s was successfully deposited to your account.') % {
                        'amount': utils.format_currency(float(request.REQUEST['mc_gross']))})

        notifications.notify_paypal_payment(request.user, int(float(request.REQUEST['mc_gross'])))

    return HttpResponseRedirect('/account/')

If you take a look at the paypal_payment_successful function you can see that currently I fetch the associated user by the paypal account provided by the ipn and the paypal account in the users profile. Is there a way I can do something else to figure out how to update a users account profile without requiring them to have one saved in their profile.

If I could somehow combine paypal_payment_successful and account_paypal_return (which is called on return from the paypal site) I could use request.user.id to determine the user.


Solution

  • Figured it out. In the view function of the page where you can add funds I modified the paypal dictionary to

     paypal_info = {
            'business': settings.PAYPAL_RECEIVER_EMAIL,
            'currency_code': settings.CURRENCY_CODE,
            'amount': 20,
            'item_name':Funds,
            'custom':str(request.user.id),
            'notify_url': url + reverse('paypal-ipn'),
            'return_url': url + '/account/paypal/return/',
            'cancel_return': url + '/account/paypal/return/'
        }
    

    And I changed my succesfful function call to

    def paypal_payment_successful(sender, **kwargs):
        #Called when the payment is sucessful
        ipn_obj = sender
        user_id = ipn_obj.custom    
    
        if ipn_obj.payment_status == "Completed":
            try:
                user_profile = models.UserProfile.objects.get(user=user_id)
                user_profile.account_balance += float(ipn_obj.mc_gross)
                user_profile.save()
    
            except models.UserProfile.DoesNotExist:
                pass # TODO email admin