我正在使用此处的标准django-paypal模块: https://github.com/spookylukey/django-paypal
我想要实现的是,一个人可以在我的网站上为他们的帐户添加资金,而无需他们将PayPal帐户存档(如下所述)。目前我所拥有的系统是
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/')
如果您查看paypal_payment_successful函数,您可以看到当前我通过ipn提供的paypal帐户和用户配置文件中的paypal帐户获取关联用户。有没有办法我可以做其他事情来弄清楚如何更新用户帐户个人资料,而无需他们在个人资料中保存一个。
如果我能以某种方式结合paypal_payment_successful和account_paypal_return(从paypal网站返回时调用),我可以使用request.user.id来确定用户。
答案 0 :(得分:0)
想出来。在您可以添加资金的页面的视图功能中,我将paypal字典修改为
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/'
}
我将成功的函数调用改为
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