-1

我正在使用 Rails,需要向我的用户汇款。

这是我的功能:

    def self.send_money(to_email, how_much_in_cents, options = {})
credentials = {
  "USER" => API_USERNAME,
  "PWD" => API_PASSWORD,
  "SIGNATURE" => API_SIGNATURE,
}

params = {
  "METHOD" => "MassPay",
  "CURRENCYCODE" => "USD",
  "RECEIVERTYPE" => "EmailAddress",
  "L_EMAIL0" => to_email,
  "L_AMT0" => ((how_much_in_cents.to_i)/100.to_f).to_s,
  "VERSION" => "51.0"
}

endpoint = RAILS_ENV == 'production' ? "https://api-3t.paypal.com" : "https://api-3t.sandbox.paypal.com"
url = URI.parse(endpoint)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
all_params = credentials.merge(params)
stringified_params = all_params.collect { |tuple| "#{tuple.first}=#{CGI.escape(tuple.last)}" }.join("&")

response = http.post("/nvp", stringified_params)
 end

我应该放入一些帮助文件吗?

我想在我的视图中使用它,例如:

 <%@merchants each do%>
   <%@merchant(current_user.email, 100)%>
 <%end%>

或者只是在控制器中传输用户数组。

我应该把这个功能放在哪里使它正确?

4

1 回答 1

2

您应该在您的控制器中分配并在您的助手中@merchants创建一个函数,如下所示:show_merchant

def show_mercant(merchant, email, value)
  ...
end

并像这样在您的视图中使用它:

<% @merchants.each do |merchant| %>
  <%= show_merchant(merchant, current_user.email, 100) %>
<% end %>

我认为您的功能send_money与这里无关,对吧?

于 2012-07-16T18:47:58.400 回答