Exemplo n.º 1
0
def event():
    if not request.json:
        return render_json(406, {'error': 'Mime-type is not application/json'})

    if request.json.get('id') is None:
        return render_json(406, {'error': 'Invalid Stripe event'})

    try:
        safe_event = PaymentEvent.retrieve(request.json.get('id'))
        parsed_event = Invoice.parse_from_event(safe_event)

        user = Invoice.prepare_and_save(parsed_event)

        if parsed_event.get('total') > 0:
            plan = Subscription.get_plan_by_id(user.subscription.plan)
            user.add_hccoins(plan)
    except InvalidRequestError as e:
        # We could not parse the event.
        return render_json(422, {'error': str(e)})
    except Exception as e:
        # Return a 200 because something is really wrong and we want Stripe to
        # stop trying to fulfill this webhook request.
        return render_json(200, {'error': str(e)})

    return render_json(200, {'success': True})
Exemplo n.º 2
0
def purchase_hccoins():
    stripe_key = current_app.config.get('STRIPE_PUBLISHABLE_KEY')
    form = PaymentForm(stripe_key=stripe_key)

    if form.validate_on_submit():
        coin_bundles = current_app.config.get('COIN_BUNDLES')
        coin_bundles_form = int(request.form.get('coin_bundles'))

        bundle = next(
            (item
             for item in coin_bundles if item['hccoins'] == coin_bundles_form),
            None)

        if bundle is not None:
            invoice = Invoice()
            created = invoice.create(
                user=current_user,
                currency=current_app.config.get('STRIPE_CURRENCY'),
                amount=bundle.get('price_in_cents'),
                hccoins=coin_bundles_form,
                coupon=request.form.get('coupon_code'),
                token=request.form.get('stripe_token'))

            if created:
                flash(
                    _('%(amount)s hccoins have been added to your account.',
                      amount=coin_bundles_form), 'success')
            else:
                flash(_('You must enable JavaScript for this request.'),
                      'warning')

            return redirect(url_for('bet.place_bet'))

    return render_template('billing/purchase_hccoins.html', form=form)
Exemplo n.º 3
0
    def test_invoice_create(self, users, mock_stripe):
        """ Successfully create an invoice item. """
        user = User.find_by_identity('*****@*****.**')

        invoice = Invoice()
        invoice.create(user=user,
                       currency='usd',
                       amount='900',
                       hccoins=1000,
                       coupon=None,
                       token='cus_000')

        assert user.hccoins == 1100
Exemplo n.º 4
0
def invoices(page):
    search_form = SearchForm()

    sort_by = Invoice.sort_by(request.args.get('sort', 'created_on'),
                              request.args.get('direction', 'desc'))
    order_values = 'invoices.{0} {1}'.format(sort_by[0], sort_by[1])

    paginated_invoices = Invoice.query.join(User) \
        .filter(Invoice.search(request.args.get('q', ''))) \
        .order_by(text(order_values)) \
        .paginate(page, 50, True)

    return render_template('admin/invoice/index.html',
                           form=search_form, invoices=paginated_invoices)
Exemplo n.º 5
0
    def test_invoice_upcoming(self, mock_stripe):
        """ Parse out the data correctly from a Stripe invoice payload. """
        parsed_payload = Invoice.upcoming('cus_000')

        next_bill_on = datetime.datetime(2015, 5, 30, 20, 46, 10)

        assert parsed_payload['plan'] == 'Gold'
        assert parsed_payload['description'] == 'GOLD MONTHLY'
        assert parsed_payload['next_bill_on'] == next_bill_on
        assert parsed_payload['amount_due'] == 500
        assert parsed_payload['interval'] == 'month'
Exemplo n.º 6
0
def billing_details(page):
    paginated_invoices = Invoice.query.filter(
      Invoice.user_id == current_user.id) \
        .order_by(Invoice.created_on.desc()).paginate(page, 12, True)

    if current_user.subscription:
        upcoming = Invoice.upcoming(current_user.payment_id)
        coupon = Coupon.query \
            .filter(Coupon.code == current_user.subscription.coupon).first()
    else:
        upcoming = None
        coupon = None

    return render_template('billing/billing_details.html',
                           paginated_invoices=paginated_invoices,
                           upcoming=upcoming,
                           coupon=coupon)
Exemplo n.º 7
0
    def test_parse_payload_from_event(self):
        """ Parse out the data correctly from a Stripe event payload. """
        event_payload = {
            'created': 1326853478,
            'livemode': False,
            'id': 'evt_000',
            'type': 'invoice.created',
            'object': 'event',
            'request': None,
            'pending_webhooks': 1,
            'api_version': '2016-03-07',
            'data': {
                'object': {
                    'date': 1433018770,
                    'id': 'in_000',
                    'period_start': 1433018770,
                    'period_end': 1433018770,
                    'lines': {
                        'data': [{
                            'id': 'sub_000',
                            'object': 'line_item',
                            'type': 'subscription',
                            'livemode': True,
                            'amount': 0,
                            'currency': 'usd',
                            'proration': False,
                            'period': {
                                'start': 1433162255,
                                'end': 1434371855
                            },
                            'subscription': None,
                            'quantity': 1,
                            'plan': {
                                'interval': 'month',
                                'name': 'Gold',
                                'created': 1424879591,
                                'amount': 500,
                                'currency': 'usd',
                                'id': 'gold',
                                'object': 'plan',
                                'livemode': False,
                                'interval_count': 1,
                                'trial_period_days': 14,
                                'metadata': {},
                                'statement_descriptor': 'GOLD MONTHLY'
                            },
                            'description': None,
                            'discountable': True,
                            'metadata': {}
                        }],
                        'total_count':
                        1,
                        'object':
                        'list',
                        'url':
                        '/v1/invoices/in_000/lines'
                    },
                    'subtotal': 0,
                    'total': 500,
                    'customer': 'cus_000',
                    'object': 'invoice',
                    'attempted': False,
                    'closed': True,
                    'forgiven': False,
                    'paid': True,
                    'livemode': False,
                    'attempt_count': 0,
                    'amount_due': 0,
                    'currency': 'usd',
                    'starting_balance': 0,
                    'ending_balance': 0,
                    'next_payment_attempt': None,
                    'webhooks_delivered_at': None,
                    'charge': None,
                    'discount': None,
                    'application_fee': None,
                    'subscription': 'sub_000',
                    'tax_percent': None,
                    'tax': None,
                    'metadata': {},
                    'statement_descriptor': None,
                    'description': None,
                    'receipt_number': '0009000'
                }
            }
        }

        parsed_payload = Invoice.parse_from_event(event_payload)

        assert parsed_payload['payment_id'] == 'cus_000'
        assert parsed_payload['plan'] == 'Gold'
        assert parsed_payload['receipt_number'] == '0009000'
        assert parsed_payload['description'] == 'GOLD MONTHLY'
        assert parsed_payload['period_start_on'] == datetime.date(2015, 6, 1)
        assert parsed_payload['period_end_on'] == datetime.date(2015, 6, 15)
        assert parsed_payload['currency'] == 'usd'
        assert parsed_payload['tax'] is None
        assert parsed_payload['tax_percent'] is None
        assert parsed_payload['total'] == 500