def sync_payment(class_, recurly_transaction=None, uuid=None): if recurly_transaction is None: recurly_transaction = recurly.Transaction.get(uuid) logger.debug("Payment.sync: %s", recurly_transaction.uuid) payment = modelify(recurly_transaction, class_, remove_empty=True, follow=['account']) payment.xml = recurly_transaction.as_log_output(full=True) if payment.invoice_id is None: payment.invoice_id = recurly_transaction.invoice().uuid # TODO: (IW) Hacky if hasattr(payment, 'account'): # Account if not payment.account.pk: payment.account.save(remote=False) payment.account_id = payment.account.pk if payment.is_dirty(): logger.debug("dirty payment: %s", payment.dirty_fields()) payment.save() return payment
def ______sync_subscription(class_, recurly_subscription=None, uuid=None): if recurly_subscription is None: recurly_subscription = recurly.Subscription.get(uuid) logger.debug("Subscription.sync: %s", recurly_subscription.uuid) subscription = modelify(recurly_subscription, class_, follow=['account']) subscription.xml = recurly_subscription.as_log_output() # TAKE NOTE: # `modelify()` doesn't assume you want to save every generated model # object, including foreign relationships. So if this is a subscription # for a new account, and you save the subscription before creating the # account, the subscription will have a null value for `account_id` # (as the account will not have been created yet). Also, simply saving # `payment.account` first isn't enough because Django doesn't # automatically set `payment.account_id` with the newly generated # account pk (note, though, that `payment.account.pk` *will* be set # after calling `payment.account.save()`). if hasattr(subscription, 'account'): # Save the account if not subscription.account.pk: subscription.account.save(remote=False) subscription.account_id = subscription.account.pk subscription.save(remote=False) return subscription
def get_pending_subscription_or_none(self): recurly_subscription = self.get_recurly_subscription() recurly_pending_subscription = getattr(recurly_subscription, "pending_subscription", None) if recurly_pending_subscription: from django_recurly.provisioning import modelify pending_subscription = modelify(recurly_pending_subscription, Subscription, save=False) return pending_subscription return None
def handle_notification(class_, **kwargs): # Get latest transaction details from Recurly recurly_transaction = recurly.Transaction.get( kwargs.get("transaction").id) payment = modelify(recurly_transaction, class_, remove_empty=True) payment.invoice_id = recurly_transaction.invoice().uuid # payment.xml = recurly_transaction.as_log_output(full=True) # Extra data sent only with notifications notification_transaction = kwargs.get('transaction') payment.message = notification_transaction.message payment.notification_xml = kwargs.get('xml') if hasattr(payment, 'account'): # Account if payment.account is not None and not payment.account.pk: payment.account.save(remote=False) payment.account_id = payment.account.pk payment.save() return payment
def __handle_notification(class_, **kwargs): """Update/create an account and its associated subscription using data from Recurly""" # First get the up-to-date account details directly from Recurly and # sync local record (update existing, or create new) account = class_.sync_account( account_code=kwargs.get("account").account_code) # Now do the same with the subscription (if there is one) if not kwargs.get("subscription"): subscription = None else: recurly_subscription = recurly.Subscription.get( kwargs.get("subscription").uuid) subscription = modelify(recurly_subscription, Subscription, context={'account': account}) subscription.xml = recurly_subscription.as_log_output() subscription.save(remote=False) return account, subscription