def test_parse_webhook(app):
    app_webhook_secret = app.config.get('STRIPE_WEBHOOK_KEY',
                                        DUMMY_WEBHOOK_SECRET)
    data, headers = mock_webhook('checkout.session.completed',
                                 webhook_secret=app_webhook_secret)
    parsed_event = checkout.parse_webhook(data, headers)
    assert parsed_event.type == 'checkout.session.completed'
Exemple #2
0
def stripe_webhook():
    event = checkout.parse_webhook(request.data.decode("utf-8"),
                                   request.headers)
    if event.type == 'checkout.session.completed':
        # This is where we will we fulfill the order
        print(event['data'])
        session = event['data']['object']
        product_id = session['client_reference_id']
        product = Product.query.get(product_id)

        customer = checkout.get_customer(session['customer'])
        if not customer or not product:
            abort(400, 'Unknown product & customer')
        send_purchase_email(customer.email, product)

    return "OK", 200
def stripe_webhook():
    # TODO: This line might raise an exception, we need to handle it more elegantly
    event = checkout.parse_webhook(request.data.decode("utf-8"),
                                   request.headers)
    if event.type == 'checkout.session.completed':
        # If this logic becomes more complicated, you may want to define functions for each event
        session = event['data']['object']
        customer = checkout.get_customer(session['customer'])
        if 'client_reference_id' not in session:
            abort(400, 'Unknown product for checkout')

        product_id = session['client_reference_id']
        product = Product.query.get(product_id)
        if not customer or not product:
            abort(400, 'Unknown product & customer')

        send_purchase_email(customer.email, product)

    return "OK", 200
def test_parse_invalid_webhook(app):
    data, headers = mock_webhook('checkout.session.completed',
                                 webhook_secret='bad secret')
    with pytest.raises(stripe.error.SignatureVerificationError):
        checkout.parse_webhook(data, headers)