-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #299 from jambonsw/templatetags
Added template tag to expose the Stripe public key.
- Loading branch information
Showing
4 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ***" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
""" | ||
) |