Class: Gold::ChargeOp
- Inherits:
-
Object
- Object
- Gold::ChargeOp
- Defined in:
- app/operations/gold/charge_op.rb
Overview
Creates a charge for a shop.
Constant Summary collapse
- MAX_ATTEMPTS =
3
Constants included from Outcomes
Outcomes::AcceptedCharge, Outcomes::AcceptedTerms, Outcomes::ActiveCharge, Outcomes::CannotApplyDiscount, Outcomes::CannotIssueCredit, Outcomes::CannotProcessCharge, Outcomes::CannotSelectTier, Outcomes::ChargeNeeded, Outcomes::ChargeNotNeeded, Outcomes::DeclinedCharge, Outcomes::DeclinedTerms, Outcomes::ExpiredCharge, Outcomes::FrozenCharge, Outcomes::MismatchCharge, Outcomes::MissingCharge, Outcomes::PendingCharge, Outcomes::SameDiscount, Outcomes::SameTier, Outcomes::TierApplied, Outcomes::TierNotFound, Outcomes::Uninstalled
Instance Method Summary collapse
-
#call ⇒ Object
Find a previous charge to use or create a new one.
-
#initialize(billing, return_url, test_charge = Gold.configuration.test_charge?) ⇒ ChargeOp
constructor
A new instance of ChargeOp.
Methods included from Retries
Constructor Details
#initialize(billing, return_url, test_charge = Gold.configuration.test_charge?) ⇒ ChargeOp
Returns a new instance of ChargeOp.
9 10 11 12 13 |
# File 'app/operations/gold/charge_op.rb', line 9 def initialize(billing, return_url, test_charge = Gold.configuration.test_charge?) @billing = billing @return_url = return_url @test_charge = test_charge end |
Instance Method Details
#call ⇒ Object
Find a previous charge to use or create a new one
16 17 18 19 20 21 22 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 48 49 50 51 52 53 |
# File 'app/operations/gold/charge_op.rb', line 16 def call return ChargeNotNeeded.new(:tier_is_free) if tier.free? return ChargeNotNeeded.new(:in_billing_state) if @billing.current_state == :billing charges = ShopifyAPI::RecurringApplicationCharge.all || [] if (charge = find_matching_charge(charges, @billing, "active")) Gold.logger.info("[#{@billing.id}] There is an existing charge (#{charge.id}) " \ "ready to use; skip charge creation") transition_to_charge_state!(charge_id: charge.id) return ActiveCharge.new(charge.id) elsif (charge = find_matching_charge(charges, @billing, "accepted")) Gold.logger.info("[#{@billing.id}] There is an accepted charge (#{charge.id}) " \ "that we haven't registered yet; redirecting to that now") # Log that this charge is the one we want to use transition_to_charge_state!(charge_id: charge.id) return AcceptedCharge.new(charge.decorated_return_url) elsif (charge = find_matching_charge(charges, @billing, "pending")) Gold.logger.info("[#{@billing.id}] Reusing an existing charge: #{charge.id}") # Mark the charge as ready for the merchant to review transition_to_charge_state!(charge_id: charge.id) return PendingCharge.new(charge.confirmation_url) else # Construct a new RecurringApplicationCharge for the tier charge = build_charge(@billing, tier) request_with_retries do charge.save! end # Mark the charge as ready for the merchant to review transition_to_charge_state!(charge_id: charge.id) return PendingCharge.new(charge.confirmation_url) end end |