This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
/
action_queue.rb
328 lines (293 loc) · 7.82 KB
/
action_queue.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
319
320
321
322
323
324
325
326
327
328
# frozen_string_literal: true
require 'active_support/concern'
module ActionQueue
module Enqueable
extend ActiveSupport::Concern
def initialize(action)
@action = action
end
def push
ChampaignQueue.push(payload.merge(meta), group_id: "action:#{@action.id}")
end
def page
@page ||= @action.page
end
def data
@data ||= @action.form_data.symbolize_keys
end
def country(iso_code)
(ISO3166::Country.search(iso_code).try(:translations) || {})['en']
end
def member
@member ||= @action.member
end
def meta
{
meta: {
title: page.title,
uri: "/a/#{page.slug}",
slug: page.slug,
first_name: member.first_name,
last_name: member.last_name,
created_at: @action.created_at,
country: country(member.country),
action_id: @action.id,
subscribed_member: @action.subscribed_member
}
}
end
def currency
data[:currency].try(:upcase)
end
class_methods do
def push(action)
new(action).push
end
end
end
module Donatable
def user_data
data.select { |k, v| v.present? && ActionKitFields.is_predefined_by_ak(k) }.merge(
first_name: member.first_name,
last_name: member.last_name,
email: member.email,
country: country(member.country),
akid: data[:akid],
source: data[:source],
user_express_cookie: data[:store_in_vault] ? 1 : 0,
user_express_account: data[:express_account] ? 1 : 0
).merge(UserLanguageISO.for(page.language))
end
def action_fields
@action_fields ||= {}.tap do |fields|
data.keys.select { |k| k =~ /^action_/ }.each do |key|
fields[key] = data[key]
end
fields[:action_bucket] = data[:bucket] if data.key? :bucket
end
end
end
class Pusher
def self.push(event, action)
case event
when :new_action
if action.donation
if action.form_data.fetch('payment_provider', '').inquiry.go_cardless?
NewDirectDebitAction.push(action)
else
NewDonationAction.push(action)
end
else
NewPetitionAction.push(action)
end
when :new_survey_response
NewSurveyResponse.push(action)
end
end
end
class NewPetitionAction
include Donatable
include Enqueable
def get_page_name
if page.status.inquiry.imported?
page.slug
else
"#{page.slug}-petition"
end
end
def payload
{
type: 'action',
params: {
page: get_page_name,
email: @action.member.email
}.merge(@action.form_data)
.merge(UserLanguageISO.for(page.language))
.tap do |params|
params[:country] = country(member.country) if member.country.present?
params[:action_bucket] = data[:bucket] if data.key? :bucket
end
}.deep_symbolize_keys
end
end
class NewSurveyResponse < NewPetitionAction
def payload
super.tap do |p|
p[:type] = 'new_survey_response'
end
end
end
class NewDirectDebitAction
include Enqueable
include Donatable
def payload
# TODO: This really needs to be handled in a less hackish solution once we refactor.
@action.form_data[:action_express_donation] = 0
if data[:is_subscription]
subscription_payload
else
transaction_payload
end
end
def subscription_payload
{
type: 'donation',
payment_provider: 'go_cardless',
params: {
donationpage: {
name: "#{@action.page.slug}-donation",
payment_account: get_payment_account
},
order: {
amount: data[:amount],
currency: currency,
recurring_id: data[:subscription_id]
}.merge(fake_card_info),
action: action_data,
user: user_data
}
}
end
def transaction_payload
{
type: 'donation',
payment_provider: 'go_cardless',
params: {
donationpage: {
name: "#{@action.page.slug}-donation",
payment_account: get_payment_account
},
order: {
amount: data[:amount],
currency: currency
}.merge(fake_card_info),
action: action_data,
user: user_data
}
}
end
def action_data
{
fields: action_fields.merge(
action_account_number_ending: data[:account_number_ending],
action_mandate_reference: data[:mandate_reference],
action_bank_name: data[:bank_name],
action_express_donation: data[:express_donation] ? 1 : 0
),
source: data[:source]
}
end
def fake_card_info
{
card_num: 'DDEB',
card_code: '007',
exp_date_month: '01',
exp_date_year: '99'
}
end
def get_payment_account
"GoCardless #{currency}"
end
def user_data
super.except(:user_express_cookie, :user_express_account)
end
end
class NewDonationAction
include Enqueable
include Donatable
def payload
if data[:is_subscription]
subscription_payload
else
transaction_payload
end
end
def subscription_payload
{
type: 'donation',
payment_provider: 'braintree',
params: {
donationpage: {
name: "#{@action.page.slug}-donation",
payment_account: get_payment_account
},
order: {
amount: data[:amount],
card_num: data[:card_num],
card_code: '007',
exp_date_month: expire_month,
exp_date_year: expire_year,
currency: currency,
recurring_id: data[:subscription_id]
},
action: {
source: data[:source],
fields: action_fields
},
user: user_data
}
}
end
def transaction_payload
{
type: 'donation',
payment_provider: 'braintree',
params: {
donationpage: {
name: "#{@action.page.slug}-donation",
payment_account: get_payment_account
},
order: {
amount: data[:amount],
card_num: data[:card_num],
card_code: '007',
exp_date_month: expire_month,
exp_date_year: expire_year,
currency: currency
},
action: {
source: data[:source],
fields: action_fields
},
user: user_data
}
}
end
# ActionKit can accept one of the following:
#
# PayPal USD
# PayPal GBP
# PayPal CAD
# PayPal EUR
# PayPal AUD
#
# Braintree USD
# Braintree CAD
# Braintree AUD
# Braintree GBP
# Braintree EUR
#
def get_payment_account
provider = is_paypal? ? 'PayPal' : 'Braintree'
"#{provider} #{currency}"
end
def is_paypal?
data[:card_num] == 'PYPL'
end
def expire_month
split_expire_date[0]
end
def expire_year
split_expire_date[1]
end
def split_expire_date
if data[:card_expiration_date] == '/' || data[:card_expiration_date].blank?
# We weren't given an expiration, probably because it's a PayPal transaction, so set a fake expiration five years
# in the future.
[Time.now.month.to_s, (Time.now.year + 5).to_s]
else
@split_date ||= data[:card_expiration_date].split('/')
end
end
end
end