paypal-rest-api 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb3399d7f3412a84242a4ba9bf2c38447cc1987ca7a90a7e9a51a2628edb7809
4
- data.tar.gz: ab029c43b63e930b30727a127b356c75388df84d8410fd45aac8edac2a37fa53
3
+ metadata.gz: 9dac5031d1b2e2fe6f58b269801cd0e818280d4ed9b54fafc948e1b758d99b87
4
+ data.tar.gz: 4992abcc8678eca0bbc31702d49e6d6a596f7e614f3e8d6966a5716004adde26
5
5
  SHA512:
6
- metadata.gz: b33f4638da4db6992030b9a5fcae5d8836e6cf50cf600adbec07518e8e182f79cb86802f271b8b7276d2391078e8ec11f4a1472865af8bc4a496bbd42f7cb290
7
- data.tar.gz: 583f9a28508b386c282fd9d1b793042abe461425da3cc4227d17f033263e71b8c17ad2abff09c722f90f62d598f09fe5bbf84c0a9ce789cbf29e948d7c223957
6
+ metadata.gz: 63cc5c666b4483eb92bd6d5282173cb2841b517eb39762421c5c546b23d4c2b1e44fe689495aa75dca15f1a86ac66064e9db7fa77d4d389deb1f0e370de107aa
7
+ data.tar.gz: 2e0e52e4fb9e324017f2af0fefe410daab14b01a3f7b322a686c00fb64ca8c1987bec7ece18958eb0c85327bb25f5f20e5fefdb5111043023efa29a049c8a893
data/README.md CHANGED
@@ -16,9 +16,7 @@ bundle add paypal-rest-api
16
16
 
17
17
  ## Usage
18
18
 
19
- - All APIs accept `:query`, `:body` and `:headers` keyword parameters.
20
- - Some APIs (like show, update, delete) require positional parameters with ID of
21
- a resource.
19
+ - All APIs accept optional `:query`, `:body` and `:headers` keyword parameters.
22
20
  - Response has `#body` method to get parsed JSON body.
23
21
  This body has `symbolized` hash keys.
24
22
  - Response contains methods to get original HTTP response.
@@ -34,13 +32,13 @@ client = PaypalAPI::Client.new(
34
32
  live: false
35
33
  )
36
34
 
37
- # APIs calls examples:
35
+ # Usage example:
38
36
  response = client.orders.create(body: body)
39
37
  response = client.orders.show(order_id)
40
- response = client.payments.capture(authorization_id, headers: headers)
38
+ response = client.authorized_payments.capture(authorization_id, headers: headers)
41
39
  response = client.webhooks.list(query: query)
42
40
 
43
- # Client can be used directly to send request to any path
41
+ # Client also can send requests directly, bypassing specific resources methods
44
42
  response = client.post(path, query: query, body: body, headers: headers)
45
43
  response = client.get(path, query: query, body: body, headers: headers)
46
44
  response = client.patch(path, query: query, body: body, headers: headers)
@@ -48,7 +46,7 @@ response = client.put(path, query: query, body: body, headers: headers)
48
46
  response = client.delete(path, query: query, body: body, headers: headers)
49
47
 
50
48
  # Getting response
51
- response.body # parsed JSON. Parsed with `JSON.load(http_body, symbolyzed_keys: true)`
49
+ response.body # Parsed JSON. JSON is parsed lazyly, keys are symbolized.
52
50
  response[:foo] # Gets :foo attribute from parsed body
53
51
  response.fetch(:foo) # Fetches :foo attribute from parsed body
54
52
  response.http_response # original Net::HTTP::Response
@@ -62,7 +60,11 @@ Also PaypalAPI client can be added globally and class methods can be used instea
62
60
 
63
61
  ```ruby
64
62
  # in config/initializers/paypal_api.rb
65
- PaypalAPI.client = PaypalAPI::Client.new(...)
63
+ PaypalAPI.client = PaypalAPI::Client.new(
64
+ client_id: ENV['PAYPAL_CLIENT_ID'],
65
+ client_secret: ENV['PAYPAL_CLIENT_SECRET'],
66
+ live: false
67
+ )
66
68
 
67
69
  # in your business logic
68
70
  response = PaypalAPI.orders.create(body: body)
@@ -91,7 +93,7 @@ When `live` is `false` all requests will be send to the sandbox endpoints.
91
93
 
92
94
  ```ruby
