Sha256: 1b2b58b3051f1e0b2d7c219653043410f7b8f529d2fbcba073d73a9a0ac99dd1

Contents?: true

Size: 1.76 KB

Versions: 32

Compression:

Stored size: 1.76 KB

Contents

module EffectiveStripeHelper

  def stripe_plan_description(obj)
    plan = (
      case obj
      when Hash            ; obj
      when ::Stripe::Plan  ; EffectiveOrders.stripe_plans.find { |plan| plan.id == obj.id }
      else                 ; raise 'unexpected object'
      end
    )

    raise("unknown stripe plan: #{obj}") unless plan.kind_of?(Hash) && plan[:id].present?

    plan[:description]
  end

  def stripe_invoice_line_description(line, simple: false)
    [
      "#{line.quantity}x",
      line.plan.name,
      price_to_currency(line.amount),
      ("#{Time.zone.at(line.period.start).strftime('%F')}" unless simple),
      ('to' unless simple),
      ("#{Time.zone.at(line.period.end).strftime('%F')}" unless simple),
      line.description.presence
    ].compact.join(' ')
  end

  def stripe_coupon_description(coupon)
    amount = coupon.amount_off.present? ? price_to_currency(coupon.amount_off) : "#{coupon.percent_off}%"

    if coupon.duration_in_months.present?
      "#{coupon.id} - #{amount} off for #{coupon.duration_in_months} months"
    else
      "#{coupon.id} - #{amount} off #{coupon.duration}"
    end
  end

  def stripe_site_image_url
    return nil unless EffectiveOrders.stripe? && (url = EffectiveOrders.stripe[:site_image].to_s).present?
    url.start_with?('http') ? url : asset_url(url)
  end

  def stripe_order_description(order)
    "#{order.num_items} items (#{price_to_currency(order.total)})"
  end

  def stripe_charge_data(order)
    {
      stripe: {
        key: EffectiveOrders.stripe[:publishable_key],
        name: EffectiveOrders.stripe[:site_title],
        image: stripe_site_image_url,
        email: order.user.email,
        amount: order.total,
        description: stripe_order_description(order)
      }.to_json
    }
  end

end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
effective_orders-4.1.5 app/helpers/effective_stripe_helper.rb
effective_orders-4.1.4 app/helpers/effective_stripe_helper.rb
effective_orders-4.1.3 app/helpers/effective_stripe_helper.rb
effective_orders-4.1.2 app/helpers/effective_stripe_helper.rb
effective_orders-4.1.1 app/helpers/effective_stripe_helper.rb
effective_orders-4.1.0 app/helpers/effective_stripe_helper.rb
effective_orders-4.0.6 app/helpers/effective_stripe_helper.rb
effective_orders-4.0.5 app/helpers/effective_stripe_helper.rb
effective_orders-4.0.4 app/helpers/effective_stripe_helper.rb
effective_orders-4.0.3 app/helpers/effective_stripe_helper.rb
effective_orders-4.0.2 app/helpers/effective_stripe_helper.rb
effective_orders-4.0.1 app/helpers/effective_stripe_helper.rb
effective_orders-4.0.0 app/helpers/effective_stripe_helper.rb
effective_orders-4.0.0beta19 app/helpers/effective_stripe_helper.rb
effective_orders-4.0.0beta18 app/helpers/effective_stripe_helper.rb
effective_orders-4.0.0beta17 app/helpers/effective_stripe_helper.rb
effective_orders-4.0.0beta16 app/helpers/effective_stripe_helper.rb
effective_orders-4.0.0beta15 app/helpers/effective_stripe_helper.rb
effective_orders-4.0.0beta14 app/helpers/effective_stripe_helper.rb
effective_orders-4.0.0beta13 app/helpers/effective_stripe_helper.rb