Sha256: ee0c143c2289b2f9ba0069b6314d2c4fe93acac74d60c85e45ce895054832519

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

module StripeLocal
  module InstanceDelegation
    # ==this is the primary interface for subscribing.
    #
    # params::
    #     * +card+  (required)           ->    the token returned by stripe.js
    #     * +plan+  (required)           ->    the id of the plan being subscribed to
    #     * +email+  (optional)          ->    the client's email address
    #     * +description+  (optional)    ->    a description to attach to the stripe object for future reference
    #     * +coupon+  (optional)         ->    the id of a coupon if the subscription should be discounted
    #     * +lines+   (optional)         ->    an array of (amount,description) tuples
    # example::
    #    :card => "tok_abc123",
    #    :plan => "MySaaS",
    #    :email => subscriber.email,
    #    :description => "a one year subscription to our flagship service at $99.00 per month"
    #    :lines => [
    #       [ 20000, "a one time setup fee of $200.00 for new members" ]
    #    ]
    def signup params
      plan  = params.delete( :plan )
      lines = params.delete( :lines ) || []

      _customer_ = Stripe::Customer.create( params )

      lines.each do |(amount,desc)|
        _customer_.add_invoice_item({currency: 'usd', amount: amount, description: desc})
      end
      _customer_.update_subscription({ plan: plan })

      StripeLocal::Customer.create _customer_.to_hash.reverse_merge({model_id: self.id})
    end


    def method_missing method, *args, &block
      if customer and customer.respond_to?( method, false )
        customer.send method, *args, &block
      else
        super
      end
    end

    def respond_to_missing? method, include_private = false
      super || customer and customer.respond_to?( method, false )
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stripe_local-0.2.5 lib/stripe_local/instance_delegation.rb
stripe_local-0.2.4 lib/stripe_local/instance_delegation.rb