pay 13.3.0 → 13.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4d9ce462af3235715c446100ba1288fb0a9abba9d6b96a2be34c9416338dfb2
4
- data.tar.gz: 7a602a7ff2d224b34b9e07ff9095721872e3728bc2e44d84d9fe75941962a8f8
3
+ metadata.gz: 8e2b20abe8c92fe08f07c4d832289e6b23b0f781fa115d32d1a63c949a92d873
4
+ data.tar.gz: 716d064999d5ec56f1bd13a7cf09be1f8507f51e0b987550d7056d15371d6fdc
5
5
  SHA512:
6
- metadata.gz: 5fe5ae9c74b16a9475c1c65f82668bfc88a131ac7ab9ef2c2b7f0645a47a52bdfbe69b969e873c7d1e7d9340055029698763637bee6f6a5fa2cc6a452c8d64b7
7
- data.tar.gz: 8152a03285f108a24646ed00651af3413d3cb28814d6e0d87392cba9b232ac5bd9c962ce3db9732c153e2c3518cb3d34d461015b0ae4613bcfb14a97f2ae13e8
6
+ metadata.gz: 5b4af7eec977db4e2b07d84671a6f0113f5721bf1285b95ef8766deeac449c39e3a4795284cdb967faecad5147bdcecb19fbeb1cb7264ffbea5e4126cd459b75
7
+ data.tar.gz: dfb0e1659fb2e314b8d0225001b254287fabb21b5b11089bb2b4692c49a7425eac661734d1cc3a8b66bfe9fc3ef3a8615895137405ca3fc91c3e4b3209239e9b
@@ -3,6 +3,9 @@ module Pay
3
3
  class Charge < Pay::Charge
4
4
  extend Pay::Sync
5
5
 
6
+ # Matches ::Braintree::Transaction::Status values. Strings so this file loads without the braintree gem.
7
+ VOIDABLE_STATUSES = Pay.make_shareable(%w[authorized submitted_for_settlement settlement_pending])
8
+
6
9
  def self.sync(charge_id, object: nil)
7
10
  sync_with_retries do
8
11
  transaction = object || Pay.braintree_gateway.transaction.find(charge_id)
@@ -14,13 +17,21 @@ module Pay
14
17
 
15
18
  def api_record
16
19
  Pay.braintree_gateway.transaction.find(processor_id)
17
- rescue ::Braintree::Braintree::Error => e
20
+ rescue ::Braintree::BraintreeError => e
18
21
  raise Pay::Braintree::Error, e
19
22
  end
20
23
 
24
+ # Braintree only refunds settled transactions, so unsettled ones are voided instead
25
+ # A void cancels the whole payment, so an unsettled transaction can't be partially refunded
21
26
  def refund!(amount_to_refund = nil)
22
27
  amount_to_refund ||= amount - amount_refunded.to_i
23
- result = Pay.braintree_gateway.transaction.refund(processor_id, Pay::Currency.from_subunit(amount_to_refund, currency: currency))
28
+
29
+ result = if VOIDABLE_STATUSES.include?(api_record.status)
30
+ raise Pay::Braintree::Error, I18n.t("pay.errors.braintree.partial_refund_unsettled") if amount_to_refund < amount
31
+ Pay.braintree_gateway.transaction.void(processor_id)
32
+ else
33
+ Pay.braintree_gateway.transaction.refund(processor_id, Pay::Currency.from_subunit(amount_to_refund, currency: currency))
34
+ end
24
35
  raise Pay::Braintree::Error, result unless result.success?
25
36
 
26
37
  update(amount_refunded: amount_refunded.to_i + amount_to_refund)
@@ -58,6 +58,46 @@ module Pay
58
58
  end
59
59
  end
60
60
  end
61
+
62
+ # Requests a refund from Paddle and returns the Paddle::Adjustment
63
+ #
64
+ # Paddle refunds start out pending approval, so amount_refunded is updated by the
65
+ # adjustment webhooks once Paddle approves it.
66
+ #
67
+ # Only refunding the whole of an unrefunded charge is a full adjustment. Anything else
68
+ # is a partial adjustment against the transaction's single line item.
69
+ #
70
+ # refund!
71
+ # refund!(5_00, reason: "Customer request")
72
+ def refund!(amount_to_refund = nil, reason: I18n.t("pay.refund"))
73
+ # Paddle's own refund total, so refunds approved since the last sync count too
74
+ transaction = ::Paddle::Transaction.retrieve(id: processor_id, extra: "adjustments_totals")
75
+ remaining = amount - transaction.adjustments_totals.breakdown.refund.to_i
76
+ raise Pay::PaddleBilling::Error, "#{processor_id} has nothing left to refund" unless remaining.positive?
77
+
78
+ amount_to_refund ||= remaining
79
+ unless amount_to_refund.positive? && amount_to_refund <= remaining
80
+ raise Pay::PaddleBilling::Error, "#{processor_id} can refund between 1 and #{remaining}, not #{amount_to_refund}"
81
+ end
82
+
83
+ params = {transaction_id: processor_id, action: "refund", reason: reason}
84
+
85
+ if amount_to_refund == amount
86
+ params[:type] = "full"
87
+ else
88
+ line_items = transaction.details.line_items
89
+ unless line_items.size == 1
90
+ raise Pay::PaddleBilling::Error, "Paddle Billing partial refunds need a transaction with one line item, but #{processor_id} has #{line_items.size}"
91
+ end
92
+
93
+ params[:type] = "partial"
94
+ params[:items] = [{item_id: line_items.first.id, type: "partial", amount: amount_to_refund.to_s}]
95
+ end
96
+
97
+ ::Paddle::Adjustment.create(**params)
98
+ rescue ::Paddle::ErrorGenerator => e
99
+ raise Pay::PaddleBilling::Error, e
100
+ end
61
101
  end
62
102
  end
63
103
  end
@@ -60,6 +60,7 @@ en:
60
60
  no_processor: "No payment processor selected. Make sure to set the %{class_name}'s `processor` attribute to either 'stripe' or 'braintree'."
61
61
  braintree:
62
62
  authorization: "Either the data you submitted is malformed and does not match the API or the API key you used may not be authorized to perform this action."
63
+ partial_refund_unsettled: "Braintree can't partially refund a transaction until it settles. Refund the full amount to void it, or wait for it to settle."
63
64
  user_mailer:
64
65
  receipt:
65
66
  subject: "[%{application}] Payment receipt"
data/lib/pay/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pay
4
- VERSION = "13.3.0"
4
+ VERSION = "13.4.1"
5
5
  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: 13.3.0
4
+ version: 13.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Charnes