Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements and updates from Bullet Train #194

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions app/concerns/koudoku/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,21 @@ def processing!

begin
raise Koudoku::NilCardToken, "No card token received. Check for JavaScript errors breaking Stripe.js on the previous page." unless credit_card_token.present?

customer_attributes = {
description: subscription_owner_description,
email: subscription_owner_email,
card: credit_card_token # obtained with Stripe.js
card: credit_card_token, # obtained with Stripe.js
metadata: subscription_owner_metadata
}

# If the class we're being included in supports Rewardful ..
if respond_to? :rewardful_id
if rewardful_id.present?
customer_attributes[:metadata] = {referral: rewardful_id}
end
end

# If the class we're being included in supports coupons ..
if respond_to? :coupon
if coupon.present? and coupon.free_trial?
Expand All @@ -86,12 +95,31 @@ def processing!
customer = Stripe::Customer.create(customer_attributes)

finalize_new_customer!(customer.id, plan.price)
customer.update_subscription(:plan => self.plan.stripe_id, :prorate => Koudoku.prorate)

subscription_attributes = {
customer: customer.id,
items:[
{
plan: self.plan.stripe_id,
quantity: subscription_owner_quantity
}
],
trial_from_plan: true
}

# If the class we're being included in supports Link Mink ..
if respond_to? :link_mink_id
if link_mink_id.present?
subscription_attributes[:metadata] = {identifier: link_mink_id}
end
end

Stripe::Subscription.create(subscription_attributes)

rescue Stripe::CardError => card_error
errors[:base] << card_error.message
card_was_declined
return false
throw :abort
end

# store the customer id.
Expand Down Expand Up @@ -176,11 +204,19 @@ def subscription_owner=(owner)
def subscription_owner_description
# assuming owner responds to name.
# we should check for whether it responds to this or not.
"#{subscription_owner.try(:name) || subscription_owner.try(:id)}"
"#{subscription_owner.try(:billing_name) || subscription_owner.try(:name) || subscription_owner.try(:id)}"
end

def subscription_owner_email
"#{subscription_owner.try(:email)}"
"#{subscription_owner.try(:formatted_email_address) || subscription_owner.try(:email)}"
end

def subscription_owner_metadata
subscription_owner.try(:stripe_metadata) || {}
end

def subscription_owner_quantity
subscription_owner.try(:subscription_quantity) || 1
end

def changing_plans?
Expand Down
30 changes: 21 additions & 9 deletions app/controllers/koudoku/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,22 @@ def show_existing_subscription

def create

@subscription = ::Subscription.new(subscription_params)
all_subscription_params = subscription_params.to_hash
all_subscription_params[:rewardful_id] = params[:referral] if params[:referral]

@subscription = ::Subscription.new(all_subscription_params)
@subscription.subscription_owner = @owner
@subscription.coupon_code = session[:koudoku_coupon_code]

if @subscription.save
flash[:notice] = after_new_subscription_message
redirect_to after_new_subscription_path
else
flash[:error] = I18n.t('koudoku.failure.problem_processing_transaction')

## commenting this out because the model callbacks are actually
## providing the error message we need here from stripe.
# flash[:error] = I18n.t('koudoku.failure.problem_processing_transaction')

render :new
end
end
Expand All @@ -152,12 +159,17 @@ def edit
end

def update
if @subscription.update_attributes(subscription_params)
flash[:notice] = I18n.t('koudoku.confirmations.subscription_updated')
redirect_to owner_subscription_path(@owner, @subscription)
else
flash[:error] = I18n.t('koudoku.failure.problem_processing_transaction')
render :edit
begin
if @subscription.update_attributes(subscription_params)
flash[:notice] = I18n.t('koudoku.confirmations.subscription_updated')
redirect_to owner_subscription_path(@owner, @subscription)
else
flash[:error] = I18n.t('koudoku.failure.problem_processing_transaction')
render :edit
end
rescue Exception => e
flash[:error] = I18n.t('koudoku.failure.plan_change_needs_updated_card', :error => e.message)
redirect_to edit_owner_subscription_path(@owner, @subscription, update: 'card')
end
end

Expand All @@ -166,7 +178,7 @@ def subscription_params

# If strong_parameters is around, use that.
if defined?(ActionController::StrongParameters)
params.require(:subscription).permit(:plan_id, :stripe_id, :current_price, :credit_card_token, :card_type, :last_four)
params.require(:subscription).permit(:plan_id, :stripe_id, :current_price, :credit_card_token, :card_type, :last_four, :link_mink_id)
else
# Otherwise, let's hope they're using attr_accessible to protect their models!
params[:subscription]
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ en:
subscription_upgraded: "You've been successfully upgraded."
failure:
problem_processing_transaction: "There was a problem processing this transaction."
plan_change_needs_updated_card: "Our payment provider returned the following message: \"%{error}\" Please update your payment information before trying to change plans."
unauthorized: "Unauthorized"
plan_intervals:
month: "month"
Expand Down