Class: Wbase::StartSubscription
- Inherits:
-
Object
- Object
- Wbase::StartSubscription
- Defined in:
- app/services/wbase/start_subscription.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#subscription ⇒ Object
readonly
Returns the value of attribute subscription.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #build_subscription ⇒ Object
- #call ⇒ Object
- #create_stripe_customer ⇒ Object
-
#initialize(user, params) ⇒ StartSubscription
constructor
A new instance of StartSubscription.
- #valid? ⇒ Boolean
Constructor Details
#initialize(user, params) ⇒ StartSubscription
Returns a new instance of StartSubscription.
5 6 7 8 9 10 |
# File 'app/services/wbase/start_subscription.rb', line 5 def initialize(user, params) @params = params @user = user @subscription = build_subscription @errors = [] end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
3 4 5 |
# File 'app/services/wbase/start_subscription.rb', line 3 def errors @errors end |
#subscription ⇒ Object (readonly)
Returns the value of attribute subscription.
3 4 5 |
# File 'app/services/wbase/start_subscription.rb', line 3 def subscription @subscription end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
3 4 5 |
# File 'app/services/wbase/start_subscription.rb', line 3 def user @user end |
Instance Method Details
#build_subscription ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/services/wbase/start_subscription.rb', line 27 def build_subscription Rails.logger.info "Building new subscription for #{ user.email }" Wbase::Subscription.new(@params) do |s| s.user = user s.current_price = s.plan.price if s.plan.monthly? s.paid_thru = 30.days.from_now else s.paid_thru = 12.months.from_now end end end |
#call ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'app/services/wbase/start_subscription.rb', line 12 def call customer = create_stripe_customer if customer && customer.id @subscription.stripe_id = customer.id @subscription.user = user @subscription.save! else @errors << 'Invalid card' end end |
#create_stripe_customer ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 |
# File 'app/services/wbase/start_subscription.rb', line 40 def create_stripe_customer Rails.logger.info "Creating Stripe Customer for #{ user.email }" begin customer = Stripe::Customer.create( :source => subscription.credit_card_token, :plan => subscription.plan.stripe_id, :email => user.email ) Rails.logger.info customer rescue Stripe::CardError => e # Since it's a decline, Stripe::CardError will be caught body = e.json_body err = body[:error] @errors << body[:error] puts "Status is: #{e.http_status}" puts "Type is: #{err[:type]}" puts "Code is: #{err[:code]}" # param is '' in this case puts "Param is: #{err[:param]}" puts "Message is: #{err[:message]}" rescue Stripe::RateLimitError => e # Too many requests made to the API too quickly Rails.logger.error e rescue Stripe::InvalidRequestError => e # Invalid parameters were supplied to Stripe's API Rails.logger.error e rescue Stripe::AuthenticationError => e # Authentication with Stripe's API failed # (maybe you changed API keys recently) Rails.logger.error e rescue Stripe::APIConnectionError => e # Network communication with Stripe failed Rails.logger.error e rescue Stripe::StripeError => e # Display a very generic error to the user, and maybe send # yourself an email Rails.logger.error e rescue => e # Something else happened, completely unrelated to Stripe Rails.logger.error e end customer end |
#valid? ⇒ Boolean
23 24 25 |
# File 'app/services/wbase/start_subscription.rb', line 23 def valid? errors.empty? end |