93
95
  client = PaypalAPI::Client.new(
94
- live: true,
96
+ live: true
95
97
  # ...
96
98
  )
97
99
  ```
@@ -137,24 +139,25 @@ All APIs can raise error in case of network error or non-2xx response status cod
137
139
  Errors structure:
138
140
 
139
141
  - `PaypalAPI::Error`
140
- - `PaypalAPI::NetworkError` - any network error
141
- - `PaypalAPI::FailedRequest` - any non-2xx code error
142
- - 400 - `PaypalAPI::BadRequestError`
143
- - 401 - `PaypalAPI::UnauthorizedError`
144
- - 403 - `PaypalAPI::ForbiddenError`
145
- - 404 - `PaypalAPI::NotFoundError`
146
- - 405 - `PaypalAPI::MethodNotAllowedError`
147
- - 406 - `PaypalAPI::NotAcceptableError`
148
- - 409 - `PaypalAPI::ConflictError`
149
- - 415 - `PaypalAPI::UnsupportedMediaTypeError`
150
- - 422 - `PaypalAPI::UnprocessableEntityError`
151
- - 429 - `PaypalAPI::TooManyRequestsError`
152
- - 5xx - `PaypalAPI::FatalError`
153
- - 500 - `PaypalAPI::InternalServerError`
154
- - 503 - `PaypalAPI::ServiceUnavailableError`
142
+ - `PaypalAPI::Errors::NetworkError` - any network error
143
+ - `PaypalAPI::Errors::FailedRequest` - any non-2xx code error
144
+ - 400 - `PaypalAPI::Errors::BadRequest`
145
+ - 401 - `PaypalAPI::Errors::Unauthorized`
146
+ - 403 - `PaypalAPI::Errors::Forbidden`
147
+ - 404 - `PaypalAPI::Errors::NotFound`
148
+ - 405 - `PaypalAPI::Errors::MethodNotAllowed`
149
+ - 406 - `PaypalAPI::Errors::NotAcceptable`
150
+ - 409 - `PaypalAPI::Errors::Conflict`
151
+ - 415 - `PaypalAPI::Errors::UnsupportedMediaType`
152
+ - 422 - `PaypalAPI::Errors::UnprocessableEntity`
153
+ - 429 - `PaypalAPI::Errors::TooManyRequests`
154
+ - 5xx - `PaypalAPI::Errors::FatalError`
155
+ - 500 - `PaypalAPI::Errors::InternalServerError`
156
+ - 503 - `PaypalAPI::Errors::ServiceUnavailable`
155
157
 
156
158
  All errors have additional methods:
157
159
 
160
+ - `#paypal_request_id` - PayPal-Request-Id header sent with request
158
161
  - `#response` - Original response object, can be nil in case of NetworkError
159
162
  - `#request` - Original request object
160
163
  - `#error_name` - Original error name
@@ -167,7 +170,16 @@ All errors have additional methods:
167
170
  begin
168
171
  response = PaypalAPI.payments.capture(authorization_id, body: body)
169
172
  rescue PaypalAPI::Error => error
170
- YourLogger.error(...)
173
+ YourLogger.error(
174
+ error,
175
+ context: {
176
+ error_name: error.error_name,
177
+ error_message: error.error_message,
178
+ error_debug_id: error.error_debug_id,
179
+ error_details: error.error_details
180
+ }
181
+ )
182
+ # `error.request` and `error.response` methods can be used also
171
183
  end
172
184
 
173
185
  ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.3
@@ -7,6 +7,16 @@ module PaypalAPI
7
7
  class AccessToken
8
8
  attr_reader :requested_at, :expires_at, :authorization_string
9
9
 
10
+ #
11
+ # Initializes AccessToken object
12
+ #
13
+ # @param requested_at [Time] Time when token was requested
14
+ # @param expires_in [Integer] Count of seconds until token expires
15
+ # @param access_token [String] Aceess token string generated by PayPal
16
+ # @param token_type [String] Aceess token type, which is constantly `Bearer`
17
+ #
18
+ # @return [AccessToken] Generated AccessToken object
19
+ #
10
20
  def initialize(requested_at:, expires_in:, access_token:, token_type:)
11
21
  @requested_at = requested_at
12
22
  @expires_at = requested_at + expires_in
@@ -14,10 +24,16 @@ module PaypalAPI
14
24
  freeze
15
25
  end
16
26
 
27
+ #
28
+ # Shows if current AccessToken was expired
29
+ #
17
30
  def expired?
18
31
  Time.now >= expires_at
19
32
  end
20
33
 
34
+ #
35
+ # Instance representation string. Default was overwritten to hide original access_token
36
+ #
21
37
  def inspect
22
38
  "#<#{self.class.name} methods: (requested_at, expires_at, expired?, authorization_string)>"
23
39
  end
@@ -4,13 +4,21 @@ module PaypalAPI
4
4
  #
5
5
  # Base class for all PayPal API collections classes
6
6
  #
7
- class Collection
7
+ class APICollection
8
+ # @return current client
8
9
  attr_reader :client
9
10
 
11
+ # Initializes API collection
12
+ #
13
+ # @param client [Client] current client
14
+ #
15
+ # @return [Collection] APIs collection
16
+ #
10
17
  def initialize(client)
11
18
  @client = client
12
19
  end
13
20
 
21
+ # @return global client
14
22
  def self.client
15
23
  PaypalAPI.client
16
24
  end
@@ -6,7 +6,7 @@ module PaypalAPI
6
6
  #
7
7
  # @see https://developer.paypal.com/api/rest/authentication/
8
8
  #
9
- class Authentication < Collection
9
+ class Authentication < APICollection
10
10
  #
11
11
  # Generate access-token API request path
12
12
  #
@@ -33,7 +33,7 @@ module PaypalAPI
33
33
  #
34
34
  # @param query [Hash, nil] Request query string parameters
35
35
  # @param body [Hash, nil] Request body parameters
36
- # @param body [Hash, nil] Request headers
36
+ # @param headers [Hash, nil] Request headers
37
37
  #
38
38
  # @raise [Error] on network error or non 2** status code returned from PayPal
