paypal_client 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/lib/paypal_client.rb +95 -14
- data/lib/paypal_client/errors.rb +4 -4
- data/lib/paypal_client/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a48ce325f1120721c1fa86079d4d31e7c4f1ed23
|
4
|
+
data.tar.gz: 48409e8f8c1f8e42f5997aa548d8d89124e0b29b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf5f59ba813b4bb5e0e3808459f9f21700e36c9ea16fa25aef0a18769306be008a922f5415d5f91f2efbda88334788ce074804b1e49c98a82d5c46c76a681cec
|
7
|
+
data.tar.gz: bd16a3f58df0d48260184711a81cc25161cdf9bab415eb9ae2cd3426e824ec74da9727878e4e7df46e03099b114875b5890ace94b937808372db6134caf708a8
|
data/Gemfile
CHANGED
data/lib/paypal_client.rb
CHANGED
@@ -4,6 +4,12 @@ require 'paypal_client/version'
|
|
4
4
|
require 'paypal_client/errors'
|
5
5
|
require 'active_support/cache'
|
6
6
|
|
7
|
+
#
|
8
|
+
# Module PaypalClient provides an easy to use API client for the Paypal
|
9
|
+
# REST API https://developer.paypal.com/docs/api/overview/
|
10
|
+
#
|
11
|
+
# @author Dennis van der Vliet <dennis.vandervliet@gmail.com>
|
12
|
+
#
|
7
13
|
module PaypalClient
|
8
14
|
require 'faraday'
|
9
15
|
require 'faraday_middleware'
|
@@ -18,6 +24,9 @@ module PaypalClient
|
|
18
24
|
# so we have them expiry 3600 seconds before they actually expire
|
19
25
|
TOKEN_EXPIRY_MARGIN = 60 * 60
|
20
26
|
|
27
|
+
# Build a new instance of PaypalClient based on defaults and environment
|
28
|
+
# variables
|
29
|
+
# @return [PaypalClient::Client] an instance of PaypalClient::Client
|
21
30
|
def self.build
|
22
31
|
@client ||= Client.new(
|
23
32
|
client_id: ENV.fetch('PAYPAL_CLIENT_ID'),
|
@@ -28,6 +37,15 @@ module PaypalClient
|
|
28
37
|
)
|
29
38
|
end
|
30
39
|
|
40
|
+
# Creates a new instance of PaypalClient::Client
|
41
|
+
#
|
42
|
+
# @param [String] client_id: Paypal Client ID
|
43
|
+
# @param [String] client_secret: Paypal Client Secret
|
44
|
+
# @param [ActiveSupport::Cache::Store] cache: ActiveSupport::Cache::Store compaitable store to store auth token
|
45
|
+
# @param [Boolean] sandbox: true <description>
|
46
|
+
# @param [String] version: <description>
|
47
|
+
# @param [Logger] logger: nil <description>
|
48
|
+
|
31
49
|
def initialize(client_id:, client_secret:, cache:, sandbox: true, version:, logger: nil)
|
32
50
|
@client_id = client_id
|
33
51
|
@client_secret = client_secret
|
@@ -37,25 +55,72 @@ module PaypalClient
|
|
37
55
|
@logger = logger
|
38
56
|
end
|
39
57
|
|
40
|
-
|
41
|
-
|
42
|
-
|
58
|
+
# Send a GET request to the Paypal API
|
59
|
+
#
|
60
|
+
# @param [String] path Path to call on Paypal API (eg. /payments/payment)
|
61
|
+
# @param [Hash] data Hash to send as query parameters
|
62
|
+
# @param [Hash] headers Hash of custom request headers
|
63
|
+
#
|
64
|
+
# @return [Faraday::Response>] Faraday response. Call the `.body` method on it to access the response data.
|
43
65
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
faraday.response :json, content_type: /\bjson$/, parser_options: { symbolize_names: true }
|
66
|
+
def get(path, data = {}, headers = {})
|
67
|
+
connection.get(merged_path(path), data, merged_headers(headers))
|
68
|
+
end
|
48
69
|
|
49
|
-
|
50
|
-
|
70
|
+
# Send a POST request to the Paypal API
|
71
|
+
#
|
72
|
+
# @param [String] path Path to call on Paypal API (eg. /payments/payment)
|
73
|
+
# @param [Hash] data Hash to send as POST data. Will be turned into JSON.
|
74
|
+
# @param [Hash] headers Hash of custom request headers
|
75
|
+
#
|
76
|
+
# @return [Faraday::Response>] Faraday response. Call the `.body` method on it to access the response data.
|
77
|
+
|
78
|
+
def post(path, data = {}, headers = {})
|
79
|
+
connection.post(merged_path(path), data, merged_headers(headers))
|
51
80
|
end
|
52
81
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
82
|
+
# Send a PUT request to the Paypal API
|
83
|
+
#
|
84
|
+
# @param [String] path Path to call on Paypal API (eg. /payments/payment)
|
85
|
+
# @param [Hash] data Hash to send as POST data. Will be turned into JSON.
|
86
|
+
# @param [Hash] headers Hash of custom request headers
|
87
|
+
#
|
88
|
+
# @return [Faraday::Response>] Faraday response. Call the `.body` method on it to access the response data.
|
89
|
+
|
90
|
+
def put(path, data = {}, headers = {})
|
91
|
+
connection.put(merged_path(path), data, merged_headers(headers))
|
92
|
+
end
|
93
|
+
|
94
|
+
# Send a PATCH request to the Paypal API
|
95
|
+
#
|
96
|
+
# @param [String] path Path to call on Paypal API (eg. /payments/payment)
|
97
|
+
# @param [Hash] data Hash to send as POST data. Will be turned into JSON.
|
98
|
+
# @param [Hash] headers Hash of custom request headers
|
99
|
+
#
|
100
|
+
# @return [Faraday::Response>] Faraday response. Call the `.body` method on it to access the response data.
|
101
|
+
|
102
|
+
def patch(path, data = {}, headers = {})
|
103
|
+
connection.patch(merged_path(path), data, merged_headers(headers))
|
57
104
|
end
|
58
105
|
|
106
|
+
# Send a DELETE request to the Paypal API
|
107
|
+
#
|
108
|
+
# @param [String] path Path to call on Paypal API (eg. /payments/payment)
|
109
|
+
# @param [Hash] data Hash to send as POST data. Will be turned into JSON.
|
110
|
+
# @param [Hash] headers Hash of custom request headers
|
111
|
+
#
|
112
|
+
# @return [Faraday::Response>] Faraday response. Call the `.body` method on it to access the response data.
|
113
|
+
|
114
|
+
def delete(path, data = {}, headers = {})
|
115
|
+
connection.delete(merged_path(path), data, merged_headers(headers))
|
116
|
+
end
|
117
|
+
|
118
|
+
# Request auth token from Paypal using the client_id and client_secret
|
119
|
+
#
|
120
|
+
# @param [<Boolean>] force: false Forces a refresh of the token even if cached
|
121
|
+
#
|
122
|
+
# @return [<String>] Valid auth token from Paypal
|
123
|
+
#
|
59
124
|
def auth_token(force: false)
|
60
125
|
return @cache.read(TOKEN_CACHE_KEY) if @cache.exist?(TOKEN_CACHE_KEY) && force == false
|
61
126
|
|
@@ -65,6 +130,21 @@ module PaypalClient
|
|
65
130
|
end
|
66
131
|
end
|
67
132
|
|
133
|
+
def connection
|
134
|
+
@conn ||= Faraday.new(url: base_url) do |faraday|
|
135
|
+
faraday.use PaypalClient::Errors::Middleware
|
136
|
+
|
137
|
+
faraday.headers = default_headers
|
138
|
+
faraday.response @logger if @logger
|
139
|
+
faraday.request :json
|
140
|
+
faraday.response :json,
|
141
|
+
content_type: /\bjson$/,
|
142
|
+
parser_options: { symbolize_names: true }
|
143
|
+
|
144
|
+
faraday.adapter *adapter
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
68
148
|
private
|
69
149
|
|
70
150
|
def base_url
|
@@ -96,7 +176,8 @@ module PaypalClient
|
|
96
176
|
|
97
177
|
response = connection.post(endpoint,
|
98
178
|
'grant_type=client_credentials',
|
99
|
-
authorization: "Basic #{basic_auth}",
|
179
|
+
authorization: "Basic #{basic_auth}",
|
180
|
+
"Content-Type": 'application/x-www-form-urlencoded')
|
100
181
|
response.body
|
101
182
|
end
|
102
183
|
|
data/lib/paypal_client/errors.rb
CHANGED
@@ -13,8 +13,8 @@ module PaypalClient
|
|
13
13
|
@body = response.body
|
14
14
|
|
15
15
|
if @body.class == Hash
|
16
|
-
@error = @body[:name] if @body.
|
17
|
-
@error_message = @body[:message] if @body.
|
16
|
+
@error = @body[:name] if @body.key?(:name)
|
17
|
+
@error_message = @body[:message] if @body.key?(:message)
|
18
18
|
else
|
19
19
|
@error = response.reason_phrase if @error.nil?
|
20
20
|
end
|
@@ -22,7 +22,7 @@ module PaypalClient
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def inspect
|
25
|
-
extra =
|
25
|
+
extra = ''
|
26
26
|
extra << " status_code: #{status_code.inspect}" unless status_code.nil?
|
27
27
|
extra << " body: #{body.inspect}" unless body.nil?
|
28
28
|
"#<#{self.class.name}: #{message}#{extra}>"
|
@@ -38,7 +38,7 @@ module PaypalClient
|
|
38
38
|
class UnsupportedMediaType < Error; end
|
39
39
|
class UnprocessableEntity < Error; end
|
40
40
|
class RateLimitReached < Error; end
|
41
|
-
|
41
|
+
|
42
42
|
class InternalServerError < Error; end
|
43
43
|
class ServiceUnavailable < Error; end
|
44
44
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paypal_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dennis van der Vliet
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-12-
|
11
|
+
date: 2018-12-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|