python - 如何在 django-paypal IPN 函数中获取我的请求数据?

标签 python django paypal request django-1.9

我正在建立一个在线商店。

我的订单数据存储在请求中。我的观点具有以下一般结构:

payment.py

def payment(request):
    order = request.order    # Getting the order instance
    # do some stuff
    return render_to_response("payment.html")

我使用 django-paypal。我正在关注 this tutorial .以下函数由 PayPal 调用,它向我发出付款已成功的信号。

payment_paypal_ipn_signal_handler.py

# Almost entirely copied from the django-paypal tutorial
from paypal.standard.models import ST_PP_COMPLETED
from paypal.standard.ipn.signals import valid_ipn_received

def payment_paypal_ipn_signal_handler(sender, **kwargs):
    ipn_obj = sender
    if ipn_obj.payment_status == ST_PP_COMPLETED:
        # WARNING !
        # Check that the receiver email is the same we previously
        # set on the business field request. (The user could tamper
        # with those fields on payment form before send it to PayPal)
        if ipn_obj.receiver_email != "receiver_email@example.com":
            # Not a valid payment
            return

        # ALSO: for the same reason, you need to check the amount
        # received etc. are all what you expect.

        # Undertake some action depending upon `ipn_obj`.
        if ipn_obj.custom == "Upgrade all users!":
            Users.objects.update(paid=True)

        # Here I should add the code that handles a successful payment
        request.order.paid = True    # How do I get my 'request' here?
    else:
        #...

valid_ipn_received.connect(show_me_the_money)

但是,例如,我仍然需要我的 order 实例才能将其设置为已付款。我怎样才能在这个由 PayPal 调用的函数中获取我的数据?

最佳答案

我找到了解决方案。您可以通过 PayPalPaymentsForm 参数中的 custom 属性传递信息。

payment_paypal.py

def payment_paypal(request):
    paypal_dict = {
        "business": ...,
        "amount": ...,
        ...,
        # Pass any information you want HERE!
        "custom": request.order.order_number,
    }
    form = PayPalPaymentsForm(initial=paypal_dict)
    context = {"paypal_form": form}
    return render(request, "payment_paypal.html", context)

payment_paypal_ipn_signal_handler.py

def payment_paypal_ipn_signal_handler(sender, **kwargs):
    ipn_obj = sender
    # Retrieve the order_number previously passed
    order_number = ipn_obj.custom
    # Get the order :D
    order = Orders.objects.get(order_number=order_number)
    ...

在处理 IPN 信号的 View 中检索订单号(或其他信息)。

关于python - 如何在 django-paypal IPN 函数中获取我的请求数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40433926/

相关文章:

php - Paypal IPN 未更新或插入数据库

paypal - 让 Paypal 结帐按钮重定向到标题为 'Pay with a debit or credit card' 的付款页面

paypal - 仅向链式支付的次要接收方收取费用?

python - 如何在SQLAlchemy中设置InnoDB存储格式?

django - 如何在 Django REST Framework 中实现资源层次结构(例如/parents/<id>/children)

python - 睡衣 + Django => CSRF 困惑,403 错误

python - XML 文件作为 django 项目的模型

python - 如何获得 [-2.3,-2.2,...] 的列表?

python - 从 Notepad++ 启动和关闭交互式 ipython session 的正确方法

python - TensorFlow - 根据另一个变量的形状动态定义变量的形状