Skip to content

Commit

Permalink
Add product to webhook calls
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdjones authored and jleclanche committed Jun 27, 2018
1 parent 8b0f6df commit cc64c08
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
8 changes: 5 additions & 3 deletions djstripe/event_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,17 @@ def customer_subscription_webhook_handler(event):
_handle_crud_like_event(target_cls=models.Subscription, event=event)


@webhooks.handler("transfer", "charge", "coupon", "invoice", "invoiceitem", "plan")
@webhooks.handler("transfer", "charge", "coupon", "invoice", "invoiceitem", "plan", "product")
def other_object_webhook_handler(event):
"""Handle updates to transfer, charge, invoice, invoiceitem and plan objects.
"""Handle updates to transfer, charge, invoice, invoiceitem, plan and product objects.
Docs for:
- charge: https://stripe.com/docs/api#charges
- coupon: https://stripe.com/docs/api#coupons
- invoice: https://stripe.com/docs/api#invoices
- invoiceitem: https://stripe.com/docs/api#invoiceitems
- plan: https://stripe.com/docs/api#plans
- product: https://stripe.com/docs/api#products
"""

if event.parts[:2] == ["charge", "dispute"]:
Expand All @@ -135,7 +136,8 @@ def other_object_webhook_handler(event):
"invoice": models.Invoice,
"invoiceitem": models.InvoiceItem,
"plan": models.Plan,
"transfer": models.Transfer
"product": models.Product,
"transfer": models.Transfer,
}.get(event.category)

_handle_crud_like_event(target_cls=target_cls, event=event)
Expand Down
19 changes: 19 additions & 0 deletions djstripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2609,6 +2609,20 @@ class Plan(StripeObject):
class Meta(object):
ordering = ["amount"]

@classmethod
def _stripe_object_to_product(cls, target_cls, data):
"""
Search the given manager for the Product matching this Plan object's ``product`` field.
:param target_cls: The target class
:type target_cls: Product
:param data: stripe object
:type data: dict
"""

if "product" in data and data["product"]:
return target_cls._get_or_create_from_stripe_object(data, "product")[0]

@classmethod
def get_or_create(cls, **kwargs):
""" Get or create a Plan."""
Expand All @@ -2634,6 +2648,11 @@ def create(cls, **kwargs):
def __str__(self):
return self.name or self.nickname or self.stripe_id

def _attach_objects_hook(self, cls, data):
product = cls._stripe_object_to_product(target_cls=Product, data=data)
if product:
self.product = product

@property
def amount_in_cents(self):
return int(self.amount * 100)
Expand Down

0 comments on commit cc64c08

Please sign in to comment.