Django:Stripe& ;;后请求

Django:Stripe& ;;后请求,django,stripe-payments,Django,Stripe Payments,我目前正在Django项目中尝试实现Stripe Connect。标准账户的条带文档状态: 假设没有发生错误,最后一步是使用提供的代码 向access\u token\u url端点发出POST请求以获取 用户的条带凭据: 我现在想知道如何在没有表单和用户操作的情况下使用Django发送POST请求(单击submit按钮)?多亏了@psmvac,我现在可以使用Stripe的oAuth以“正确”的方式实现它。这里有一些参考/示例Django代码,如果有人尝试相同的方法。显然,必须配置urls.py

我目前正在Django项目中尝试实现Stripe Connect。标准账户的条带文档状态:

假设没有发生错误,最后一步是使用提供的代码 向access\u token\u url端点发出POST请求以获取 用户的条带凭据:


我现在想知道如何在没有表单和用户操作的情况下使用Django发送POST请求(单击submit按钮)?

多亏了@psmvac,我现在可以使用Stripe的oAuth以“正确”的方式实现它。这里有一些参考/示例Django代码,如果有人尝试相同的方法。显然,必须配置urls.py。这在my views.py中:

def stripe_authorize(request):
    import stripe

    stripe.api_key = ''
    stripe.client_id = 'XYZ'

    url = stripe.OAuth.authorize_url(scope='read_only')
    return redirect(url)


def stripe_callback(request):
    import stripe
    from django.http import HttpResponse
    # import requests

    stripe.api_key = 'XYZ'
    ## import json  # ?

    code = request.GET.get('code', '')
    try:
        resp = stripe.OAuth.token(grant_type='authorization_code', code=code)
    except stripe.oauth_error.OAuthError as e:
        full_response = 'Error: ' + str(e)
        return HttpResponse(full_response)

    full_response = '''
<p>Success! Account <code>{stripe_user_id}</code> is connected.</p>
<p>Click <a href="/stripe-deauthorize?stripe_user_id={stripe_user_id}">here</a> to
disconnect the account.</p>
'''.format(stripe_user_id=resp['stripe_user_id'])
    return HttpResponse(full_response)


def stripe_deauthorize(request):
    from django.http import HttpResponse
    import stripe

    stripe_user_id = request.GET.get('stripe_user_id', '')
    try:
        stripe.OAuth.deauthorize(stripe_user_id=stripe_user_id)
    except stripe.oauth_error.OAuthError as e:
        return 'Error: ' + str(e)

    full_response = '''
<p>Success! Account <code>{stripe_user_id}</code> is disconnected.</p>
<p>Click <a href="/">here</a> to restart the OAuth flow.</p>
'''.format(stripe_user_id=stripe_user_id)
    return HttpResponse(full_response)

由于Standard Connect的连接流依赖于OAuth:

因此,正如您所提到的,您可以使用像Rauth这样的OAuth python库来处理流

另外请注意,Stripe Python库在此处提供了OAuth流的实现:

您可以在此处看到其用法示例:

该示例使用的不是Django,而是Flask,它应该会让您对它的使用有一个很好的了解


关于使用现有OAuth实现而不是自己直接实现调用的优点:我看到的一个优点是,您的代码将重用一个库,该库通常涵盖所有不同的用例(例如更好的错误处理),并且经过良好的测试

看看这个插件,它拥有所有的功能,你需要从你的服务器向Stripe发出请求-这不是客户端在浏览器中发出的请求。你救了我一天!我甚至不知道这个例子存在,但它帮助实现了它。(见下文)非常感谢@psmvac
def stripe_authorize(request):
    import stripe

    stripe.api_key = ''
    stripe.client_id = 'XYZ'

    url = stripe.OAuth.authorize_url(scope='read_only')
    return redirect(url)


def stripe_callback(request):
    import stripe
    from django.http import HttpResponse
    # import requests

    stripe.api_key = 'XYZ'
    ## import json  # ?

    code = request.GET.get('code', '')
    try:
        resp = stripe.OAuth.token(grant_type='authorization_code', code=code)
    except stripe.oauth_error.OAuthError as e:
        full_response = 'Error: ' + str(e)
        return HttpResponse(full_response)

    full_response = '''
<p>Success! Account <code>{stripe_user_id}</code> is connected.</p>
<p>Click <a href="/stripe-deauthorize?stripe_user_id={stripe_user_id}">here</a> to
disconnect the account.</p>
'''.format(stripe_user_id=resp['stripe_user_id'])
    return HttpResponse(full_response)


def stripe_deauthorize(request):
    from django.http import HttpResponse
    import stripe

    stripe_user_id = request.GET.get('stripe_user_id', '')
    try:
        stripe.OAuth.deauthorize(stripe_user_id=stripe_user_id)
    except stripe.oauth_error.OAuthError as e:
        return 'Error: ' + str(e)

    full_response = '''
<p>Success! Account <code>{stripe_user_id}</code> is disconnected.</p>
<p>Click <a href="/">here</a> to restart the OAuth flow.</p>
'''.format(stripe_user_id=stripe_user_id)
    return HttpResponse(full_response)