Skip to content

Instantly share code, notes, and snippets.

@MichelleGlauser
Created June 5, 2015 19:46
Show Gist options
  • Save MichelleGlauser/8f40c75e5cd7a199ff55 to your computer and use it in GitHub Desktop.
Save MichelleGlauser/8f40c75e5cd7a199ff55 to your computer and use it in GitHub Desktop.
Django StripeCouponView
# Stripe coupons!
class StripeCouponView(generic.View):
def get(self, request):
log.debug(request.GET)
coupon_code = request.GET.get('coupon', '')
api_url = 'https://api.stripe.com/v1/coupons/' + coupon_code
try:
r = requests.get(api_url, auth=(settings.STRIPE_SECRET_KEY, ''))
r.raise_for_status()
response_json = r.json()
if not response_json.get('is_valid', False):
raise Exception
except (requests.exceptions.HTTPError, Exception):
return HttpResponseNotFound('BAD request, Michelle')
return HttpResponse(response_json.get('percent_off', 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment