pay 2.6.7 → 2.6.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8901d69060546acd46c628f40bad3c72f0353e37dd0feff1ef94593d56c07f74
4
- data.tar.gz: 8f5d3e21c8c17904483ee98e6996186a9e15ce368d4161c024464b4731db4a15
3
+ metadata.gz: d2e815585d6d3e171b89f184781db67eafddbd7d10ec8e5635b1d6e53b6475ab
4
+ data.tar.gz: 178f9466b98867c2bbaaa16047f78d5a55f35f4056bc964cd39d9bc29023c1d6
5
5
  SHA512:
6
- metadata.gz: 2e2b6490e777ecfff88673c295ee91367125a18d7e49ef5127827dff080bfec151b34f832617aef06512c3630ac0bcec936cc9ffdcddfaec80a80a7ccb865b60
7
- data.tar.gz: 5f0962f4ecb9cfafa8dfeed0e437e801eaa8dc239481477fdf8b689f359383bef0c9907f1ad0b1d7475ccc87391355fcdc1548229cc2bdf3c45cb03ce17f26be
6
+ metadata.gz: fb149c7f58a970019d4f65aa1d461e62bbc3d58a8478f715d9f8f3b858accaa6b9d12e951ea159472bc087635040601ab95d3a270bc9f544c777951f6964e513
7
+ data.tar.gz: efb10c4e80ea59dda1b0f9d24a835dcbfebb91feb13a0e882b9acf0ca6d446d1930369c922ef5353562bdf1c7690a5702f6ea81b27a667743fccadcaa2d541ca
@@ -13,7 +13,7 @@
13
13
  sessionId: '<%= session.id %>'
14
14
  }).then(function (result) {
15
15
  if (result.error) {
16
- document.getElementById('error-message').innerText = result.error.message;
16
+ document.getElementById("error-for-#{session.id}").innerText = result.error.message;
17
17
  }
18
18
  });
19
19
  });
data/lib/pay/paddle.rb CHANGED
@@ -44,6 +44,13 @@ module Pay
44
44
  options.merge(owner_sgid: owner.to_sgid.to_s).to_json
45
45
  end
46
46
 
47
+ def self.owner_from_passthrough(passthrough)
48
+ passthrough_json = JSON.parse(passthrough)
49
+ GlobalID::Locator.locate_signed(passthrough_json["owner_sgid"])
50
+ rescue JSON::ParserError
51
+ nil
52
+ end
53
+
47
54
  def self.configure_webhooks
48
55
  Pay::Webhooks.configure do |events|
49
56
  events.subscribe "paddle.subscription_created", Pay::Paddle::Webhooks::SubscriptionCreated.new
@@ -13,7 +13,7 @@ module Pay
13
13
  owner = Pay.find_billable(processor: :paddle, processor_id: event["user_id"])
14
14
 
15
15
  if owner.nil?
16
- owner = owner_by_passtrough(event["passthrough"], event["subscription_plan_id"])
16
+ owner = Pay::Paddle.owner_from_passthrough(event["passthrough"])
17
17
  owner&.update!(processor: "paddle", processor_id: event["user_id"])
18
18
  end
19
19
 
@@ -44,15 +44,6 @@ module Pay
44
44
 
45
45
  subscription.save!
46
46
  end
47
-
48
- private
49
-
50
- def owner_by_passtrough(passthrough, product_id)
51
- passthrough_json = JSON.parse(passthrough)
52
- GlobalID::Locator.locate_signed(passthrough_json["owner_sgid"])
53
- rescue JSON::ParserError
54
- nil
55
- end
56
47
  end
57
48
  end
58
49
  end
@@ -4,7 +4,17 @@ module Pay
4
4
  class SubscriptionPaymentSucceeded
5
5
  def call(event)
6
6
  billable = Pay.find_billable(processor: :paddle, processor_id: event["user_id"])
7
- return unless billable.present?
7
+
8
+ if billable.nil?
9
+ billable = Pay::Paddle.owner_from_passthrough(event["passthrough"])
10
+ billable&.update!(processor: "paddle", processor_id: event["user_id"])
11
+ end
12
+
13
+ if billable.nil?
14
+ Rails.logger.error("[Pay] Unable to find Pay::Billable with owner: '#{event["passthrough"]}'. Searched these models: #{Pay.billable_models.join(", ")}")
15
+ return
16
+ end
17
+
8
18
  return if billable.charges.where(processor_id: event["subscription_payment_id"]).any?
9
19
 
10
20
  charge = create_charge(billable, event)
