2011-03-14 52 views
2

我正在寻找一种方式将购买的文件交付给网络应用程序的用户。基本上用户会从我的网站购买一个'产品',在这一点上,他们可以下载他们购买的文件(可能是一个zip文件,我已经预编译)Rails - 付款后提供可下载的文件

我正在使用Rails 2.3.8,支付处理Braintree解决方案。有没有一种标准的方式来实现这一点,使用短代码或什么? Braintree有内置的东西,插件/宝石存在吗?

我真的很只是在正确的方向一推,如何这通常做..

谢谢!

回答

1

你可以做的是每个Product模型have_many核准的用户。当BrainTree向您提供用户为产品付款的确定时,您可以将该用户添加到批准的用户列表中。因此,在您的ProductController中,您检查current_user是否是已批准的用户,如果是这样,请下载该文件,否则重定向它们。

对于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 

或类似的东西。我的语法可能有点偏离,但这应该会让你走。

+0

看起来像一个公平的想法.. – Rabbott 2011-03-14 20:44:59