poundpay 0.2.9 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/.gitignore +3 -0
  2. data/README.rdoc +50 -3
  3. data/examples/simple_application/.rvmrc +1 -1
  4. data/examples/simple_application/application.rb +152 -48
  5. data/examples/simple_application/config.rb +14 -5
  6. data/examples/simple_application/config.ru +16 -2
  7. data/examples/simple_application/index.html.erb +125 -14
  8. data/examples/simple_application/static/css/simplemp.css +13 -0
  9. data/examples/simple_application/static/css/ui-lightness/images/ui-bg_diagonals-thick_18_b81900_40x40.png +0 -0
  10. data/examples/simple_application/static/css/ui-lightness/images/ui-bg_diagonals-thick_20_666666_40x40.png +0 -0
  11. data/examples/simple_application/static/css/ui-lightness/images/ui-bg_flat_10_000000_40x100.png +0 -0
  12. data/examples/simple_application/static/css/ui-lightness/images/ui-bg_glass_100_f6f6f6_1x400.png +0 -0
  13. data/examples/simple_application/static/css/ui-lightness/images/ui-bg_glass_100_fdf5ce_1x400.png +0 -0
  14. data/examples/simple_application/static/css/ui-lightness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  15. data/examples/simple_application/static/css/ui-lightness/images/ui-bg_gloss-wave_35_f6a828_500x100.png +0 -0
  16. data/examples/simple_application/static/css/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png +0 -0
  17. data/examples/simple_application/static/css/ui-lightness/images/ui-bg_highlight-soft_75_ffe45c_1x100.png +0 -0
  18. data/examples/simple_application/static/css/ui-lightness/images/ui-icons_222222_256x240.png +0 -0
  19. data/examples/simple_application/static/css/ui-lightness/images/ui-icons_228ef1_256x240.png +0 -0
  20. data/examples/simple_application/static/css/ui-lightness/images/ui-icons_ef8c08_256x240.png +0 -0
  21. data/examples/simple_application/static/css/ui-lightness/images/ui-icons_ffd27a_256x240.png +0 -0
  22. data/examples/simple_application/static/css/ui-lightness/images/ui-icons_ffffff_256x240.png +0 -0
  23. data/examples/simple_application/static/css/ui-lightness/jquery-ui-1.8.13.custom.css +578 -0
  24. data/examples/simple_application/static/favicon.ico +0 -0
  25. data/examples/simple_application/static/js/jquery-1.6.1.js +8936 -0
  26. data/examples/simple_application/static/js/jquery-ui-1.8.13.custom.min.js +784 -0
  27. data/examples/simple_application/static/js/simplemp.js +177 -0
  28. data/lib/poundpay.rb +2 -1
  29. data/lib/poundpay/elements.rb +38 -18
  30. data/lib/poundpay/exceptions.rb +10 -0
  31. data/lib/poundpay/version.rb +1 -1
  32. data/spec/fixtures/charge_permissions.rb +24 -0
  33. data/spec/fixtures/payments.rb +0 -6
  34. data/spec/poundpay/elements_spec.rb +34 -43
  35. data/spec/poundpay/exceptions_spec.rb +11 -0
  36. metadata +29 -21
data/.gitignore CHANGED
@@ -1,3 +1,6 @@
1
1
  pkg/
2
2
  Gemfile.lock
3
3
  *.gem
4
+ .project
5
+ .idea
6
+ .bundle
data/README.rdoc CHANGED
@@ -7,7 +7,7 @@ Poundpay is a payments platform for marketplaces
7
7
 
8
8
  1. Add the following to your Gemfile
9
9
 
10
- gem 'poundpay', '~> 0.2.0'
10
+ gem 'poundpay', '~> 0.2.9'
11
11
 
12
12
  2. At the command prompt, install the gem with bundler
13
13
 
@@ -35,6 +35,13 @@ Poundpay is a payments platform for marketplaces
35
35
 
36
36
  before_filter :verify_poundpay_callback
37
37
 
38
+ == Creating a user
39
+
40
+ @user = Poundpay::User.create(
41
+ :first_name => "Dart",
42
+ :last_name => "Master",
43
+ :email_address => "dart-master@example.com")
44
+
38
45
 
39
46
  == Creating a payment
40
47
 
@@ -47,7 +54,7 @@ Poundpay is a payments platform for marketplaces
47
54
  :description => "Beats by Dr. Dre")
48
55
 
49
56
 
50
- == Serving IFRAME
57
+ == Serving the payment IFRAME
51
58
 
52
59
  <script src="https://www.poundpay.com/js/poundpay.js"></script>
53
60
 
