Permalink
Browse files

implemented wallrazer's v2 changes

  • Loading branch information...
1 parent 211e753 commit d7af536f4f79a887faec3efc10554a67f68d4379 @mdorn committed Feb 8, 2012
Showing with 137 additions and 7 deletions.
  1. +48 −4 README.md
  2. +68 −2 zebra/signals.py
  3. +2 −1 zebra/urls.py
  4. +19 −0 zebra/views.py
View
@@ -24,9 +24,8 @@ Usage
INSTALLED_APPS += ("zebra",)
STRIPE_SECRET = "YOUR-SECRET-API-KEY"
STRIPE_PUBLISHABLE = "YOUR-PUBLISHABLE-API-KEY"
-
- # Set any optional settings (below)
- ```
+ # Set any optional settings (below)
+ ```
3. (optional) `./manage.py syncdb` if you have `ZEBRA_ENABLE_APP = True`
@@ -59,6 +58,7 @@ Zebra handles all the webhooks that stripe sends back and calls a set of signals
* Update your stripe account to point to your webhook URL (aka https://www.mysite.com/zebra/webhooks)
* Plug into any webhook signals you care about.
+**Note: The initial Stripe webhook system is being deprecated. See below for a description of Zebra's support for the new system.**
Zebra provides:
@@ -89,6 +89,51 @@ def update_last_invoice_date(sender, **kwargs):
zebra_webhook_recurring_payment_succeeded.connect(update_last_invoice_date)
```
+### Webhooks Update ###
+
+Stripe recently updated their webhok implementation (see https://stripe.com/blog/webhooks). Zebra includes an implementation of the new system.
+
+* Include the zebra urls
+* Update your stripe account to point to your webhook URL (aka https://www.mysite.com/zebra/webhooks2)
+* Plug into any webhook signals you care about.
+
+Zebra provides:
+
+* `zebra_webhook_charge_succeeded`
+* `zebra_webhook_charge_failed`
+* `zebra_webhook_charge_refunded`
+* `zebra_webhook_charge_disputed`
+* `zebra_webhook_customer_created`
+* `zebra_webhook_customer_updated`
+* `zebra_webhook_customer_deleted`
+* `zebra_webhook_customer_subscription_created`
+* `zebra_webhook_customer_subscription_updated`
+* `zebra_webhook_customer_subscription_deleted`
+* `zebra_webhook_customer_subscription_trial_will_end`
+* `zebra_webhook_customer_discount_created`
+* `zebra_webhook_customer_discount_updated`
+* `zebra_webhook_customer_discount_deleted`
+* `zebra_webhook_invoice_created`
+* `zebra_webhook_invoice_updated`
+* `zebra_webhook_invoice_payment_succeeded`
+* `zebra_webhook_invoice_payment_failed`
+* `zebra_webhook_invoiceitem_created`
+* `zebra_webhook_invoiceitem_updated`
+* `zebra_webhook_invoiceitem_deleted`
+* `zebra_webhook_plan_created`
+* `zebra_webhook_plan_updated`
+* `zebra_webhook_plan_deleted`
+* `zebra_webhook_coupon_created`
+* `zebra_webhook_coupon_updated`
+* `zebra_webhook_coupon_deleted`
+* `zebra_webhook_transfer_created`
+* `zebra_webhook_transfer_failed`
+* `zebra_webhook_ping`
+
+Zebra also provides an easy map of all the signals as `zebra.signals.WEBHOOK_MAP`, which maps events (`charge_succeeded`) to the Zebra signal (`zebra_webhook_charge_succeeded`). To assign a handler to all the signals that zebra sends, for example, loop over the items in the map:
+
+ for event_key, webhook_signal in WEBHOOK_MAP.iteritems():
+ webhook_signal.connect(webhook_logger)
## Forms ##
@@ -162,4 +207,3 @@ I did not write any of stripe. It just makes me happy to use, and inspired to m
Code credits are in the AUTHORS file. Pull requests welcome!
-
View
@@ -1,11 +1,77 @@
import django.dispatch
-
WEBHOOK_ARGS = ["customer", "full_json"]
zebra_webhook_recurring_payment_failed = django.dispatch.Signal(providing_args=WEBHOOK_ARGS)
zebra_webhook_invoice_ready = django.dispatch.Signal(providing_args=WEBHOOK_ARGS)
zebra_webhook_recurring_payment_succeeded = django.dispatch.Signal(providing_args=WEBHOOK_ARGS)
zebra_webhook_subscription_trial_ending = django.dispatch.Signal(providing_args=WEBHOOK_ARGS)
zebra_webhook_subscription_final_payment_attempt_failed = django.dispatch.Signal(providing_args=WEBHOOK_ARGS)
-zebra_webhook_subscription_ping_sent = django.dispatch.Signal(providing_args=[])
+zebra_webhook_subscription_ping_sent = django.dispatch.Signal(providing_args=[])
+
+# v2 webhooks
+WEBHOOK2_ARGS = ["full_json"]
+
+zebra_webhook_charge_succeeded = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_charge_failed = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_charge_refunded = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_charge_disputed = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_customer_created = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_customer_updated = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_customer_deleted = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_customer_subscription_created = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_customer_subscription_updated = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_customer_subscription_deleted = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_customer_subscription_trial_will_end = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_customer_discount_created = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_customer_discount_updated = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_customer_discount_deleted = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_invoice_created = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_invoice_updated = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_invoice_payment_succeeded = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_invoice_payment_failed = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_invoiceitem_created = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_invoiceitem_updated = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_invoiceitem_deleted = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_plan_created = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_plan_updated = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_plan_deleted = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_coupon_created = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_coupon_updated = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_coupon_deleted = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_transfer_created = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_transfer_failed = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+zebra_webhook_ping = django.dispatch.Signal(providing_args=WEBHOOK2_ARGS)
+
+WEBHOOK_MAP = {
+ 'charge_succeeded': zebra_webhook_charge_succeeded,
+ 'charge_failed': zebra_webhook_charge_failed,
+ 'charge_refunded': zebra_webhook_charge_refunded,
+ 'charge_disputed': zebra_webhook_charge_disputed,
+ 'customer_created': zebra_webhook_customer_created,
+ 'customer_updated': zebra_webhook_customer_updated,
+ 'customer_deleted': zebra_webhook_customer_deleted,
+ 'customer_subscription_created': zebra_webhook_customer_subscription_created,
+ 'customer_subscription_updated': zebra_webhook_customer_subscription_updated,
+ 'customer_subscription_deleted': zebra_webhook_customer_subscription_deleted,
+ 'customer_subscription_trial_will_end': zebra_webhook_customer_subscription_trial_will_end,
+ 'customer_discount_created': zebra_webhook_customer_discount_created,
+ 'customer_discount_updated': zebra_webhook_customer_discount_updated,
+ 'customer_discount_deleted': zebra_webhook_customer_discount_deleted,
+ 'invoice_created': zebra_webhook_invoice_created,
+ 'invoice_updated': zebra_webhook_invoice_updated,
+ 'invoice_payment_succeeded': zebra_webhook_invoice_payment_succeeded,
+ 'invoice_payment_failed': zebra_webhook_invoice_payment_failed,
+ 'invoiceitem_created': zebra_webhook_invoiceitem_created,
+ 'invoiceitem_updated': zebra_webhook_invoiceitem_updated,
+ 'invoiceitem_deleted': zebra_webhook_invoiceitem_deleted,
+ 'plan_created': zebra_webhook_plan_created,
+ 'plan_updated': zebra_webhook_plan_updated,
+ 'plan_deleted': zebra_webhook_plan_deleted,
+ 'coupon_created': zebra_webhook_coupon_created,
+ 'coupon_updated': zebra_webhook_coupon_updated,
+ 'coupon_deleted': zebra_webhook_coupon_deleted,
+ 'transfer_created': zebra_webhook_transfer_created,
+ 'transfer_failed': zebra_webhook_transfer_failed,
+ 'ping': zebra_webhook_ping,
+}
View
@@ -4,4 +4,5 @@
urlpatterns = patterns('',
url(r'webhooks/$', views.webhooks, name='webhooks'),
-)
+ url(r'webhooks2/$', views.webhooks2, name='webhooks2'),
+)
View
@@ -6,6 +6,8 @@
from zebra.signals import *
from django.views.decorators.csrf import csrf_exempt
+import logging
+log = logging.getLogger("zebra.%s" % __name__)
stripe.api_key = options.STRIPE_SECRET
@@ -52,3 +54,20 @@ def webhooks(request):
return HttpResponse(status=400)
return HttpResponse(status=200)
+
+@csrf_exempt
+def webhooks2(request):
+ """
+ Handles all known webhooks from stripe, and calls signals.
+ Plug in as you need.
+ """
+ if request.method != "POST":
+ return HttpResponse("Invalid Request.", status=400)
+
+ event_json = simplejson.loads(request.raw_post_data)
+ event_key = event_json['type'].replace('.', '_')
+
+ if event_key in WEBHOOK_MAP:
+ WEBHOOK_MAP[event_key].send(sender=None, full_json=event_json)
+
+ return HttpResponse(status=200)

0 comments on commit d7af536

Please sign in to comment.