我一直在尝试在提交条带按钮时发送额外的参数,这些参数集成在我的Django应用程序中,但是我无法使其工作。
到目前为止,我有这个:
views.py
stripe.api_key = "XXXX"
class StripeApi(View):
@staticmethod
def post(request):
a = request.body
event_json = json.dumps(a)
print a
return HttpResponse(
event_json, content_type="application/x-javascript-config")
urls.py
urlpatterns = [
url(r'^stripe/', ApiViews.StripeApi.as_view()),
]
的index.html
<form action="" method="POST">
<input type="text" name="extraParam2" value="55555fghjkldfgdgfasdfghhjjj"> <!-- here, I tried to add extraParam2 -->
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="XXXX"
data-amount="2000"
data-name="Demo Site"
data-description="2 widgets ($20.00)"
data-image="/128x128.png"
data-locale="auto">
</script>
</form>
对此有何提示?
//编辑:
我尝试将Ywain给我的内容整合到我的应用程序中,并在完成并发送表单后在控制台中获得"POST /stripe/ HTTP/1.1" 405 0
。我做错了什么?
views.py
class StripeApi(View):
@staticmethod
def index(request):
return HttpResponse(request, 'index.html', {
'stripe_pub_key': settings.STRIPE_PUBLISHABLE_KEY},
content_type="application/x-javascript-config")
@staticmethod
def charge(request):
charge = stripe.Charge.create(
amount=2000,
currency='usd',
source=request.POST['stripeToken'],
description='Charge for {}'.format(request.POST['stripeEmail'])
)
return HttpResponse(request, 'stripe.html', {'charge_id': charge.id,
'extra_param': request.POST['extraParam2']},
content_type="application/x-javascript-config")
settings.py:
'''stripe'''
STRIPE_SECRET_KEY = 'sk_test_secret',
STRIPE_PUBLISHABLE_KEY = 'pk_test_secret'
stripe.api_key = STRIPE_SECRET_KEY
urls.py
...
url(r'^stripe/', ApiViews.StripeApi.as_view()),
的index.html
<form action="/stripe/" method="POST">
<input type="text" name="extraParam2" value="Test extraParam">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_secret"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-name="Stripe.com"
data-description="2 widgets"
data-amount="2000"
data-locale="auto">
</script>
</form>
stripe.html
{% extends "base_site.html" %}
{% block content %}
<pre>
Charge ID: {{ charge_id }}
Extra param: {{ extra_param }}
</pre>
{% endblock %}
答案 0 :(得分:2)
这是一个最小的Django应用,它说明了如何使用Stripe Checkout并将结果令牌用于create a charge,以及传递额外的参数:
<强> django-stripe.py 强>
import sys
from django.conf import settings
from django.conf.urls import url
from django.core.management import execute_from_command_line
from django.shortcuts import render
from django.views.generic import View
import stripe
settings.configure(
DEBUG=True,
ROOT_URLCONF=sys.modules[__name__],
TEMPLATE_DIRS=['.'],
STRIPE_SECRET_KEY='sk_test_...',
STRIPE_PUBLISHABLE_KEY='pk_test_...'
)
stripe.api_key = settings.STRIPE_SECRET_KEY
class StripeView(View):
@staticmethod
def get(request):
return render(request, 'index.html',
{
'stripe_pub_key': settings.STRIPE_PUBLISHABLE_KEY
})
@staticmethod
def post(request):
charge = stripe.Charge.create(
amount=2000,
currency="usd",
source=request.POST['stripeToken'],
description="Charge for {}".format(request.POST['stripeEmail'])
)
return render(request, 'charge.html',
{
'charge_id': charge.id,
'extra_param': request.POST['extraParam2']
})
urlpatterns = [
url(r'^stripe/', StripeView.as_view()),
]
if __name__ == "__main__":
execute_from_command_line(sys.argv)
<强>的index.html 强>
<form action="" method="POST">
{% csrf_token %}
<input type="text" name="extraParam2" value="55555fghjkldfgdgfasdfghhjjj">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="{{ stripe_pub_key }}"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-name="Stripe.com"
data-description="2 widgets"
data-amount="2000"
data-locale="auto">
</script>
</form>
<强> charge.html 强>
<pre>
Charge ID: {{ charge_id }}
Extra param: {{ extra_param }}
</pre>
您可以通过粘贴Stripe API密钥(在STRIPE_SECRET_KEY
函数调用的STRIPE_PUBLISHABLE_KEY
和settings.configure()
参数中)来测试它,使用python django-stripe.py runserver
启动应用,并且将您的网络浏览器指向http://localhost:8000/stripe。
基本上,index.html包含Checkout表单以及您的额外参数。表单将提交到同一页面(action
标记的<form>
属性为空),并由post()
类的StripeView
方法处理。该方法使用Stripe的API创建实际费用,并将结果费用ID和您的额外参数传递给charge.html模板。
答案 1 :(得分:0)
条纹页面提到使用条带按钮的“自定义”选项,这可能是您想要的,您可以为按钮添加额外的值:
https://stripe.com/docs/checkout#integration-custom
<script src="https://checkout.stripe.com/checkout.js"></script>
<button id="customButton">Purchase</button>
<script>
var handler = StripeCheckout.configure({
key: 'pk_test_6pRNASCoBOKtIshFeQd4XMUh',
image: '/img/documentation/checkout/marketplace.png',
locale: 'auto',
token: function(token) {
// Use the token to create the charge with a server-side script.
// You can access the token ID with `token.id`
}
});
$('#customButton').on('click', function(e) {
// Open Checkout with further options
handler.open({
name: 'Stripe.com',
description: '2 widgets',
amount: 2000
});
e.preventDefault();
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
</script>