Set up stripe donation button (#158)

* Set up stripe donation button

* Add crsf_exempt import

* rebase and update
This commit is contained in:
Paul Hallett 2016-04-22 10:55:42 +01:00
parent 00a968787a
commit 8c8f80d66c
5 changed files with 77 additions and 15 deletions

View file

@ -170,3 +170,21 @@ MARKDOWN_DEUX_STYLES = {
"safe_mode": False,
},
}
# Stripe
STRIPE_TEST_SECRET_KEY = os.environ.get('STRIPE_TEST_SECRET_KEY', '')
STRIPE_SECRET_KEY = os.environ.get('STRIPE_SECRET_KEY', '')
STRIPE_PUBLISHABLE_KEY = os.environ.get('STRIPE_PUBLISHABLE_KEY', '')
STRIPE_TEST_PUBLISHABLE_KEY = os.environ.get('STRIPE_TEST_PUBLISHABLE_KEY', '')
if DEBUG:
STRIPE_KEYS = {
"secret": STRIPE_TEST_SECRET_KEY,
"publishable": STRIPE_TEST_PUBLISHABLE_KEY
}
else:
STRIPE_KEYS = {
"secret": STRIPE_SECRET_KEY,
"publishable": STRIPE_PUBLISHABLE_KEY
}

View file

@ -2,11 +2,13 @@
from __future__ import unicode_literals
from django.conf import settings
from django.conf.urls import url, include
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.views.generic import TemplateView
from pokemon import urls as pokemon_urls
from pokemon_v2 import urls as pokemon_v2_urls
# need to make sure v2 urls resolve last so angular routes have control
@ -15,7 +17,7 @@ from pokemon_v2 import urls as pokemon_v2_urls
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r"^stripe/donation", "config.views.stripe_donation"),
url(r'^media/(?P<path>.*)',
'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),

View file

@ -1,13 +1,17 @@
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
from django.conf import settings
from django.shortcuts import render_to_response
from django.template import RequestContext
from pokemon_v2.models import * # NOQA
from django.views.decorators.csrf import csrf_exempt
from hits.models import ResourceView
import stripe
from pokemon_v2.models import * # NOQA
def _total_site_data():
@ -244,13 +248,42 @@ def home(request):
total_views = ResourceView.objects.total_count()
if total_views > 100:
total_views = int(round(total_views, -2))
total_views = int(round(total_views, -2))
stripe_key = settings.STRIPE_KEYS['publishable']
return render_to_response(
'pages/home.html',
{
'total_views': total_views,
'stripe_key': stripe_key
},
context_instance=RequestContext(request)
)
@csrf_exempt
def stripe_donation(request):
if request.method == 'POST':
# Amount in cents
amount = 1000
stripe.api_key = settings.STRIPE_KEYS['secret']
customer = stripe.Customer.create(
email=request.POST.get('stripeEmail', ''),
card=request.POST.get('stripeToken', '')
)
try:
stripe.Charge.create(
customer=customer.id,
amount=amount,
currency='usd',
description='PokeAPI donation'
)
except:
pass
return redirect('/')
return redirect('/')

View file

@ -3,6 +3,7 @@ Pillow==2.6.1
Unipath==1.0
coverage==3.6
django-appconf==0.6
dj-stripe==0.3.5
django-cors-headers==1.0.0
django-discover-runner==0.4
django-imagekit==3.2.4

View file

@ -51,10 +51,10 @@
<p>Finally; all the Pokémon data you'll ever need, in one place,<br /> and easily accessible through a modern RESTful API.</p>
<p>Whats new in V2? Check out the <a href="/docsv2/">docs</a>!</p>
</div>
<div class="container">
<div class="row">
@ -187,9 +187,9 @@
<div class="row">
<div class="col-md-10 center-block">
<div class="row">
<div class="col-md-4">
<p class="lead center">
What is this?
@ -223,12 +223,20 @@
<div class="col-md-4">
<p class="lead center">
I want to help.
Keep PokéAPI Running
</p>
<div class="well">
<p>Amazing!</p>
<p>Head over to <a href="https://github.com/phalt/pokeapi">GitHub</a> and read some of the issues and feature requests. If you can contribute then go ahead and start making pull requests :)</p>
</div>
<form action="/stripe/donation" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="{{stripe_key}}"
data-image="http://i.imgur.com/Jya9cm0.png"
data-name="pokeapi.co"
data-description="Donate and keep pokeapi alive!"
data-amount="1000"
data-panel-label="Donate">
</script>
</form></p></div>
</div>
</div>
</div>