Esempio n. 1
0
    def create_report(self):
        total_count = 0
        current_ongoing_count = 0
        past_due_members = []
        pending_cancellation_members = []
        long_trial_members = []
        per_month_average = 0.0
        per_month_average_before_fees = 0.0
        per_month_baseline = 0.0

        for subscription in stripe.Subscription.list(
                expand=['data.customer', 'data.plan.product'
                        ]).auto_paging_iter():
            # Some plans don't have names. It looks like these are all deleted plans and it looks like all of those
            # correspond to subscriptions created by Paid Memberships Pro. They should be counted as memberships since they
            # do represent recurring revenue, but we should look into this further to see if there's a better way to decide
            # what to do with these.
            if hasattr(
                    subscription.plan.product, 'name'
            ) and not 'membership' in subscription.plan.product.name.lower():
                continue
            total_count += 1
            if subscription.status in ('active', 'trialing'):
                if subscription.plan.interval != 'month':
                    raise RuntimeError('wtf')
                if subscription.cancel_at_period_end:
                    pending_cancellation_members.append(
                        summarize_stripe_customer(subscription.customer))
                elif subscription.trial_end and (
                        subscription.trial_end -
                        THIRTY_TWO_DAYS) > time.time():
                    long_trial_members.append(
                        summarize_stripe_customer(subscription.customer))
                else:
                    current_ongoing_count += 1
                    amount_after_transaction_fees = subscription.plan.amount * (
                        1 - 0.029) - 0.3
                    per_month_average += amount_after_transaction_fees / subscription.plan.interval_count
                    per_month_average_before_fees += subscription.plan.amount / subscription.plan.interval_count
                    if subscription.plan.interval_count == 1:
                        per_month_baseline += amount_after_transaction_fees
            else:
                past_due_members.append(
                    summarize_stripe_customer(subscription.customer))

        return Report(
            total_count=total_count,
            current_ongoing_count=current_ongoing_count,
            past_due_members=past_due_members,
            pending_cancellation_members=pending_cancellation_members,
            long_trial_members=long_trial_members,
            per_month_average=round(per_month_average / 100, 2),
            per_month_average_before_fees=round(
                per_month_average_before_fees / 100, 2),
            per_month_baseline=round(per_month_baseline / 100, 2))
Esempio n. 2
0
 def process_customer_subscription_updated(self, event):
     cancel_at_period_end = event['data']['object']['cancel_at_period_end']
     previous_attributes = event['data'].get('previous_attributes', {})
     if 'cancel_at_period_end' in previous_attributes and cancel_at_period_end != previous_attributes[
             'cancel_at_period_end']:
         if cancel_at_period_end:
             self.send_slack_message(
                 text=
                 "Scheduled cancellation: %s's subscription will be cancelled on %s"
                 % (summarize_stripe_customer(
                     event['data']['object']['customer']),
                    self.month_and_day(
                        event['data']['object']['current_period_end'])),
                 attachments=self.create_report_attachments())
         else:
             self.send_slack_message(
                 text=
                 "Reinstatement: %s's subscription will no longer be cancelled."
                 % summarize_stripe_customer(
                     event['data']['object']['customer']),
                 attachments=self.create_report_attachments())
Esempio n. 3
0
def membership_delta_report():
    global membership_delta_cache_date
    global membership_delta_cache_data

    check_token(request.args['token'], 'REPORT_TOKEN')

    if membership_delta_cache_date + timedelta(
            days=MEMBERSHIP_DELTA_CACHE_DAYS) < datetime.utcnow():
        print('recomputing membership delta report')

        include_customers = os.getenv(
            'MEMBERSHIP_REPORTS_INCLUDE_CUSTOMERS') == '1'

        events = []
        for subscription in stripe.Subscription.list(
                status='all', limit=100,
                expand=['data.customer']).auto_paging_iter():
            # subscription id is present to break ties between events with the same date
            events.append((datetime.utcfromtimestamp(subscription.start),
                           subscription.id, 1, subscription.customer))
            if subscription.ended_at:
                events.append(
                    (datetime.utcfromtimestamp(subscription.ended_at),
                     subscription.id, -1, subscription.customer))

        events.sort()

        output = io.StringIO()
        writer = csv.writer(output)
        if include_customers:
            writer.writerow(['Date', 'Membership Change', 'Description'])
        else:
            writer.writerow(['Date', 'Membership Change'])

        for date, _, membership_change, customer in events:
            if include_customers:
                writer.writerow([
                    date.strftime('%Y-%m-%d %H:%M:%S'), membership_change,
                    summarize_stripe_customer(customer)
                ])
            else:
                writer.writerow(
                    [date.strftime('%Y-%m-%d %H:%M:%S'), membership_change])

        membership_delta_cache_data = output.getvalue()
        membership_delta_cache_date = datetime.utcnow()
    else:
        print('using cached membership delta report')

    return flask.Response(membership_delta_cache_data, mimetype='text/csv')
Esempio n. 4
0
 def process_invoice_payment_failed(self, event):
     self.send_slack_message(
         text="Failed payment: %s's payment failed :alert:" %
         summarize_stripe_customer(event['data']['object']['customer']),
         attachments=self.create_report_attachments())
Esempio n. 5
0
 def process_customer_subscription_deleted(self, event):
     self.send_slack_message(
         text="Cancellation: %s's subscription has been cancelled." %
         summarize_stripe_customer(event['data']['object']['customer']),
         attachments=self.create_report_attachments())
Esempio n. 6
0
 def process_customer_subscription_created(self, event):
     self.send_slack_message(
         text='New member: %s' %
         summarize_stripe_customer(event['data']['object']['customer']),
         attachments=self.create_report_attachments())