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,50 @@
1
+ module Wepay
2
+ module Api
3
+ class Subscription < Base
4
+ ##
5
+ # @param params [Hash] See documentation below
6
+ # @return [Hashie::Mash]
7
+ # @docs [/subscription](https://www.wepay.com/developer/reference/subscription#lookup)
8
+ #
9
+ def show(params)
10
+ post 'subscription', params
11
+ end
12
+
13
+ ##
14
+ # @param params [Hash] See documentation below
15
+ # @return [Hashie::Mash]
16
+ # @docs [/subscription/find](https://www.wepay.com/developer/reference/subscription#find)
17
+ #
18
+ def find(params)
19
+ post 'subscription/find', params
20
+ end
21
+
22
+ ##
23
+ # @param params [Hash] See documentation below
24
+ # @return [Hashie::Mash]
25
+ # @docs [/subscription/create](https://www.wepay.com/developer/reference/subscription#create)
26
+ #
27
+ def create(params)
28
+ post 'subscription/create', params
29
+ end
30
+
31
+ ##
32
+ # @param params [Hash] See documentation below
33
+ # @return [Hashie::Mash]
34
+ # @docs [/subscription/cancel](https://www.wepay.com/developer/reference/subscription#cancel)
35
+ #
36
+ def cancel(params)
37
+ post 'subscription/cancel', params
38
+ end
39
+
40
+ ##
41
+ # @param params [Hash] See documentation below
42
+ # @return [Hashie::Mash]
43
+ # @docs [/subscription/modify](https://www.wepay.com/developer/reference/subscription#modify)
44
+ #
45
+ def modify(params)
46
+ post 'subscription/modify', params
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,59 @@
1
+ module Wepay
2
+ module Api
3
+ class SubscriptionPlan < Base
4
+ ##
5
+ # @param params [Hash] See documentation below
6
+ # @return [Hashie::Mash]
7
+ # @docs [/subscription_plan](https://www.wepay.com/developer/reference/subscription_plan#lookup)
8
+ #
9
+ def show(params)
10
+ post 'subscription_plan', params
11
+ end
12
+
13
+ ##
14
+ # @param params [Hash] See documentation below
15
+ # @return [Hashie::Mash]
16
+ # @docs [/subscription_plan/find](https://www.wepay.com/developer/reference/subscription_plan#find)
17
+ #
18
+ def find(params)
19
+ post 'subscription_plan/find', params
20
+ end
21
+
22
+ ##
23
+ # @param params [Hash] See documentation below
24
+ # @return [Hashie::Mash]
25
+ # @docs [/subscription_plan/create](https://www.wepay.com/developer/reference/subscription_plan#create)
26
+ #
27
+ def create(params)
28
+ post 'subscription_plan/create', params
29
+ end
30
+
31
+ ##
32
+ # @param params [Hash] See documentation below
33
+ # @return [Hashie::Mash]
34
+ # @docs [/subscription_plan/delete](https://www.wepay.com/developer/reference/subscription_plan#delete)
35
+ #
36
+ def delete(params)
37
+ post 'subscription_plan/delete', params
38
+ end
39
+
40
+ ##
41
+ # @param params [Hash] See documentation below
42
+ # @return [Hashie::Mash]
43
+ # @docs [/subscription_plan/get_button](https://www.wepay.com/developer/reference/subscription_plan#get_button)
44
+ #
45
+ def get_button(params)
46
+ post 'subscription_plan/get_button', params
47
+ end
48
+
49
+ ##
50
+ # @param params [Hash] See documentation below
51
+ # @return [Hashie::Mash]
52
+ # @docs [/subscription_plan/modify](https://www.wepay.com/developer/reference/subscription_plan#modify)
53
+ #
54
+ def modify(params)
55
+ post 'subscription_plan/modify', params
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,41 @@
1
+ module Wepay
2
+ module Api
3
+ class Withdrawal < Base
4
+ ##
5
+ # @param params [Hash] See documentation below
6
+ # @return [Hashie::Mash]
7
+ # @docs [/withdrawal](https://www.wepay.com/developer/reference/withdrawal#lookup)
8
+ #
9
+ def show(params)
10
+ post 'withdrawal', params
11
+ end
12
+
13
+ ##
14
+ # @param params [Hash] See documentation below
15
+ # @return [Hashie::Mash]
16
+ # @docs [/withdrawal/find](https://www.wepay.com/developer/reference/withdrawal#find)
17
+ #
18
+ def find(params)
19
+ post 'withdrawal/find', params
20
+ end
21
+
22
+ ##
23
+ # @param params [Hash] See documentation below
24
+ # @return [Hashie::Mash]
25
+ # @docs [/withdrawal/create](https://www.wepay.com/developer/reference/withdrawal#create)
26
+ #
27
+ def create(params)
28
+ post 'withdrawal/create', params
29
+ end
30
+
31
+ ##
32
+ # @param params [Hash] See documentation below
33
+ # @return [Hashie::Mash]
34
+ # @docs [/withdrawal/modify](https://www.wepay.com/developer/reference/withdrawal#modify)
35
+ #
36
+ def modify(params)
37
+ post 'withdrawal/modify', params
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,63 @@
1
+ require "faraday"
2
+ require "faraday_middleware"
3
+
4
+ module Wepay
5
+ class Client
6
+ DEFAULTS = {
7
+ open_timeout: 5,
8
+ timeout: 10
9
+ }
10
+
11
+ def initialize
12
+ @connection = Faraday.new(url, connection_options) do |builder|
13
+ # Request
14
+ builder.use Wepay::Middleware::CatchTimeout
15
+
16
+ yield builder if block_given?
17
+
18
+ # Response
19
+ builder.use FaradayMiddleware::Mashify
20
+ builder.use Wepay::Middleware::CheckStatus
21
+ builder.use FaradayMiddleware::ParseJson
22
+
23
+ builder.adapter :net_http
24
+ end
25
+ end
26
+
27
+ def get(*args, &block)
28
+ make_request(:get, *args, &block)
29
+ end
30
+
31
+ def post(*args, &block)
32
+ make_request(:post, *args, &block)
33
+ end
34
+
35
+ private
36
+
37
+ def connection_options
38
+ {
39
+ request: {
40
+ open_timeout: open_timeout,
41
+ timeout: timeout,
42
+ }
43
+ }
44
+ end
45
+
46
+ def url
47
+ raise NotImplementedError
48
+ end
49
+
50
+ def make_request(http_method, *args, &block)
51
+ response = @connection.send(http_method, *args, &block)
52
+ response.body
53
+ end
54
+
55
+ def open_timeout
56
+ Wepay.open_timeout || DEFAULTS[:open_timeout]
57
+ end
58
+
59
+ def timeout
60
+ Wepay.timeout || DEFAULTS[:timeout]
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,31 @@
1
+ module Wepay
2
+ ##
3
+ # Client for query and manipulation of Wepay's data.
4
+ #
5
+ class Client::Data < Client
6
+ def initialize
7
+ super() do |builder|
8
+ # Request
9
+ builder.use FaradayMiddleware::EncodeJson
10
+ end
11
+ end
12
+
13
+ private
14
+ def url
15
+ Wepay.api_endpoint
16
+ end
17
+
18
+ def make_request(http_method, *args, &block)
19
+ params = args[1]
20
+ access_token = params.delete(:access_token) if params.is_a?(Hash)
21
+ access_token ||= Wepay.access_token
22
+
23
+ response = @connection.send http_method, *args do |builder|
24
+ builder.headers['Authorization'] = "Bearer #{access_token}"
25
+ block.call(builder) if block_given?
26
+ end
27
+
28
+ response.body
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,30 @@
1
+ module Wepay
2
+ ##
3
+ # Client for getting access token from Wepay's oauth endpoint.
4
+ #
5
+ class Client::Token < Client
6
+ def get_access_token(code, redirect_uri)
7
+ params = {
8
+ client_id: client_id,
9
+ client_secret: client_secret,
10
+ code: code,
11
+ redirect_uri: redirect_uri
12
+ }
13
+
14
+ get '', params
15
+ end
16
+
17
+ private
18
+ def url
19
+ Wepay.oauth_token_endpoint
20
+ end
21
+
22
+ def client_id
23
+ Wepay.client_id
24
+ end
25
+
26
+ def client_secret
27
+ Wepay.client_secret
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,34 @@
1
+ module Wepay
2
+ module Configuration
3
+ attr_accessor :client_id, :client_secret, :access_token, :account_id, :scope, :using_stage
4
+
5
+ ##
6
+ # Time to wait for the connection to Wepay to open. After that
7
+ # {Wepay::TimeoutError} is thrown.
8
+ #
9
+ attr_accessor :open_timeout
10
+
11
+ ##
12
+ # Time to wait for the first block of response from Wepay. After that
13
+ # {Wepay::TimeoutError} is thrown.
14
+ #
15
+ attr_accessor :timeout
16
+
17
+ def configure
18
+ yield self
19
+ self
20
+ end
21
+
22
+ def api_endpoint
23
+ using_stage ? "https://stage.wepayapi.com/v2/" : "https://wepayapi.com/v2/"
24
+ end
25
+
26
+ def oauth_authorization_endpoint
27
+ using_stage ? "https://stage.wepay.com/v2/oauth2/authorize" : "https://www.wepay.com/v2/oauth2/authorize"
28
+ end
29
+
30
+ def oauth_token_endpoint
31
+ using_stage ? "https://stage.wepayapi.com/v2/oauth2/token" : "https://wepayapi.com/v2/oauth2/token"
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,28 @@
1
+ module Wepay
2
+ class Error < StandardError
3
+ end
4
+
5
+ ##
6
+ # This error is raised when there is an error in the request.
7
+ #
8
+ class ApiError < Error
9
+ attr_reader :type, :code
10
+
11
+ def initialize(type, message, code)
12
+ super(message)
13
+ @type = type
14
+ @code = code
15
+ end
16
+
17
+ def message
18
+ "#{type} (#{code}): #{super}"
19
+ end
20
+ end
21
+
22
+ ##
23
+ # @see Wepay::Configuration#open_timeout
24
+ # @see Wepay::Configuration#timeout
25
+ #
26
+ class TimeoutError < Error
27
+ end
28
+ end
@@ -0,0 +1,44 @@
1
+ require "faraday"
2
+ require "faraday_middleware/response_middleware"
3
+
4
+ module Wepay
5
+ module Middleware
6
+ ##
7
+ # Translates `Faraday::Error::TimeoutError` into `Wepay::TimeoutError`.
8
+ #
9
+ # @raise Wepay::TimeoutError
10
+ #
11
+ class CatchTimeout
12
+ def initialize(app)
13
+ @app = app
14
+ end
15
+
16
+ def call(env)
17
+ begin
18
+ @app.call(env)
19
+ rescue Faraday::Error::TimeoutError
20
+ raise Wepay::TimeoutError
21
+ end
22
+ end
23
+ end
24
+
25
+ ##
26
+ # Checks for errors in responses from Wepay, in which case it raises a
27
+ # `Wepay::ApiError` which contains the code and the message found in the response.
28
+ #
29
+ # @raise Wepay::ApiError
30
+ #
31
+ class CheckStatus < Faraday::Response::Middleware
32
+ def on_complete(env)
33
+ response = env[:body]
34
+ if response.is_a?(Hash) && response["error"]
35
+ type = response["error"]
36
+ message = response["error_description"]
37
+ code = response["error_code"]
38
+
39
+ raise Wepay::ApiError.new(type, message, code)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,32 @@
1
+ require 'uri'
2
+
3
+ module Wepay
4
+ module OAuth
5
+ ##
6
+ # @param redirect_uri [String] The uri the user will be redirected to after authorization. Must have the same domain as the application.
7
+ # @return [String] URL to redirect user to Wepay Authorization page
8
+ #
9
+ def authorization_url(redirect_uri)
10
+ url = URI.parse(Wepay.oauth_authorization_endpoint)
11
+
12
+ query = {
13
+ client_id: Wepay.client_id,
14
+ scope: Wepay.scope,
15
+ redirect_uri: redirect_uri
16
+ }
17
+
18
+ url.query = query.map { |k, v| "#{k}=#{v}" }.join("&")
19
+ url.to_s
20
+ end
21
+
22
+ ##
23
+ # @param code [String] The authorization code used to get the access token
24
+ # @param redirect_uri [String] The uri the user was redirected to after authorization. Must be the same as passed in /oauth2/authorize
25
+ # @return [Hashie::Mash]
26
+ #
27
+ def get_access_token(code, redirect_uri)
28
+ client = Wepay::Client::Token.new
29
+ client.get_access_token(code, redirect_uri)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Wepay
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,35 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://stage.wepayapi.com/v2/null
6
+ body:
7
+ encoding: UTF-8
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.8.8
12
+ Authorization:
13
+ - Bearer access_token
14
+ response:
15
+ status:
16
+ code: 501
17
+ message:
18
+ headers:
19
+ server:
20
+ - nginx
21
+ date:
22
+ - Tue, 03 Dec 2013 09:34:30 GMT
23
+ content-type:
24
+ - application/json
25
+ transfer-encoding:
26
+ - chunked
27
+ connection:
28
+ - close
29
+ body:
30
+ encoding: UTF-8
31
+ string: '{"error":"invalid_request","error_description":"that is not a recognized
32
+ WePay API call","error_code":1001}'
33
+ http_version:
34
+ recorded_at: Tue, 03 Dec 2013 09:34:31 GMT
35
+ recorded_with: VCR 2.8.0