wepay-api 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.travis.yml +6 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +118 -0
  7. data/Rakefile +7 -0
  8. data/lib/wepay-api.rb +2 -0
  9. data/lib/wepay.rb +23 -0
  10. data/lib/wepay/api.rb +27 -0
  11. data/lib/wepay/api/account.rb +86 -0
  12. data/lib/wepay/api/base.rb +25 -0
  13. data/lib/wepay/api/checkout.rb +68 -0
  14. data/lib/wepay/api/preapproval.rb +50 -0
  15. data/lib/wepay/api/subscription.rb +50 -0
  16. data/lib/wepay/api/subscription_plan.rb +59 -0
  17. data/lib/wepay/api/withdrawal.rb +41 -0
  18. data/lib/wepay/client.rb +63 -0
  19. data/lib/wepay/client/data.rb +31 -0
  20. data/lib/wepay/client/token.rb +30 -0
  21. data/lib/wepay/configuration.rb +34 -0
  22. data/lib/wepay/error.rb +28 -0
  23. data/lib/wepay/middleware.rb +44 -0
  24. data/lib/wepay/oauth.rb +32 -0
  25. data/lib/wepay/version.rb +3 -0
  26. data/spec/fixtures/vcr_cassettes/Wepay_Middleware/Wepay_Middleware_CheckStatus/raises_a_Wepay_ApiError_with_proper_error_type_message_and_code.yml +35 -0
  27. data/spec/fixtures/vcr_cassettes/wepay_account.yml +38 -0
  28. data/spec/fixtures/vcr_cassettes/wepay_account_add_bank.yml +38 -0
  29. data/spec/fixtures/vcr_cassettes/wepay_account_balance.yml +38 -0
  30. data/spec/fixtures/vcr_cassettes/wepay_account_create.yml +46 -0
  31. data/spec/fixtures/vcr_cassettes/wepay_account_delete.yml +48 -0
  32. data/spec/fixtures/vcr_cassettes/wepay_account_find.yml +43 -0
  33. data/spec/fixtures/vcr_cassettes/wepay_account_get_tax.yml +38 -0
  34. data/spec/fixtures/vcr_cassettes/wepay_account_modify.yml +49 -0
  35. data/spec/fixtures/vcr_cassettes/wepay_account_set_tax.yml +38 -0
  36. data/spec/fixtures/vcr_cassettes/wepay_checkout.yml +40 -0
  37. data/spec/fixtures/vcr_cassettes/wepay_checkout_cancel.yml +38 -0
  38. data/spec/fixtures/vcr_cassettes/wepay_checkout_capture.yml +38 -0
  39. data/spec/fixtures/vcr_cassettes/wepay_checkout_create.yml +38 -0
  40. data/spec/fixtures/vcr_cassettes/wepay_checkout_find.yml +51 -0
  41. data/spec/fixtures/vcr_cassettes/wepay_checkout_modify.yml +40 -0
  42. data/spec/fixtures/vcr_cassettes/wepay_checkout_refund.yml +38 -0
  43. data/spec/fixtures/vcr_cassettes/wepay_preapproval.yml +39 -0
  44. data/spec/fixtures/vcr_cassettes/wepay_preapproval_cancel.yml +38 -0
  45. data/spec/fixtures/vcr_cassettes/wepay_preapproval_create.yml +38 -0
  46. data/spec/fixtures/vcr_cassettes/wepay_preapproval_find.yml +39 -0
  47. data/spec/fixtures/vcr_cassettes/wepay_preapproval_modify.yml +39 -0
  48. data/spec/fixtures/vcr_cassettes/wepay_subscription.yml +38 -0
  49. data/spec/fixtures/vcr_cassettes/wepay_subscription_cancel.yml +36 -0
  50. data/spec/fixtures/vcr_cassettes/wepay_subscription_create.yml +48 -0
  51. data/spec/fixtures/vcr_cassettes/wepay_subscription_find.yml +38 -0
  52. data/spec/fixtures/vcr_cassettes/wepay_subscription_modify.yml +38 -0
  53. data/spec/fixtures/vcr_cassettes/wepay_subscription_plan.yml +39 -0
  54. data/spec/fixtures/vcr_cassettes/wepay_subscription_plan_create.yml +48 -0
  55. data/spec/fixtures/vcr_cassettes/wepay_subscription_plan_delete.yml +48 -0
  56. data/spec/fixtures/vcr_cassettes/wepay_subscription_plan_find.yml +39 -0
  57. data/spec/fixtures/vcr_cassettes/wepay_subscription_plan_get_button.yml +48 -0
  58. data/spec/fixtures/vcr_cassettes/wepay_subscription_plan_modify.yml +49 -0
  59. data/spec/fixtures/vcr_cassettes/wepay_withdrawal.yml +38 -0
  60. data/spec/fixtures/vcr_cassettes/wepay_withdrawal_create.yml +38 -0
  61. data/spec/fixtures/vcr_cassettes/wepay_withdrawal_find.yml +38 -0
  62. data/spec/fixtures/vcr_cassettes/wepay_withdrawal_modify.yml +38 -0
  63. data/spec/settings.yml.example +6 -0
  64. data/spec/spec_helper.rb +12 -0
  65. data/spec/support/matchers.rb +5 -0
  66. data/spec/support/rspec_helper.rb +30 -0
  67. data/spec/support/settings.rb +14 -0
  68. data/spec/support/vcr.rb +17 -0
  69. data/spec/wepay/api/account_spec.rb +110 -0
  70. data/spec/wepay/api/checkout_spec.rb +86 -0
  71. data/spec/wepay/api/preapproval_spec.rb +69 -0
  72. data/spec/wepay/api/subscription_plan_spec.rb +83 -0
  73. data/spec/wepay/api/subscription_spec.rb +65 -0
  74. data/spec/wepay/api/withdrawal_spec.rb +53 -0
  75. data/spec/wepay/api_spec.rb +9 -0
  76. data/spec/wepay/configuration_spec.rb +25 -0
  77. data/spec/wepay/middleware_spec.rb +21 -0
  78. data/spec/wepay/oauth_spec.rb +23 -0
  79. data/wepay-api.gemspec +29 -0
  80. metadata +273 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 69fe9591ca7ea909e13e1301d6b93b87c2c21588