39
39
  # @return [Response] detailed http request-response representation
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PaypalAPI
4
+ #
5
+ # Authorized payments APIs `/v2/payments/authorizations`
6
+ #
7
+ # @see https://developer.paypal.com/docs/api/payments/v2
8
+ #
9
+ class AuthorizedPayments < APICollection
10
+ #
11
+ # Common class and instance methods
12
+ #
13
+ module APIs
14
+ # @!macro [new] request
15
+ # @param query [Hash, nil] Request query parameters
16
+ # @param body [Hash, nil] Request body parameters
17
+ # @param headers [Hash, nil] Request headers
18
+ # @return [Response] Response object
19
+
20
+ #
21
+ # Show details for authorized payment
22
+ #
23
+ # @see https://developer.paypal.com/docs/api/payments/v2/#authorizations_get
24
+ #
25
+ # @param authorization_id [String] Authorization ID
26
+ # @macro request
27
+ #
28
+ def show(authorization_id, query: nil, body: nil, headers: nil)
29
+ client.get("/v2/payments/authorizations/#{authorization_id}", query: query, body: body, headers: headers)
30
+ end
31
+
32
+ #
33
+ # Capture authorized payment
34
+ #
35
+ # @see https://developer.paypal.com/docs/api/payments/v2/#authorizations_capture
36
+ #
37
+ # @param authorization_id [String] Authorization ID
38
+ # @macro request
39
+ #
40
+ def capture(authorization_id, query: nil, body: nil, headers: nil)
41
+ client.post("/v2/payments/authorizations/#{authorization_id}/capture", query: query, body: body, headers: headers)
42
+ end
43
+
44
+ # Reauthorize authorized payment
45
+ #
46
+ # @see https://developer.paypal.com/docs/api/payments/v2/#authorizations_capture
47
+ #
48
+ # @param authorization_id [String] Authorization ID
49
+ # @macro request
50
+ #
51
+ def reauthorize(authorization_id, query: nil, body: nil, headers: nil)
52
+ client.post("/v2/payments/authorizations/#{authorization_id}/reauthorize", query: query, body: body, headers: headers)
53
+ end
54
+
55
+ # Void authorized payment
56
+ #
57
+ # @see https://developer.paypal.com/docs/api/payments/v2/#authorizations_void
58
+ #
59
+ # @param authorization_id [String] Authorization ID
60
+ # @macro request
61
+ #
62
+ def void(authorization_id, query: nil, body: nil, headers: nil)
63
+ client.post("/v2/payments/authorizations/#{authorization_id}/void", query: query, body: body, headers: headers)
64
+ end
65
+ end
66
+
67
+ include APIs
68
+ extend APIs
69
+ end
70
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PaypalAPI
4
+ #
5
+ # Captured payments APIs `/v2/payments/captures`
6
+ #
7
+ # @see https://developer.paypal.com/docs/api/payments/v2
8
+ #
9
+ class CapturedPayments < APICollection
10
+ #
11
+ # Common class and instance methods
12
+ #
13
+ module APIs
14
+ # @!macro [new] request
15
+ # @param query [Hash, nil] Request query parameters
16
+ # @param body [Hash, nil] Request body parameters
17
+ # @param headers [Hash, nil] Request headers
18
+ # @return [Response] Response object
19
+
20
+ #
21
+ # Show captured payment details
22
+ #
23
+ # @see https://developer.paypal.com/docs/api/payments/v2/#captures_get
24
+ #
25
+ # @param capture_id [String] Capture ID
26
+ # @macro request
27
+ #
28
+ def show(capture_id, query: nil, body: nil, headers: nil)
29
+ client.get("/v2/payments/captures/#{capture_id}", query: query, body: body, headers: headers)
30
+ end
31
+
32
+ #
33
+ # Refund captured payment
34
+ #
35
+ # @see https://developer.paypal.com/docs/api/payments/v2/#captures_refund
36
+ #
37
+ # @param capture_id [String] Capture ID
38
+ # @macro request
39
+ #
40
+ def refund(capture_id, query: nil, body: nil, headers: nil)
41
+ client.post("/v2/payments/captures/#{capture_id}/refund", query: query, body: body, headers: headers)
42
+ end
43
+ end
44
+
45
+ include APIs
46
+ extend APIs
47
+ end
48
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PaypalAPI
4
+ #
5
+ # Merchants can use the Catalog Products API to create products, which are goods and services.
6
+ #
7
+ # @see https://developer.paypal.com/docs/api/catalog-products/v1/
8
+ #
9
+ class CatalogProducts < APICollection
10
+ #
11
+ # Common class and instance methods
12
+ #
13
+ module APIs
14
+ # @!macro [new] request
15
+ # @param query [Hash, nil] Request query parameters
16
+ # @param body [Hash, nil] Request body parameters
17
+ # @param headers [Hash, nil] Request headers
18
+ # @return [Response] Response object
19
+
20
+ #
21
+ # Create product
22
+ #
23
+ # @see https://developer.paypal.com/docs/api/catalog-products/v1/#products_create
24
+ #
25
+ # @macro request
26
+ #
27
+ def create(query: nil, body: nil, headers: nil)
28
+ client.post("/v1/catalogs/products", query: query, body: body, headers: headers)
29
+ end
30
+
31
+ #
32
+ # List products
33
+ #
34
+ # @see https://developer.paypal.com/docs/api/catalog-products/v1/#products_list
35
+ #
36
+ # @macro request
37
+ #
38
+ def list(query: nil, body: nil, headers: nil)
39
+ client.get("/v1/catalogs/products", query: query, body: body, headers: headers)
40
+ end
41
+
42
+ #
43
+ # Show product details
44
+ #
45
+ # @see https://developer.paypal.com/docs/api/catalog-products/v1/#products_get
46
+ #
47
+ # @param product_id [String] Product ID
48
+ # @macro request
49
+ #
50
+ def show(product_id, query: nil, body: nil, headers: nil)
51
+ client.get("/v1/catalogs/products/#{product_id}", query: query, body: body, headers: headers)
52
+ end
53
+
54
+ #
55
+ # Update product
56
+ #
57
+ # @see https://developer.paypal.com/docs/api/catalog-products/v1/#products_patch
58
+ #
59
+ # @param product_id [String] Product ID
60
+ # @macro request
61
+ #
62
+ def update(product_id, query: nil, body: nil, headers: nil)
63
+ client.patch("/v1/catalogs/products/#{product_id}", query: query, body: body, headers: headers)
64
+ end
65
+ end
66
+
67
+ include APIs
68
+ extend APIs
69
+ end
70
+ end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PaypalAPI
4
+ #
5
+ # Create, update, retrieve, authorize, and capture orders.
6
+ #
7
+ # @see https://developer.paypal.com/docs/api/orders/v2/
8
+ #
9
+ class Orders < APICollection
10
+ #
11
+ # Common class and instance methods
12
+ #
13
+ module APIs
14
+ # @!macro [new] request
15
+ # @param query [Hash, nil] Request query parameters
16
+ # @param body [Hash, nil] Request body parameters
17
+ # @param headers [Hash, nil] Request headers
18
+ # @return [Response] Response object
19
+
20
+ #
21
+ # Create Order
22
+ #
23
+ # @see https://developer.paypal.com/docs/api/orders/v2/#orders_create
24
+ #
25
+ # @macro request
26
+ #
27
+ def create(query: nil, body: nil, headers: nil)
28
+ client.post("/v2/checkout/orders", query: query, body: body, headers: headers)
29
+ end
30
+
31
+ #
32
+ # Show order details
33
+ #
34
+ # @see https://developer.paypal.com/docs/api/orders/v2/#orders_get
35
+ #
36
+ # @param id [String] Order ID
37
+ # @macro request
38
+ #
39
+ def show(id, query: nil, body: nil, headers: nil)
40
+ client.get("/v2/checkout/orders/#{id}", query: query, body: body, headers: headers)
41
+ end
42
+
43
+ #
44
+ # Update order
45
+ #
46
+ # @see https://developer.paypal.com/docs/api/orders/v2/#orders_patch
47
+ #
48
+ # @param id [String] Order ID
49
+ # @macro request
50
+ #
51
+ def update(id, query: nil, body: nil, headers: nil)
52
+ client.patch("/v2/checkout/orders/#{id}", query: query, body: body, headers: headers)
53
+ end
54
+
55
+ #
56
+ # Confirm the Order
57
+ #
58
+ # @see https://developer.paypal.com/docs/api/orders/v2/#orders_confirm
59
+ #
60
+ # @param id [String] Order ID
61
+ # @macro request
62
+ #
63
+ def confirm(id, query: nil, body: nil, headers: nil)
64
+ client.post("/v2/checkout/orders/#{id}/confirm-payment-source", query: query, body: body, headers: headers)
65
+ end
66
+
67
+ #
68
+ # Authorize payment for order
69
+ #
70
+ # @see https://developer.paypal.com/docs/api/orders/v2/#orders_authorize
71
+ #
72
+ # @param id [String] Order ID
73
+ # @macro request
74
+ #
75
+ def authorize(id, query: nil, body: nil, headers: nil)
76
+ client.post("/v2/checkout/orders/#{id}/authorize", query: query, body: body, headers: headers)
77
+ end
78
+
79
+ #
80
+ # Capture payment for order
81
+ #
82
+ # @see https://developer.paypal.com/docs/api/orders/v2/#orders_capture
83
+ #
84
+ # @param id [String] Order ID
85
+ # @macro request
86
+ #
87
+ def capture(id, query: nil, body: nil, headers: nil)
88
+ client.post("/v2/checkout/orders/#{id}/capture", query: query, body: body, headers: headers)
89
+ end
90
+
91
+ #
92
+ # Add tracking information for an Order
93
+ #
94
+ # @see https://developer.paypal.com/docs/api/orders/v2/#orders_track_create
95
+ #
96
+ # @param id [String] Order ID
97
+ # @macro request
98
+ #
99
+ def track(id, query: nil, body: nil, headers: nil)
100
+ client.post("/v2/checkout/orders/#{id}/track", query: query, body: body, headers: headers)
101
+ end
102
+
103
+ #
104
+ # Update or cancel tracking information for a PayPal order
105
+ #
106
+ # @see https://developer.paypal.com/docs/api/orders/v2/#orders_trackers_patch
107
+ #
108
+ # @param id [String] Order ID
109
+ # @param tracker_id [String] Tracker ID
110
+ # @macro request
111
+ #
112
+ def update_tracker(id, tracker_id, query: nil, body: nil, headers: nil)
113
+ client.patch("/v2/checkout/orders/#{id}/trackers/#{tracker_id}", query: query, body: body, headers: headers)
114
+ end
115
+ end
116
+
117
+ include APIs
118
+ extend APIs
119
+ end
120
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PaypalAPI
4
+ #
5
+ # Refunds APIs `/v2/payments/refunds`
6
+ #
7
+ # @see https://developer.paypal.com/docs/api/payments/v2
8
+ #
9
+ class Refunds < APICollection
10
+ #
11
+ # Common class and instance methods
12
+ #
13
+ module APIs
14
+ #
15
+ # Show refund details
16
+ #
17
+ # @see https://developer.paypal.com/docs/api/payments/v2/#refunds_get
18
+ #
19
+ # @param refund_id [String]
20
+ # @macro request
21
+ #
22
+ def show(refund_id, query: nil, body: nil, headers: nil)
23
+ client.get("/v2/payments/refunds/#{refund_id}", query: query, body: body, headers: headers)
24
+ end
25
+ end
26
+
27
+ include APIs
28
+ extend APIs
29
+ end
30
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PaypalAPI
4
+ #
5
+ # Manages tracking information
6
+ #
7
+ # @see https://developer.paypal.com/docs/api/tracking/v1/
8
+ #
9
+ class ShipmentTracking < APICollection
10
+ #
11
+ # Common class and instance methods
12
+ #
13
+ module APIs
14
+ # @!macro [new] request
15
+ # @param query [Hash, nil] Request query parameters
16
+ # @param body [Hash, nil] Request body parameters
17
+ # @param headers [Hash, nil] Request headers
18
+ # @return [Response] Response object
19
+
20
+ #
21
+ # Add tracking information for multiple PayPal transactions
22
+ #
23
+ # @see https://developer.paypal.com/docs/api/tracking/v1/#trackers-batch_post
24
+ #
25
+ # @macro request
26
+ #
27
+ def add(query: nil, body: nil, headers: nil)
28
+ client.post("/v1/shipping/trackers-batch", query: query, body: body, headers: headers)
29
+ end
30
+
31
+ #
32
+ # Update or cancel tracking information for PayPal transaction
33
+ #
34
+ # @see https://developer.paypal.com/docs/api/tracking/v1/#trackers_put
35
+ #
36
+ # @param id [String] Tracker ID
37
+ # @macro request
38
+ #
39
+ def update(id, query: nil, body: nil, headers: nil)
40
+ client.put("/v1/shipping/trackers/#{id}", query: query, body: body, headers: headers)
41
+ end
42
+
43
+ #
44
+ # Shows tracking information, by tracker ID, for a PayPal transaction.
45
+ #
46
+ # @see https://developer.paypal.com/docs/api/tracking/v1/#trackers_get
47
+ #
48
+ # @param id [String] Order ID
49
+ # @macro request
50
+ #
51
+ def show(id, query: nil, body: nil, headers: nil)
52
+ client.get("/v1/shipping/trackers/#{id}", query: query, body: body, headers: headers)
53
+ end
54
+ end
55
+
56
+ include APIs
57
+ extend APIs
58
+ end
59
+ end