pay 2.6.2 → 2.6.7
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of pay might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +22 -4
- data/app/models/pay/charge.rb +1 -13
- data/app/models/pay/subscription.rb +1 -1
- data/config/locales/en.yml +1 -113
- data/lib/pay.rb +1 -0
- data/lib/pay/billable.rb +13 -1
- data/lib/pay/braintree/billable.rb +10 -8
- data/lib/pay/braintree/charge.rb +0 -1
- data/lib/pay/braintree/error.rb +16 -2
- data/lib/pay/braintree/subscription.rb +3 -3
- data/lib/pay/errors.rb +1 -5
- data/lib/pay/fake_processor.rb +8 -0
- data/lib/pay/fake_processor/billable.rb +60 -0
- data/lib/pay/fake_processor/charge.rb +21 -0
- data/lib/pay/fake_processor/error.rb +6 -0
- data/lib/pay/fake_processor/subscription.rb +55 -0
- data/lib/pay/paddle.rb +5 -0
- data/lib/pay/paddle/error.rb +1 -3
- data/lib/pay/paddle/subscription.rb +5 -1
- data/lib/pay/stripe.rb +3 -0
- data/lib/pay/stripe/billable.rb +1 -1
- data/lib/pay/stripe/error.rb +1 -3
- data/lib/pay/stripe/subscription.rb +8 -4
- data/lib/pay/stripe/webhooks/payment_intent_succeeded.rb +27 -0
- data/lib/pay/stripe_marketplace/billable.rb +246 -0
- data/lib/pay/stripe_marketplace/charge.rb +26 -0
- data/lib/pay/stripe_marketplace/error.rb +9 -0
- data/lib/pay/stripe_marketplace/subscription.rb +83 -0
- data/lib/pay/stripe_marketplace/webhooks/charge_refunded.rb +23 -0
- data/lib/pay/stripe_marketplace/webhooks/charge_succeeded.rb +24 -0
- data/lib/pay/stripe_marketplace/webhooks/customer_deleted.rb +29 -0
- data/lib/pay/stripe_marketplace/webhooks/customer_updated.rb +17 -0
- data/lib/pay/stripe_marketplace/webhooks/payment_action_required.rb +26 -0
- data/lib/pay/stripe_marketplace/webhooks/payment_method_updated.rb +17 -0
- data/lib/pay/stripe_marketplace/webhooks/subscription_created.rb +45 -0
- data/lib/pay/stripe_marketplace/webhooks/subscription_deleted.rb +19 -0
- data/lib/pay/stripe_marketplace/webhooks/subscription_renewing.rb +24 -0
- data/lib/pay/stripe_marketplace/webhooks/subscription_updated.rb +38 -0
- data/lib/pay/version.rb +1 -1
- metadata +22 -2
@@ -0,0 +1,26 @@
|
|
1
|
+
module Pay
|
2
|
+
module StripeMarketplace
|
3
|
+
class Charge
|
4
|
+
attr_reader :pay_charge
|
5
|
+
|
6
|
+
delegate :processor_id, :owner, to: :pay_charge
|
7
|
+
|
8
|
+
def initialize(pay_charge)
|
9
|
+
@pay_charge = pay_charge
|
10
|
+
end
|
11
|
+
|
12
|
+
def charge
|
13
|
+
::Stripe::Charge.retrieve(processor_id)
|
14
|
+
rescue ::Stripe::StripeError => e
|
15
|
+
raise Pay::Stripe::Error, e
|
16
|
+
end
|
17
|
+
|
18
|
+
def refund!(amount_to_refund)
|
19
|
+
::Stripe::Refund.create(charge: processor_id, amount: amount_to_refund)
|
20
|
+
pay_charge.update(amount_refunded: amount_to_refund)
|
21
|
+
rescue ::Stripe::StripeError => e
|
22
|
+
raise Pay::Stripe::Error, e
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module Pay
|
2
|
+
module StripeMarketplace
|
3
|
+
class Subscription
|
4
|
+
attr_reader :pay_subscription
|
5
|
+
|
6
|
+
delegate :active?,
|
7
|
+
:canceled?,
|
8
|
+
:ends_at,
|
9
|
+
:name,
|
10
|
+
:on_trial?,
|
11
|
+
:owner,
|
12
|
+
:processor_id,
|
13
|
+
:processor_plan,
|
14
|
+
:processor_subscription,
|
15
|
+
:prorate,
|
16
|
+
:prorate?,
|
17
|
+
:quantity,
|
18
|
+
:quantity?,
|
19
|
+
:trial_ends_at,
|
20
|
+
to: :pay_subscription
|
21
|
+
|
22
|
+
def initialize(pay_subscription)
|
23
|
+
@pay_subscription = pay_subscription
|
24
|
+
end
|
25
|
+
|
26
|
+
def cancel
|
27
|
+
subscription = processor_subscription
|
28
|
+
subscription.cancel_at_period_end = true
|
29
|
+
subscription.save
|
30
|
+
|
31
|
+
new_ends_at = on_trial? ? trial_ends_at : Time.at(subscription.current_period_end)
|
32
|
+
pay_subscription.update(ends_at: new_ends_at)
|
33
|
+
rescue ::Stripe::StripeError => e
|
34
|
+
raise Pay::Stripe::Error, e
|
35
|
+
end
|
36
|
+
|
37
|
+
def cancel_now!
|
38
|
+
processor_subscription.delete
|
39
|
+
pay_subscription.update(ends_at: Time.zone.now, status: :canceled)
|
40
|
+
rescue ::Stripe::StripeError => e
|
41
|
+
raise Pay::Stripe::Error, e
|
42
|
+
end
|
43
|
+
|
44
|
+
def on_grace_period?
|
45
|
+
canceled? && Time.zone.now < ends_at
|
46
|
+
end
|
47
|
+
|
48
|
+
def paused?
|
49
|
+
false
|
50
|
+
end
|
51
|
+
|
52
|
+
def pause
|
53
|
+
raise NotImplementedError, "Stripe does not support pausing subscriptions"
|
54
|
+
end
|
55
|
+
|
56
|
+
def resume
|
57
|
+
unless on_grace_period?
|
58
|
+
raise StandardError, "You can only resume subscriptions within their grace period."
|
59
|
+
end
|
60
|
+
|
61
|
+
subscription = processor_subscription
|
62
|
+
subscription.plan = processor_plan
|
63
|
+
subscription.trial_end = on_trial? ? trial_ends_at.to_i : "now"
|
64
|
+
subscription.cancel_at_period_end = false
|
65
|
+
subscription.save
|
66
|
+
rescue ::Stripe::StripeError => e
|
67
|
+
raise Pay::Stripe::Error, e
|
68
|
+
end
|
69
|
+
|
70
|
+
def swap(plan)
|
71
|
+
subscription = processor_subscription
|
72
|
+
subscription.cancel_at_period_end = false
|
73
|
+
subscription.plan = plan
|
74
|
+
subscription.proration_behavior = (prorate ? "create_prorations" : "none")
|
75
|
+
subscription.trial_end = on_trial? ? trial_ends_at.to_i : "now"
|
76
|
+
subscription.quantity = quantity if quantity?
|
77
|
+
subscription.save
|
78
|
+
rescue ::Stripe::StripeError => e
|
79
|
+
raise Pay::Stripe::Error, e
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Pay
|
2
|
+
module Stripe
|
3
|
+
module Webhooks
|
4
|
+
class ChargeRefunded
|
5
|
+
def call(event)
|
6
|
+
object = event.data.object
|
7
|
+
charge = Pay.charge_model.find_by(processor: :stripe, processor_id: object.id)
|
8
|
+
|
9
|
+
return unless charge.present?
|
10
|
+
|
11
|
+
charge.update(amount_refunded: object.amount_refunded)
|
12
|
+
notify_user(charge.owner, charge)
|
13
|
+
end
|
14
|
+
|
15
|
+
def notify_user(billable, charge)
|
16
|
+
if Pay.send_emails
|
17
|
+
Pay::UserMailer.with(billable: billable, charge: charge).refund.deliver_later
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Pay
|
2
|
+
module Stripe
|
3
|
+
module Webhooks
|
4
|
+
class ChargeSucceeded
|
5
|
+
def call(event)
|
6
|
+
object = event.data.object
|
7
|
+
billable = Pay.find_billable(processor: :stripe, processor_id: object.customer)
|
8
|
+
|
9
|
+
return unless billable.present?
|
10
|
+
return if billable.charges.where(processor_id: object.id).any?
|
11
|
+
|
12
|
+
charge = Pay::Stripe::Billable.new(billable).save_pay_charge(object)
|
13
|
+
notify_user(billable, charge)
|
14
|
+
end
|
15
|
+
|
16
|
+
def notify_user(billable, charge)
|
17
|
+
if Pay.send_emails && charge.respond_to?(:receipt)
|
18
|
+
Pay::UserMailer.with(billable: billable, charge: charge).receipt.deliver_later
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Pay
|
2
|
+
module Stripe
|
3
|
+
module Webhooks
|
4
|
+
class CustomerDeleted
|
5
|
+
def call(event)
|
6
|
+
object = event.data.object
|
7
|
+
billable = Pay.find_billable(processor: :stripe, processor_id: object.id)
|
8
|
+
|
9
|
+
# Couldn't find user, we can skip
|
10
|
+
return unless billable.present?
|
11
|
+
|
12
|
+
billable.update(
|
13
|
+
processor_id: nil,
|
14
|
+
trial_ends_at: nil,
|
15
|
+
card_type: nil,
|
16
|
+
card_last4: nil,
|
17
|
+
card_exp_month: nil,
|
18
|
+
card_exp_year: nil
|
19
|
+
)
|
20
|
+
|
21
|
+
billable.subscriptions.update_all(
|
22
|
+
trial_ends_at: nil,
|
23
|
+
ends_at: Time.zone.now
|
24
|
+
)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Pay
|
2
|
+
module Stripe
|
3
|
+
module Webhooks
|
4
|
+
class CustomerUpdated
|
5
|
+
def call(event)
|
6
|
+
object = event.data.object
|
7
|
+
billable = Pay.find_billable(processor: :stripe, processor_id: object.id)
|
8
|
+
|
9
|
+
# Couldn't find user, we can skip
|
10
|
+
return unless billable.present?
|
11
|
+
|
12
|
+
Pay::Stripe::Billable.new(billable).sync_card_from_stripe
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Pay
|
2
|
+
module Stripe
|
3
|
+
module Webhooks
|
4
|
+
class PaymentActionRequired
|
5
|
+
def call(event)
|
6
|
+
# Event is of type "invoice" see:
|
7
|
+
# https://stripe.com/docs/api/invoices/object
|
8
|
+
|
9
|
+
object = event.data.object
|
10
|
+
|
11
|
+
subscription = Pay.subscription_model.find_by(processor: :stripe, processor_id: object.subscription)
|
12
|
+
return if subscription.nil?
|
13
|
+
billable = subscription.owner
|
14
|
+
|
15
|
+
notify_user(billable, event.data.object.payment_intent, subscription)
|
16
|
+
end
|
17
|
+
|
18
|
+
def notify_user(billable, payment_intent_id, subscription)
|
19
|
+
if Pay.send_emails
|
20
|
+
Pay::UserMailer.with(billable: billable, payment_intent_id: payment_intent_id, subscription: subscription).payment_action_required.deliver_later
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Pay
|
2
|
+
module Stripe
|
3
|
+
module Webhooks
|
4
|
+
class PaymentMethodUpdated
|
5
|
+
def call(event)
|
6
|
+
object = event.data.object
|
7
|
+
billable = Pay.find_billable(processor: :stripe, processor_id: object.customer)
|
8
|
+
|
9
|
+
# Couldn't find user, we can skip
|
10
|
+
return unless billable.present?
|
11
|
+
|
12
|
+
Pay::Stripe::Billable.new(billable).sync_card_from_stripe
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Pay
|
2
|
+
module Stripe
|
3
|
+
module Webhooks
|
4
|
+
class SubscriptionCreated
|
5
|
+
def call(event)
|
6
|
+
object = event.data.object
|
7
|
+
|
8
|
+
# We may already have the subscription in the database, so we can update that record
|
9
|
+
subscription = Pay.subscription_model.find_by(processor: :stripe, processor_id: object.id)
|
10
|
+
|
11
|
+
# Create the subscription in the database if we don't have it already
|
12
|
+
if subscription.nil?
|
13
|
+
# The customer should already be in the database
|
14
|
+
owner = Pay.find_billable(processor: :stripe, processor_id: object.customer)
|
15
|
+
|
16
|
+
if owner.nil?
|
17
|
+
Rails.logger.error("[Pay] Unable to find Pay::Billable with processor: :stripe and processor_id: '#{object.customer}'. Searched these models: #{Pay.billable_models.join(", ")}")
|
18
|
+
return
|
19
|
+
end
|
20
|
+
|
21
|
+
subscription = Pay.subscription_model.new(name: Pay.default_product_name, owner: owner, processor: :stripe, processor_id: object.id)
|
22
|
+
end
|
23
|
+
|
24
|
+
subscription.quantity = object.quantity
|
25
|
+
subscription.status = object.status
|
26
|
+
subscription.processor_plan = object.plan.id
|
27
|
+
subscription.trial_ends_at = Time.at(object.trial_end) if object.trial_end.present?
|
28
|
+
|
29
|
+
# If user was on trial, their subscription ends at the end of the trial
|
30
|
+
subscription.ends_at = if object.cancel_at_period_end && subscription.on_trial?
|
31
|
+
subscription.trial_ends_at
|
32
|
+
|
33
|
+
# User wasn't on trial, so subscription ends at period end
|
34
|
+
elsif object.cancel_at_period_end
|
35
|
+
Time.at(object.current_period_end)
|
36
|
+
|
37
|
+
# Subscription isn't marked to cancel at period end
|
38
|
+
end
|
39
|
+
|
40
|
+
subscription.save!
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Pay
|
2
|
+
module Stripe
|
3
|
+
module Webhooks
|
4
|
+
class SubscriptionDeleted
|
5
|
+
def call(event)
|
6
|
+
object = event.data.object
|
7
|
+
subscription = Pay.subscription_model.find_by(processor: :stripe, processor_id: object.id)
|
8
|
+
|
9
|
+
# We couldn't find the subscription for some reason, maybe it's from another service
|
10
|
+
return if subscription.nil?
|
11
|
+
|
12
|
+
# User canceled subscriptions have an ends_at
|
13
|
+
# Automatically canceled subscriptions need this value set
|
14
|
+
subscription.update!(ends_at: Time.at(object.ended_at)) if subscription.ends_at.blank? && object.ended_at.present?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Pay
|
2
|
+
module Stripe
|
3
|
+
module Webhooks
|
4
|
+
class SubscriptionRenewing
|
5
|
+
def call(event)
|
6
|
+
# Event is of type "invoice" see:
|
7
|
+
# https://stripe.com/docs/api/invoices/object
|
8
|
+
subscription = Pay.subscription_model.find_by(
|
9
|
+
processor: :stripe,
|
10
|
+
processor_id: event.data.object.subscription
|
11
|
+
)
|
12
|
+
date = Time.zone.at(event.data.object.next_payment_attempt)
|
13
|
+
notify_user(subscription.owner, subscription, date) if subscription.present?
|
14
|
+
end
|
15
|
+
|
16
|
+
def notify_user(billable, subscription, date)
|
17
|
+
if Pay.send_emails
|
18
|
+
Pay::UserMailer.with(billable: billable, subscription: subscription, date: date).subscription_renewing.deliver_later
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Pay
|
2
|
+
module Stripe
|
3
|
+
module Webhooks
|
4
|
+
class SubscriptionUpdated
|
5
|
+
def call(event)
|
6
|
+
object = event.data.object
|
7
|
+
subscription = Pay.subscription_model.find_by(processor: :stripe, processor_id: object.id)
|
8
|
+
|
9
|
+
return if subscription.nil?
|
10
|
+
|
11
|
+
# Delete any subscription attempts that have expired
|
12
|
+
if object.status == "incomplete_expired"
|
13
|
+
subscription.destroy
|
14
|
+
return
|
15
|
+
end
|
16
|
+
|
17
|
+
subscription.status = object.status
|
18
|
+
subscription.quantity = object.quantity
|
19
|
+
subscription.processor_plan = object.plan.id
|
20
|
+
subscription.trial_ends_at = Time.at(object.trial_end) if object.trial_end.present?
|
21
|
+
|
22
|
+
# If user was on trial, their subscription ends at the end of the trial
|
23
|
+
subscription.ends_at = if object.cancel_at_period_end && subscription.on_trial?
|
24
|
+
subscription.trial_ends_at
|
25
|
+
|
26
|
+
# User wasn't on trial, so subscription ends at period end
|
27
|
+
elsif object.cancel_at_period_end
|
28
|
+
Time.at(object.current_period_end)
|
29
|
+
|
30
|
+
# Subscription isn't marked to cancel at period end
|
31
|
+
end
|
32
|
+
|
33
|
+
subscription.save!
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/pay/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pay
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.6.
|
4
|
+
version: 2.6.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Charnes
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2021-
|
12
|
+
date: 2021-04-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -166,6 +166,11 @@ files:
|
|
166
166
|
- lib/pay/engine.rb
|
167
167
|
- lib/pay/env.rb
|
168
168
|
- lib/pay/errors.rb
|
169
|
+
- lib/pay/fake_processor.rb
|
170
|
+
- lib/pay/fake_processor/billable.rb
|
171
|
+
- lib/pay/fake_processor/charge.rb
|
172
|
+
- lib/pay/fake_processor/error.rb
|
173
|
+
- lib/pay/fake_processor/subscription.rb
|
169
174
|
- lib/pay/paddle.rb
|
170
175
|
- lib/pay/paddle/billable.rb
|
171
176
|
- lib/pay/paddle/charge.rb
|
@@ -189,11 +194,26 @@ files:
|
|
189
194
|
- lib/pay/stripe/webhooks/customer_deleted.rb
|
190
195
|
- lib/pay/stripe/webhooks/customer_updated.rb
|
191
196
|
- lib/pay/stripe/webhooks/payment_action_required.rb
|
197
|
+
- lib/pay/stripe/webhooks/payment_intent_succeeded.rb
|
192
198
|
- lib/pay/stripe/webhooks/payment_method_updated.rb
|
193
199
|
- lib/pay/stripe/webhooks/subscription_created.rb
|
194
200
|
- lib/pay/stripe/webhooks/subscription_deleted.rb
|
195
201
|
- lib/pay/stripe/webhooks/subscription_renewing.rb
|
196
202
|
- lib/pay/stripe/webhooks/subscription_updated.rb
|
203
|
+
- lib/pay/stripe_marketplace/billable.rb
|
204
|
+
- lib/pay/stripe_marketplace/charge.rb
|
205
|
+
- lib/pay/stripe_marketplace/error.rb
|
206
|
+
- lib/pay/stripe_marketplace/subscription.rb
|
207
|
+
- lib/pay/stripe_marketplace/webhooks/charge_refunded.rb
|
208
|
+
- lib/pay/stripe_marketplace/webhooks/charge_succeeded.rb
|
209
|
+
- lib/pay/stripe_marketplace/webhooks/customer_deleted.rb
|
210
|
+
- lib/pay/stripe_marketplace/webhooks/customer_updated.rb
|
211
|
+
- lib/pay/stripe_marketplace/webhooks/payment_action_required.rb
|
212
|
+
- lib/pay/stripe_marketplace/webhooks/payment_method_updated.rb
|
213
|
+
- lib/pay/stripe_marketplace/webhooks/subscription_created.rb
|
214
|
+
- lib/pay/stripe_marketplace/webhooks/subscription_deleted.rb
|
215
|
+
- lib/pay/stripe_marketplace/webhooks/subscription_renewing.rb
|
216
|
+
- lib/pay/stripe_marketplace/webhooks/subscription_updated.rb
|
197
217
|
- lib/pay/version.rb
|
198
218
|
- lib/pay/webhooks.rb
|
199
219
|
- lib/pay/webhooks/delegator.rb
|