4
+ data.tar.gz: ce7367f286d651a7efbc83ff695118a96389602c
5
+ SHA512:
6
+ metadata.gz: 196804d90615ef5986048dedc4a5c99fd2e0a6abe73326632dcc211586021ea0ba062b336e915d2ca44d6133bdfacc945903b4fcc3d1a873808c1344aaa2d73f
7
+ data.tar.gz: 11402411fcd33b29c230c716d464270d0c35b928e86e325fda68a34ea0cac484bcbf1d657445f09c523f99dbdb348a2c063b3b32b40bfdbfcda9ccc7ffed76e5
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.iml
19
+ .idea
20
+ .rvmrc
21
+ spec/settings.yml
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - jruby-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wepay-api.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Anh Nguyen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,118 @@
1
+ [![Build Status](https://travis-ci.org/B-Sides/wepay-api.png?branch=master)](https://travis-ci.org/B-Sides/wepay-api)
2
+
3
+ # wepay-api
4
+
5
+ `wepay-api` is a Ruby wrapper for Wepay API.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'wepay-api'
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install wepay-api
16
+
17
+ ## Configuration
18
+ `wepay-api` can be configured as following:
19
+
20
+ ```ruby
21
+ Wepay.configure do |config|
22
+ config.client_id = ... # app id (compulsory)
23
+ config.client_secret = ... # app secret (compulsory)
24
+ config.access_token = ... # access token of the app (compulsory)
25
+ config.account_id = ... # caller's account id (compulsory)
26
+ config.scope = ... # the permission for OAuth authorization (compulsory)
27
+ config.using_stage = ... # set to true/false (compulsory)
28
+ config.open_timeout = 5 # default is 5 seconds (optional)
29
+ config.timeout = 10 # default is 10 seconds (optional)
30
+ end
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ ### OAuth
36
+ #### OAuth authorization URL
37
+ To get the authorization URL for OAuth, use following method:
38
+
39
+ ```ruby
40
+ Wepay.authorization_url(redirect_uri)
41
+ ```
42
+
43
+ Refer to [https://stage.wepay.com/developer/reference/oauth2#authorize](https://stage.wepay.com/developer/reference/oauth2#authorize) for more details.
44
+
45
+ #### User's access token
46
+ To get the access token from user, use following method:
47
+
48
+ ```ruby
49
+ Wepay.get_access_token(code, redirect_uri)
50
+ ```
51
+
52
+ Refer to [https://stage.wepay.com/developer/reference/oauth2#token](https://stage.wepay.com/developer/reference/oauth2#token) for more details.
53
+
54
+ ### API
55
+ #### Available API
56
+ - [/account](lib/wepay/api/account.rb)
57
+ - [/checkout](lib/wepay/api/checkout.rb)
58
+ - [/preapproval](lib/wepay/api/preapproval.rb)
59
+ - [/subscription_plan](lib/wepay/api/subscription_plan.rb)
60
+ - [/subscription](lib/wepay/api/subscription.rb)
61
+ - [/withdrawal](lib/wepay/api/withdrawal.rb)
62
+
63
+ Notice: WePay is using **POST** for **ALL** the requests, **even the API method says "get_..."**
64
+
65
+ #### API calls
66
+ In order to make an API call, we can follow the convention for the method call:
67
+
68
+ Wepay.`api_type`.`api_method`(params)
69
+
70
+ For example, to create a checkout:
71
+
72
+ ```ruby
73
+ Wepay.checkout.create(
74
+ short_description: "my description for the checkout",
75
+ type: "GOODS",
76
+ fee_payer: "payee",
77
+ amount: 10,
78
+ redirect_uri: "redirect_uri",
79
+ ...
80
+ )
81
+ ```
82
+
83
+ `params` is a Hash which captures the whole query as in Wepay API documentation, so refer to the documentation if any doubts.
84
+
85
+ Response from these API calls will be converted to `Hashied::Mash` object to make it easier for data accessing.
86
+ Refer to [https://github.com/intridea/hashie#mash](https://github.com/intridea/hashie#mash) for more details.
87
+
88
+ #### User's authorized calls
89
+ After configured, the http client from `wepay-api` will try to make use of app's `access_token` (in the configuration) as the default authorization of the api calls.
90
+ There are some situations that user's `access_token` need to be used, e.g. create an account for the user, so we can embed the user's `access_token`
91
+ in the call by adding field `access_token` to the params, for example:
92
+
93
+ ```ruby
94
+ Wepay.account.create(
95
+ ...
96
+ access_token: "user's access token"
97
+ )
98
+ ```
99
+
100
+ #### Error/ Exception
101
+ If an API call has errors, it will raise an Wepay::ApiError with following fields:
102
+ - type, e.g. invalid_request
103
+ - code, e.g. 1001
104
+ - message
105
+
106
+ Give a rescue block to the API calls to make sure the error is handled in any situation.
107
+
108
+ ## Development notes
109
+ - Copy `spec/settings.yml.example` into `spec/settings.yml` and replace with your credentials to make record the requests for testing using VCR
110
+ - Before committing VCR fixtures, remember to remove any sensitive data from the fixtures: account id, client secret, ...
111
+
112
+ ## Contributing
113
+
114
+ 1. Fork it
115
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
116
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
117
+ 4. Push to the branch (`git push origin my-new-feature`)
118
+ 5. Create new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task default: :spec
7
+ task test: :spec
@@ -0,0 +1,2 @@
1
+ require "wepay"
2
+ require "wepay/version"
@@ -0,0 +1,23 @@
1
+ require 'wepay/error'
2
+ require 'wepay/configuration'
3
+ require 'wepay/oauth'
4
+
5
+ require 'wepay/middleware'
6
+ require 'wepay/client'
7
+ require 'wepay/client/data'
8
+ require 'wepay/client/token'
9
+
10
+ require 'wepay/api'
11
+ require 'wepay/api/base'
12
+ require 'wepay/api/checkout'
13
+ require 'wepay/api/account'
14
+ require 'wepay/api/preapproval'
15
+ require 'wepay/api/withdrawal'
16
+ require 'wepay/api/subscription_plan'
17
+ require 'wepay/api/subscription'
18
+
19
+ module Wepay
20
+ extend Wepay::Configuration
21
+ extend Wepay::Api
22
+ extend Wepay::OAuth
23
+ end
@@ -0,0 +1,27 @@
1
+ module Wepay
2
+ module Api
3
+ def account
4
+ Wepay::Api::Account.new
5
+ end
6
+
7
+ def checkout
8
+ Wepay::Api::Checkout.new
9
+ end
10
+
11
+ def preapproval
12
+ Wepay::Api::Preapproval.new
13
+ end
14
+
15
+ def withdrawal
16
+ Wepay::Api::Withdrawal.new
17
+ end
18
+
19
+ def subscription_plan
20
+ Wepay::Api::SubscriptionPlan.new
21
+ end
22
+
23
+ def subscription
24
+ Wepay::Api::Subscription.new
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,86 @@
1
+ module Wepay
2
+ module Api
3
+ class Account < Base
4
+ ##
5
+ # @param params [Hash] See documentation below
6
+ # @return [Hashie::Mash]
7
+ # @docs [/account](https://www.wepay.com/developer/reference/account#lookup)
8
+ #
9
+ def show(params)
10
+ post 'account', params
11
+ end
12
+
13
+ ##
14
+ # @param params [Hash] See documentation below
15
+ # @return [Hashie::Mash]
16
+ # @docs [/account/find](https://www.wepay.com/developer/reference/account#find)
17
+ #
18
+ def find(params)
19
+ post 'account/find', params
20
+ end
21
+
22
+ ##
23
+ # @param params [Hash] See documentation below
24
+ # @return [Hashie::Mash]
25
+ # @docs [/account/create](https://www.wepay.com/developer/reference/account#create)
26
+ #
27
+ def create(params)
28
+ post 'account/create', params
29
+ end
30
+
31
+ ##
32
+ # @param params [Hash] See documentation below
33
+ # @return [Hashie::Mash]
34
+ # @docs [/account/modify](https://www.wepay.com/developer/reference/account#modify)
35
+ #
36
+ def modify(params)
37
+ post 'account/modify', params
38
+ end
39
+
40
+ ##
41
+ # @param params [Hash] See documentation below
42
+ # @return [Hashie::Mash]
43
+ # @docs [/account/delete](https://www.wepay.com/developer/reference/account#delete)
44
+ #
45
+ def delete(params)
46
+ post 'account/delete', params
47
+ end
48
+
49
+ ##
50
+ # @param params [Hash] See documentation below
51
+ # @return [Hashie::Mash]
52
+ # @docs [/account/balance](https://www.wepay.com/developer/reference/account#balance)
53
+ #
54
+ def balance(params)
55
+ post 'account/balance', params
56
+ end
57
+
58
+ ##
59
+ # @param params [Hash] See documentation below
60
+ # @return [Hashie::Mash]
61
+ # @docs [/account/add_bank](https://www.wepay.com/developer/reference/account#add_bank)
62
+ #
63
+ def add_bank(params)
64
+ post 'account/add_bank', params
65
+ end
66
+
67
+ ##
68
+ # @param params [Hash] See documentation below
69
+ # @return [Hashie::Mash]
70
+ # @docs [/account/set_tax](https://www.wepay.com/developer/reference/account#set_tax)
71
+ #
72
+ def set_tax(params)
73
+ post 'account/set_tax', params
74
+ end
75
+
76
+ ##
77
+ # @param params [Hash] See documentation below
78
+ # @return [Hashie::Mash]
79
+ # @docs [/account/get_tax](https://www.wepay.com/developer/reference/account#get_tax)
80
+ #
81
+ def get_tax(params)
82
+ post 'account/get_tax', params
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,25 @@
1
+ module Wepay
2
+ module Api
3
+ class Base
4
+ def initialize
5
+ end
6
+
7
+ private
8
+ def get(path, params = {})
9
+ make_request(:get, path, params)
10
+ end
11
+
12
+ def post(path, params = {})
13
+ make_request(:post, path, params)
14
+ end
15
+
16
+ def make_request(http_method, path, params = {})
17
+ client.send(http_method, path, params)
18
+ end
19
+
20
+ def client
21
+ @client ||= ::Wepay::Client::Data.new
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,68 @@
1
+ module Wepay
2
+ module Api
3
+ class Checkout < Base
4
+ ##
5
+ # @param params [Hash] See documentation below
6
+ # @return [Hashie::Mash]
7
+ # @docs [/checkout](https://www.wepay.com/developer/reference/checkout#lookup)
8
+ #
9
+ def show(params)
10
+ post 'checkout', params
11
+ end
12
+
13
+ ##
14
+ # @param params [Hash] See documentation below
15
+ # @return [Hashie::Mash]
16
+ # @docs [/checkout/find](https://www.wepay.com/developer/reference/checkout#find)
17
+ #
18
+ def find(params)
19
+ post 'checkout/find', params
20
+ end
21
+
22
+ ##
23
+ # @param params [Hash] See documentation below
24
+ # @return [Hashie::Mash]
25
+ # @docs [/checkout/create](https://www.wepay.com/developer/reference/checkout#create)
26
+ #
27
+ def create(params)
28
+ post 'checkout/create', params
29
+ end
30
+
31
+ ##
32
+ # @param params [Hash] See documentation below
33
+ # @return [Hashie::Mash]
34
+ # @docs [/checkout/cancel](https://www.wepay.com/developer/reference/checkout#cancel)
35
+ #
36
+ def cancel(params)
37
+ post 'checkout/cancel', params
38
+ end
39
+
40
+ ##
41
+ # @param params [Hash] See documentation below
42
+ # @return [Hashie::Mash]
43
+ # @docs [/checkout/refund](https://www.wepay.com/developer/reference/checkout#refund)
44
+ #
45
+ def refund(params)
46
+ post 'checkout/refund', params
47
+ end
48
+
49
+ ##
50
+ # @param params [Hash] See documentation below
51
+ # @return [Hashie::Mash]
52
+ # @docs [/checkout/capture](https://www.wepay.com/developer/reference/checkout#capture)
53
+ #
54
+ def capture(params)
55
+ post 'checkout/capture', params
56
+ end
57
+
58
+ ##
59
+ # @param params [Hash] See documentation below
60
+ # @return [Hashie::Mash]
61
+ # @docs [/checkout/modify](https://www.wepay.com/developer/reference/checkout#modify)
62
+ #
63
+ def modify(params)
64
+ post 'checkout/modify', params
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,50 @@
1
+ module Wepay
2
+ module Api
3
+ class Preapproval < Base
4
+ ##
5
+ # @param params [Hash] See documentation below
6
+ # @return [Hashie::Mash]
7
+ # @docs [/preapproval](https://www.wepay.com/developer/reference/preapproval#lookup)
8
+ #
9
+ def show(params)
10
+ post 'preapproval', params
11
+ end
12
+
13
+ ##
14
+ # @param params [Hash] See documentation below
15
+ # @return [Hashie::Mash]
16
+ # @docs [/preapproval/find](https://www.wepay.com/developer/reference/preapproval#find)
17
+ #
18
+ def find(params)
19
+ post 'preapproval/find', params
20
+ end
21
+
22
+ ##
23
+ # @param params [Hash] See documentation below
24
+ # @return [Hashie::Mash]
25
+ # @docs [/preapproval/create](https://www.wepay.com/developer/reference/preapproval#create)
26
+ #
27
+ def create(params)
28
+ post 'preapproval/create', params
29
+ end
30
+
31
+ ##
32
+ # @param params [Hash] See documentation below
33
+ # @return [Hashie::Mash]
34
+ # @docs [/preapproval/cancel](https://www.wepay.com/developer/reference/preapproval#cancel)
35
+ #
36
+ def cancel(params)
37
+ post 'preapproval/cancel', params
38
+ end
39
+
40
+ ##
41
+ # @param params [Hash] See documentation below
42
+ # @return [Hashie::Mash]
43
+ # @docs [/preapproval/modify](https://www.wepay.com/developer/reference/preapproval#modify)
44
+ #
45
+ def modify(params)
46
+ post 'preapproval/modify', params
47
+ end
48
+ end
49
+ end
50
+ end