@@ -80,6 +87,46 @@ Poundpay is a payments platform for marketplaces
80
87
 
81
88
  payment = Poundpay::Payment.find(payment_sid)
82
89
  payment.escrow # AUTHORIZED -> ESCROWED. Credit card is charged
83
- payment.partially_release(amount) # ESCROWED or PARTIALLY_RELEASED -> PARTIALLY_RELEASED or PARTIALLY_RELEASED. Recipieint recieves a portion of funds.
84
90
  payment.release # ESCROWED or PARTIALLY_RELEASED -> RELEASED. Recipient receives money
85
91
  payment.cancel # ESCROWED or PARTIALLY_RELEASED -> CANCELED. Payer receives refund
92
+
93
+
94
+ == Creating a charge permission
95
+
96
+ @charge_permission = Poundpay::ChargePermission.create(
97
+ :email_address => "fred@example.com")
98
+
99
+
100
+ == Serving the charge permission IFRAME
101
+
102
+ <script src="https://www.poundpay.com/js/poundpay.js"></script>
103
+
104
+ <div id="pound-root"></div>
105
+
106
+ <script>
107
+ function handleChargePermissionSuccess() {
108
+ // do something
109
+ }
110
+
111
+ function handleChargePermissionError() {
112
+ // handle error
113
+ }
114
+
115
+ PoundPay.init({
116
+ charge_permission_sid: "<%= @charge_permission.sid %>",
117
+ success: handleChargePermissionSuccess,
118
+ error: handleChargePermissionError,
119
+ name: "Fred Nietzsche", // Optional
120
+ address_street: "330 Townsend St", // Optional
121
+ address_city: "San Francisco", // Optional
122
+ address_state: "California", // Optional
123
+ address_zip: "94107", // Optional
124
+ server: "<%= Poundpay.www_url %>"
125
+ });
126
+ </script>
127
+
128
+
129
+ == Charge permission methods
130
+
131
+ charge_permission = Poundpay::ChargePermission.find(charge_permission_sid)
132
+ charge_permission.deactivate # CREATED or ACTIVE -> INACTIVE. Charge permission is deactivated and can no longer be used to authorize payments for the associated payer.
@@ -1,3 +1,3 @@
1
1
  # echo 'Switching rvm gemset ...'
2
- rvm use --create --install 1.9.1@poundpay-simple_application >& /dev/null
2
+ rvm use --create --install 1.9.2@poundpay-simple_application >& /dev/null
3
3
  # rvm gemset import >& /dev/null
@@ -1,65 +1,169 @@
1
- require 'uri'
2
- require 'net/http'
3
- require 'net/https'
4
- require 'json'
5
- require 'erb'
1
+ require 'pp'
2
+ require 'poundpay'
6
3
 
7
- require 'config'
4
+ require './config'
8
5
 
6
+ class SimpleController
7
+ attr_reader :poundpay_client
9
8
 
10
- class PoundPay
11
- attr_reader :api_url, :version, :sid, :token
9
+ def initialize
10
+ if Poundpay.configured?
11
+ return
12
+ end
13
+ config = SimpleApplication::CONFIG[:poundpay]
14
+ puts config
15
+ Poundpay.configure_from_hash(config)
16
+ end
12
17
 
13
- def initialize(api_url, version, sid, token)
14
- @api_url = api_url
15
- @version = version
16
- @sid = sid
17
- @token = token
18
+ def return_404
19
+ response = Rack::Response.new(["Page Not Found"], 404, {"Content-Type" => "text/plain"})
20
+ response.finish
18
21
  end
19
22
 
20
- def post(endpoint, params)
21
- request_url = "#{@api_url}/#{@version}/#{endpoint}"
22
- puts request_url, params
23
- uri = URI.parse request_url
24
- http = Net::HTTP.new uri.host, uri.port
25
- http.use_ssl = true
26
- req = Net::HTTP::Post.new uri.request_uri
27
- req.set_form_data params
28
- req.basic_auth @sid, @token
29
- response = http.request req
30
- JSON.load response.body
23
+ end
24
+
25
+ class Index < SimpleController
26
+
27
+ def call env
28
+ request = Rack::Request.new(env)
29
+ unless request.path == '/' and request.get?
30
+ return return_404
31
+ end
32
+ # Create payment request
33
+ payment = SimpleApplication::CONFIG[:default_payment]
34
+ # Render and return page
35
+ www_poundpay_url = Poundpay.www_url
36
+ template = ERB.new(open("index.html.erb").read)
37
+ page = template.result(binding)
38
+ response = Rack::Response.new([page], 200, {"Content-Type" => "text/html"})
39
+ response.finish
31
40
  end
