Receiving a webhook event for Stripe in Flask

不羁的心 提交于 2020-01-15 08:08:05

问题


I am new to Flask and will appreciate little help with stripe payments. I currently can "Send" data to stripe with simple view and it communicates with stripe site as it shows payment charged against the subscription plan. But I need to "receive" signal from stripe so I can capture the event and take action as needed. For that I would appreciated if according to the following view someone help me to figure it out. Following view is to charge customer and now I need to retrieve expiration from stripe end.

@app.route('/charged', methods=['POST'])
def charge():
    # Amount in cents
    amount = 9900
    # customer
    customer = stripe.Customer.create(
        email='testing@gmail.com',
        source=request.form['stripeToken']
    )
    try:
        charge = stripe.Charge.create(
            customer=customer.id,
            capture='true',
            amount=amount,
            currency='usd',
            description='standard',
        )
        data="$" + str(float(amount) / 100) + " " + charge.currency.upper()
    except stripe.error.CardError as e:
        # The card has been declined
        body = e.json_body
        err = body['error']
        print
        "Status is: %s" % e.http_status
        print
        "Type is: %s" % err['type']
        print
        "Code is: %s" % err['code']
        print
        "Message is: %s" % err['message']

    return render_template('/charge.html', data=data)

HTML Template:

<form action="/charged" method="post">
            <div class="form-group">
                <label for="email">Amount is $99.00 USD </label>
                <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                    data-key="{{ key }}"
                    data-description="Standard Package"
                    data-name="Demo Site"
                    data-amount="9900"
                    data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
                    data-locale="auto">
                </script>
            </div>
        </form>

I will really appreciate if someone can give me just one example of receiving the expiration date of the package from stripe or any other event.

来源:https://stackoverflow.com/questions/42256026/receiving-a-webhook-event-for-stripe-in-flask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!