forked from pay-rails/pay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpay.rb
112 lines (91 loc) · 2.62 KB
/
pay.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
require "pay/engine"
require "pay/billable"
require "pay/receipts"
require "pay/payment"
module Pay
# Define who owns the subscription
mattr_accessor :billable_class
mattr_accessor :billable_table
mattr_accessor :braintree_gateway
@@billable_class = "User"
@@billable_table = @@billable_class.tableize
mattr_accessor :chargeable_class
mattr_accessor :chargeable_table
@@chargeable_class = "Pay::Charge"
@@chargeable_table = "pay_charges"
mattr_accessor :subscription_class
mattr_accessor :subscription_table
@@subscription_class = "Pay::Subscription"
@@subscription_table = "pay_subscriptions"
# Business details for receipts
mattr_accessor :application_name
mattr_accessor :business_address
mattr_accessor :business_name
mattr_accessor :support_email
# Email configuration
mattr_accessor :send_emails
@@send_emails = true
mattr_accessor :email_receipt_subject
@@email_receipt_subject = "Payment receipt"
mattr_accessor :email_refund_subject
@@email_refund_subject = "Payment refunded"
mattr_accessor :email_renewing_subject
@@email_renewing_subject = "Your upcoming subscription renewal"
mattr_accessor :email_action_required_subject
@@email_action_required_subject = "Confirm your payment"
mattr_accessor :automount_routes
@@automount_routes = true
mattr_accessor :routes_path
@@routes_path = "/pay"
def self.setup
yield self
end
def self.user_model
if Rails.application.config.cache_classes
@@user_model ||= billable_class.constantize
else
billable_class.constantize
end
end
def self.charge_model
if Rails.application.config.cache_classes
@@charge_model ||= chargeable_class.constantize
else
chargeable_class.constantize
end
end
def self.subscription_model
if Rails.application.config.cache_classes
@@subscription_model ||= subscription_class.constantize
else
subscription_class.constantize
end
end
def self.receipts_supported?
charge_model.respond_to?(:receipt) &&
application_name.present? &&
business_name &&
business_address &&
support_email
end
class Error < StandardError
end
class InvalidPaymentMethod < Error
attr_reader :payment
def initialize(payment)
@payment = payment
end
def message
"This payment attempt failed beacuse of an invalid payment method."
end
end
class ActionRequired < Error
attr_reader :payment
def initialize(payment)
@payment = payment
end
def message
"This payment attempt failed because additional action is required before it can be completed."
end
end
end