Sha256: 34c3621e897f9c1647a6656b56443518eddebb1462ad07b4973f9bb8d64b3050

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

module Tang
  class PayInvoice
    def self.call(stripe_invoice)

      # ensure the subscription exists
      stripe_subscription = Stripe::Subscription.retrieve(stripe_invoice.subscription)
      subscription = Subscription.find_by(stripe_id: stripe_invoice.subscription)
      if subscription.nil?
        subscription = Subscription.from_stripe(stripe_subscription)
      elsif stripe_subscription.discount.nil?
        # update discount
        subscription.update(coupon: nil, coupon_start: nil)
      end

      # create the invoice
      invoice = Invoice.from_stripe(stripe_invoice)

      # create charge
      charge = nil
      if stripe_invoice.charge.present?
        stripe_charge = Stripe::Charge.retrieve(stripe_invoice.charge)
        charge = Charge.from_stripe(stripe_charge, invoice)
      end

      # update customer active until
      if subscription.present?
        customer = subscription.customer
        customer.update_subscription_end(subscription)
      end

      return charge
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tang-0.2.2 app/services/tang/pay_invoice.rb
tang-0.2.1 app/services/tang/pay_invoice.rb