pay 2.6.7 → 2.7.0

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.

Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -0
  3. data/app/models/pay.rb +5 -0
  4. data/app/models/pay/charge.rb +2 -0
  5. data/app/models/pay/subscription.rb +9 -2
  6. data/app/views/pay/stripe/_checkout_button.html.erb +1 -1
  7. data/db/migrate/20200603134434_add_data_to_pay_models.rb +2 -9
  8. data/db/migrate/20210309004259_add_data_to_pay_billable.rb +19 -0
  9. data/db/migrate/20210406215234_add_currency_to_pay_charges.rb +5 -0
  10. data/db/migrate/20210406215506_add_application_fee_to_pay_models.rb +7 -0
  11. data/lib/generators/active_record/templates/migration.rb +1 -1
  12. data/lib/pay.rb +22 -0
  13. data/lib/pay/adapter.rb +13 -0
  14. data/lib/pay/billable.rb +4 -0
  15. data/lib/pay/braintree/billable.rb +11 -2
  16. data/lib/pay/braintree/subscription.rb +4 -0
  17. data/lib/pay/fake_processor/subscription.rb +4 -0
  18. data/lib/pay/merchant.rb +37 -0
  19. data/lib/pay/paddle.rb +7 -0
  20. data/lib/pay/paddle/subscription.rb +7 -0
  21. data/lib/pay/paddle/webhooks/subscription_created.rb +1 -10
  22. data/lib/pay/paddle/webhooks/subscription_payment_succeeded.rb +14 -2
  23. data/lib/pay/stripe.rb +5 -0
  24. data/lib/pay/stripe/billable.rb +46 -34
  25. data/lib/pay/stripe/charge.rb +9 -4
  26. data/lib/pay/stripe/merchant.rb +66 -0
  27. data/lib/pay/stripe/subscription.rb +35 -20
  28. data/lib/pay/stripe/webhooks/account_updated.rb +17 -0
  29. data/lib/pay/stripe/webhooks/subscription_created.rb +2 -1
  30. data/lib/pay/stripe/webhooks/subscription_updated.rb +3 -2
  31. data/lib/pay/version.rb +1 -1
  32. metadata +11 -17
  33. data/lib/pay/stripe_marketplace/billable.rb +0 -246
  34. data/lib/pay/stripe_marketplace/charge.rb +0 -26
  35. data/lib/pay/stripe_marketplace/error.rb +0 -9
  36. data/lib/pay/stripe_marketplace/subscription.rb +0 -83
  37. data/lib/pay/stripe_marketplace/webhooks/charge_refunded.rb +0 -23
  38. data/lib/pay/stripe_marketplace/webhooks/charge_succeeded.rb +0 -24
  39. data/lib/pay/stripe_marketplace/webhooks/customer_deleted.rb +0 -29
  40. data/lib/pay/stripe_marketplace/webhooks/customer_updated.rb +0 -17
  41. data/lib/pay/stripe_marketplace/webhooks/payment_action_required.rb +0 -26
  42. data/lib/pay/stripe_marketplace/webhooks/payment_method_updated.rb +0 -17
  43. data/lib/pay/stripe_marketplace/webhooks/subscription_created.rb +0 -45
  44. data/lib/pay/stripe_marketplace/webhooks/subscription_deleted.rb +0 -19
  45. data/lib/pay/stripe_marketplace/webhooks/subscription_renewing.rb +0 -24
  46. data/lib/pay/stripe_marketplace/webhooks/subscription_updated.rb +0 -38
@@ -1,26 +0,0 @@
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
@@ -1,9 +0,0 @@
1
- module Pay
2
- module Stripe
3
- class Error < Pay::Error
4
- def message
5
- I18n.t("errors.stripe.#{result.code}", default: result.message)
6
- end
7
- end
8
- end
9
- end
@@ -1,83 +0,0 @@
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
@@ -1,23 +0,0 @@
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
@@ -1,24 +0,0 @@
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
@@ -1,29 +0,0 @@
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
@@ -1,17 +0,0 @@
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
@@ -1,26 +0,0 @@
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
@@ -1,17 +0,0 @@
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
@@ -1,45 +0,0 @@
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
@@ -1,19 +0,0 @@
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
@@ -1,24 +0,0 @@
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
@@ -1,38 +0,0 @@
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