Class: Payola::CreateSubscription

Inherits:
Object
  • Object
show all
Defined in:
app/services/payola/create_subscription.rb

Class Method Summary collapse

Class Method Details

.call(params, owner = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/payola/create_subscription.rb', line 3

def self.call(params, owner=nil)
  plan = params[:plan]
  affiliate = params[:affiliate]

  if params[:stripe_customer_id].present?
    customer = Stripe::Customer.retrieve(params[:stripe_customer_id])
    email = customer.email
  else
    email = params[:stripeEmail]
  end

  sub = Payola::Subscription.new do |s|
    s.plan = plan
    s.email = email
    s.stripe_token = params[:stripeToken] if plan.amount > 0
    s.affiliate_id = affiliate.try(:id)
    s.currency = plan.respond_to?(:currency) ? plan.currency : Payola.default_currency
    s.coupon = params[:coupon]
    s.signed_custom_fields = params[:signed_custom_fields]
    s.setup_fee = params[:setup_fee]
    s.quantity = params[:quantity]
    s.trial_end = params[:trial_end]
    s.tax_percent = params[:tax_percent]
    s.stripe_customer_id = customer.id if customer

    s.owner = owner
    s.amount = plan.amount
  end
  
  Payola.queue!(Payola::ProcessSubscription, sub.guid) if sub.save

  sub
end