-
-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathpay.rb
More file actions
144 lines (118 loc) · 3.99 KB
/
pay.rb
File metadata and controls
144 lines (118 loc) · 3.99 KB
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
require "pay/version"
require "pay/engine"
require "pay/errors"
require "pay/adapter"
require "action_mailer"
require "active_support"
module Pay
autoload :Attributes, "pay/attributes"
autoload :Env, "pay/env"
autoload :NanoId, "pay/nano_id"
autoload :Payment, "pay/payment"
autoload :Receipts, "pay/receipts"
autoload :Currency, "pay/currency"
# Payment processors
autoload :Braintree, "pay/braintree"
autoload :FakeProcessor, "pay/fake_processor"
autoload :PaddleBilling, "pay/paddle_billing"
autoload :PaddleClassic, "pay/paddle_classic"
autoload :LemonSqueezy, "pay/lemon_squeezy"
autoload :Stripe, "pay/stripe"
autoload :Webhooks, "pay/webhooks"
module Billable
autoload :SyncCustomer, "pay/billable/sync_customer"
end
mattr_accessor :braintree_gateway
mattr_accessor :model_parent_class
@@model_parent_class = "ApplicationRecord"
# Business details for receipts
mattr_accessor :application_name
mattr_accessor :business_address
mattr_accessor :business_name
mattr_accessor :business_logo
mattr_accessor :support_email
def self.support_email=(value)
@@support_email = value.is_a?(::Mail::Address) ? value : ::Mail::Address.new(value)
end
mattr_accessor :automount_routes
@@automount_routes = true
mattr_accessor :default_product_name
@@default_product_name = "default"
mattr_accessor :default_plan_name
@@default_plan_name = "default"
mattr_accessor :routes_path
@@routes_path = "/pay"
mattr_accessor :enabled_processors
@@enabled_processors = [:stripe, :braintree, :paddle_billing, :paddle_classic, :lemon_squeezy]
mattr_accessor :send_emails
@@send_emails = true
mattr_accessor :emails
@@emails = ActiveSupport::OrderedOptions.new
@@emails.payment_action_required = true
@@emails.payment_failed = true
@@emails.receipt = true
@@emails.refund = true
# This only applies to Stripe, therefor we supply the second argument of price
@@emails.subscription_renewing = ->(pay_subscription, price) {
(price&.type == "recurring") && (price.recurring&.interval == "year")
}
@@emails.subscription_trial_will_end = true
@@emails.subscription_trial_ended = true
@@mailer = "Pay::UserMailer"
def self.mailer=(value)
@@mailer = value
@@mailer_ref = nil
end
def self.mailer
@@mailer_ref ||= @@mailer&.constantize
end
mattr_accessor :parent_mailer
@@parent_mailer = "Pay::ApplicationMailer"
# Should return a hash of arguments for the `mail` call in UserMailer
mattr_accessor :mail_arguments
@@mail_arguments = -> {
{
to: instance_exec(&Pay.mail_to),
subject: default_i18n_subject(application: Pay.application_name)
}
}
# Should return String or Array of email recipients
mattr_accessor :mail_to
@@mail_to = -> {
if ::ActionMailer::Base.respond_to?(:email_address_with_name)
::ActionMailer::Base.email_address_with_name(params[:pay_customer].email, params[:pay_customer].customer_name)
else
::Mail::Address.new.tap do |builder|
builder.address = params[:pay_customer].email
builder.display_name = params[:pay_customer].customer_name.presence
end.to_s
end
}
def self.setup
yield self
end
def self.send_email?(email_option, *remaining_args)
if resolve_option(send_emails, *remaining_args)
email_config_option_enabled = emails.send(email_option)
resolve_option(email_config_option_enabled, *remaining_args)
else
false
end
end
def self.resolve_option(option, *remaining_args)
if option.respond_to?(:call)
option.call(*remaining_args)
else
option
end
end
def self.sync(params)
if (session_id = params[:stripe_checkout_session_id] || params[:session_id])
Pay::Stripe.sync_checkout_session(session_id)
elsif (transaction_id = params[:paddle_billing_transaction_id] || params[:transaction_id])
Pay::PaddleBilling.sync_transaction(transaction_id)
elsif (order_id = params[:lemon_squeezy_order_id])
Pay::LemonSqueezy.sync_order(order_id)
end
end
end