如何在用户付款时将已完成的字段更改为True?

时间:2013-06-18 12:36:50

标签: django paypal django-paypal

我正在尝试使用django-paypal https://github.com/johnboxall/django-paypal

def view_that_asks_for_money(request):
    my_order = Order.objects.get(id=1)

    # What you want the button to do.
    paypal_dict = {
        "business": "sampleemail@mail.com",
        "amount": my_order.total_price, 
        "item_name": my_order.name,
        "invoice": "unique-invoice-id",
        "notify_url": "http://www.example.com/your-ipn-location/",
        "return_url": "http://www.example.com/your-return-location/",
        "cancel_return": "http://www.example.com/your-cancel-location/",

    }

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

如果用户付款,如何将finished字段更改为True

class Order(models.Model):
    ...
    finished = models.BooleanField(default=False)
    ...

1 个答案:

答案 0 :(得分:2)

您需要侦听ipn信号payment_was_successful。收到后会更改您的订单状态:

from django.dispatch import receiver
from paypal.standard.ipn.signals import payment_was_successful

@receiver(payment_was_successful)
def complete_order(sender, **kwargs):
    """
    Receiver function for successful payment.
    Calls when PayPal returns success.
    """
    ipn_obj = sender
    # do your work here