如果烧瓶中的事务成功,则条带不会保存 [英] stripe does not save if transaction is successful in flask

查看:52
本文介绍了如果烧瓶中的事务成功,则条带不会保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果交易成功,我试图在stripe_webhook视图中将一些信息保存在数据库中.但是没有成功.不能将数据直接保存在webhook中吗?对我来说真是令人沮丧.我在网上检查了示例代码,但找不到为成功交易插入或更新数据库的代码.

I am trying to save some information in database if transaction is successful, in the stripe_webhook view. But not successful. is it that data cannot be saved directly in webhook ? so frustrating for me. I checked online for sample codes but could not find the ones that insert or update database for successful transaction.

from site.models import Post, Chapter, Order
import stripe
from sqlalchemy import desc

@posts.route("/paynow")
@login_required
def paynow():
    return render_template('paynow.html',)
        


@posts.route('/stripe_pay')
@login_required
def stripe_pay():
    amt = 10000
    stripe.api_key = current_app.config['STRIPE_SECRET_KEY']
    session = stripe.checkout.Session.create(
        payment_method_types=['card'],
        line_items=[{
            'price_data': {
            'currency': 'usd',
            'product_data': {
            'name': 'T-shirt',
            },
            'unit_amount': amt,
            },
            'quantity': 1,
        }],
        mode='payment',
        success_url=url_for('posts.payment_success', _external=True) + '?session_id={CHECKOUT_SESSION_ID}',
        cancel_url=url_for('posts.paynow', _external=True),
    )
    return {
    'checkout_session_id': session['id'], 
    'checkout_public_key': current_app.config['STRIPE_PUBLIC_KEY']
    }




@posts.route('/stripe_webhook', methods=['POST'])
@login_required
def stripe_webhook():
    print('WEBHOOK CALLED')

    if request.content_length > 1024 * 1024:
        print('REQUEST TOO BIG')
        abort(400)
    payload = request.get_data()
    sig_header = request.environ.get('HTTP_STRIPE_SIGNATURE')
    endpoint_secret = 'whsec_*************************************'
    event = None

    try:
        event = stripe.Webhook.construct_event(
            payload, sig_header, endpoint_secret
        )
    except ValueError as e:
        # Invalid payload
        print('INVALID PAYLOAD')
        return {}, 400
    except stripe.error.SignatureVerificationError as e:
        # Invalid signature
        print('INVALID SIGNATURE')
        return {}, 400

    # Handle the checkout.session.completed event
    if event['type'] == 'checkout.session.completed':
        session = event['data']['object']
        print(session)
        line_items = stripe.checkout.Session.list_line_items(session['id'], limit=1)
        print(line_items['data'][0]['description'])
        # save to database if successful
        save_order = Order(trans_id = "pppppppppp")
        db.session.add(save_order)
        db.session.commit()
        


    return {}

@posts.route('/payment_success')
@login_required
def payment_success():
    cart=Cart.query.filter_by(username = current_user.username).all()
    cart.status = "paid"
    db.session.commit()
    return render_template('payment_success.html')

推荐答案

根据Stripe客户服务,我必须使用活动域而不是本地主机

According to the Stripe customer care, I must used a live domain instead of local host

这篇关于如果烧瓶中的事务成功,则条带不会保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