Module: UsageCredits::PayChargeExtension
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/usage_credits/models/concerns/pay_charge_extension.rb
Overview
Extends Pay::Charge with credit pack functionality
This extension integrates with the Pay gem (https://github.com/pay-rails/pay) to automatically fulfill credit packs when charges succeed and handle refunds when charges are refunded.
Instance Method Summary collapse
-
#charge_object_data ⇒ Object
Returns the Stripe charge object data, checking both
object(Pay 10+) anddata(older Pay versions) for backward compatibility. - #init_metadata ⇒ Object
- #refunded? ⇒ Boolean
- #succeeded? ⇒ Boolean
Instance Method Details
#charge_object_data ⇒ Object
Returns the Stripe charge object data, checking both object (Pay 10+)
and data (older Pay versions) for backward compatibility
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/usage_credits/models/concerns/pay_charge_extension.rb', line 51 def charge_object_data # Pay 10+ stores full Stripe object in `object` column if respond_to?(:object) && object.is_a?(Hash) && object.any? object.with_indifferent_access # Older Pay versions stored charge details in `data` column elsif data.is_a?(Hash) && data.any? data.with_indifferent_access else {}.with_indifferent_access end end |
#init_metadata ⇒ Object
18 19 20 21 |
# File 'lib/usage_credits/models/concerns/pay_charge_extension.rb', line 18 def self. ||= {} self.data ||= {} end |
#refunded? ⇒ Boolean
63 64 65 66 |
# File 'lib/usage_credits/models/concerns/pay_charge_extension.rb', line 63 def refunded? return false unless amount_refunded amount_refunded > 0 end |
#succeeded? ⇒ Boolean
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/usage_credits/models/concerns/pay_charge_extension.rb', line 23 def succeeded? case type when "Pay::Stripe::Charge" # Pay 10+ stores the full Stripe charge object in `object` column # Older Pay versions stored charge details in `data` column # We check both for backward compatibility stripe_object = charge_object_data status = stripe_object["status"] # Explicitly check for failure states return false if status == "failed" return false if status == "pending" return false if status == "canceled" return true if status == "succeeded" # Fallback: check if amount was actually captured amount_captured = stripe_object["amount_captured"].to_i return amount_captured == amount.to_i && amount.to_i.positive? end # For non-Stripe charges, we assume Pay only creates charges after successful payment # This is a reasonable assumption based on Pay gem's behavior # TODO: Implement for more payment processors if needed true end |