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_coins(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})
Beispiel #2
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_coins(plan)
    except InvalidRequestError as e:
        
        return render_json(422, {'error': str(e)})
    except Exception as e:

        return render_json(200, {'error': str(e)})

    return render_json(200, {'success': True})
Beispiel #3
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