This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
populator.rb
142 lines (121 loc) · 3.77 KB
/
populator.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
# frozen_string_literal: true
module PaymentProcessor
module GoCardless
class Populator
def self.client
GoCardlessPro::Client.new(
access_token: Settings.gocardless.token,
environment: Settings.gocardless.environment.to_sym
)
end
def request_params
{
amount: amount_in_cents,
currency: currency,
links: {
mandate: mandate.id
},
metadata: {
customer_id: customer_id
}
}
end
def transaction_params
request_params.merge(charge_date: charge_date)
end
def subscription_params
request_params.merge(
name: 'donation',
interval_unit: 'monthly',
start_date: charge_date
)
end
def mandate
@mandate ||= client.mandates.get(complete_redirect_flow.links.mandate)
end
def bank_account
@bank_account ||= client.customer_bank_accounts.get(complete_redirect_flow.links.customer_bank_account)
end
def customer_id
complete_redirect_flow.links.customer
end
def amount_in_cents
# we let the donor pick any amount and currency, then convert it to the right currency
# for their bank according to the current exchange rate
@amount ||= PaymentProcessor::Currency.convert(@original_amount_in_cents, currency, @original_currency).cents
end
def amount_in_whole_currency
amount_in_cents.to_f / 100
end
def currency
case mandate.scheme.downcase
when 'becs'
'AUD'
when 'sepa_core'
'EUR'
when 'betalingsservice'
'DKK'
when 'pad'
'CAD'
when 'bacs'
'GBP'
when 'ach'
'USD'
when 'autogiro'
'SEK'
else
'EUR'
end
end
def bacs?
mandate.scheme.downcase.inquiry.bacs?
end
def complete_redirect_flow
@complete_redirect_flow ||= client
.redirect_flows
.complete(@redirect_flow_id, params: { session_token: @session_token })
rescue GoCardlessPro::InvalidStateError => e
raise e unless e.message.match?(/already completed/)
@complete_redirect_flow = client.redirect_flows.get(@redirect_flow_id)
end
def client
self.class.client
end
def charge_date
return mandate.next_possible_charge_date if Settings.gocardless.gbp_charge_day.blank? || !bacs?
mandate_date = Date.parse(mandate.next_possible_charge_date)
gbp_date = create_gbp_date(mandate_date)
if mandate_date <= gbp_date
# if mandate becomes available before the specified date this month, charge the payment on the desired date.
gbp_date.to_s
else
# if the mandate becomes available only after the date this month, charge on the desired date next month.
gbp_date.next_month.to_s
end
end
def settings_charge_day
@settings_charge_day ||= Settings.gocardless.gbp_charge_day.to_i
end
def create_gbp_date(mandate_date)
# GBP needs to be charged on the specified date. Use the next possible time that date is possible.
Date.new(
mandate_date.year,
mandate_date.month,
Settings.gocardless.gbp_charge_day.to_i
)
rescue ArgumentError
Rails.logger.error(
"With #{mandate_date.year}-#{mandate_date.month}-#{Settings.gocardless.gbp_charge_day.to_i}, \
your GBP charge date is invalid! Resorting to the mandate's next possible charge date."
)
mandate_date
end
def error_container
@error
end
def success?
@error.blank?
end
end
end
end