forked from chrisbeach/discourse-donations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.rb
318 lines (274 loc) · 9.17 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# name: discourse-donations
# about: Integrates Stripe into Discourse to allow forum visitors to make donations
# version: 1.12.0
# url: https://github.com/haroldus-/discourse-donations
# authors: Rimian Perkins, Chris Beach, Angus McLeod, haroldus-
gem 'net-http-persistent', '3.0.1', {require: false}
gem 'stripe', '5.18.0'
register_asset "stylesheets/common/discourse-donations.scss"
register_asset "stylesheets/desktop/discourse-donations.scss", :desktop
register_asset "stylesheets/mobile/discourse-donations.scss", :mobile
enabled_site_setting :discourse_donations_enabled
register_html_builder('server:before-head-close') do
"<script src='https://js.stripe.com/v3/'></script>"
end
extend_content_security_policy(
script_src: ['https://js.stripe.com/v3/']
)
module SessionControllerPrepend
def create
if SiteSetting.discourse_donations_subscription_required?
params.require(:login)
login = params[:login].strip
login = login[1..-1] if login[0] == "@"
if user = User.find_by_username_or_email(login)
# If they need an active subscription
if login_has_no_active_subscription_for?(user)
cookies[:email] = { value: user.email, expires: 1.day.from_now }
no_active_subscription
else
super
end
else
super
end
else
super
end
end
def email_login
if SiteSetting.discourse_donations_subscription_required?
token = params[:token]
# Only check if it is confirmable so as not to 'expire' the token
if user = EmailToken.confirmable(token).user
# If they need an active subscription
if login_has_no_active_subscription_for?(user)
# Confirm the token if login is not allowed per email login pattern
EmailToken.confirm(token)
cookies[:email] = { value: user.email, expires: 1.day.from_now }
no_active_subscription
else
super
end
else
super
end
else
super
end
end
private
def login_has_no_active_subscription_for?(user)
SiteSetting.discourse_donations_subscription_required? && !user.subscription_active? && !user.admin? && !user.staff?
end
def no_active_subscription
render json: {
error: I18n.t("discourse_donations.login.has_no_active_subscription"),
reason: 'no_active_subscription',
redirect_to: '/donate'
}
end
end
module UsersControllerPrepend
def logon_after_password_reset
if SiteSetting.discourse_donations_subscription_required?
@success = I18n.t("discourse_donations.login.has_no_active_subscription")
else
super
end
else
super
end
end
end
module InvitesControllerPrepend
def perform_accept_invitation
if SiteSetting.discourse_donations_subscription_required?
params.require(:id)
params.permit(:username, :name, :password, user_custom_fields: {})
invite = Invite.find_by(invite_key: params[:id])
if invite.present?
begin
user = invite.redeem(username: params[:username], name: params[:name], password: params[:password], user_custom_fields: params[:user_custom_fields], ip_address: request.remote_ip)
if user.present? && (user.subscription_active? || user.admin? || user.staff?)
super
else
if user.present?
post_process_invite(user)
end
response = { success: true }
if user.present? && user.active?
response[:redirect_to] = '/donate'
response[:message] = I18n.t("discourse_donations.login.has_no_active_subscription")
else
response[:message] = I18n.t('invite.confirm_email')
end
render json: response
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
render json: {
success: false,
errors: e.record&.errors&.to_hash || {},
message: I18n.t('invite.error_message')
}
end
else
super
end
else
super
end
end
end
load File.expand_path('../lib/validators/subscription_required_validator.rb', __FILE__)
after_initialize do
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__)
load File.expand_path('../app/jobs/jobs.rb', __FILE__)
load File.expand_path('../app/services/services.rb', __FILE__)
Discourse::Application.routes.append do
mount ::DiscourseDonations::Engine, at: 'donate'
end
::SessionController.class_eval do
prepend SessionControllerPrepend
end
::UsersController.class_eval do
prepend UsersControllerPrepend
end
::InvitesController.class_eval do
prepend InvitesControllerPrepend
end
class ::User
def stripe_customer_id
if custom_fields['stripe_customer_id']
custom_fields['stripe_customer_id'].to_s
else
nil
end
end
def subscription_active?
stripe = DiscourseDonations::Stripe.new(SiteSetting.discourse_donations_secret_key, {})
customer = stripe.customer(self, {})
if customer&.subscriptions&.any? { |s| s.status == "active" }
true
else
false
end
end
end
Category.register_custom_field_type('donations_show_amounts', :boolean)
class ::Category
def donations_cause
SiteSetting.discourse_donations_causes_categories.split('|').include? self.id.to_s
end
def donations_total
if custom_fields['donations_total']
custom_fields['donations_total']
else
0
end
end
def donations_show_amounts
if custom_fields['donations_show_amounts'] != nil
custom_fields['donations_show_amounts']
else
false
end
end
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
def donations_maintainers_label
if custom_fields['donations_maintainers_label']
custom_fields['donations_maintainers_label']
else
nil
end
end
def donations_github
if custom_fields['donations_github']
custom_fields['donations_github']
else
nil
end
end
def donations_meta
if custom_fields['donations_meta']
custom_fields['donations_meta']
else
nil
end
end
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
end
[
'donations_cause',
'donations_total',
'donations_month',
'donations_backers',
'donations_show_amounts',
'donations_maintainers',
'donations_maintainers_label',
'donations_github',
'donations_meta',
'donations_release_latest',
'donations_release_oldest'
].each do |key|
Site.preloaded_category_custom_fields << key if Site.respond_to? :preloaded_category_custom_fields
end
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 }
add_to_serializer(:basic_category, :donations_release_latest) { object.donations_release_latest }
add_to_serializer(:basic_category, :donations_release_oldest) { object.donations_release_oldest }
DiscourseEvent.trigger(:donations_ready)
end