mirror of
https://github.com/pretix/pretix.git
synced 2026-05-03 14:54:04 +00:00
Stripe: Mark order as paid on successful webhook call
This commit is contained in:
@@ -7,7 +7,7 @@ from django.views.decorators.csrf import csrf_exempt
|
||||
from django.views.decorators.http import require_POST
|
||||
|
||||
from pretix.base.models import Order
|
||||
from pretix.base.services.orders import mark_order_refunded
|
||||
from pretix.base.services.orders import mark_order_paid, mark_order_refunded
|
||||
from pretix.plugins.stripe.payment import Stripe
|
||||
from pretix.presale.utils import event_view
|
||||
|
||||
@@ -54,7 +54,10 @@ def webhook(request, *args, **kwargs):
|
||||
|
||||
order.log_action('pretix.plugins.stripe.event', data=event_json)
|
||||
|
||||
if order.status == Order.STATUS_PAID and (charge['refunds']['total_count'] or charge['dispute']):
|
||||
is_refund = charge['refunds']['total_count'] or charge['dispute']
|
||||
if order.status == Order.STATUS_PAID and is_refund:
|
||||
mark_order_refunded(order, user=None)
|
||||
elif order.status == Order.STATUS_PENDING and charge['status'] == 'succeeded' and not is_refund:
|
||||
mark_order_paid(order, user=None)
|
||||
|
||||
return HttpResponse(status=200)
|
||||
|
||||
@@ -121,6 +121,39 @@ def test_webhook_all_good(env, client, monkeypatch):
|
||||
assert order.status == Order.STATUS_PAID
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_webhook_mark_paid(env, client, monkeypatch):
|
||||
order = env[1]
|
||||
order.status = Order.STATUS_PENDING
|
||||
order.save()
|
||||
|
||||
charge = get_test_charge(env[1])
|
||||
monkeypatch.setattr("stripe.Charge.retrieve", lambda *args: charge)
|
||||
|
||||
client.post('/dummy/dummy/stripe/webhook/', json.dumps(
|
||||
{
|
||||
"id": "evt_18otImGGWE2Ias8TUyVRDB1G",
|
||||
"object": "event",
|
||||
"api_version": "2016-03-07",
|
||||
"created": 1472729052,
|
||||
"data": {
|
||||
"object": {
|
||||
"id": "ch_18TY6GGGWE2Ias8TZHanef25",
|
||||
"object": "charge",
|
||||
# Rest of object is ignored anway
|
||||
}
|
||||
},
|
||||
"livemode": True,
|
||||
"pending_webhooks": 1,
|
||||
"request": "req_977XOWC8zk51Z9",
|
||||
"type": "charge.succeeded"
|
||||
}
|
||||
), content_type='application_json')
|
||||
|
||||
order.refresh_from_db()
|
||||
assert order.status == Order.STATUS_PAID
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_webhook_partial_refund(env, client, monkeypatch):
|
||||
charge = get_test_charge(env[1])
|
||||
|
||||
Reference in New Issue
Block a user