41
+
32
42
  end
33
43
 
44
+ class Payment < SimpleController
34
45
 
35
- class SimpleApplication
36
- attr_reader :poundpay_client
46
+ def call env
47
+ request = Rack::Request.new(env)
48
+ return_value = case request.path.gsub(/\/$/, '') # trim trailing /
49
+ when '/payment' then request.post? ? create(request) : nil
50
+ when '/payment/release' then request.post? ? release(request) : nil
51
+ when '/payment/authorize' then request.post? ? authorize(request) : nil
52
+ when '/payment/cancel' then request.post? ? cancel(request) : nil
53
+ when '/payment/escrow' then request.post? ? escrow(request) : nil
54
+ else nil
55
+ end
37
56
 
38
- def initialize
39
- config = SimpleApplication::CONFIG[:poundpay]
40
- @poundpay_client = PoundPay.new(config[:api_url], config[:version], config[:sid], config[:token])
57
+ if return_value
58
+ response = Rack::Response.new([return_value], 201, {"Content-Type" => "text/html"})
59
+ response.finish
60
+ else
61
+ return_404
62
+ end
63
+ end
64
+
65
+ def create request
66
+ payment = Poundpay::Payment.create(request.POST)
67
+ payment.sid
41
68
  end
42
69
 
43
- def call(env)
44
- unless env['REQUEST_PATH'] == '/' and env['REQUEST_METHOD'] == 'GET'
45
- return [404, {"Content-Type" => "text/plain"}, ["Page Not Found"]]
70
+ def authorize request
71
+ payment = Poundpay::Payment.find(request.POST['sid'])
72
+ payment.authorize
73
+ payment.save
74
+ PP.pp(payment.schema, '')
75
+ end
76
+
77
+ def release request
78
+ payment = Poundpay::Payment.find(request.POST['sid'])
79
+ payment.release
80
+ payment.save
81
+ PP.pp(payment.schema, '')
82
+ end
83
+
84
+ def cancel request
85
+ payment = Poundpay::Payment.find(request.POST['sid'])
86
+ payment.cancel
87
+ payment.save
88
+ PP.pp(payment.schema, '')
89
+ end
90
+
91
+ def escrow request
92
+ payment = Poundpay::Payment.find(request.POST['sid'])
93
+ payment.escrow
94
+ payment.save
95
+ PP.pp(payment.schema, '')
96
+ end
97
+
98
+ end
99
+
100
+
101
+ class User < SimpleController
102
+
103
+ def call env
104
+ request = Rack::Request.new(env)
105
+ return_value = case request.path.gsub(/\/$/, '') # trim trailing /
106
+ when '/user' then request.post? ? create(request) : nil
107
+ else nil
108
+ end
109
+
110
+ if return_value
111
+ response = Rack::Response.new([return_value], 201, {"Content-Type" => "text/html"})
112
+ response.finish
113
+ else
114
+ return_404
46
115
  end
47
- # Create payment request
48
- params = {
49
- 'amount' => 20000, # In USD cents
50
- 'payer_fee_amount' => 0,
51
- 'payer_email_address' => 'goliath@example.com',
52
- 'recipient_fee_amount' => 500,
53
- 'recipient_email_address' => 'david@example.com',
54
- 'description' => 'Beats by Dr. Dre',
55
- }
56
- payment = @poundpay_client.post 'payments', params
57
- puts payment
116
+ end
58
117
 
59
- # Render and return page
60
- www_poundpay_url = SimpleApplication::CONFIG[:poundpay][:www_url]
61
- template = ERB.new(open("index.html.erb").read)
62
- page = template.result(binding)
63
- [200, {"Content-Type" => "text/html"}, [page]]
118
+ def create request
119
+ user = Poundpay::User.create({
120
+ :first_name => request.POST['user_first_name'],
121
+ :last_name => request.POST['user_last_name'],
122
+ :email_address => request.POST['user_email_address']
123
+ })
124
+ PP.pp(user.schema, '')
64
125
  end
