2017-02-24 12:42:59 +11:00
|
|
|
# name: discourse-donations
|
2017-11-26 12:24:27 +00:00
|
|
|
# about: Integrates Stripe into Discourse to allow forum visitors to make donations
|
2019-08-26 16:01:57 +10:00
|
|
|
# version: 1.11.2
|
|
|
|
# url: https://github.com/rimian/discourse-donations
|
2018-06-25 18:14:50 +10:00
|
|
|
# authors: Rimian Perkins, Chris Beach, Angus McLeod
|
2017-01-31 13:28:41 +11:00
|
|
|
|
2017-05-01 10:48:40 +10:00
|
|
|
gem 'stripe', '2.8.0'
|
2017-02-01 11:35:21 +11:00
|
|
|
|
2019-04-03 12:40:03 +11:00
|
|
|
register_asset "stylesheets/common/discourse-donations.scss"
|
|
|
|
register_asset "stylesheets/mobile/discourse-donations.scss"
|
2017-10-09 08:39:21 +01:00
|
|
|
|
2017-02-28 13:36:41 +11:00
|
|
|
enabled_site_setting :discourse_donations_enabled
|
|
|
|
|
2017-11-12 11:16:47 +08:00
|
|
|
register_html_builder('server:before-head-close') do
|
|
|
|
"<script src='https://js.stripe.com/v3/'></script>"
|
|
|
|
end
|
|
|
|
|
2019-01-29 15:00:29 +11:00
|
|
|
extend_content_security_policy(
|
|
|
|
script_src: ['https://js.stripe.com/v3/']
|
|
|
|
)
|
|
|
|
|
2017-02-17 13:15:32 +11:00
|
|
|
after_initialize do
|
2018-06-25 18:14:50 +10:00
|
|
|
load File.expand_path('../lib/discourse_donations/engine.rb', __FILE__)
|
|
|
|
load File.expand_path('../config/routes.rb', __FILE__)
|
|
|
|
load File.expand_path('../app/controllers/controllers.rb', __FILE__)
|
2017-05-04 17:23:41 +10:00
|
|
|
load File.expand_path('../app/jobs/jobs.rb', __FILE__)
|
2018-06-25 18:14:50 +10:00
|
|
|
load File.expand_path('../app/services/services.rb', __FILE__)
|
|
|
|
|
|
|
|
Discourse::Application.routes.append do
|
|
|
|
mount ::DiscourseDonations::Engine, at: 'donate'
|
|
|
|
end
|
2018-02-02 16:06:43 +08:00
|
|
|
|
|
|
|
class ::User
|
|
|
|
def stripe_customer_id
|
|
|
|
if custom_fields['stripe_customer_id']
|
2018-06-21 19:00:43 +10:00
|
|
|
custom_fields['stripe_customer_id'].to_s
|
2018-02-02 16:06:43 +08:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-09-22 14:03:30 +10:00
|
|
|
|
2018-09-28 17:33:27 +10:00
|
|
|
Category.register_custom_field_type('donations_show_amounts', :boolean)
|
|
|
|
|
2018-09-22 14:03:30 +10:00
|
|
|
class ::Category
|
2018-09-22 17:09:06 +10:00
|
|
|
def donations_cause
|
|
|
|
SiteSetting.discourse_donations_causes_categories.split('|').include? self.id.to_s
|
|
|
|
end
|
|
|
|
|
2018-09-22 14:03:30 +10:00
|
|
|
def donations_total
|
|
|
|
if custom_fields['donations_total']
|
|
|
|
custom_fields['donations_total']
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-28 17:33:27 +10:00
|
|
|
def donations_show_amounts
|
|
|
|
if custom_fields['donations_show_amounts'] != nil
|
|
|
|
custom_fields['donations_show_amounts']
|
|
|
|
else
|
2018-09-30 11:53:33 +10:00
|
|
|
false
|
2018-09-28 17:33:27 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-22 14:03:30 +10:00
|
|
|
def donations_month
|
|
|
|
if custom_fields['donations_month']
|
|
|
|
custom_fields['donations_month']
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def donations_backers
|
|
|
|
if custom_fields['donations_backers']
|
|
|
|
[*custom_fields['donations_backers']].map do |user_id|
|
|
|
|
User.find_by(id: user_id.to_i)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def donations_maintainers
|
|
|
|
if custom_fields['donations_maintainers']
|
|
|
|
custom_fields['donations_maintainers'].split(',').map do |username|
|
|
|
|
User.find_by(username: username)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-28 17:33:27 +10:00
|
|
|
def donations_maintainers_label
|
|
|
|
if custom_fields['donations_maintainers_label']
|
|
|
|
custom_fields['donations_maintainers_label']
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-22 14:03:30 +10:00
|
|
|
def donations_github
|
|
|
|
if custom_fields['donations_github']
|
|
|
|
custom_fields['donations_github']
|
|
|
|
else
|
2018-09-28 17:33:27 +10:00
|
|
|
nil
|
2018-09-22 14:03:30 +10:00
|
|
|
end
|
|
|
|
end
|
2018-09-22 17:48:38 +10:00
|
|
|
|
|
|
|
def donations_meta
|
|
|
|
if custom_fields['donations_meta']
|
|
|
|
custom_fields['donations_meta']
|
|
|
|
else
|
2018-09-28 17:33:27 +10:00
|
|
|
nil
|
2018-09-22 17:48:38 +10:00
|
|
|
end
|
|
|
|
end
|
2019-04-03 12:32:24 +11:00
|
|
|
|
|
|
|
def donations_release_latest
|
|
|
|
if custom_fields['donations_release_latest']
|
|
|
|
custom_fields['donations_release_latest']
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def donations_release_oldest
|
|
|
|
if custom_fields['donations_release_oldest']
|
|
|
|
custom_fields['donations_release_oldest']
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2018-09-22 14:03:30 +10:00
|
|
|
end
|
|
|
|
|
2019-03-19 12:22:06 +11:00
|
|
|
[
|
|
|
|
'donations_cause',
|
|
|
|
'donations_total',
|
|
|
|
'donations_month',
|
|
|
|
'donations_backers',
|
|
|
|
'donations_show_amounts',
|
|
|
|
'donations_maintainers',
|
|
|
|
'donations_maintainers_label',
|
|
|
|
'donations_github',
|
2019-04-03 12:32:24 +11:00
|
|
|
'donations_meta',
|
|
|
|
'donations_release_latest',
|
|
|
|
'donations_release_oldest'
|
2019-03-19 12:22:06 +11:00
|
|
|
].each do |key|
|
|
|
|
Site.preloaded_category_custom_fields << key if Site.respond_to? :preloaded_category_custom_fields
|
|
|
|
end
|
|
|
|
|
2018-09-28 17:33:27 +10:00
|
|
|
add_to_serializer(:basic_category, :donations_cause) { object.donations_cause }
|
|
|
|
add_to_serializer(:basic_category, :donations_total) { object.donations_total }
|
|
|
|
add_to_serializer(:basic_category, :include_donations_total?) { object.donations_show_amounts }
|
|
|
|
add_to_serializer(:basic_category, :donations_month) { object.donations_month }
|
|
|
|
add_to_serializer(:basic_category, :include_donations_month?) { object.donations_show_amounts && SiteSetting.discourse_donations_cause_month }
|
|
|
|
add_to_serializer(:basic_category, :donations_backers) {
|
|
|
|
ActiveModel::ArraySerializer.new(object.donations_backers, each_serializer: BasicUserSerializer).as_json
|
|
|
|
}
|
|
|
|
add_to_serializer(:basic_category, :donations_maintainers) {
|
|
|
|
ActiveModel::ArraySerializer.new(object.donations_maintainers, each_serializer: BasicUserSerializer).as_json
|
|
|
|
}
|
|
|
|
add_to_serializer(:basic_category, :donations_maintainers_label) { object.donations_maintainers_label }
|
|
|
|
add_to_serializer(:basic_category, :include_donations_maintainers_label?) { object.donations_maintainers_label.present? }
|
|
|
|
add_to_serializer(:basic_category, :donations_github) { object.donations_github }
|
|
|
|
add_to_serializer(:basic_category, :donations_meta) { object.donations_meta }
|
2019-04-03 12:32:24 +11:00
|
|
|
add_to_serializer(:basic_category, :donations_release_latest) { object.donations_release_latest }
|
|
|
|
add_to_serializer(:basic_category, :donations_release_oldest) { object.donations_release_oldest }
|
2018-09-22 14:03:30 +10:00
|
|
|
|
|
|
|
DiscourseEvent.trigger(:donations_ready)
|
2017-02-17 13:15:32 +11:00
|
|
|
end
|