html Flask + Stripe Connect

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html Flask + Stripe Connect相关的知识,希望对你有一定的参考价值。

"""
Be sure you set your stripe keys as environment variables
Be sure to include your client id as a variable
To get a client id, create an application in Stripe, grab the client id appropriate for the environment
"""

@app.route('/charge_collect', methods=['POST', 'GET'])
def charge_test():
    """
    Collects credit card information and saves a shared customer, you should save the returned stripe customer id to your model, then use later to charge
    """
    if request.method == 'POST':
        try:
            customer = stripe.Customer.create(
                email=request.form['stripeEmail'],
                card=request.form['stripeToken']
            )
            customer_id = customer.id
            # save this customer_id!
        except stripe.CardError, e:
            # The card has been declined, should show a decline page
            pass
        return "charged - %s" % customer_id
    return render_template('charge.test.html' key=stripe_keys['publishable_key'])


@app.route('/paymentinformation')
def paymentinformation():
    """
    paymentinformation - displays a stripe connect button so a provider can connect their stripe account to your stripe application
    """
    return render_template('payment.html')


@app.route('/authorize')
def authorize():
    """
    authorize - is called in payment.html 
    """
    import urllib    
    site   = current_app.config['STRIPE_SITE'] + current_app.config['STRIPE_AUTHORIZE_URI']
    params = {
                'response_type': 'code',
                'scope': 'read_write',
                'client_id': stripe_keys['client_id']
            }

    # Redirect to Stripe /oauth/authorize endpoint
    url = site + '?' + urllib.urlencode(params)
    return redirect(url)    


@app.route('/oauth/callback')
def callback():
    """
    callback - requests access tokens for provider
    """
    import requests    
    code   = request.args.get('code')
    data   = {
                'client_secret': stripe_keys['secret_key'],
                'grant_type': 'authorization_code',
                'client_id': stripe_keys['client_id'],
                'code': code
             }

    # Make /oauth/token endpoint POST request
    url = current_app.config['STRIPE_SITE'] + current_app.config['STRIPE_TOKEN_URI']
    resp = requests.post(url, params=data)

    # Grab access_token (use this as your user's API key)
    token = resp.json().get('access_token')
    refresh_token = resp.json().get('refresh_token')
    # save these tokens to your model!
    return 'Received Access Token'


@app.route('/charge')
def charge():
    """
    charge - charge an existing customer on behalf of a connected users account
    """
    customer_id = get_customer_id() # get this from your customer model
    access_token = get_connect_stripe_access_token() # get this from your connect user model

    token = stripe.Token.create(
                                   customer=customer_id,
                                   api_key=access_token # user's access token from the Stripe Connect flow
                               )
    charge = stripe.Charge.create(
        amount=1000, # amount in cents
        currency="usd",
        card=token.id,
        description="Test Stripe Charge",
        application_fee=123, # fee if any
        api_key=access_token
    )    
    return 'Charged'
        <h1>
            Payment Information            
        </h1>        
        <p>In order to recieve payment, you must create and connect an account with Stripe.</p>
        <a href="{{ url_for('authorize') }}" class="stripe-connect dark"><span>Connect with Stripe</span></a>
STRIPE_SITE = 'https://connect.stripe.com'
STRIPE_AUTHORIZE_URI = '/oauth/authorize'
STRIPE_TOKEN_URI = '/oauth/token'
        <h1>
            Test Charges
        </h1>
        <p>Your credit card will charged later.</p>
        <form action="{{ url_for('charge_test') }}" id="pay_form" class="pull-right" method="post">
            <script src="https://checkout.stripe.com/v2/checkout.js" class="stripe-button" data-key="{{ key }}" data-name="Application Name Here" data-description="Test Reservation" data-amount="5000"></script>
        </form>

以上是关于html Flask + Stripe Connect的主要内容,如果未能解决你的问题,请参考以下文章

Stripe Webhook - 设计模式

在NestJS 中添加对Stripe 的WebHook 验证

CodeForces 21C Stripe 2 构造题

html 动画边框。 (将“.flash_border”提供给想要闪烁的div)(需要stripe.gif)

覆盖 Payum Stripe Bundle 模板

Webpacker 找不到 https://js.stripe.com/v3/.js