126
+
127
+ end
128
+
129
+
130
+ class ChargePermission < SimpleController
131
+
132
+ def call env
133
+ request = Rack::Request.new(env)
134
+ return_value = case request.path.gsub(/\/$/, '') # trim trailing /
135
+ when '/charge_permission' then request.post? ? create(request) : nil
136
+ when '/charge_permission/find' then request.post? ? show(request) : nil
137
+ when '/charge_permission/deactivate' then request.post? ? deactivate(request) : nil
138
+ else nil
139
+ end
140
+
141
+ if return_value
142
+ response = Rack::Response.new([return_value], 201, {"Content-Type" => "text/html"})
143
+ response.finish
144
+ else
145
+ return_404
146
+ end
147
+ end
148
+
149
+ def create request
150
+ charge_permission = Poundpay::ChargePermission.create(request.POST)
151
+ PP.pp(charge_permission.schema, '')
152
+ end
153
+
154
+ def show request
155
+ charge_permissions = Poundpay::ChargePermission.all(:email_address => request.POST['email_address'])
156
+ if charge_permissions
157
+ PP.pp(charge_permissions.map {|cp| cp.schema}, '')
158
+ else
159
+ nil
160
+ end
161
+ end
162
+
163
+ def deactivate request
164
+ charge_permission = Poundpay::ChargePermission.find(request.POST['sid'])
165
+ charge_permission.deactivate
166
+ PP.pp(charge_permission.schema, '')
167
+ end
168
+
65
169
  end
@@ -1,11 +1,20 @@
1
1
  class SimpleApplication
2
2
  CONFIG = {
3
3
  poundpay: {
4
- api_url: "https://api-sandbox.poundpay.com",
5
- www_url: "https://www-sandbox.poundpay.com",
6
- version: "silver",
7
- sid: "DVxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
8
- token: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
4
+ "api_url" => "https://api-sandbox.poundpay.com",
5
+ "www_url" => "https://www-sandbox.poundpay.com",
6
+ "version" => "silver",
7
+ "developer_sid" => "DV2f8a5168710c11e0aab3123140005921",
8
+ "auth_token" => "f309a5f600a630f7293c64783eaebc8b67a34428cfcd96e82599657a12769924",
9
+ "callback_url" => '',
10
+ },
11
+ default_payment: {
12
+ "amount" => "67890",
13
+ "payer_fee_amount" => "100",
14
+ "recipient_fee_amount" => "200",
15
+ "payer_email_address" => "sam@example.com",
16
+ "recipient_email_address" => "jacob@example.net",
17
+ "description" => "this is a simple description that just loves developers, developers",
9
18
  }
10
19
  }
11
20
  end
@@ -1,6 +1,20 @@
1
- require 'application'
1
+ # http://stackoverflow.com/questions/2900370/
2
+ # why-does-ruby-1-9-2-remove-from-load-path-and-whats-the-alternative
3
+
4
+ require 'rack'
5
+ require 'rack/urlmap'
2
6
 
3
7
  use Rack::ShowExceptions
4
8
  use Rack::Lint
9
+ use Rack::Static, :urls => ["/static"]
10
+
11
+ require './application'
12
+
13
+ app = Rack::URLMap.new(
14
+ '/' => Index.new,
15
+ '/payment' => Payment.new,
16
+ '/charge_permission'=> ChargePermission.new,
17
+ '/user' => User.new,
18
+ )
5
19
 
6
- run SimpleApplication.new
20
+ run app
@@ -1,18 +1,129 @@
1
+ <!DOCTYPE html>
1
2
  <html>
2
- <title>Simple Marketplace</title>
3
- <script src="<%= www_poundpay_url %>/js/poundpay.js"></script>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="description" content="Simple Marketplace">
6
+ <title>Simple MarketPlace</title>
7
+ <link rel="icon" href="static/favicon.ico" type="image/x-icon">
8
+ <link rel="stylesheet" href="static/css/simplemp.css">
9
+ <script src="static/js/jquery-1.6.1.js"></script>
10
+ <script src="static/js/jquery-ui-1.8.13.custom.min.js"></script>
11
+ <script src="<%= www_poundpay_url %>/js/poundpay.js"></script>
12
+ <script src="static/js/simplemp.js"></script>
13
+ </head>
4
14
  <body>
