Stripe Checkout can be created via BBCode markup

This commit is contained in:
Chris Beach 2017-10-09 08:39:21 +01:00
parent 0e19102139
commit 6814c80540
7 changed files with 129 additions and 3 deletions

View File

@ -53,6 +53,33 @@ Card numbers in **bold** have beed tested.
* **4000 0000 0000 0119** Charge is declined with a processing_error code.
* 4242 4242 4242 4241 Charge is declined with an incorrect_number code as the card number fails the Luhn check.
## Markup
Stripe checkout forms to be added to forum posts using markup:
#### Example
[stripe-checkout amount=999 image=/img/someImage.png]
Widget
[/stripe-checkout]
Which renders as:
<form action="" method="POST">
<script
src="https://checkout.stripe.com/checkout.js"
class="stripe-button"
data-key="pk_test_6pRXXX"
data-amount="999"
data-name="Your Shop"
data-description="Widget"
data-image="/img/someImage.png"
data-locale="auto">
</script>
</form>
## Warranty
This software comes with no warranty of any kind.

View File

@ -1,3 +0,0 @@
<a href="/donate">
{{i18n 'discourse_donations.nav_item'}}
</a>

View File

@ -0,0 +1,87 @@
import { registerOption } from 'pretty-text/pretty-text';
registerOption((siteSettings, opts) => {
opts.features['discourse_donations'] = !!siteSettings.discourse_donations_enabled;
});
function validationErrors(tagInfo, content) {
let errors = [];
if (!Discourse.SiteSettings['discourse_donations_public_key']) { errors.push("missing key (site setting)"); }
if (!Discourse.SiteSettings['discourse_donations_currency']) { errors.push("missing currency (site setting)"); }
if (!Discourse.SiteSettings['stripe_name']) { errors.push("missing name (site setting)"); }
if (!Discourse.SiteSettings['discourse_donations_hide_zip_code']) { errors.push("missing hide zip code (site setting)"); }
if (!tagInfo.attrs['amount']) { errors.push("missing amount"); }
if (!content) { errors.push("missing description"); }
return errors;
}
function replaceWithStripeOrError(state, tagInfo, content) {
let errors = validationErrors(tagInfo, content);
if (errors.length) {
displayErrors(state, errors);
} else {
insertCheckout(state, tagInfo, content);
}
return true;
}
function displayErrors(state, errors) {
let token = state.push('div-open', 'div', 1);
token.attrs = [['class', 'stripe-errors']];
token = state.push('html_inline', '', 0);
token.content = "Stripe checkout can't be rendered: " + errors.join(", ");
state.push('div-close', 'div', -1);
}
function insertCheckout(state, tagInfo, content) {
let token = state.push('stripe-checkout-form-open', 'form', 1);
token.attrs = [['method', 'POST'], ['action', '/charges']];
token = state.push('stripe-checkout-script-open', 'script', 0);
token.attrs = [
['src', 'https://checkout.stripe.com/checkout.js'],
['class', 'stripe-button'],
['data-key', Discourse.SiteSettings['discourse_donations_public_key']],
['data-amount', tagInfo.attrs['amount']],
['data-name', Discourse.SiteSettings['discourse_donations_shop_name']],
['data-description', content],
['data-image', tagInfo.attrs['image'] || ''],
['data-locale', 'auto'],
['data-zip-code', !Discourse.SiteSettings['discourse_donations_hide_zip_code']],
['data-currency', Discourse.SiteSettings['discourse_donations_currency']]
];
state.push('stripe-checkout-script-close', 'script', -1);
state.push('stripe-checkout-form-close', 'form', -1);
}
function setupMarkdownIt(helper) {
helper.registerPlugin(md => {
md.inline.bbcode.ruler.push('stripe-checkout', {
tag: 'stripe',
replace: replaceWithStripeOrError
});
});
}
export function setup(helper) {
helper.whiteList([
'div[class]',
'form[method]', 'form[action]',
'script[class]', 'script[src]',
'script[data-key]',
'script[data-amount]',
'script[data-name]',
'script[data-description]',
'script[data-image]',
'script[data-zip-code]',
'script[data-currency]',
'script[data-locale]'
]);
if (helper.markdownIt) {
setupMarkdownIt(helper);
} else {
console.log("Please upgrade Discourse to a later version in order to use this plugin");
}
}

View File

@ -0,0 +1,7 @@
div.stripe-errors {
border: 1px solid #c33;
border-radius: 5px;
color: #600;
background-color: #fdd;
padding: 5px 10px;
}

View File

@ -4,6 +4,7 @@ en:
discourse_donations_enable_create_accounts: "EXPERIMENTAL: Enable anonymous users to create accounts after successful payment"
discourse_donations_secret_key: Stripe Secret Key
discourse_donations_public_key: Stripe Public Key
discourse_donations_shop_name: Stripe Shop Name
discourse_donations_currency: Currency Code
discourse_donations_hide_zip_code: Hide Zip Code
discourse_donations_reward_badge_name: Grant this badge to user when a payment is successful

View File

@ -3,8 +3,10 @@ plugins:
default: false
client: true
discourse_donations_secret_key:
default: 'YOUR STRIPE API SECRET'
client: false
discourse_donations_public_key:
default: 'YOUR STRIPE API KEY'
client: true
discourse_donations_enable_create_accounts:
client: true
@ -12,6 +14,9 @@ plugins:
discourse_donations_description:
client: true
default: 'Donation'
discourse_donations_shop_name:
client: true
default: 'Shop Name'
discourse_donations_currency:
client: true
default: 'USD'

View File

@ -8,6 +8,8 @@ gem 'stripe', '2.8.0'
load File.expand_path('../lib/discourse_donations/engine.rb', __FILE__)
register_asset "stylesheets/discourse-donations.css"
enabled_site_setting :discourse_donations_enabled
after_initialize do