Permalink
Browse files

added `authorize` method to the ruby client and fleshed a full fledge…

…d simple marketplace app
  • Loading branch information...
1 parent 8a0a30e commit 5b2eb5c4004b042c79a37673a688effedefd7a1f @mahmoudimus mahmoudimus committed Nov 18, 2011
Showing with 10,815 additions and 74 deletions.
  1. +2 −0 .gitignore
  2. +1 −1 examples/simple_application/.rvmrc
  3. +152 −48 examples/simple_application/application.rb
  4. +14 −5 examples/simple_application/config.rb
  5. +16 −2 examples/simple_application/config.ru
  6. +125 −14 examples/simple_application/index.html.erb
  7. +13 −0 examples/simple_application/static/css/simplemp.css
  8. BIN examples/simple_application/static/css/ui-lightness/images/ui-bg_diagonals-thick_18_b81900_40x40.png
  9. BIN examples/simple_application/static/css/ui-lightness/images/ui-bg_diagonals-thick_20_666666_40x40.png
  10. BIN examples/simple_application/static/css/ui-lightness/images/ui-bg_flat_10_000000_40x100.png
  11. BIN examples/simple_application/static/css/ui-lightness/images/ui-bg_glass_100_f6f6f6_1x400.png
  12. BIN examples/simple_application/static/css/ui-lightness/images/ui-bg_glass_100_fdf5ce_1x400.png
  13. BIN examples/simple_application/static/css/ui-lightness/images/ui-bg_glass_65_ffffff_1x400.png
  14. BIN examples/simple_application/static/css/ui-lightness/images/ui-bg_gloss-wave_35_f6a828_500x100.png
  15. BIN examples/simple_application/static/css/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png
  16. BIN examples/simple_application/static/css/ui-lightness/images/ui-bg_highlight-soft_75_ffe45c_1x100.png
  17. BIN examples/simple_application/static/css/ui-lightness/images/ui-icons_222222_256x240.png
  18. BIN examples/simple_application/static/css/ui-lightness/images/ui-icons_228ef1_256x240.png
  19. BIN examples/simple_application/static/css/ui-lightness/images/ui-icons_ef8c08_256x240.png
  20. BIN examples/simple_application/static/css/ui-lightness/images/ui-icons_ffd27a_256x240.png
  21. BIN examples/simple_application/static/css/ui-lightness/images/ui-icons_ffffff_256x240.png
  22. +578 −0 examples/simple_application/static/css/ui-lightness/jquery-ui-1.8.13.custom.css
  23. BIN examples/simple_application/static/favicon.ico
  24. +8,936 −0 examples/simple_application/static/js/jquery-1.6.1.js
  25. +784 −0 examples/simple_application/static/js/jquery-ui-1.8.13.custom.min.js
  26. +177 −0 examples/simple_application/static/js/simplemp.js
  27. +17 −4 lib/poundpay/elements.rb
