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.
@@ -0,0 +1,213 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PaypalAPI
4
+ #
5
+ # Manages subscriptions for recurring PayPal payments
6
+ #
7
+ # @see https://developer.paypal.com/docs/api/subscriptions/v1/
8
+ #
9
+ class Subscriptions < 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
+ # Creates a plan that defines pricing and billing cycle details for subscriptions.
22
+ #
23
+ # @see https://developer.paypal.com/docs/api/subscriptions/v1/#plans_create
24
+ #
25
+ # @macro request
26
+ #
27
+ def create_plan(query: nil, body: nil, headers: nil)
28
+ client.post("/v1/billing/plans", query: query, body: body, headers: headers)
29
+ end
30
+
31
+ #
32
+ # List plans
33
+ #
34
+ # @see https://developer.paypal.com/docs/api/subscriptions/v1/#plans_list
35
+ #
36
+ # @macro request
37
+ #
38
+ def list_plans(query: nil, body: nil, headers: nil)
39
+ client.get("/v1/billing/plans", query: query, body: body, headers: headers)
40
+ end
41
+
42
+ #
43
+ # Show plan details
44
+ #
45
+ # @see https://developer.paypal.com/docs/api/subscriptions/v1/#plans_get
46
+ #
47
+ # @param plan_id [String] Plan ID
48
+ # @macro request
49
+ #
50
+ def show_plan(plan_id, query: nil, body: nil, headers: nil)
51
+ client.get("/v1/billing/plans/#{plan_id}", query: query, body: body, headers: headers)
52
+ end
53
+
54
+ #
55
+ # Update plan
56
+ #
57
+ # @see https://developer.paypal.com/docs/api/subscriptions/v1/#plans_patch
58
+ #
59
+ # @param plan_id [String] Plan ID
60
+ # @macro request
61
+ #
62
+ def update_plan(plan_id, query: nil, body: nil, headers: nil)
63
+ client.patch("/v1/billing/plans/#{plan_id}", query: query, body: body, headers: headers)
64
+ end
65
+
66
+ #
67
+ # Activate plan
68
+ #
69
+ # @see https://developer.paypal.com/docs/api/subscriptions/v1/#plans_activate
70
+ #
71
+ # @param plan_id [String] Plan ID
72
+ # @macro request
73
+ #
74
+ def activate_plan(plan_id, query: nil, body: nil, headers: nil)
75
+ client.post("/v1/billing/plans/#{plan_id}/activate", query: query, body: body, headers: headers)
76
+ end
77
+
78
+ #
79
+ # Deactivate plan
80
+ #
81
+ # @see https://developer.paypal.com/docs/api/subscriptions/v1/#plans_deactivate
82
+ #
83
+ # @param plan_id [String] Plan ID
84
+ # @macro request
85
+ #
86
+ def deactivate_plan(plan_id, query: nil, body: nil, headers: nil)
87
+ client.post("/v1/billing/plans/#{plan_id}/deactivate", query: query, body: body, headers: headers)
88
+ end
89
+
90
+ #
91
+ # Update pricing
92
+ #
93
+ # @see https://developer.paypal.com/docs/api/subscriptions/v1/#plans_deactivate
94
+ #
95
+ # @param plan_id [String] Plan ID
96
+ # @macro request
97
+ #
98
+ def update_plan_pricing(plan_id, query: nil, body: nil, headers: nil)
99
+ client.post("/v1/billing/plans/#{plan_id}/update-pricing-schemes", query: query, body: body, headers: headers)
100
+ end
101
+
102
+ #
103
+ # Create subscription
104
+ #
105
+ # @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_create
106
+ #
107
+ # @macro request
108
+ #
109
+ def create_subscription(query: nil, body: nil, headers: nil)
110
+ client.post("/v1/billing/subscriptions", query: query, body: body, headers: headers)
111
+ end
112
+
113
+ #
114
+ # Show subscription details
115
+ #
116
+ # @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_get
117
+ #
118
+ # @param subscription_id [String] Subscripton ID
119
+ # @macro request
120
+ #
121
+ def show_subscription(subscription_id, query: nil, body: nil, headers: nil)
122
+ client.get("/v1/billing/subscriptions/#{subscription_id}", query: query, body: body, headers: headers)
123
+ end
124
+
125
+ #
126
+ # Update subscription
127
+ #
128
+ # @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_patch
129
+ #
130
+ # @param subscription_id [String] Subscripton ID
131
+ # @macro request
132
+ #
133
+ def update_subscription(subscription_id, query: nil, body: nil, headers: nil)
134
+ client.patch("/v1/billing/subscriptions/#{subscription_id}", query: query, body: body, headers: headers)
135
+ end
136
+
137
+ #
138
+ # Revise plan or quantity of subscription
139
+ #
140
+ # @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_revise
141
+ #
142
+ # @param subscription_id [String] Subscripton ID
143
+ # @macro request
144
+ #
145
+ def revise_subscription(subscription_id, query: nil, body: nil, headers: nil)
146
+ client.post("/v1/billing/subscriptions/#{subscription_id}/revise", query: query, body: body, headers: headers)
147
+ end
148
+
149
+ #
150
+ # Suspend subscription
151
+ #
152
+ # @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_suspend
153
+ #
154
+ # @param subscription_id [String] Subscripton ID
155
+ # @macro request
156
+ #
157
+ def suspend_subscription(subscription_id, query: nil, body: nil, headers: nil)
158
+ client.post("/v1/billing/subscriptions/#{subscription_id}/suspend", query: query, body: body, headers: headers)
159
+ end
160
+
161
+ #
162
+ # Cancel subscription
163
+ #
164
+ # @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel
165
+ #
166
+ # @param subscription_id [String] Subscripton ID
167
+ # @macro request
168
+ #
169
+ def cancel_subscription(subscription_id, query: nil, body: nil, headers: nil)
170
+ client.post("/v1/billing/subscriptions/#{subscription_id}/cancel", query: query, body: body, headers: headers)
171
+ end
172
+
173
+ #
174
+ # Activate subscription
175
+ #
176
+ # @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_activate
177
+ #
178
+ # @param subscription_id [String] Subscripton ID
179
+ # @macro request
180
+ #
181
+ def activate_subscription(subscription_id, query: nil, body: nil, headers: nil)
182
+ client.post("/v1/billing/subscriptions/#{subscription_id}/activate", query: query, body: body, headers: headers)
183
+ end
184
+
185
+ #
186
+ # Capture authorized payment on subscription
187
+ #
188
+ # @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_capture
189
+ #
190
+ # @param subscription_id [String] Subscripton ID
191
+ # @macro request
192
+ #
193
+ def capture_subscription(subscription_id, query: nil, body: nil, headers: nil)
194
+ client.post("/v1/billing/subscriptions/#{subscription_id}/capture", query: query, body: body, headers: headers)
195
+ end
196
+
197
+ #
198
+ # List transactions for subscription
199
+ #
200
+ # @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_transactions
201
+ #
202
+ # @param subscription_id [String] Subscripton ID
203
+ # @macro request
204
+ #
205
+ def transactions(subscription_id, query: nil, body: nil, headers: nil)
206
+ client.get("/v1/billing/subscriptions/#{subscription_id}/transactions", query: query, body: body, headers: headers)
207
+ end
208
+ end
209
+
210
+ include APIs
211
+ extend APIs
212
+ end
213
+ end
@@ -0,0 +1,206 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PaypalAPI
4
+ #
5
+ # https://developer.paypal.com/docs/api/webhooks/v1/
6
+ #
7
+ class Webhooks < APICollection
8
+ #
9
+ # Common class and instance methods
10
+ #
11
+ module APIs
12
+ # @!macro [new] request
13
+ # @param query [Hash, nil] Request query parameters
14
+ # @param body [Hash, nil] Request body parameters
15
+ # @param headers [Hash, nil] Request headers
16
+ # @return [Response] Response object
17
+
18
+ #
19
+ # Create webhook
20
+ #
21
+ # @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_post
22
+ #
23
+ # @macro request
24
+ #
25
+ def create(query: nil, body: nil, headers: nil)
26
+ client.post("/v1/notifications/webhooks", query: query, body: body, headers: headers)
27
+ end
28
+
29
+ #
30
+ # List webhooks
31
+ #
32
+ # @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_list
33
+ #
34
+ # @macro request
35
+ #
36
+ def list(query: nil, body: nil, headers: nil)
37
+ client.get("/v1/notifications/webhooks", query: query, body: body, headers: headers)
38
+ end
39
+
40
+ #
41
+ # Show webhook details
42
+ #
43
+ # @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_get
44
+ #
45
+ # @param webhook_id [String] Webhook ID
46
+ # @macro request
47
+ #
48
+ def show(webhook_id, query: nil, body: nil, headers: nil)
49
+ client.get("/v1/notifications/webhooks/#{webhook_id}", query: query, body: body, headers: headers)
50
+ end
51
+
52
+ #
53
+ # Update webhook
54
+ #
55
+ # @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_update
56
+ #
57
+ # @param webhook_id [String] Webhook ID
58
+ # @macro request
59
+ #
60
+ def update(webhook_id, query: nil, body: nil, headers: nil)
61
+ client.patch("/v1/notifications/webhooks/#{webhook_id}", query: query, body: body, headers: headers)
62
+ end
63
+
64
+ #
65
+ # Delete webhook
66
+ #
67
+ # @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_delete
68
+ #
69
+ # @param webhook_id [String] Webhook ID
70
+ # @macro request
71
+ #
72
+ def delete(webhook_id, query: nil, body: nil, headers: nil)
73
+ client.delete("/v1/notifications/webhooks/#{webhook_id}", query: query, body: body, headers: headers)
74
+ end
75
+
76
+ #
77
+ # List event subscriptions for webhook
78
+ #
79
+ # @see https://developer.paypal.com/docs/api/webhooks/v1/#event-types_list
80
+ #
81
+ # @param webhook_id [String] Webhook ID
82
+ # @macro request
83
+ #
84
+ def list_event_types(webhook_id, query: nil, body: nil, headers: nil)
85
+ client.get("/v1/notifications/webhooks/#{webhook_id}/event-types", query: query, body: body, headers: headers)
86
+ end
87
+
88
+ #
89
+ # Create webhook lookup
90
+ #
91
+ # @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks-lookup_post
92
+ #
93
+ # @macro request
94
+ #
95
+ def create_lookup(query: nil, body: nil, headers: nil)
96
+ client.post("/v1/notifications/webhooks-lookup", query: query, body: body, headers: headers)
97
+ end
98
+
99
+ #
100
+ # List webhook lookups
101
+ #
102
+ # @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks-lookup_list
103
+ #
104
+ # @macro request
105
+ #
106
+ def list_lookups(query: nil, body: nil, headers: nil)
107
+ client.get("/v1/notifications/webhooks-lookup", query: query, body: body, headers: headers)
108
+ end
109
+
110
+ #
111
+ # Show webhook lookup details
112
+ #
113
+ # @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks-lookup_get
114
+ #
115
+ # @param webhook_lookup_id [String] Webhook lookup ID
116
+ # @macro request
117
+ #
118
+ def show_lookup(webhook_lookup_id, query: nil, body: nil, headers: nil)
119
+ client.get("/v1/notifications/webhooks-lookup/#{webhook_lookup_id}", query: query, body: body, headers: headers)
120
+ end
121
+
122
+ #
123
+ # Delete webhook lookup
124
+ #
125
+ # @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks-lookup_delete
126
+ #
127
+ # @param webhook_lookup_id [String] Webhook lookup ID
128
+ # @macro request
129
+ #
130
+ def delete_lookup(webhook_lookup_id, query: nil, body: nil, headers: nil)
131
+ client.delete("/v1/notifications/webhooks-lookup/#{webhook_lookup_id}", query: query, body: body, headers: headers)
132
+ end
133
+
134
+ #
135
+ # Verify webhook signature
136
+ #
137
+ # @see https://developer.paypal.com/docs/api/webhooks/v1/#verify-webhook-signature_post
138
+ #
139
+ # @macro request
140
+ #
141
+ def verify(query: nil, body: nil, headers: nil)
142
+ client.post("/v1/notifications/verify-webhook-signature", query: query, body: body, headers: headers)
143
+ end
144
+
145
+ #
146
+ # Lists available events to which any webhook can subscribe
147
+ #
148
+ # @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks-event-types_list
149
+ #
150
+ # @macro request
151
+ #
152
+ def list_available_events(query: nil, body: nil, headers: nil)
153
+ client.get("/v1/notifications/webhooks-event-types", query: query, body: body, headers: headers)
154
+ end
155
+
156
+ #
157
+ # List event notifications
158
+ #
159
+ # @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks-events_list
160
+ #
161
+ # @macro request
162
+ #
163
+ def list_events(query: nil, body: nil, headers: nil)
164
+ client.get("/v1/notifications/webhooks-events", query: query, body: body, headers: headers)
165
+ end
166
+
167
+ #
168
+ # Show event notification details
169
+ #
170
+ # @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks-events_get
171
+ #
172
+ # @param event_id [String] Event ID
173
+ # @macro request
174
+ #
175
+ def show_event(event_id, query: nil, body: nil, headers: nil)
176
+ client.get("/v1/notifications/webhooks-events/#{event_id}", query: query, body: body, headers: headers)
177
+ end
178
+
179
+ #
180
+ # Resend event notification
181
+ #
182
+ # @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks-events_resend
183
+ #
184
+ # @param event_id [String] Event ID
185
+ # @macro request
186
+ #
187
+ def resend_event(event_id, query: nil, body: nil, headers: nil)
188
+ client.post("/v1/notifications/webhooks-events/#{event_id}/resend", query: query, body: body, headers: headers)
189
+ end
190
+
191
+ #
192
+ # Simulate webhook event
193
+ #
194
+ # @see https://developer.paypal.com/docs/api/webhooks/v1/#simulate-event_post
195
+ #
196
+ # @macro request
197
+ #
198
+ def simulate_event(query: nil, body: nil, headers: nil)
199
+ client.post("/v1/notifications/simulate-event", query: query, body: body, headers: headers)
200
+ end
201
+ end
202
+
203
+ include APIs
204
+ extend APIs
205
+ end
206
+ end
@@ -7,6 +7,16 @@ module PaypalAPI
7
7
  class Client
