def push_notifications(request): data = recurly.objects_for_push_notification(request.body) try: signal = getattr(signals, data['type']) except AttributeError: return HttpResponseBadRequest("Invalid notification name.") signal.send(sender=request, data=data) return HttpResponse()
def push_notifications(request): logger.debug(request.raw_post_data) xml = request.raw_post_data objects = objects_for_push_notification(xml) try: signal = getattr(signals, objects['type']) except AttributeError: return HttpResponseBadRequest("Invalid notification name.") # data is being passed for backwards capability. signal.send(sender=recurly, xml=xml, **objects) return HttpResponse()
def test_objects_for_push_notification(self): import recurly objs = recurly.objects_for_push_notification( """<?xml version="1.0" encoding="UTF-8"?> <new_subscription_notification> <account> <account_code>[email protected]</account_code> <username>verena</username> <email>[email protected]</email> <first_name>Verena</first_name> <last_name>Test</last_name> <company_name>Company, Inc.</company_name> </account> <subscription> <plan> <plan_code>bronze</plan_code> <name>Bronze Plan</name> <version type="integer">2</version> </plan> <state>active</state> <quantity type="integer">2</quantity> <unit_amount_in_cents type="integer">2000</unit_amount_in_cents> <activated_at type="datetime">2009-11-22T13:10:38-08:00</activated_at> <canceled_at type="datetime"></canceled_at> <expires_at type="datetime"></expires_at> <current_period_started_at type="datetime">2009-11-22T13:10:38-08:00</current_period_started_at> <current_period_ends_at type="datetime">2009-11-29T13:10:38-08:00</current_period_ends_at> <trial_started_at type="datetime">2009-11-22T13:10:38-08:00</trial_started_at> <trial_ends_at type="datetime">2009-11-29T13:10:38-08:00</trial_ends_at> </subscription> </new_subscription_notification>""") self.assertEqual(objs['type'], 'new_subscription_notification') self.assertTrue('account' in objs) self.assertTrue(isinstance(objs['account'], recurly.Account)) self.assertEqual(objs['account'].username, 'verena') self.assertTrue('subscription' in objs) self.assertTrue(isinstance(objs['subscription'], recurly.Subscription)) self.assertEqual(objs['subscription'].state, 'active')
def push_notification(request): # big try catch because we don't want to send recurly any # technical error response with all our settings if we happen to # test the webhooks with DEBUG=True try: data = recurly.objects_for_push_notification(request.body) try: _type = data['type'] logger.info("Recurly notification: %s", _type) signal = getattr(signals, _type) except AttributeError: return HttpResponseBadRequest("Unrecognized notification type.") signal.send(sender=request, data=data) except Exception as e: if settings.DEBUG: logger.exception(e) return HttpResponse(status=500) raise return HttpResponse()
def test_objects_for_push_notification(self): import recurly objs = recurly.objects_for_push_notification("""<?xml version="1.0" encoding="UTF-8"?> <new_subscription_notification> <account> <account_code>[email protected]</account_code> <username>verena</username> <email>[email protected]</email> <first_name>Verena</first_name> <last_name>Test</last_name> <company_name>Company, Inc.</company_name> </account> <subscription> <plan> <plan_code>bronze</plan_code> <name>Bronze Plan</name> <version type="integer">2</version> </plan> <state>active</state> <quantity type="integer">2</quantity> <unit_amount_in_cents type="integer">2000</unit_amount_in_cents> <activated_at type="datetime">2009-11-22T13:10:38-08:00</activated_at> <canceled_at type="datetime"></canceled_at> <expires_at type="datetime"></expires_at> <current_period_started_at type="datetime">2009-11-22T13:10:38-08:00</current_period_started_at> <current_period_ends_at type="datetime">2009-11-29T13:10:38-08:00</current_period_ends_at> <trial_started_at type="datetime">2009-11-22T13:10:38-08:00</trial_started_at> <trial_ends_at type="datetime">2009-11-29T13:10:38-08:00</trial_ends_at> </subscription> </new_subscription_notification>""") self.assertEqual(objs['type'], 'new_subscription_notification') self.assertTrue('account' in objs) self.assertTrue(isinstance(objs['account'], recurly.Account)) self.assertEqual(objs['account'].username, 'verena') self.assertTrue('subscription' in objs) self.assertTrue(isinstance(objs['subscription'], recurly.Subscription)) self.assertEqual(objs['subscription'].state, 'active')