Skip to content

Commit

Permalink
Fixed Stripe subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
lumandra committed Jan 20, 2020
1 parent 6f845a7 commit fbcf02f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
7 changes: 6 additions & 1 deletion app/assets/javascripts/active_admin/subscription.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ $ ->

if $(e.target).val() == 'BeachApiCore::Organisation'
$('#owner_user_input').parent().hide()
$('#owner_organisation_input').parent().show()
$('#owner_organisation_input').parent().show()

$('#subscription_cvc').on 'keyup', (e)->
if $(e.target).val().length>3
$(e.target).val($(e.target).val().slice(0,-1))
e.preventDefault()
36 changes: 33 additions & 3 deletions app/models/beach_api_core/subscription.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module BeachApiCore
class Subscription < ApplicationRecord
attr_accessor :cvc, :exp_year, :exp_month, :number
belongs_to :plan, class_name: "BeachApiCore::Plan"
belongs_to :owner, polymorphic: true

Expand All @@ -14,9 +15,10 @@ class Subscription < ApplicationRecord
Stripe.api_key = ENV['STRIPE_SECRET_KEY']

def create_subscription
self.errors.add :subscription, "can't be created because you have active subscription" unless self.owner.subscription.nil?
self.errors.add :subscription, "can't be created because you have active subscription" unless self.owner.nil? || self.owner.subscription.nil?
return unless errors.blank?
if self.owner.nil? || self.owner.stripe_customer_token.nil?
create_customer if self.owner.stripe_customer_token.blank?
if self.owner.nil? || self.owner.stripe_customer_token.blank?
self.errors.add :owner, 'wrong subscription owner'
else
begin
Expand Down Expand Up @@ -90,9 +92,37 @@ def get_quantity
quantity
end

def create_customer
Stripe.api_key = ENV['STRIPE_SECRET_KEY']
client = self.owner
unless client.nil?
card_token = Stripe::Token.create(
{
card: {
number: number,
exp_month: exp_month,
exp_year: exp_year,
cvc: cvc
}
}
)
customer = Stripe::Customer.create(email: client.email, card: card_token.id)
client.update_attribute(:stripe_customer_token, customer.id)
end
rescue Stripe::CardError => e
render_json_error({:message => "Wrong card"})
end

private
def check_subscription_for
self.errors.add :plan, "wrong for indicated type" unless self.owner_type.gsub("BeachApiCore::",'').downcase == self.plan.plan_for
if self.plan.plan_for == 0
type = "organisation"
elsif self.plan.plan_for == 1
type = "user"
end
self_plan = self.owner_type.gsub("BeachApiCore::",'').downcase

self.errors.add :plan, "wrong for indicated type" unless self_plan == self.plan.plan_for || self_plan == type
end
end

Expand Down
2 changes: 1 addition & 1 deletion config/initializers/stripe.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StripeEvent.signing_secret = "whsec_DdKmsdowdYllbFOcLOqhmmHNFmuT1bgG" # e.g. whsec_...
StripeEvent.signing_secret = ENV['Stripe_Webhook_Key'] # e.g. whsec_...

StripeEvent.configure do |events|
events.all Stripe::InvoiceEventHandler.new
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ChangeSubscriptionOwnerIdToEmail < ActiveRecord::Migration[5.1]
def change
rename_column :beach_api_core_organisations, :subscription_owner_id, :email
end
end
2 changes: 1 addition & 1 deletion lib/admin/organisations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
div { image_tag attachment_url(f.object.logo_image, :file, :fill, 150, 150) }
end
end
f.input :email
f.input :email, as: :string
f.fields_for :organisation_plan do |p|
p.input :plan, as: :select, collection: BeachApiCore::Plan.all
end
Expand Down
8 changes: 7 additions & 1 deletion lib/admin/subscriptions.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
ActiveAdmin.register BeachApiCore::Subscription, as: 'Subscription' do
menu priority: 66, parent: 'Services'

permit_params :plan_id, :owner_type, :owner_id, :user_id, :organisation_id
permit_params :plan_id, :owner_type, :owner_id, :user_id, :organisation_id, :cvc, :exp_year, :exp_month, :number

form do |f|

f.inputs do
#f.semantic_errors *f.object.errors.keys

f.input :plan

f.input :number, as: :number
f.input :exp_month, as: :number
f.input :exp_year, as: :number
f.input :cvc, as: :number

if f.object.new_record?
f.input :owner_type,
as: :select,
Expand Down

0 comments on commit fbcf02f

Please sign in to comment.