8
8
  attr_reader :config
9
9
 
10
+ # Initializes Client
11
+ #
12
+ # @param client_id [String] PayPal client id
13
+ # @param client_secret [String] PayPal client secret
14
+ # @param live [Boolean] PayPal live/sandbox mode
15
+ # @param http_opts [Hash] Net::Http opts for all requests
16
+ # @param retries [Hash] Retries configuration
17
+ #
18
+ # @return [Client] Initialized client
19
+ #
10
20
  def initialize(client_id:, client_secret:, live: nil, http_opts: nil, retries: nil)
11
21
  @config = PaypalAPI::Config.new(
12
22
  client_id: client_id,
@@ -19,12 +29,22 @@ module PaypalAPI
19
29
  @access_token = nil
20
30
  end
21
31
 
32
+ #
33
+ # Checks cached access token is expired and returns it or generates new one
34
+ #
35
+ # @return [AccessToken] AccessToken object
36
+ #
22
37
  def access_token
23
38
  (@access_token.nil? || @access_token.expired?) ? refresh_access_token : @access_token
24
39
  end
25
40
 
41
+ #
42
+ # Generates and caches new AccessToken
43
+ #
44
+ # @return [AccessToken] new AccessToken object
45
+ #
26
46
  def refresh_access_token
27
- response = authorization.generate_access_token
47
+ response = authentication.generate_access_token
28
48
 
29
49
  @access_token = AccessToken.new(
30
50
  requested_at: response.requested_at,
@@ -34,38 +54,100 @@ module PaypalAPI
34
54
  )
35
55
  end
36
56
 
57
+ # @!macro [new] request
58
+ # @param path [String] Request path
59
+ # @param query [Hash, nil] Request query parameters
60
+ # @param body [Hash, nil] Request body parameters
61
+ # @param headers [Hash, nil] Request headers
62
+ #
63
+ # @return [Response] Response object
64
+
65
+ #
66
+ # Executes POST http request
67
+ #
68
+ # @macro request
69
+ #
37
70
  def post(path, query: nil, body: nil, headers: nil)
38
71
  execute_request(Net::HTTP::Post, path, query: query, body: body, headers: headers)
39
72
  end
40
73
 
74
+ #
75
+ # Executes GET http request
76
+ #
77
+ # @macro request
78
+ #
41
79
  def get(path, query: nil, body: nil, headers: nil)
42
80
  execute_request(Net::HTTP::Get, path, query: query, body: body, headers: headers)
43
81
  end
44
82
 
83
+ #
84
+ # Executes PATCH http request
85
+ #
86
+ # @macro request
87
+ #
45
88
  def patch(path, query: nil, body: nil, headers: nil)
46
89
  execute_request(Net::HTTP::Patch, path, query: query, body: body, headers: headers)
47
90
  end
48
91
 
92
+ #
93
+ # Executes PUT http request
94
+ #
95
+ # @macro request
96
+ #
49
97
  def put(path, query: nil, body: nil, headers: nil)
50
98
  execute_request(Net::HTTP::Put, path, query: query, body: body, headers: headers)
51
99
  end
52
100
 
101
+ #
102
+ # Executes DELETE http request
103
+ #
104
+ # @macro request
105
+ #
53
106
  def delete(path, query: nil, body: nil, headers: nil)
54
107
  execute_request(Net::HTTP::Delete, path, query: query, body: body, headers: headers)
55
108
  end
56
109
 
57
- def authorization
110
+ # @return [AuthorizedPayments] AuthorizedPayments APIs collection
111
+ def authorized_payments
112
+ AuthorizedPayments.new(self)
113
+ end
114
+
115
+ # @return [CapturedPayments] CapturedPayments APIs collection
116
+ def captured_payments
117
+ CapturedPayments.new(self)
118
+ end
119
+
120
+ # @return [Authentication] Authentication APIs collection
121
+ def authentication
58
122
  Authentication.new(self)
59
123
  end
60
124
 
125
+ # @return [CatalogProducts] Catalog Products APIs collection
126
+ def catalog_products
127
+ CatalogProducts.new(self)
128
+ end
129
+
130
+ # @return [Orders] Orders APIs collection
61
131
  def orders
62
132
  Orders.new(self)
63
133
  end
64
134
 
65
- def payments
66
- Payments.new(self)
135
+ # @return [Redunds] Refunds APIs collection
136
+ def refunds
137
+ Refunds.new(self)
138
+ end
139
+
140
+ # @return [ShipmentTracking] Shipment Tracking APIs collection
141
+ def shipment_tracking
142
+ ShipmentTracking.new(self)
143
+ end
144
+
145
+ # @return [Subscriptions] Subscriptions APIs collection
146
+ def subscriptions
147
+ Subscriptions.new(self)
67
148
  end
68
149
 
150
+ # @return [Webhooks] Webhooks APIs collection
69
151
  def webhooks
70
152
  Webhooks.new(self)
71
153
  end
@@ -1,21 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PaypalAPI
4
- LIVE_URL = "https://api-m.paypal.com"
5
- SANDBOX_URL = "https://api-m.sandbox.paypal.com"
6
-
7
- DEFAULTS = {
8
- live: false,
9
- http_opts: {}.freeze,
10
- retries: {enabled: true, count: 3, sleep: [0.25, 0.75, 1.5].freeze}.freeze
11
- }.freeze
12
-
13
4
  #
14
5
  # Stores configuration for PaypalAPI Client
15
6
  #
16
7
  class Config
8
+ # Live PayPal URL
9
+ LIVE_URL = "https://api-m.paypal.com"
10
+
11
+ # Sandbox PayPal URL
12
+ SANDBOX_URL = "https://api-m.sandbox.paypal.com"
13
+
14
+ # Default config options
15
+ DEFAULTS = {
16
+ live: false,
17
+ http_opts: {}.freeze,
18
+ retries: {enabled: true, count: 3, sleep: [0.25, 0.75, 1.5].freeze}.freeze
19
+ }.freeze
20
+
17
21
  attr_reader :client_id, :client_secret, :live, :http_opts, :retries
18
22
 
23
+ # Initializes Config
24
+ #
25
+ # @param client_id [String] PayPal client id
26
+ # @param client_secret [String] PayPal client secret
27
+ # @param live [Boolean] PayPal live/sandbox mode
28
+ # @param http_opts [Hash] Net::Http opts for all requests
29
+ # @param retries [Hash] Retries configuration
30
+ #
31
+ # @return [Client] Initialized config object
32
+ #
19
33
  def initialize(client_id:, client_secret:, live: nil, http_opts: nil, retries: nil)
20
34
  @client_id = client_id
21
35
  @client_secret = client_secret
@@ -25,10 +39,14 @@ module PaypalAPI
25
39
  freeze
26
40
  end
27
41
 
42
+ # @return [String] PayPal live or sandbox URL
28
43
  def url
29
44
  live ? LIVE_URL : SANDBOX_URL
30
45
  end
31
46
 
47
+ #
48
+ # Instance representation string. Default was overwritten to hide secrets
49
+ #
32
50
  def inspect
33
51
  "#<#{self.class.name} live: #{live}>"
34
52
  end