Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Custom Pricing via URL Parameter

  • Loading branch information...
commit 6cbcf1e5f116fef06403153bbf13ed41b1045ca2 1 parent e7e3ed8
@zhendi authored
View
32 app/controllers/saasy_simple/subscriptions_controller.rb
@@ -11,11 +11,12 @@ def activate
def billing
return unless current_user
+ price = cal_price(current_user)
if current_user.status == 'active'
subscription = SaasySimple.subscription(current_user)
- redirect_to subscription['customerUrl']
+ redirect_to subscription['customerUrl']+"&tags=total=#{price*100}"
else
- redirect_to SaasySimple.signup(current_user)
+ redirect_to SaasySimple.signup(current_user)+"&tags=total=#{price*100}"
end
end
@@ -32,5 +33,32 @@ def deactivate
SaasySimple.config.model.deactivate( params['token'], params['id'] )
end
end
+
+ private
+
+ def cal_price(user)
+ al = []
+ site_price = 5
+ user_price = 2.5
+ gateway_price = 10
+ device_price = 4.5
+ message_price = 0.03
+
+ user_count = User.where(:created_by=>current_user.id).count + 1
+ gateway_count, device_count, message_count, site_count = 0, 0, 0, 0
+ UserGateway.where(:user_id=>user.id).each do |ug|
+ site = ug["zone"].blank? ? ug["mac"] : ug["zone"]
+ next if al.include?(site)
+ site_count += 1
+ al << site
+ gateway_count += 1
+ registered_gateway = RegisteredGateway.where(:mac=>ug['mac']).first
+ latest_timestamp = registered_gateway.devices.where(:timestamp.exists=>true).order_by([[:timestamp, -1]]).limit(1).first.timestamp rescue nil
+ latest_timestamp = Time.parse(latest_timestamp).advance(:minutes => -5).strftime("%Y%m%d%H%M%S") if latest_timestamp
+ device_count += registered_gateway.devices.where(:timestamp.gte => latest_timestamp).count
+ message_count += Message.where(:mac=>ug['mac']).count
+ end
+ site_count*site_price + user_count*user_price + gateway_count*gateway_price + device_count*device_price + message_count*message_price
+ end
end
end
View
16 lib/SaasySimple.rb
@@ -35,22 +35,6 @@ def self.subscription(user)
end
s
end
-
- def update_subscription(subscription_update)
- url = subscription_url(subscription_update.reference)
- @auth = { :username => SaasySimple.config.username, :password => SaasySimple.config.password }
- options = { :headers => { 'Content-Type' => 'application/xml' }, :body => subscription_update.to_xml, :basic_auth => @auth }
- response = HTTParty.put(url, options)
-
- if response.code == 200
- sub = parse_subscription(response.parsed_response.fetch('subscription'))
- else
- exception = FsprgException.new(response.code, nil)
- raise exception, "An error occurred calling the FastSpring subscription service", caller
- end
-
- sub
- end
def self.cancel(user)
s = {}
Please sign in to comment.
Something went wrong with that request. Please try again.