I've been trying to send extra parameters when submiting the stripe button, which is integrated in my Django application, but I cannot make it work.
So far, I have this:
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>
Any hints on this, please ?
//EDIT:
I tried to integrate what Ywain gave me into my app and I get "POST /stripe/ HTTP/1.1" 405 0 in console after completing and sending the form. What do I do wrong ?
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 %}
<form>tag has an emptyactionattribute, so it's going to be submitted to the same page. Are you sure this is what you want? Aside from that, your extra parameter should be posted along with Checkout'sstripeTokenandstripeEmail, and be accessible viarequest.POST['extraParam2'].request.POST['extraParam2']didn't work. I just don't receive in the response myextraParam2.request.POST['stripeToken'].