Skip to content

Commit

Permalink
Merge pull request #299 from jambonsw/templatetags
Browse files Browse the repository at this point in the history
Added template tag to expose the Stripe public key.
  • Loading branch information
paltman authored Dec 13, 2016
2 parents 8333755 + 885a461 commit d12c367
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/reference/templatetags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Template tags

## stripe

#### stripe_public_key

Returns the value of the `PINAX_STRIPE_PUBLIC_KEY` setting as a Javascript
single quoted string. This value can be used to initialize the Stripe
Javascript library. For example:

{% load stripe %}
...
<script>
Stripe.setPublishableKey({% stripe_public_key %});
</script>

will generate:

<script>
Stripe.setPublishableKey('pk_<your public key here>');
</script>

If `PINAX_STRIPE_PUBLIC_KEY` has not been defined, the value
`*** PINAX_STRIPE_PUBLIC_KEY NOT SET ***` is returned **unquoted**. This
will force a JavaScript syntax error to be raised wherever it has been used.
Empty file.
14 changes: 14 additions & 0 deletions pinax/stripe/templatetags/stripe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django import template
from django.conf import settings
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe

register = template.Library()


@register.simple_tag
def stripe_public_key():
if settings.PINAX_STRIPE_PUBLIC_KEY:
return mark_safe("'%s'" % conditional_escape(settings.PINAX_STRIPE_PUBLIC_KEY))
else:
return "*** PINAX_STRIPE_PUBLIC_KEY NOT SET ***"
40 changes: 40 additions & 0 deletions pinax/stripe/tests/test_templatetags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from django.test import TestCase, override_settings
from django.template import Template, Context


class TemplateTagTests(TestCase):

@override_settings(PINAX_STRIPE_PUBLIC_KEY='this-is-the-stripe-public-key')
def test_stripe_public_key(self):
self.maxDiff = None
template = Template("""{% load stripe %}
<script>
Stripe.setPublishableKey({% stripe_public_key %});
</script>
""")

self.assertEqual(
template.render(Context()),
"""
<script>
Stripe.setPublishableKey('this-is-the-stripe-public-key');
</script>
"""
)

def test_no_stripe_public_key(self):
self.maxDiff = None
template = Template("""{% load stripe %}
<script>
Stripe.setPublishableKey({% stripe_public_key %});
</script>
""")

self.assertEqual(
template.render(Context()),
"""
<script>
Stripe.setPublishableKey(*** PINAX_STRIPE_PUBLIC_KEY NOT SET ***);
</script>
"""
)

0 comments on commit d12c367

Please sign in to comment.