Created
February 25, 2010 06:32
-
-
Save rjungemann/314297 to your computer and use it in GitHub Desktop.
Ruby PayPal example
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
require 'sinatra/base' | |
module PaypalSample | |
class App < Sinatra::Base | |
get "/" do | |
@business_name = "" | |
@login = "[email protected]" | |
@amount = "1.00" | |
@return_url = "" | |
@cancel_url = "" | |
@currency_code = "USD" | |
@tax = nil | |
@header_image = "" # 750 px wide, 90px tall | |
%{<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="POST"> | |
<p> | |
<b>business:</b> #{@business_name}<br/> | |
<b>amount:</b> #{@amount}<br/> | |
</p> | |
<input type="hidden" name="business" value="#{@login}"> | |
<input type="hidden" name="amount" value="#{@amount}"> | |
<!--<input type="hidden" name="cmd" value="_ext-enter">--> | |
<input type="hidden" name="cmd" value="_notify-validate"> | |
<input type="hidden" name="redirect_cmd" value="_xclick"> | |
<input type="hidden" name="return" value="#{@return_url}"> | |
<input type="hidden" name="cancel_return" value="#{@cancel_url}"> | |
<input type="hidden" name="currency_code" value="#{@currency_code}"> | |
<input type="hidden" name="cpp_header_image" value="#{@header_image}"> | |
#{%{<input type="hidden" name="cart_tax" value="#{@tax}">} unless @tax.nil?} | |
<input type="submit"> | |
</form>} | |
end | |
get("/receive_ipn") { recieve_ipn } | |
post("/receive_ipn") { recieve_ipn } | |
private | |
def cart_item name, amount, tax = nil | |
@i ||= 0 | |
str = %{<input type="hidden" name="item_#{@i}" value="#{name}">} + | |
%{<input type="hidden" name="amount_#{@i}" value="#{amount}">} | |
str += %{<input type="hidden" name="tax_#{@i}" value="#{tax}">} unless tax.nil? | |
end | |
def receive_ipn | |
verify_ipn params[:raw_post] | |
end | |
def verify_ipn raw | |
uri = URI.parse("http://www.sandbox.paypal.com/cgi-bin/webscr") | |
status = nil | |
Net::HTTP.start(uri.host, uri.port) do |request| | |
status = request.post(uri.path, @raw + "&cmd=_notify-validate").body | |
end | |
puts "status = '#{status}'" | |
status == "VERIFIED" | |
end | |
end | |
end | |
run PaypalSample::App.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment