Django信号不适用于Django-PayPal

时间:2018-12-31 21:13:21

标签: python django paypal

我正在尝试处理django-paypal包中的信号valid_ipn_received(文档:https://django-paypal.readthedocs.io/en/stable/standard/ipn.html

engine / signals.py

from paypal.standard.models import ST_PP_COMPLETED
from paypal.standard.ipn.signals import valid_ipn_received
from engine.models import DatasetRequest
from django.views.decorators.csrf import csrf_exempt

def show_me_the_money(sender, **kwargs):
    print('test')
    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. (The user could tamper with
        # that fields on the payment form before it goes to PayPal)
        if ipn_obj.receiver_email != "paypalemail@gmail.com":
            # Not a valid payment
            return

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

        if ipn_obj.mc_gross == ipn_obj.amount and ipn_obj.mc_currency == 'USD':
            pk = ipn_obj.invoice
            dsr = DatasetRequest.objects.get(pk=pk)
            dsr.is_paid = True
            dsr.save()
    else:
        pass

valid_ipn_received.connect(show_me_the_money)

engine / apps.py

from django.apps import AppConfig

class EngineConfig(AppConfig):
    name = 'engine'

    def ready(self):
        import engine.signals

engine / views.py

def pay_for_dataset_request(request, dsr_pk):
    # dsr_pk = dataset request primary key

    dsr = DatasetRequest.objects.get(pk=dsr_pk)
    paypal_dict = {
        "business": "paypalemail@gmail.com",
        "amount": dsr.reward,
        "item_name": dsr.title,
        "invoice": dsr.pk,
        "notify_url": request.build_absolute_uri(reverse('paypal-ipn')),
        "return": request.build_absolute_uri(reverse('datasetrequest_detail', kwargs={'pk': dsr.pk, 'slug': dsr.slug})),
        "cancel_return": request.build_absolute_uri(reverse('datasetrequest_detail', kwargs={'pk': dsr.pk, 'slug': dsr.slug})),
    }

    # Create the instance.
    form = PayPalPaymentsForm(initial=paypal_dict)
    context = {"form": form}
    return render(request, "payment.html", context)

当我在127.0.0.1:8000、ngrok或生产服务器上付款时,“ valid_ipn_received”未触发。我的代码有什么问题?我是信号新手。

1 个答案:

答案 0 :(得分:0)

您确定您的apps.py文件实际上正在运行吗?您可以通过插入breakpoint()或仅进行print()调用进行测试。

您可能需要像这样在模块的__init__.py中添加一行:

default_app_config = 'app.engine.apps.EngineConfig'

或类似名称以确保已加载。参见the documentation on AppConfigs