Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added template tag to expose the Stripe public key. #299

Merged
merged 3 commits into from
Dec 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
"""
)