mirror of
https://github.com/discourse/discourse-subscriptions.git
synced 2025-02-26 21:48:32 +00:00
Use siteSettings properly
This commit is contained in:
parent
92639a483f
commit
943343ffa4
@ -1,28 +1,24 @@
|
||||
import { registerOption } from 'pretty-text/pretty-text';
|
||||
|
||||
registerOption((siteSettings, opts) => {
|
||||
opts.features['discourse_donations'] = !!siteSettings.discourse_donations_enabled;
|
||||
});
|
||||
|
||||
function validationErrors(tagInfo, content) {
|
||||
function validationErrors(tagInfo, content, siteSettings) {
|
||||
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['discourse_donations_shop_name']) { errors.push("missing name (site setting)"); }
|
||||
if (!Discourse.SiteSettings['discourse_donations_hide_zip_code']) { errors.push("missing hide zip code (site setting)"); }
|
||||
if (!siteSettings.discourse_donations_public_key) { errors.push("missing key (site setting)"); }
|
||||
if (!siteSettings.discourse_donations_currency) { errors.push("missing currency (site setting)"); }
|
||||
if (!siteSettings.discourse_donations_shop_name) { errors.push("missing name (site setting)"); }
|
||||
if (!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 replaceWithStripeOrError(siteSettings) {
|
||||
return function (state, tagInfo, content) {
|
||||
let errors = validationErrors(tagInfo, content, siteSettings);
|
||||
if (errors.length) {
|
||||
displayErrors(state, errors);
|
||||
} else {
|
||||
insertCheckout(state, tagInfo, content, siteSettings);
|
||||
}
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
function displayErrors(state, errors) {
|
||||
@ -33,7 +29,7 @@ function displayErrors(state, errors) {
|
||||
state.push('div-close', 'div', -1);
|
||||
}
|
||||
|
||||
function insertCheckout(state, tagInfo, content) {
|
||||
function insertCheckout(state, tagInfo, content, siteSettings) {
|
||||
let token = state.push('stripe-checkout-form-open', 'form', 1);
|
||||
token.attrs = [['method', 'POST'], ['action', '/charges']];
|
||||
|
||||
@ -41,14 +37,14 @@ function insertCheckout(state, tagInfo, content) {
|
||||
token.attrs = [
|
||||
['src', 'https://checkout.stripe.com/checkout.js'],
|
||||
['class', 'stripe-button'],
|
||||
['data-key', Discourse.SiteSettings['discourse_donations_public_key']],
|
||||
['data-key', siteSettings.discourse_donations_public_key],
|
||||
['data-amount', tagInfo.attrs['amount']],
|
||||
['data-name', Discourse.SiteSettings['discourse_donations_shop_name']],
|
||||
['data-name', 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']]
|
||||
['data-zip-code', !siteSettings.discourse_donations_hide_zip_code],
|
||||
['data-currency', siteSettings.discourse_donations_currency]
|
||||
];
|
||||
|
||||
state.push('stripe-checkout-script-close', 'script', -1);
|
||||
@ -56,32 +52,36 @@ function insertCheckout(state, tagInfo, content) {
|
||||
state.push('stripe-checkout-form-close', 'form', -1);
|
||||
}
|
||||
|
||||
function setupMarkdownIt(helper) {
|
||||
function setupMarkdownIt(helper, siteSettings) {
|
||||
helper.registerPlugin(md => {
|
||||
md.inline.bbcode.ruler.push('stripe-checkout', {
|
||||
tag: 'stripe',
|
||||
replace: replaceWithStripeOrError
|
||||
replace: replaceWithStripeOrError(siteSettings)
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
helper.registerOptions((opts,siteSettings)=>{
|
||||
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, siteSettings);
|
||||
} else {
|
||||
console.log("Please upgrade Discourse to a later version in order to use this plugin");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user