pay 3.0.10 → 3.0.14

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.

@@ -21,7 +21,7 @@ module Pay
21
21
  if saved_change_to_email?
22
22
  # Queue job to update each payment processor for this customer
23
23
  pay_customers.pluck(:id).each do |pay_customer_id|
24
- CustomerSyncJob.perform_later(id)
24
+ CustomerSyncJob.perform_later(pay_customer_id)
25
25
  end
26
26
  end
27
27
  end
@@ -0,0 +1,74 @@
1
+ module Pay
2
+ class Currency
3
+ include ActionView::Helpers::NumberHelper
4
+
5
+ attr_reader :attributes
6
+
7
+ def self.all
8
+ @currencies ||= begin
9
+ path = Engine.root.join("config", "currencies", "iso.json")
10
+ JSON.parse File.read(path)
11
+ end
12
+ end
13
+
14
+ # Takes an amount (in cents) and currency and returns the formatted version for the currency
15
+ def self.format(amount, currency:)
16
+ currency ||= :usd
17
+ new(currency).format_amount(amount)
18
+ end
19
+
20
+ def initialize(iso_code)
21
+ @attributes = self.class.all[iso_code.to_s.downcase]
22
+ end
23
+
24
+ def format_amount(amount, **options)
25
+ number_to_currency(
26
+ amount.to_i / subunit_to_unit.to_f,
27
+ {
28
+ precision: precision,
29
+ unit: unit,
30
+ separator: separator,
31
+ delimiter: delimiter,
32
+ format: format
33
+ }.compact.merge(options)
34
+ )
35
+ end
36
+
37
+ # Returns the precision to display
38
+ #
39
+ # If 1, returns 0
40
+ # If 100, returns 2
41
+ # If 1000, returns 3
42
+ def precision
43
+ subunit_to_unit.digits.count - 1
44
+ end
45
+
46
+ def unit
47
+ attributes["unit"]
48
+ end
49
+
50
+ def separator
51
+ attributes["separator"]
52
+ end
53
+
54
+ def delimiter
55
+ attributes["delimiter"]
56
+ end
57
+
58
+ def format
59
+ attributes["format"]
60
+ end
61
+
62
+ def subunit?
63
+ subunit.blank?
64
+ end
65
+
66
+ def subunit
67
+ attributes["subunit"]
68
+ end
69
+
70
+ def subunit_to_unit
71
+ attributes["subunit_to_unit"]
72
+ end
73
+ end
74
+ end
data/lib/pay/payment.rb CHANGED
@@ -2,7 +2,7 @@ module Pay
2
2
  class Payment
3
3
  attr_reader :intent
4
4
 
5
- delegate :id, :amount, :client_secret, :customer, :status, :confirm, to: :intent
5
+ delegate :id, :amount, :client_secret, :currency, :customer, :status, :confirm, to: :intent
6
6
 
7
7
  def self.from_id(id)
8
8
  intent = id.start_with?("seti_") ? ::Stripe::SetupIntent.retrieve(id) : ::Stripe::PaymentIntent.retrieve(id)
@@ -41,6 +41,10 @@ module Pay
41
41
  intent.is_a?(::Stripe::SetupIntent)
42
42
  end
43
43
 
44
+ def amount_with_currency
45
+ Pay::Currency.format(amount, currency: currency)
46
+ end
47
+
44
48
  def validate
45
49
  if requires_payment_method?
46
50
  raise Pay::InvalidPaymentMethod.new(self)
data/lib/pay/receipts.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  module Pay
2
2
  module Receipts
3
- include ActionView::Helpers::NumberHelper
4
-
5
3
  def product
6
4
  Pay.application_name
7
5
  end
@@ -20,11 +18,11 @@ module Pay
20
18
  [I18n.t("pay.receipt.date"), I18n.l(created_at, format: :long)],
21
19
  [I18n.t("pay.receipt.account_billed"), "#{customer.customer_name} (#{customer.email})"],
22
20
  [I18n.t("pay.receipt.product"), product],
23
- [I18n.t("pay.receipt.amount"), number_to_currency(amount / 100.0)],
21
+ [I18n.t("pay.receipt.amount"), Pay::Currency.format(amount, currency: currency)],
24
22
  [I18n.t("pay.receipt.charged_to"), charged_to]
25
23
  ]
26
24
  line_items << [I18n.t("pay.receipt.additional_info"), customer.owner.extra_billing_info] if customer.owner.extra_billing_info?
27
- line_items << [I18n.t("pay.receipt.refunded"), number_to_currency(amount_refunded / 100.0)] if refunded?
25
+ line_items << [I18n.t("pay.receipt.refunded"), Pay::Currency.format(amount_refunded, currency: currency)] if refunded?
28
26
 
