def handle_notification(class_, data): """Update/create an account and it's associated subscription using data from Recurly""" # First get/create the account account_data = modelify(data.get("account"), Account) account, created = class_.objects.get_or_create( account_code=account_data["account_code"], defaults=account_data ) if not created: for k, v in account_data.items(): setattr(account, k, v) account.save() # Now get/create the subscription if not data.get("subscription"): return account, None subscription_data = modelify(data.get("subscription"), Subscription) subscription = account.get_current_subscription() if not subscription: # Not found, create it subscription = Subscription.objects.create(account=account, **subscription_data) was_current = False now_current = subscription.is_current() else: was_current = subscription.is_current() # Found, update it for k, v in subscription_data.items(): setattr(subscription, k, v) subscription.save() now_current = subscription.is_current() # Send account closed/opened signals if was_current and not now_current: signals.account_closed.send(sender=account, account=account, subscription=subscription) elif not was_current and now_current: signals.account_opened.send(sender=account, account=account, subscription=subscription) return account, subscription
def handle_notification(class_, data): transaction = data.get("transaction") account_code = data["account"]["account_code"] transaction_data = modelify(transaction, class_, remove_empty=True, date_fields=["date"]) transaction_data["transaction_id"] = transaction["id"] transaction_data["account"] = Account.objects.get(account_code=account_code) payment, created = class_.objects.get_or_create( transaction_id=transaction["id"], defaults=transaction_data ) return payment