View
@@ -2,3 +2,5 @@ pkg/
Gemfile.lock
*.gem
.project
+.idea
+.bundle
@@ -1,3 +1,3 @@
# echo 'Switching rvm gemset ...'
-rvm use --create --install 1.9.1@poundpay-simple_application >& /dev/null
+rvm use --create --install 1.9.2@poundpay-simple_application >& /dev/null
# rvm gemset import >& /dev/null
@@ -1,65 +1,169 @@
-require 'uri'
-require 'net/http'
-require 'net/https'
-require 'json'
-require 'erb'
+require 'pp'
+require 'poundpay'
-require 'config'
+require './config'
+class SimpleController
+ attr_reader :poundpay_client
-class PoundPay
- attr_reader :api_url, :version, :sid, :token
+ def initialize
+ if Poundpay.configured?
+ return
+ end
+ config = SimpleApplication::CONFIG[:poundpay]
+ puts config
+ Poundpay.configure_from_hash(config)
+ end
- def initialize(api_url, version, sid, token)
- @api_url = api_url
- @version = version
- @sid = sid
- @token = token
+ def return_404
+ response = Rack::Response.new(["Page Not Found"], 404, {"Content-Type" => "text/plain"})
+ response.finish
end
- def post(endpoint, params)
- request_url = "#{@api_url}/#{@version}/#{endpoint}"
- puts request_url, params
- uri = URI.parse request_url
- http = Net::HTTP.new uri.host, uri.port
- http.use_ssl = true
- req = Net::HTTP::Post.new uri.request_uri
- req.set_form_data params
- req.basic_auth @sid, @token
- response = http.request req
- JSON.load response.body
+end
+
+class Index < SimpleController
+
+ def call env
+ request = Rack::Request.new(env)
+ unless request.path == '/' and request.get?
+ return return_404
+ end
+ # Create payment request
+ payment = SimpleApplication::CONFIG[:default_payment]
+ # Render and return page
+ www_poundpay_url = Poundpay.www_url
+ template = ERB.new(open("index.html.erb").read)
+ page = template.result(binding)
+ response = Rack::Response.new([page], 200, {"Content-Type" => "text/html"})
+ response.finish
end
+
end
+class Payment < SimpleController
-class SimpleApplication
- attr_reader :poundpay_client
+ def call env
+ request = Rack::Request.new(env)
+ return_value = case request.path.gsub(/\/$/, '') # trim trailing /
+ when '/payment' then request.post? ? create(request) : nil
+ when '/payment/release' then request.post? ? release(request) : nil
+ when '/payment/authorize' then request.post? ? authorize(request) : nil
+ when '/payment/cancel' then request.post? ? cancel(request) : nil
+ when '/payment/escrow' then request.post? ? escrow(request) : nil
+ else nil
+ end
- def initialize
- config = SimpleApplication::CONFIG[:poundpay]
- @poundpay_client = PoundPay.new(config[:api_url], config[:version], config[:sid], config[:token])
+ if return_value
+ response = Rack::Response.new([return_value], 201, {"Content-Type" => "text/html"})
+ response.finish
+ else
+ return_404
+ end
+ end
+
+ def create request
+ payment = Poundpay::Payment.create(request.POST)
+ payment.sid
end
- def call(env)
- unless env['REQUEST_PATH'] == '/' and env['REQUEST_METHOD'] == 'GET'
- return [404, {"Content-Type" => "text/plain"}, ["Page Not Found"]]
+ def authorize request
+ payment = Poundpay::Payment.find(request.POST['sid'])
+ payment.authorize
+ payment.save
+ PP.pp(payment.schema, '')
+ end
+
+ def release request
+ payment = Poundpay::Payment.find(request.POST['sid'])
+ payment.release
+ payment.save
+ PP.pp(payment.schema, '')
+ end
+
+ def cancel request
+ payment = Poundpay::Payment.find(request.POST['sid'])
+ payment.cancel
+ payment.save
+ PP.pp(payment.schema, '')
+ end
+
+ def escrow request
+ payment = Poundpay::Payment.find(request.POST['sid'])
+ payment.escrow
+ payment.save
+ PP.pp(payment.schema, '')
+ end
+
+end
+
+
+class User < SimpleController
+
+ def call env
+ request = Rack::Request.new(env)
+ return_value = case request.path.gsub(/\/$/, '') # trim trailing /
+ when '/user' then request.post? ? create(request) : nil
+ else nil
+ end
+
+ if return_value
+ response = Rack::Response.new([return_value], 201, {"Content-Type" => "text/html"})
+ response.finish
+ else
+ return_404
end
- # Create payment request
- params = {
- 'amount' => 20000, # In USD cents
- 'payer_fee_amount' => 0,
- 'payer_email_address' => 'goliath@example.com',
- 'recipient_fee_amount' => 500,
- 'recipient_email_address' => 'david@example.com',
- 'description' => 'Beats by Dr. Dre',
- }
- payment = @poundpay_client.post 'payments', params
- puts payment
+ end
- # Render and return page
- www_poundpay_url = SimpleApplication::CONFIG[:poundpay][:www_url]
- template = ERB.new(open("index.html.erb").read)
- page = template.result(binding)
- [200, {"Content-Type" => "text/html"}, [page]]
+ def create request
+ user = Poundpay::User.create({
+ :first_name => request.POST['user_first_name'],
+ :last_name => request.POST['user_last_name'],
+ :email_address => request.POST['user_email_address']
+ })
+ PP.pp(user.schema, '')
end
+
+end
+
+
+class ChargePermission < SimpleController
+
+ def call env
+ request = Rack::Request.new(env)
+ return_value = case request.path.gsub(/\/$/, '') # trim trailing /
+ when '/charge_permission' then request.post? ? create(request) : nil
+ when '/charge_permission/find' then request.post? ? show(request) : nil
+ when '/charge_permission/deactivate' then request.post? ? deactivate(request) : nil
+ else nil
+ end
+
+ if return_value
+ response = Rack::Response.new([return_value], 201, {"Content-Type" => "text/html"})
+ response.finish
+ else
+ return_404
+ end
+ end
+
+ def create request
+ charge_permission = Poundpay::ChargePermission.create(request.POST)
+ PP.pp(charge_permission.schema, '')
+ end
+
+ def show request
+ charge_permissions = Poundpay::ChargePermission.all(:email_address => request.POST['email_address'])
+ if charge_permissions
+ PP.pp(charge_permissions.map {|cp| cp.schema}, '')
+ else
+ nil
+ end
+ end
+
+ def deactivate request
+ charge_permission = Poundpay::ChargePermission.find(request.POST['sid'])
+ charge_permission.deactivate
+ PP.pp(charge_permission.schema, '')
+ end
+
end
@@ -1,11 +1,20 @@
class SimpleApplication
CONFIG = {
poundpay: {
- api_url: "https://api-sandbox.poundpay.com",
- www_url: "https://www-sandbox.poundpay.com",
- version: "silver",
- sid: "DVxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
- token: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
+ "api_url" => "http://localhost:8000",
+ "www_url" => "http://localhost:5000",
+ "version" => "silver",
+ "developer_sid" => "DV2f8a5168710c11e0aab3123140005921",
+ "auth_token" => "f309a5f600a630f7293c64783eaebc8b67a34428cfcd96e82599657a12769924",
+ "callback_url" => '',
+ },
+ default_payment: {
+ "amount" => "67890",
+ "payer_fee_amount" => "100",
+ "recipient_fee_amount" => "200",
+ "payer_email_address" => "sam@example.com",
+ "recipient_email_address" => "jacob@example.net",
+ "description" => "this is a simple description that just loves developers, developers",
}
}
end
@@ -1,6 +1,20 @@
-require 'application'
+# http://stackoverflow.com/questions/2900370/
+# why-does-ruby-1-9-2-remove-from-load-path-and-whats-the-alternative
+
+require 'rack'
+require 'rack/urlmap'
use Rack::ShowExceptions
use Rack::Lint
+use Rack::Static, :urls => ["/static"]
+
+require './application'
+
+app = Rack::URLMap.new(
+ '/' => Index.new,
+ '/payment' => Payment.new,
+ '/charge_permission'=> ChargePermission.new,
+ '/user' => User.new,
+)
-run SimpleApplication.new
+run app
Oops, something went wrong.

0 comments on commit 5b2eb5c

Please sign in to comment.