从3个不同型号中接收PayPal ipn的正确方法是什么?



我使用Django-Paypal接收3种不同服务的付款。每个服务都有一个独立的模型(比如ServiceA、ServiceB和ServiceC),但它们都在同一个应用程序中。我需要在收到相应服务的付款后,将正确模型的付款状态更新为"paid=True"。

我有3个支付处理视图(每个服务)工作良好,因为所有支付都成功完成。同样,我创建了3个信号/接收器函数(每个模型)并将它们放在signals.py中。当我一次测试一个信号时,它工作得很好。付款状态成功更新为"paid=True"。但是,当我在signals.py中包含所有三个信号时,事情就停止工作了。我注意到每次收到付款时,前两个接收函数都会被触发,从而导致错误。查看底部的代码

当从特定模型收到付款时,我如何指定应该调用哪个接收函数?或者哪一种是成功实现上述目标的最佳方式?当我使用预保存等内置信号时,可以通过在装饰器中添加发送者来指定要更新的模型,例如

@receiver(pre-save, sender = MyModel)

关于PayPal的IPN信号,我完全卡住了,欢迎任何帮助,包括想法。提前谢谢。

@receiver(valid_ipn_received)
def Servicea_order_item_paid(sender, **kwargs):
ipn_obj = sender
if ipn_obj.payment_status == ST_PP_COMPLETED:
servicea = ServiceA.objects.get(order_id =ipn_obj.invoice)
if (
ipn_obj.mc_gross == servicea.total_cost() 
and ipn_obj.mc_currency == 'USD' 
and ipn_obj.receiver_email == "xxxxxx@business.example.com"
):
servicea.paid = True
servicea.save()
@receiver(valid_ipn_received)
def serviceb_order_item_paid(sender, **kwargs):
ipn_obj = sender
if ipn_obj.payment_status == ST_PP_COMPLETED:
serviceb = ServiceB.objects.get(order_id =ipn_obj.invoice)
if (
ipn_obj.mc_gross == serviceb.total_cost() 
and ipn_obj.mc_currency == 'USD' 
and ipn_obj.receiver_email == "xxxxxx@business.example.com"
):
serviceb.paid = True
serviceb.save()
@receiver(valid_ipn_received)
def servicec_order_item_paid(sender, **kwargs):
ipn_obj = sender
if ipn_obj.payment_status == ST_PP_COMPLETED:
servicec = ServiceC.objects.get(order_id =ipn_obj.invoice)
if (
ipn_obj.mc_gross == servicec.total_cost() 
and ipn_obj.mc_currency == 'USD' 
and ipn_obj.receiver_email == "xxxxxx@business.example.com"
):
servicec.paid = True
servicec.save()

虽然我在这里无法得到答案,但我最终解决了这个问题。我将三个信号组合成一个单一的"复合"信号,使用:

<<ul>
  • 过滤器/gh>
  • if指令和
  • exists()方法
  • 最后的代码很长,但它运行良好,没有任何错误。甚至'Duplicate txn_id'错误也最终消失了。为了共享代码,我分别用ServiceA、B和C代替了实际的服务。请看下面的代码:

    @receiver(valid_ipn_received)
    def order_item_paid(sender, **kwargs):
    ipn_obj = sender
    if ipn_obj.payment_status == ST_PP_COMPLETED:
    print('Payment was sucessful!!!!, pending other verifications')
    if ServiceA.objects.filter(order_id =ipn_obj.invoice).exists():
    print('!!!! ServiceA Oder object exists')
    servicea = get_object_or_404(ServiceA, order_id =ipn_obj.invoice)
    
    if (
    ipn_obj.mc_gross == servicea.total_cost() 
    and ipn_obj.mc_currency == 'USD' 
    and ipn_obj.receiver_email == "xxxxxxx@business.example.com"
    ):
    print('!!!. Amount, Currency and Email Verified')
    servicea.paid = True
    servicea.save()
    else:
    print('!!!! ServiceA order item does not exist')
    if ServiceB.objects.filter(order_id =ipn_obj.invoice).exists():
    print('!!!! ServiceB order item exists')
    serviceb = get_object_or_404(ServiceB, order_id =ipn_obj.invoice)
    
    if (
    ipn_obj.mc_gross == serviceb.total_cost() 
    and ipn_obj.mc_currency == 'USD' 
    and ipn_obj.receiver_email == "xxxxxx@business.example.com"
    ):
    print('!!!!!!. Amount, Currency and Email Verified')
    serviceb.paid = True
    serviceb.save()
    else:
    print('!!!!!!!!Service B Order object does not exist')
    
    if ServiceC.objects.filter(order_id =ipn_obj.invoice).exists():
    print('!!!!!!!!Service C order item exists')
    servicec = get_object_or_404(ServiceC, order_id =ipn_obj.invoice)
    
    if (
    ipn_obj.mc_gross == servicec.total_cost() 
    and ipn_obj.mc_currency == 'USD' 
    and ipn_obj.receiver_email == "xxxxxxx@business.example.com"
    ):
    print('!!!!!!. Amount, Currency and Email Verified')
    servicec.paid = True
    servicec.save()
    
    else:
    print('!!! Service C order object does not exist')
    

    最新更新