5
- <h1>Simple Marketplace</h1>
6
- <h2><%= payment['description'] %></h2>
7
- <div id="pound-root"></div>
8
- <script type="text/javascript">
9
- PoundPay.init({
10
- // success: successCallback, // Optional js callback
11
- payment_sid: "<%= payment['sid'] %>",
12
- server: "<%= www_poundpay_url %>",
13
- cardholder_name: "David Stonethrower", // Optional
14
- phone_number: "6505551234" // Optional
15
- });
16
- </script>
15
+ <h1>Create charge permission</h1>
16
+ <table id="chargePermissionTable">
17
+ <tr>
18
+ <td>Email
19
+ <td><input type="text" class="text" id="email_address" value="<%= payment['payer_email_address'] %>">
20
+ </tr>
21
+ </table>
22
+ <a href="javascript:;" onclick="createChargePermission();">Create Charge Permission</a><br>
23
+ <a href="javascript:;" onclick="findChargePermission();">Find Charge Permission</a><br>
24
+ <pre id="find_charge_permission_results"></pre>
25
+
26
+ <h1>Display charge permission iframe</h1>
27
+ <table>
28
+ <tr>
29
+ <td>Charge permission id
30
+ <td><input type="text" class="text" id="charge_permission_id">
31
+ </tr>
32
+ <tr>
33
+ <td>Card holder name</td>
34
+ <td><input type="text" class="text" id="charge_permission_cardholder_name">
35
+ </tr>
36
+ <tr>
37
+ <td>server
38
+ <td><input type="text" class="text" id="charge_permission_server" value="<%= www_poundpay_url %>">
39
+ </tr>
40
+ </table>
41
+ <a href="javascript:;" onclick="deactivateChargePermission();">Deactivate Charge Permission</a><br>
42
+ <a href="javascript:;" onclick="startChargePermissionIFrame();">Start Charge Permission IFrame</a><br>
43
+ <div id="pound-pcp"></div>
44
+
45
+ <h1>Create payment</h1>
46
+ <table id="paymentsTable">
47
+ <tr>
48
+ <td>Payment Amount
49
+ <td><input type="text" class="text" id="amount" value="<%= payment['amount'] %>">
50
+ </tr>
51
+ <tr>
52
+ <td>Payer Fee</td>
53
+ <td><input type="text" class="text" id="payer_fee_amount" value="<%= payment['payer_fee_amount'] %>">
54
+ </tr>
55
+ <tr>
56
+ <td>Recipient Fee</td>
57
+ <td><input type="text" class="text" id="recipient_fee_amount" value="<%= payment['recipient_fee_amount'] %>">
58
+ </tr>
59
+ <tr>
60
+ <td>Payer Email</td>
61
+ <td><input type="text" class="text" id="payer_email_address" value="<%= payment['payer_email_address'] %>">
62
+ </tr>
63
+ <tr>
64
+ <td>Recipient Email</td>
65
+ <td><input type="text" class="text" id="recipient_email_address" value="<%= payment['recipient_email_address'] %>">
66
+ </tr>
67
+ <tr>
68
+ <td>Description</td>
69
+ <td><input type="text" class="text" id="description" value="<%= payment['description'] %>">
70
+ </tr>
71
+ </table>
72
+
73
+ <a href="javascript:;" onclick="createPayment();">Create Payment</a>
74
+
75
+ <h1>Display payment iframe</h1>
76
+ <table>
77
+ <tr>
78
+ <td>Payment request id</td>
79
+ <td><input type="text" class="text" id="payment_id">
80
+ </tr>
81
+ <tr>
82
+ <td>Card holder name</td>
83
+ <td><input type="text" class="text" id="cardholder_name">
84
+ </tr>
85
+ <tr>
86
+ <td>server</td>
87
+ <td><input type="text" class="text" id="server" value="<%= www_poundpay_url %>">
88
+ </tr>
89
+ </table>
90
+ <a href="javascript:;" onclick="startIFrame();">Start Payment IFrame</a>
91
+ <div id="pound-root"></div>
92
+ <h1 id="paymentComplete" style="display:none;color:green;">
93
+ Payment Complete
94
+ </h1>
95
+ <h1>Create User</h1>
96
+ <table id="create_user_table">
97
+ <tr>
98
+ <td>First Name</td>
99
+ <td><input type="text" class="text" id="user_first_name">
100
+ </tr>
101
+ <tr>
102
+ <td>Last Name</td>
103
+ <td><input type="text" class="text" id="user_last_name">
104
+ </tr>
105
+ <tr>
106
+ <td>Email Address</td>
107
+ <td><input type="text" class="text" id="user_email_address">
108
+ </tr>
109
+ </table>
110
+ <a href="javascript:;" onclick="createUser();">Create User</a>
111
+
112
+ <pre id="created_user_results"></pre>
113
+
114
+
115
+ <h1>Payment Operations</h1>
116
+ <table>
117
+ <tr>
118
+ <td>Payment SID
119
+ <td><input type="text" class="text" id="operating_payment_sid">
120
+ </tr>
121
+ </table>
122
+ <a href="javascript:;" onclick="authorizePayment();">Authorize Payment</a><br>
123
+ <a href="javascript:;" onclick="escrowPayment();">Escrow Payment</a><br>
124
+ <a href="javascript:;" onclick="releasePayment();">Release Payment</a><br>
125
+ <a href="javascript:;" onclick="cancelPayment();">Cancel Payment</a><br>
126
+
127
+ <pre id="operation_results"></pre>
17
128
  </body>
18
129
  </html>