29
27
  defaults = {
30
28
  id: id,
@@ -50,11 +48,11 @@ module Pay
50
48
  end
51
49
 
52
50
  def invoice_pdf(**options)
53
- bill_to = [owner.name]
54
- bill_to += [owner.extra_billing_info] if owner.extra_billing_info?
55
- bill_to += [nil, owner.email]
51
+ bill_to = [customer.owner.name]
52
+ bill_to += [customer.owner.extra_billing_info] if customer.owner.extra_billing_info?
53
+ bill_to += [nil, customer.owner.email]
56
54
 
57
- total = ActionController::Base.helpers.number_to_currency(amount / 100.0)
55
+ total = Pay::Currency.format(amount, currency: currency)
58
56
 
59
57
  line_items = [
60
58
  ["<b>#{I18n.t("pay.invoice.product")}</b>", nil, "<b>#{I18n.t("pay.invoice.amount")}</b>"],
@@ -175,13 +175,14 @@ module Pay
175
175
  # checkout(line_items, "price_12345", allow_promotion_codes: true)
176
176
  #
177
177
  def checkout(**options)
178
+ customer unless processor_id?
178
179
  args = {
179
180
  customer: processor_id,
180
181
  payment_method_types: ["card"],
181
182
  mode: "payment",
182
183
  # These placeholder URLs will be replaced in a following step.
183
- success_url: options.delete(:success_url) || root_url,
184
- cancel_url: options.delete(:cancel_url) || root_url
184
+ success_url: options.delete(:success_url) || root_url(session_id: "{CHECKOUT_SESSION_ID}"),
185
+ cancel_url: options.delete(:cancel_url) || root_url(session_id: "{CHECKOUT_SESSION_ID}")
185
186
  }
186
187
 
187
188
  # Line items are optional
@@ -203,6 +204,7 @@ module Pay
203
204
  # checkout_charge(amount: 15_00, name: "T-shirt", quantity: 2)
204
205
  #
205
206
  def checkout_charge(amount:, name:, quantity: 1, **options)
207
+ customer unless processor_id?
206
208
  currency = options.delete(:currency) || "usd"
207
209
  checkout(
208
210
  line_items: {
@@ -218,6 +220,7 @@ module Pay
218
220
  end
219
221
 
220
222
  def billing_portal(**options)
223
+ customer unless processor_id?
221
224
  args = {
222
225
  customer: processor_id,
223
226
  return_url: options.delete(:return_url) || root_url
@@ -18,7 +18,7 @@ module Pay
18
18
  default_payment_method_id = pay_customer.customer.invoice_settings.default_payment_method
19
19
  default = (id == default_payment_method_id)
20
20
 
21
- attributes = extract_attributes(object).merge(default: default)
21
+ attributes = extract_attributes(object).merge(default: default, stripe_account: stripe_account)
22
22
 
23
23
  pay_customer.payment_methods.update_all(default: false) if default
24
24
  pay_payment_method = pay_customer.payment_methods.where(processor_id: object.id).first_or_initialize
@@ -4,7 +4,7 @@ module Pay
4
4
  class ChargeRefunded
5
5
  def call(event)
6
6
  pay_charge = Pay::Stripe::Charge.sync(event.data.object.id, stripe_account: event.try(:account))
7
- notify_user(pay_charge.owner, pay_charge) if pay_charge
7
+ notify_user(pay_charge.customer.owner, pay_charge) if pay_charge
8
8
  end
9
9
 
10
10
  def notify_user(billable, charge)
data/lib/pay/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pay
2
- VERSION = "3.0.10"
2
+ VERSION = "3.0.14"
3
3
  end
data/lib/pay.rb CHANGED
@@ -9,6 +9,7 @@ module Pay
9
9
  autoload :NanoId, "pay/nano_id"
10
10
  autoload :Payment, "pay/payment"
11
11
  autoload :Receipts, "pay/receipts"
12
+ autoload :Currency, "pay/currency"
12
13
 
13
14
  # Payment processors
14
15
  autoload :Braintree, "pay/braintree"
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: 3.0.10
4
+ version: 3.0.14
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-09-05 00:00:00.000000000 Z
12
+ date: 2021-09-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -119,6 +119,7 @@ files:
119
119
  - app/views/pay/user_mailer/receipt.html.erb
120
120
  - app/views/pay/user_mailer/refund.html.erb
121
121
  - app/views/pay/user_mailer/subscription_renewing.html.erb
122
+ - config/currencies/iso.json
122
123
  - config/locales/en.yml
123
124
  - config/routes.rb
124
125
  - db/migrate/1_create_pay_tables.rb
@@ -149,6 +150,7 @@ files:
149
150
  - lib/pay/braintree/webhooks/subscription_trial_ended.rb
150
151
  - lib/pay/braintree/webhooks/subscription_went_active.rb
151
152
  - lib/pay/braintree/webhooks/subscription_went_past_due.rb
153
+ - lib/pay/currency.rb
152
154
  - lib/pay/engine.rb
153
155
  - lib/pay/env.rb
154
156
  - lib/pay/errors.rb