Search code examples
ruby-on-railsdownloadpayment-processingbraintree

Rails - Delivering downloadable files after payment


I am looking for a way to deliver purchased files to users of a web app. Basically a user will purchase a 'product' from my site, at which point they can download the files they purchased (will likely be a zip file that I have precompiled)

I am using Rails 2.3.8, and have the payment processing working the Braintree Solutions. Is there a standard way of acheiving this, using short codes, or something? Does Braintree have something built in, does a plugin/gem exists?

I'm really looking for just a push in the right direction as to how this is typically done..

Thanks!


Solution

  • What you could do is have each Product model have_many approved users. When BrainTree gives you the OK that a user paid for a product, you can add that user to the approved users list. So in your ProductController you check if the current_user is a approved user, if so download the file, else redirect them.

    For Exsample:

    product.rb

    class Product < ActiveRecord::Model
      has_many approved_users, :class => User
    end
    

    product_controller.rb

    class ProductController 
      def download
        @product = Product.find_by_id(:id)
        if @product.approved_users.includes?(current_user)
          # Give them the file
        else
          flash[:notice] = "You must buy the product first!"
          redirect_to product_sales_url(@product)
        end
      end
    end 
    

    Or something like that. My syntax might be a little off, but this should get you going.