Sha256: 191e3c40619da3820aaa1ca2f0b4095ad579c7eddec5b76bb767fa418f5f5081

Contents?: true

Size: 935 Bytes

Versions: 2

Compression:

Stored size: 935 Bytes

Contents

module Tang
  class UpdateCustomer
    def self.call(customer)
      return customer if !customer.valid?

      if customer.stripe_id.present?
        begin
          c = Stripe::Customer.retrieve(customer.stripe_id)
          c = populate_customer(c, customer)
          c.save
        rescue Stripe::StripeError => e
          customer.errors.add(:base, :invalid, message: e.message)
        end
      end

      return customer
    end

    def self.populate_customer(stripe_customer, customer)
      stripe_customer.email = customer.email
      stripe_customer.account_balance = customer.account_balance if customer.account_balance.present?
      stripe_customer.business_vat_id = customer.business_vat_id if customer.business_vat_id.present?
      stripe_customer.description = customer.description
      stripe_customer.coupon = customer.coupon.stripe_id if customer.coupon.present?
      return stripe_customer
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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