Class: Payola::StartSubscription
- Inherits:
-
Object
- Object
- Payola::StartSubscription
- Defined in:
- app/services/payola/start_subscription.rb
Instance Attribute Summary collapse
-
#secret_key ⇒ Object
readonly
Returns the value of attribute secret_key.
-
#subscription ⇒ Object
readonly
Returns the value of attribute subscription.
Class Method Summary collapse
Instance Method Summary collapse
- #find_or_create_customer ⇒ Object
-
#initialize(subscription, secret_key) ⇒ StartSubscription
constructor
A new instance of StartSubscription.
- #run ⇒ Object
Constructor Details
#initialize(subscription, secret_key) ⇒ StartSubscription
Returns a new instance of StartSubscription.
12 13 14 15 |
# File 'app/services/payola/start_subscription.rb', line 12 def initialize(subscription, secret_key) @subscription = subscription @secret_key = secret_key end |
Instance Attribute Details
#secret_key ⇒ Object (readonly)
Returns the value of attribute secret_key.
3 4 5 |
# File 'app/services/payola/start_subscription.rb', line 3 def secret_key @secret_key end |
#subscription ⇒ Object (readonly)
Returns the value of attribute subscription.
3 4 5 |
# File 'app/services/payola/start_subscription.rb', line 3 def subscription @subscription end |
Class Method Details
.call(subscription) ⇒ Object
5 6 7 8 9 10 |
# File 'app/services/payola/start_subscription.rb', line 5 def self.call(subscription) subscription.save! secret_key = Payola.secret_key_for_sale(subscription) new(subscription, secret_key).run end |
Instance Method Details
#find_or_create_customer ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/services/payola/start_subscription.rb', line 56 def find_or_create_customer subs = Subscription.where(owner: subscription.owner).where("state in ('active', 'canceled')") if subscription.owner if subs && subs.length >= 1 first_sub = subs.first customer_id = first_sub.stripe_customer_id unless customer_id.blank? customer = Stripe::Customer.retrieve(customer_id, secret_key) return customer unless customer.try(:deleted) end end customer_create_params = { source: subscription.stripe_token, email: subscription.email } customer = Stripe::Customer.create(customer_create_params, secret_key) if subscription.setup_fee.present? plan = subscription.plan description = plan.try(:setup_fee_description, subscription) || 'Setup Fee' Stripe::InvoiceItem.create({ customer: customer.id, amount: subscription.setup_fee, currency: subscription.currency, description: description }, secret_key) end customer end |
#run ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/services/payola/start_subscription.rb', line 17 def run begin subscription.verify_charge! customer = find_or_create_customer create_params = { plan: subscription.plan.stripe_id, quantity: subscription.quantity } create_params[:coupon] = subscription.coupon if subscription.coupon.present? stripe_sub = customer.subscriptions.create(create_params) card = customer.sources.data.first subscription.update_attributes( stripe_id: stripe_sub.id, stripe_customer_id: customer.id, card_last4: card.last4, card_expiration: Date.new(card.exp_year, card.exp_month, 1), card_type: card.respond_to?(:brand) ? card.brand : card.type, current_period_start: Time.at(stripe_sub.current_period_start), current_period_end: Time.at(stripe_sub.current_period_end), ended_at: stripe_sub.ended_at ? Time.at(stripe_sub.ended_at) : nil, trial_start: stripe_sub.trial_start ? Time.at(stripe_sub.trial_start) : nil, trial_end: stripe_sub.trial_end ? Time.at(stripe_sub.trial_end) : nil, canceled_at: stripe_sub.canceled_at ? Time.at(stripe_sub.canceled_at) : nil, quantity: stripe_sub.quantity, stripe_status: stripe_sub.status, cancel_at_period_end: stripe_sub.cancel_at_period_end ) subscription.activate! rescue Stripe::StripeError, RuntimeError => e subscription.update_attributes(error: e.) subscription.fail! end subscription end |