Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #users.rb controller create method - restful_authentication
- def create
- logout_keeping_session!
- @user = User.new(params[:user])
- @amount = params[:plan][:amount].to_i
- @creditcard = ActiveMerchant::Billing::CreditCard.new(params[:creditcard])
- @creditcard.first_name = params[:creditcard][:firstname]
- @creditcard.last_name = params[:creditcard][:lastname]
- @subscription = Subscription.new
- @subscription = @subscription.process_card(@user, @creditcard, @amount)
- @user.register! if @user && @user.valid?
- success = @user && @user.valid?
- if success && @user.errors.empty?
- redirect_back_or_default('/')
- flash[:notice] = "Thanks for signing up! We're sending you an email with your activation code."
- else
- flash[:error] = "We couldn't set up that account, sorry. Please try again, or contact an admin (link is above)."
- render :action => 'new'
- end
- end
- #subscription.rb model
- # process the subscription - called from users controller on new
- def process_card(user, creditcard, amount)
- # create a new gateway object with credentials from development/production config
- gateway = ActiveMerchant::Billing::AuthorizeNetGateway.new(AUTHNET_CREDENTIALS)
- # has of keys and values required by the gateway to process the request
- options = {
- :billing_address => {
- :first_name => user.firstname,
- :last_name => user.lastname,
- :address1 => user.address,
- :address2 => '',
- :city => user.city,
- :state => user.billing_state,
- :country => 'US',
- :zip => user.zip
- },
- :interval => { :length => 1, :unit => :months },
- :duration => { :start_date => Date.today, :occurrences => 9999 }
- }
- # actually send the request
- response = gateway.recurring(amount, creditcard, options)
- unless response.success?
- # saving responses even in the case of an error for troubleshooting more easily
- self.amount = amount
- self.message = response.message
- self.params = response.params.to_yaml
- self.save!
- error = "Credit card processing failed, try again."
- else
- # save the responses from the transaction in the subscription record
- self.user_id = user.id
- self.amount = amount
- self.message = response.message
- self.reference = response.authorization
- self.test = response.test?
- self.params = response.params.to_yaml
- self.user_id = user.id
- self.save!
- end
- end
- end
Add Comment
Please, Sign In to add comment