def process_subhub_event_subscription_updated(data): statsd.incr('news.tasks.process_subhub_event.subscription_updated') user_data = get_user_data(payee_id=data['customer_id'], extra_fields=['id']) if not user_data: statsd.incr( 'news.tasks.process_subhub_event.subscription_updated.user_not_found' ) raise RetryTask('Could not find user. Try again.') direction = 'Down' if data['event_type'].endswith('downgrade') else 'Up' stage_name = f'Subscription {direction}grade' sfdc.opportunity.create({ 'Amount': cents_to_dollars(data['plan_amount_new']), 'Plan_Amount_Old__c': cents_to_dollars(data['plan_amount_old']), 'Billing_Cycle_End__c': iso_format_unix_timestamp(data['current_period_end']), 'CloseDate': iso_format_unix_timestamp(data.get('close_date', time())), 'Donation_Contact__c': user_data['id'], 'Event_Id__c': data['event_id'], 'Event_Name__c': data['event_type'], 'Invoice_Number__c': data['invoice_number'], 'Name': 'Subscription Services', 'Payment_Interval__c': data['interval'], 'Payment_Source__c': 'Stripe', 'PMT_Invoice_ID__c': data['invoice_id'], 'PMT_Subscription_ID__c': data['subscription_id'], 'Proration_Amount__c': data['proration_amount'], 'RecordTypeId': settings.SUBHUB_OPP_RECORD_TYPE, 'Service_Plan__c': data['nickname_new'], 'Nickname_Old__c': data['nickname_old'], 'StageName': stage_name, })
def process_subhub_event_subscription_cancel(data): """ Event name: customer.subscription_cancelled or customer.deleted """ statsd.incr('news.tasks.process_subhub_event.subscription_cancel') user_data = get_user_data(payee_id=data['customer_id'], extra_fields=['id']) if not user_data: statsd.incr( 'news.tasks.process_subhub_event_subscription_cancel.user_not_found' ) raise RetryTask('Could not find user. Try again.') nickname = data['nickname'] if isinstance(nickname, list): nickname = nickname[0] sfdc.opportunity.create({ 'Amount': cents_to_dollars(data['plan_amount']), 'Billing_Cycle_End__c': iso_format_unix_timestamp(data['current_period_end']), 'Billing_Cycle_Start__c': iso_format_unix_timestamp(data['current_period_start']), 'CloseDate': iso_format_unix_timestamp(data.get('cancel_at', time())), 'Donation_Contact__c': user_data['id'], 'Event_Id__c': data['event_id'], 'Event_Name__c': data['event_type'], 'Name': 'Subscription Services', 'Payment_Source__c': 'Stripe', 'PMT_Subscription_ID__c': data['subscription_id'], 'RecordTypeId': settings.SUBHUB_OPP_RECORD_TYPE, 'Service_Plan__c': nickname, 'StageName': SUB_STAGE_NAMES[data['event_type']], }) if data['event_type'] == 'customer.deleted': sfdc.update(user_data, {'fxa_deleted': True})
def process_subhub_event_payment_failed(data): """ Event name: invoice.payment_failed """ statsd.incr('news.tasks.process_subhub_event.payment_failed') user_data = get_user_data(payee_id=data['customer_id'], extra_fields=['id']) # the only user identifiable information available is the payment # processor/Stripe ID, so if the user wasn't found by that, there's really # nothing to be done here but retry. if not user_data: statsd.incr( 'news.tasks.process_subhub_event.payment_failed.user_not_found') raise RetryTask('Could not find user. Try again.') nickname = data['nickname'] if isinstance(nickname, list): nickname = nickname[0] sfdc.opportunity.create({ 'Amount': cents_to_dollars(data['amount_due']), 'CloseDate': iso_format_unix_timestamp(data['created']), 'Donation_Contact__c': user_data['id'], 'Event_Id__c': data['event_id'], 'Event_Name__c': data['event_type'], 'Name': 'Subscription Services', 'PMT_Subscription_ID__c': data['subscription_id'], 'PMT_Transaction_ID__c': data['charge_id'], 'Payment_Source__c': 'Stripe', 'RecordTypeId': settings.SUBHUB_OPP_RECORD_TYPE, 'Service_Plan__c': nickname, 'StageName': 'Payment Failed', 'currency__c': data['currency'], })
def process_subhub_event_subscription_reactivated(data): statsd.incr('news.tasks.process_subhub_event.subscription_reactivated') user_data = get_user_data(payee_id=data['customer_id'], extra_fields=['id']) if not user_data: statsd.incr( 'news.tasks.process_subhub_event.subscription_reactivated.user_not_found' ) raise RetryTask('Could not find user. Try again.') nickname = data['nickname'] if isinstance(nickname, list): nickname = nickname[0] sfdc.opportunity.create({ 'Amount': cents_to_dollars(data['plan_amount']), 'Billing_Cycle_End__c': iso_format_unix_timestamp(data['current_period_end']), 'CloseDate': iso_format_unix_timestamp(data.get('close_date', time())), 'Credit_Card_Type__c': data['brand'], 'Last_4_Digits__c': data['last4'], 'Donation_Contact__c': user_data['id'], 'Event_Id__c': data['event_id'], 'Event_Name__c': data['event_type'], 'Name': 'Subscription Services', 'Payment_Source__c': 'Stripe', 'PMT_Subscription_ID__c': data['subscription_id'], 'RecordTypeId': settings.SUBHUB_OPP_RECORD_TYPE, 'Service_Plan__c': nickname, 'StageName': 'Reactivation', })
def process_subhub_event_subscription_charge(data): """ Event names: customer.subscription.created, customer.recurring_charge This method handles both new and recurring charges. Each of the handled events contains the same payload data. The only variation below is in regards to Initial_Purchase__c, which will be True for the `customer.subscription.created` event, and False for the `customer.recurring_charge` event. """ statsd.incr('news.tasks.process_subhub_event.subscription_charge') user_data = get_user_data(payee_id=data['customer_id'], extra_fields=['id']) if not user_data: statsd.incr( 'news.tasks.process_subhub_event.subscription_charge.user_not_found' ) raise RetryTask('Could not find user. Try again.') nickname = data['nickname'] if isinstance(nickname, list): nickname = nickname[0] # if a customer re-instates service after a cancellation, the record needs to be updated oppy_data = { 'Amount': cents_to_dollars(data['plan_amount']), 'Billing_Cycle_End__c': iso_format_unix_timestamp(data['current_period_end']), 'Billing_Cycle_Start__c': iso_format_unix_timestamp(data['current_period_start']), 'CloseDate': iso_format_unix_timestamp(data['created']), 'Credit_Card_Type__c': data['brand'], 'currency__c': data['currency'], 'Donation_Contact__c': user_data['id'], 'Event_Id__c': data['event_id'], 'Event_Name__c': data['event_type'], 'Initial_Purchase__c': data['event_type'] == 'customer.subscription.created', 'Invoice_Number__c': data['invoice_number'], 'Last_4_Digits__c': data['last4'], 'Name': 'Subscription Services', 'Next_Invoice_Date__c': iso_format_unix_timestamp(data['next_invoice_date']), 'Payment_Source__c': 'Stripe', 'PMT_Subscription_ID__c': data['subscription_id'], 'PMT_Transaction_ID__c': data['charge'], 'RecordTypeId': settings.SUBHUB_OPP_RECORD_TYPE, 'Service_Plan__c': nickname, 'StageName': 'Closed Won', } if 'proration_amount' in data: oppy_data['Proration_Amount__c'] = cents_to_dollars( data['proration_amount']) if 'total_amount' in data: oppy_data['Total_Amount__c'] = cents_to_dollars(data['total_amount']) sfdc.opportunity.upsert(f'PMT_Invoice_ID__c/{data["invoice_id"]}', oppy_data)
def test_invalid_data(self): self.assertEqual(cents_to_dollars('briefcase'), 0)
def test_valid_string_data(self): self.assertEqual(cents_to_dollars('5005'), 50.05)
def test_valid_int_data(self): self.assertEqual(cents_to_dollars(5005), 50.05)