我正在使用 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%>
或者只是在控制器中传输用户数组。
我应该把这个功能放在哪里使它正确?