Guest User

Untitled

a guest
May 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. #users.rb controller create method - restful_authentication
  2.  
  3. def create
  4. logout_keeping_session!
  5. @user = User.new(params[:user])
  6. @amount = params[:plan][:amount].to_i
  7. @creditcard = ActiveMerchant::Billing::CreditCard.new(params[:creditcard])
  8. @creditcard.first_name = params[:creditcard][:firstname]
  9. @creditcard.last_name = params[:creditcard][:lastname]
  10. @subscription = Subscription.new
  11. @subscription = @subscription.process_card(@user, @creditcard, @amount)
  12. @user.register! if @user && @user.valid?
  13. success = @user && @user.valid?
  14. if success && @user.errors.empty?
  15. redirect_back_or_default('/')
  16. flash[:notice] = "Thanks for signing up! We're sending you an email with your activation code."
  17. else
  18. flash[:error] = "We couldn't set up that account, sorry. Please try again, or contact an admin (link is above)."
  19. render :action => 'new'
  20. end
  21. end
  22.  
  23. #subscription.rb model
  24.  
  25. # process the subscription - called from users controller on new
  26. def process_card(user, creditcard, amount)
  27. # create a new gateway object with credentials from development/production config
  28. gateway = ActiveMerchant::Billing::AuthorizeNetGateway.new(AUTHNET_CREDENTIALS)
  29. # has of keys and values required by the gateway to process the request
  30. options = {
  31. :billing_address => {
  32. :first_name => user.firstname,
  33. :last_name => user.lastname,
  34. :address1 => user.address,
  35. :address2 => '',
  36. :city => user.city,
  37. :state => user.billing_state,
  38. :country => 'US',
  39. :zip => user.zip
  40. },
  41. :interval => { :length => 1, :unit => :months },
  42. :duration => { :start_date => Date.today, :occurrences => 9999 }
  43. }
  44.  
  45. # actually send the request
  46. response = gateway.recurring(amount, creditcard, options)
  47.  
  48. unless response.success?
  49. # saving responses even in the case of an error for troubleshooting more easily
  50. self.amount = amount
  51. self.message = response.message
  52. self.params = response.params.to_yaml
  53. self.save!
  54. error = "Credit card processing failed, try again."
  55. else
  56. # save the responses from the transaction in the subscription record
  57. self.user_id = user.id
  58. self.amount = amount
  59. self.message = response.message
  60. self.reference = response.authorization
  61. self.test = response.test?
  62. self.params = response.params.to_yaml
  63. self.user_id = user.id
  64. self.save!
  65. end
  66. end
  67. end
Add Comment
Please, Sign In to add comment