Module: Pay::Stripe
- Extended by:
- Env
- Defined in:
- lib/pay/stripe.rb,
app/models/pay/stripe/charge.rb,
app/models/pay/stripe/customer.rb,
app/models/pay/stripe/merchant.rb,
app/models/pay/stripe/subscription.rb,
app/models/pay/stripe/payment_method.rb,
lib/pay/stripe/webhooks/charge_updated.rb,
lib/pay/stripe/webhooks/payment_failed.rb,
lib/pay/stripe/webhooks/account_updated.rb,
lib/pay/stripe/webhooks/charge_refunded.rb,
lib/pay/stripe/webhooks/charge_succeeded.rb,
lib/pay/stripe/webhooks/customer_deleted.rb,
lib/pay/stripe/webhooks/customer_updated.rb,
lib/pay/stripe/webhooks/subscription_created.rb,
lib/pay/stripe/webhooks/subscription_deleted.rb,
lib/pay/stripe/webhooks/subscription_updated.rb,
lib/pay/stripe/webhooks/subscription_renewing.rb,
lib/pay/stripe/webhooks/payment_method_updated.rb,
lib/pay/stripe/webhooks/payment_action_required.rb,
lib/pay/stripe/webhooks/payment_method_attached.rb,
lib/pay/stripe/webhooks/payment_method_detached.rb,
lib/pay/stripe/webhooks/payment_intent_succeeded.rb,
lib/pay/stripe/webhooks/checkout_session_completed.rb,
lib/pay/stripe/webhooks/subscription_trial_will_end.rb,
lib/pay/stripe/webhooks/checkout_session_async_payment_succeeded.rb
Defined Under Namespace
Modules: Webhooks
Classes: Charge, Customer, Error, Merchant, PaymentMethod, Subscription
Constant Summary
collapse
- REQUIRED_VERSION =
"~> 15"
Class Method Summary
collapse
Class Method Details
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
|
# File 'lib/pay/stripe.rb', line 71
def self.configure_webhooks
Pay::Webhooks.configure do |events|
events.subscribe "stripe.charge.refunded", Pay::Stripe::Webhooks::ChargeRefunded.new
events.subscribe "stripe.charge.succeeded", Pay::Stripe::Webhooks::ChargeSucceeded.new
events.subscribe "stripe.charge.updated", Pay::Stripe::Webhooks::ChargeUpdated.new
events.subscribe "stripe.payment_intent.succeeded", Pay::Stripe::Webhooks::PaymentIntentSucceeded.new
events.subscribe "stripe.invoice.upcoming", Pay::Stripe::Webhooks::SubscriptionRenewing.new
events.subscribe "stripe.invoice.payment_action_required", Pay::Stripe::Webhooks::PaymentActionRequired.new
events.subscribe "stripe.invoice.payment_failed", Pay::Stripe::Webhooks::PaymentFailed.new
events.subscribe "stripe.customer.subscription.created", Pay::Stripe::Webhooks::SubscriptionCreated.new
events.subscribe "stripe.customer.subscription.updated", Pay::Stripe::Webhooks::SubscriptionUpdated.new
events.subscribe "stripe.customer.subscription.deleted", Pay::Stripe::Webhooks::SubscriptionDeleted.new
events.subscribe "stripe.customer.subscription.trial_will_end", Pay::Stripe::Webhooks::SubscriptionTrialWillEnd.new
events.subscribe "stripe.customer.updated", Pay::Stripe::Webhooks::CustomerUpdated.new
events.subscribe "stripe.customer.deleted", Pay::Stripe::Webhooks::CustomerDeleted.new
events.subscribe "stripe.payment_method.attached", Pay::Stripe::Webhooks::PaymentMethodAttached.new
events.subscribe "stripe.payment_method.updated", Pay::Stripe::Webhooks::PaymentMethodUpdated.new
events.subscribe "stripe.payment_method.card_automatically_updated", Pay::Stripe::Webhooks::PaymentMethodUpdated.new
events.subscribe "stripe.payment_method.detached", Pay::Stripe::Webhooks::PaymentMethodDetached.new
events.subscribe "stripe.account.updated", Pay::Stripe::Webhooks::AccountUpdated.new
events.subscribe "stripe.checkout.session.completed", Pay::Stripe::Webhooks::CheckoutSessionCompleted.new
events.subscribe "stripe.checkout.session.async_payment_succeeded", Pay::Stripe::Webhooks::CheckoutSessionAsyncPaymentSucceeded.new
end
end
|
.enabled? ⇒ Boolean
36
37
38
39
40
|
# File 'lib/pay/stripe.rb', line 36
def self.enabled?
return false unless Pay.enabled_processors.include?(:stripe) && defined?(::Stripe)
Pay::Engine.version_matches?(required: REQUIRED_VERSION, current: ::Stripe::VERSION) || (raise "[Pay] stripe gem must be version #{REQUIRED_VERSION}")
end
|
.find_by_client_reference_id(client_reference_id) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/pay/stripe.rb', line 131
def self.find_by_client_reference_id(client_reference_id)
model_name, id = client_reference_id.split("_", 2)
return unless model_names.include?(model_name)
model_name.constantize.find(id)
rescue ActiveRecord::RecordNotFound
Rails.logger.error "[Pay] Unable to locate record with: #{client_reference_id}"
nil
end
|
.private_key ⇒ Object
58
59
60
|
# File 'lib/pay/stripe.rb', line 58
def self.private_key
find_value_by_name(:stripe, :private_key)
end
|
.public_key ⇒ Object
54
55
56
|
# File 'lib/pay/stripe.rb', line 54
def self.public_key
find_value_by_name(:stripe, :public_key)
end
|
.setup ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/pay/stripe.rb', line 42
def self.setup
::Stripe.api_key = private_key
::Stripe.set_app_info("PayRails", partner_id: "pp_partner_IqhY0UExnJYLxg", version: Pay::VERSION, url: "https://github.com/pay-rails/pay")
::Stripe.max_network_retries = 2
end
|
.signing_secret ⇒ Object
62
63
64
|
# File 'lib/pay/stripe.rb', line 62
def self.signing_secret
find_value_by_name(:stripe, :signing_secret)
end
|
.sync_checkout_session(session_id, stripe_account: nil, try: 0, retries: 5) ⇒ Object
Subscriptions aren’t always immediately associated, so we want to retry by default
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/pay/stripe.rb', line 146
def self.sync_checkout_session(session_id, stripe_account: nil, try: 0, retries: 5)
checkout_session = ::Stripe::Checkout::Session.retrieve({id: session_id, expand: ["payment_intent.latest_charge"]}, {stripe_account: stripe_account}.compact)
case checkout_session.mode
when "payment"
if (id = checkout_session.payment_intent.try(:latest_charge)&.id)
Pay::Stripe::Charge.sync(id, stripe_account: stripe_account, retries: 5)
end
when "subscription"
Pay::Stripe::Subscription.sync(checkout_session.subscription, stripe_account: stripe_account)
end
rescue ::Stripe::InvalidRequestError
if try > retries
raise
else
try += 1
sleep 0.15**try
retry
end
end
|
.to_client_reference_id(record) ⇒ Object
126
127
128
129
|
# File 'lib/pay/stripe.rb', line 126
def self.to_client_reference_id(record)
raise ArgumentError, "#{record.class.name} does not include Pay. Allowed models: #{model_names.to_a.join(", ")}" unless model_names.include?(record.class.name)
[record.class.name, record.id].join("_")
end
|
.webhook_receive_test_events ⇒ Object
66
67
68
69
|
# File 'lib/pay/stripe.rb', line 66
def self.webhook_receive_test_events
value = find_value_by_name(:stripe, :webhook_receive_test_events)
value.blank? || ActiveModel::Type::Boolean.new.cast(value)
end
|