data/lib/pay/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pay
2
- VERSION = "2.6.7"
2
+ VERSION = "2.6.8"
3
3
  end
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.7
4
+ version: 2.6.8
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-04-07 00:00:00.000000000 Z
12
+ date: 2021-04-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -200,20 +200,6 @@ files:
200
200
  - lib/pay/stripe/webhooks/subscription_deleted.rb
201
201
  - lib/pay/stripe/webhooks/subscription_renewing.rb
202
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
217
203
  - lib/pay/version.rb
218
204
  - lib/pay/webhooks.rb
219
205
  - lib/pay/webhooks/delegator.rb
@@ -1,246 +0,0 @@
1
- module Pay
2
- module StripeMarketplace
3
- class Billable
4
- include Rails.application.routes.url_helpers
5
-
6
- attr_reader :billable
7
-
8
- delegate :processor_id,
9
- :processor_id?,
10
- :email,
11
- :customer_name,
12
- :card_token,
13
- to: :billable
14
-
15
- class << self
16
- def default_url_options
17
- Rails.application.config.action_mailer.default_url_options || {}
18
- end
19
- end
20
-
21
- def initialize(billable)
22
- @billable = billable
23
- end
24
-
25
- # Handles Billable#customer
26
- #
27
- # Returns Stripe::Customer
28
- def customer
29
- if processor_id?
30
- ::Stripe::Customer.retrieve(processor_id)
31
- else
32
- stripe_customer = ::Stripe::Customer.create(email: email, name: customer_name)
33
- billable.update(processor: :stripe, processor_id: stripe_customer.id)
34
-
35
- # Update the user's card on file if a token was passed in
36
- if card_token.present?
37
- payment_method = ::Stripe::PaymentMethod.attach(card_token, {customer: stripe_customer.id})
38
- stripe_customer.invoice_settings.default_payment_method = payment_method.id
39
- stripe_customer.save
40
-
41
- update_card_on_file ::Stripe::PaymentMethod.retrieve(card_token).card
42
- end
43
-
44
- stripe_customer
45
- end
46
- rescue ::Stripe::StripeError => e
47
- raise Pay::Stripe::Error, e
48
- end
49
-
50
- # Handles Billable#charge
51
- #
52
- # Returns Pay::Charge
53
- def charge(amount, options = {})
54
- stripe_customer = customer
55
- args = {
56
- amount: amount,
57
- confirm: true,
58
- confirmation_method: :automatic,
59
- currency: "usd",
60
- customer: stripe_customer.id,
61
- payment_method: stripe_customer.invoice_settings.default_payment_method
62
- }.merge(options)
63
-
64
- payment_intent = ::Stripe::PaymentIntent.create(args)
65
- Pay::Payment.new(payment_intent).validate
66
-
67
- # Create a new charge object
68
- save_pay_charge(payment_intent.charges.first)
69
- rescue ::Stripe::StripeError => e
70
- raise Pay::Stripe::Error, e
71
- end
72
-
73
- # Handles Billable#subscribe
74
- #
75
- # Returns Pay::Subscription
76
- def subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **options)
77
- quantity = options.delete(:quantity) || 1
78
- opts = {
79
- expand: ["pending_setup_intent", "latest_invoice.payment_intent"],
80
- items: [plan: plan, quantity: quantity],
81
- off_session: true
82
- }.merge(options)
83
-
84
- # Inherit trial from plan unless trial override was specified
85
- opts[:trial_from_plan] = true unless opts[:trial_period_days]
86
-
87
- opts[:customer] = customer.id
88
-
89
- stripe_sub = ::Stripe::Subscription.create(opts)
90
- subscription = billable.create_pay_subscription(stripe_sub, "stripe", name, plan, status: stripe_sub.status, quantity: quantity)
91
-
92
- # No trial, card requires SCA
93
- if subscription.incomplete?
94
- Pay::Payment.new(stripe_sub.latest_invoice.payment_intent).validate
95
-
96
- # Trial, card requires SCA
97
- elsif subscription.on_trial? && stripe_sub.pending_setup_intent
98
- Pay::Payment.new(stripe_sub.pending_setup_intent).validate
99
- end
100
-
101
- subscription
102
- rescue ::Stripe::StripeError => e
103
- raise Pay::Stripe::Error, e
104
- end
105
-
106
- # Handles Billable#update_card
107
- #
108
- # Returns true if successful
109
- def update_card(payment_method_id)
110
- stripe_customer = customer
111
-
112
- return true if payment_method_id == stripe_customer.invoice_settings.default_payment_method
113
-
114
- payment_method = ::Stripe::PaymentMethod.attach(payment_method_id, customer: stripe_customer.id)
115
- ::Stripe::Customer.update(stripe_customer.id, invoice_settings: {default_payment_method: payment_method.id})
116
-
117
- update_card_on_file(payment_method.card)
118
- true
119
- rescue ::Stripe::StripeError => e
120
- raise Pay::Stripe::Error, e
121
- end
122
-
123
- def update_email!
124
- ::Stripe::Customer.update(processor_id, {email: email, name: customer_name})
125
- end
126
-
127
- def processor_subscription(subscription_id, options = {})
128
- ::Stripe::Subscription.retrieve(options.merge(id: subscription_id))
129
- end
130
-
131
- def invoice!(options = {})
132
- return unless processor_id?
133
- ::Stripe::Invoice.create(options.merge(customer: processor_id)).pay
134
- end
135
-
136
- def upcoming_invoice
137
- ::Stripe::Invoice.upcoming(customer: processor_id)
138
- end
139
-
140
- # Used by webhooks when the customer or source changes
141
- def sync_card_from_stripe
142
- if (payment_method_id = customer.invoice_settings.default_payment_method)
143
- update_card_on_file ::Stripe::PaymentMethod.retrieve(payment_method_id).card
144
- else
145
- billable.update(card_type: nil, card_last4: nil)
146
- end
147
- end
148
-
149
- def create_setup_intent
150
- ::Stripe::SetupIntent.create(customer: processor_id, usage: :off_session)
151
- end
152
-
153
- def trial_end_date(stripe_sub)
154
- # Times in Stripe are returned in UTC
155
- stripe_sub.trial_end.present? ? Time.at(stripe_sub.trial_end) : nil
156
- end
157
-
158
- # Save the card to the database as the user's current card
159
- def update_card_on_file(card)
160
- billable.update!(
161
- card_type: card.brand.capitalize,
162
- card_last4: card.last4,
163
- card_exp_month: card.exp_month,
164
- card_exp_year: card.exp_year
165
- )
166
-
167
- billable.card_token = nil
168
- end
169
-
170
- def save_pay_charge(object)
171
- charge = billable.charges.find_or_initialize_by(processor: :stripe, processor_id: object.id)
172
-
173
- charge.update(
174
- amount: object.amount,
175
- card_last4: object.payment_method_details.card.last4,
176
- card_type: object.payment_method_details.card.brand,
177
- card_exp_month: object.payment_method_details.card.exp_month,
178
- card_exp_year: object.payment_method_details.card.exp_year,
179
- created_at: Time.zone.at(object.created)
180
- )
181
-
182
- charge
183
- end
184
-
185
- # https://stripe.com/docs/api/checkout/sessions/create
186
- #
187
- # checkout(mode: "payment")
188
- # checkout(mode: "setup")
189
- # checkout(mode: "subscription")
190
- #
191
- # checkout(line_items: "price_12345", quantity: 2)
192
- # checkout(line_items [{ price: "price_123" }, { price: "price_456" }])
193
- # checkout(line_items, "price_12345", allow_promotion_codes: true)
194
- #
195
- def checkout(**options)
196
- args = {
197
- customer: processor_id,
198
- payment_method_types: ["card"],
199
- mode: "payment",
200
- # These placeholder URLs will be replaced in a following step.
201
- success_url: root_url,
202
- cancel_url: root_url
203
- }
204
-
205
- # Line items are optional
206
- if (line_items = options.delete(:line_items))
207
- args[:line_items] = Array.wrap(line_items).map { |item|
208
- if item.is_a? Hash
209
- item
210
- else
211
- {price: item, quantity: options.fetch(:quantity, 1)}
212
- end
213
- }
214
- end
215
-
216
- ::Stripe::Checkout::Session.create(args.merge(options))
217
- end
218
-
219
- # https://stripe.com/docs/api/checkout/sessions/create
220
- #
221
- # checkout_charge(amount: 15_00, name: "T-shirt", quantity: 2)
222
- #
223
- def checkout_charge(amount:, name:, quantity: 1, **options)
224
- checkout(
225
- line_items: {
226
- price_data: {
227
- currency: options[:currency] || "usd",
228
- product_data: {name: name},
229
- unit_amount: amount
230
- },
231
- quantity: quantity
232
- },
233
- **options
234
- )
235
- end
236
-
237
- def billing_portal(**options)
238
- args = {
239
- customer: processor_id,
240
- return_url: options[:return_url] || root_url
241
- }
242
- ::Stripe::BillingPortal::Session.create(args.merge(options))
243
- end
244
- end
245
- end
246
- end
@@ -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