Created
April 21, 2012 19:40
-
-
Save coderforhire/2439259 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Purchase < ActiveRecord::Base | |
| belongs_to :user | |
| # attr_accessible :title, :body | |
| attr_accessible :stripe_card_token, :picture | |
| attr_accessor :picture_file_name | |
| attr_accessor :picture_content_type | |
| attr_accessor :picture_file_size | |
| attr_accessor :picture_updated_at | |
| Paperclip.interpolates :normalized_picture_name do |attachment, style| | |
| attachment.instance.normalized_picture_name | |
| end | |
| def normalized_picture_name | |
| "#{self.email}-#{self.id}-#{self.picture_file_name.gsub(/[^a-zA-Z0-9_\.]/,'_')}" | |
| end | |
| has_attached_file :picture, | |
| :path => ":rails_root/public/system/:attachment/:id/:style/:normalized_picture_name", | |
| :url => "/system/:attachment/:id/:style/:normalized_video_file_name", | |
| :storage => :s3, | |
| :bucket => ENV['S3_BUCKET_NAME'], | |
| :s3_credentials => S3_CREDENTIALS | |
| def save_with_payment | |
| if valid? | |
| customer = Stripe::Customer.create(description: "test", card: stripe_card_token) | |
| self.stripe_customer_token = customer.id | |
| save! | |
| end | |
| end | |
| rescue Stripe::InvalidRequestError => e | |
| logger.error "Stripe error while creating customer: #{e.message}" | |
| errors.add :base, "There was a problem with your credit card." | |
| false | |
| end | |
| lass MyDevise::RegistrationsController < Devise::RegistrationsController | |
| def create | |
| build_resource | |
| if resource.save | |
| if resource.active_for_authentication? | |
| set_flash_message :notice, :signed_up if is_navigational_format? | |
| sign_in(resource_name, resource) | |
| @user = current_user | |
| @purchase = Purchase.new(params[:purchase],:user_id => @user.id ) | |
| @purchase.save_with_payment | |
| respond_with resource, :location => after_sign_up_path_for(resource) | |
| else | |
| set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format? | |
| expire_session_data_after_sign_in! | |
| respond_with resource, :location => after_inactive_sign_up_path_for(resource) | |
| end | |
| else | |
| clean_up_passwords resource | |
| respond_with resource | |
| end | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment