amazon-pay-api-sdk-ruby 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +201 -0
- data/README.md +218 -0
- data/lib/amazon_pay/client.rb +102 -0
- data/lib/amazon_pay/client_helper.rb +176 -0
- data/lib/amazon_pay/constants.rb +42 -0
- data/lib/amazon_pay.rb +3 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: be95e6a94ffd92f2b63c434fae57106167e710b78e37ec2ee769055400498153
|
4
|
+
data.tar.gz: b37aec741850a99fb89cc241f396da9fe49e0de47f910232101ba32c8c8bafb0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 82928d3a73a42da02eb620166c86056402773fd700ff6d0e717d0610ff25ef02f1c84ef9bf4cd4288b70df489d0333b30297cee3b40bfbcdb34e3a3a057816b9
|
7
|
+
data.tar.gz: 575aa9e43e074704128e085ac94d2dc3063d2dc4add84eebb5ea9d860c051fea17c1fe139b2339ceca0f2028a2ecd1e0cc8fb7fd48bf47d547a12d1f8a8b457e
|
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License").
|
192
|
+
You may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,218 @@
|
|
1
|
+
## Amazon Pay Ruby SDK Usage
|
2
|
+
|
3
|
+
This guide provides step-by-step instructions on how to use the Amazon Pay Client for creating and updating merchant accounts, as well as claiming merchant accounts.
|
4
|
+
|
5
|
+
|
6
|
+
### Prerequisites
|
7
|
+
|
8
|
+
- Ruby installed on your system.
|
9
|
+
- `amazon-pay-api-sdk-ruby` gem installed or this source code has been imported in your project.
|
10
|
+
- Your `publicKeyId` & `privateKey.pem` file available.
|
11
|
+
|
12
|
+
### Install
|
13
|
+
```
|
14
|
+
gem install amazon-pay-api-sdk-ruby
|
15
|
+
```
|
16
|
+
or add the following in your Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'amazon-pay-api-sdk-ruby'
|
20
|
+
```
|
21
|
+
```
|
22
|
+
bundle install
|
23
|
+
```
|
24
|
+
|
25
|
+
### Configuration
|
26
|
+
|
27
|
+
Create a configuration with your Amazon Pay credentials and region:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
config = {
|
31
|
+
region: 'jp', # Supported Values: na, eu, jp
|
32
|
+
public_key_id: 'PUBLIC_KEY_ID',
|
33
|
+
private_key: File.read('privateKey.pem'),
|
34
|
+
sandbox: true # Optional. Set this paramter true/false if your public_key_id does not have an environment prefix (does not begin with 'SANDBOX' or 'LIVE')
|
35
|
+
}
|
36
|
+
```
|
37
|
+
|
38
|
+
### Initialize the Amazon Pay Client:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
client = AmazonPayClient.new(config)
|
42
|
+
```
|
43
|
+
|
44
|
+
|
45
|
+
### Create Merchant Account
|
46
|
+
|
47
|
+
Define the payload and headers for the `create_merchant_account` API call:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
require './lib/amazon_pay'
|
51
|
+
|
52
|
+
client = AmazonPayClient.new(config)
|
53
|
+
|
54
|
+
createMerchantAccountPayload = {
|
55
|
+
"uniqueReferenceId": "XXXXX",
|
56
|
+
"ledgerCurrency": "JPY",
|
57
|
+
"businessInfo": {
|
58
|
+
"email": "abc@abc.com",
|
59
|
+
"businessType": "CORPORATE",
|
60
|
+
"businessLegalName": "Legal Name",
|
61
|
+
"businessCategory": "Beauty",
|
62
|
+
"businessAddress": {
|
63
|
+
"addressLine1": "122, ABC XYZ",
|
64
|
+
"addressLine2": "XYZ",
|
65
|
+
"city": "XYZ",
|
66
|
+
"stateOrRegion": "XYZ",
|
67
|
+
"postalCode": "123456",
|
68
|
+
"countryCode": "JP",
|
69
|
+
"phoneNumber": {
|
70
|
+
"countryCode": "123",
|
71
|
+
"number": "1234567890"
|
72
|
+
}
|
73
|
+
},
|
74
|
+
"businessDisplayName": "Business Name",
|
75
|
+
"annualSalesVolume": {
|
76
|
+
"amount": "100000",
|
77
|
+
"currencyCode": "JPY"
|
78
|
+
},
|
79
|
+
"countryOfEstablishment": "JP",
|
80
|
+
"customerSupportInformation": {
|
81
|
+
"customerSupportEmail": "merchant@abc.com",
|
82
|
+
"customerSupportPhoneNumber": {
|
83
|
+
"countryCode": "1",
|
84
|
+
"number": "1234567",
|
85
|
+
"extension": "123"
|
86
|
+
}
|
87
|
+
}
|
88
|
+
},
|
89
|
+
"beneficiaryOwners": [
|
90
|
+
{
|
91
|
+
"personId": "BO1",
|
92
|
+
"personFullName": "ABC ABC",
|
93
|
+
"residentialAddress": {
|
94
|
+
"addressLine1": "122, ABC XYZ",
|
95
|
+
"addressLine2": "XYZ",
|
96
|
+
"city": "XYZ",
|
97
|
+
"stateOrRegion": "XYZ",
|
98
|
+
"postalCode": "123456",
|
99
|
+
"countryCode": "JP",
|
100
|
+
"phoneNumber": {
|
101
|
+
"countryCode": "123",
|
102
|
+
"number": "2062062061"
|
103
|
+
}
|
104
|
+
}
|
105
|
+
},
|
106
|
+
{
|
107
|
+
"personId": "BO2",
|
108
|
+
"personFullName": "ABC ABC",
|
109
|
+
"residentialAddress": {
|
110
|
+
"addressLine1": "122, ABC XYZ",
|
111
|
+
"addressLine2": "XYZ",
|
112
|
+
"city": "XYZ",
|
113
|
+
"stateOrRegion": "XYZ",
|
114
|
+
"postalCode": "123456",
|
115
|
+
"countryCode": "JP",
|
116
|
+
"phoneNumber": {
|
117
|
+
"countryCode": "123",
|
118
|
+
"number": "2062062061"
|
119
|
+
}
|
120
|
+
}
|
121
|
+
}
|
122
|
+
],
|
123
|
+
"primaryContactPerson": {
|
124
|
+
"personFullName": "ABC ABC"
|
125
|
+
},
|
126
|
+
"integrationInfo": {
|
127
|
+
"ipnEndpointUrls": [
|
128
|
+
"https://website.com/ipnendpoint", "https://website.com/ipnendpoint"
|
129
|
+
]
|
130
|
+
},
|
131
|
+
"defaultStore": {
|
132
|
+
"domainUrls": [
|
133
|
+
"http://www.abc.com"
|
134
|
+
],
|
135
|
+
"storeName": "ABC ABC",
|
136
|
+
"privacyPolicyUrl": "http://www.abc.com/privacy",
|
137
|
+
"storeStatus": {
|
138
|
+
"state": "Active",
|
139
|
+
"reasonCode": nil
|
140
|
+
}
|
141
|
+
},
|
142
|
+
"merchantStatus": {
|
143
|
+
"statusProvider": "ABC",
|
144
|
+
"state": "ACTIVE",
|
145
|
+
"reasonCode": nil
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
149
|
+
createMerchantAccountHeader = {
|
150
|
+
"x-amz-pay-Idempotency-Key": "idempotency-key"
|
151
|
+
}
|
152
|
+
|
153
|
+
response = client.create_merchant_account(createMerchantAccountPayload, headers: createMerchantAccountHeader)
|
154
|
+
if response.code.to_i == 201 || response.code.to_i == 200
|
155
|
+
puts "Create Merchant Account API Response:"
|
156
|
+
puts response.body
|
157
|
+
else
|
158
|
+
puts "Error: Create Merchant Account API"
|
159
|
+
puts "Status: #{response.code}"
|
160
|
+
puts response.body
|
161
|
+
end
|
162
|
+
```
|
163
|
+
|
164
|
+
### Update Merchant Account
|
165
|
+
|
166
|
+
Define the payload and headers for the `update_merchant_account` API call:
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
updateMerchantAccountPayload = {
|
170
|
+
"businessInfo": {
|
171
|
+
"businessAddress": {
|
172
|
+
"addressLine1": "122, ABC XYZ",
|
173
|
+
"addressLine2": "XYZ",
|
174
|
+
"city": "XYZ",
|
175
|
+
"stateOrRegion": "XYZ",
|
176
|
+
"postalCode": "123456",
|
177
|
+
"countryCode": "JP",
|
178
|
+
"phoneNumber": {
|
179
|
+
"countryCode": "123",
|
180
|
+
"number": "2062062061"
|
181
|
+
}
|
182
|
+
}
|
183
|
+
}
|
184
|
+
}
|
185
|
+
|
186
|
+
updateMerchantAccountHeader = {}
|
187
|
+
|
188
|
+
response = client.update_merchant_account('XXXXXXXXX', updateMerchantAccountPayload, headers: updateMerchantAccountHeader)
|
189
|
+
if response.code.to_i == 200
|
190
|
+
puts "Update Merchant Account API Response:"
|
191
|
+
puts response.body
|
192
|
+
else
|
193
|
+
puts "Error: Update Merchant Account API"
|
194
|
+
puts "Status: #{response.code}"
|
195
|
+
puts response.body
|
196
|
+
end
|
197
|
+
```
|
198
|
+
|
199
|
+
### Merchant Account Claim
|
200
|
+
|
201
|
+
Define the payload and headers for the `merchant_account_claim` API call:
|
202
|
+
|
203
|
+
```ruby
|
204
|
+
|
205
|
+
merchantAccountClaimPayload = {
|
206
|
+
"uniqueReferenceId": "XXXXXX"
|
207
|
+
}
|
208
|
+
|
209
|
+
response = client.merchant_account_claim('XXXXXXXXX', merchantAccountClaimPayload, headers: {})
|
210
|
+
if response.code.to_i == 303
|
211
|
+
puts "Merchant Account Claim API Response:"
|
212
|
+
puts response.body
|
213
|
+
else
|
214
|
+
puts "Error: Merchant Account Claim API"
|
215
|
+
puts "Status: #{response.code}"
|
216
|
+
puts response.body
|
217
|
+
end
|
218
|
+
```
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require_relative 'client_helper'
|
3
|
+
require_relative 'constants'
|
4
|
+
|
5
|
+
# AmazonPayClient class provides methods to interact with Amazon Pay API
|
6
|
+
class AmazonPayClient
|
7
|
+
|
8
|
+
# Initialize the client with configuration settings
|
9
|
+
def initialize(config)
|
10
|
+
@helper = ClientHelper.new(config)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Perform an API call to Amazon Pay
|
14
|
+
# @param url_fragment [String] The URL fragment for the API endpoint
|
15
|
+
# @param method [String] The HTTP method for the API call (e.g., 'POST', 'PATCH')
|
16
|
+
# @param payload [Hash] The payload for the API call, default is an empty string
|
17
|
+
# @param headers [Hash] Optional headers for the API call, default is an empty hash
|
18
|
+
# @param query_params [Hash] Optional query parameters for the API call, default is an empty hash
|
19
|
+
# @return [HTTPResponse] The response from the API call
|
20
|
+
def api_call(url_fragment, method, payload: '', headers: {}, query_params: {})
|
21
|
+
# Convert query parameters into a URL-encoded query string
|
22
|
+
query = @helper.to_query(query_params)
|
23
|
+
|
24
|
+
# Build the full URI by combining the URL fragment and query string
|
25
|
+
uri = @helper.build_uri(url_fragment, query)
|
26
|
+
|
27
|
+
# Initialize the retry counter
|
28
|
+
retries = 0
|
29
|
+
|
30
|
+
begin
|
31
|
+
# Create a new HTTP request with the specified method, URI, and payload
|
32
|
+
request = @helper.create_request(method, uri, payload)
|
33
|
+
|
34
|
+
# Generate signed headers for the request
|
35
|
+
signed_headers = @helper.signed_headers(method, uri, request.body, headers, query)
|
36
|
+
|
37
|
+
# Set the signed headers on the request
|
38
|
+
@helper.set_request_headers(request, signed_headers)
|
39
|
+
|
40
|
+
# Send the HTTP request and get the response
|
41
|
+
response = @helper.send_request(uri, request)
|
42
|
+
|
43
|
+
# Check if the response code indicates a retryable error and if we haven't exceeded the maximum retries
|
44
|
+
if Constants::RETRYABLE_ERROR_CODES.include?(response.code.to_i) && retries < Constants::MAX_RETRIES
|
45
|
+
# Wait for a specified backoff period before retrying
|
46
|
+
sleep Constants::BACKOFF_TIMES[retries]
|
47
|
+
|
48
|
+
# Increment the retry counter
|
49
|
+
retries += 1
|
50
|
+
print "Retrying\n"
|
51
|
+
# Raise an exception to force a retry
|
52
|
+
raise "Transient error: #{response.code}" # Force retry
|
53
|
+
end
|
54
|
+
|
55
|
+
# Return the response if no retry is needed
|
56
|
+
response
|
57
|
+
|
58
|
+
rescue => e
|
59
|
+
# Catches any exceptions that occur during the request
|
60
|
+
if retries < Constants::MAX_RETRIES
|
61
|
+
|
62
|
+
# Increment the retry counter
|
63
|
+
retries += 1
|
64
|
+
|
65
|
+
# Wait for a specified backoff period before retrying
|
66
|
+
sleep Constants::BACKOFF_TIMES[retries - 1] # Backoff before retry
|
67
|
+
|
68
|
+
# Retry the request
|
69
|
+
retry
|
70
|
+
else
|
71
|
+
# After maximum retries are exhausted, return the response from the last attempt
|
72
|
+
response
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Creates a merchant account
|
78
|
+
# @param payload [Hash] The payload for the API call
|
79
|
+
# @param headers [Hash] - Optional headers for the API call, default is an empty hash
|
80
|
+
# @return [HTTPResponse] The response from the API call
|
81
|
+
def create_merchant_account(payload, headers: {})
|
82
|
+
api_call(Constants::MERCHANT_ACCOUNTS_BASE_URL, Constants::POST, payload: payload, headers: headers)
|
83
|
+
end
|
84
|
+
|
85
|
+
# Updates a merchant account
|
86
|
+
# @param merchant_account_id [String] The ID of the merchant account to update
|
87
|
+
# @param payload [Hash] The payload for the API call
|
88
|
+
# @param headers [Hash] - Optional headers for the API call but requires x-amz-pay-authToken header for this API, default is an empty hash
|
89
|
+
# @return [HTTPResponse] The response from the API call
|
90
|
+
def update_merchant_account(merchant_account_id, payload, headers: {})
|
91
|
+
api_call("#{Constants::MERCHANT_ACCOUNTS_BASE_URL}/#{merchant_account_id}", Constants::PATCH, payload: payload, headers: headers)
|
92
|
+
end
|
93
|
+
|
94
|
+
# Claims a merchant account
|
95
|
+
# @param merchant_account_id [String] The ID of the merchant account to claim
|
96
|
+
# @param payload [Hash] The payload for the API call
|
97
|
+
# @param headers [Hash] Optional headers for the API call, default is an empty hash
|
98
|
+
# @return [HTTPResponse] The response from the API call
|
99
|
+
def merchant_account_claim(merchant_account_id, payload, headers: {})
|
100
|
+
api_call("#{Constants::MERCHANT_ACCOUNTS_BASE_URL}/#{merchant_account_id}/claim", Constants::POST, payload: payload, headers: headers)
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,176 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
require 'time'
|
3
|
+
require 'json'
|
4
|
+
require 'base64'
|
5
|
+
require_relative 'constants'
|
6
|
+
require 'uri'
|
7
|
+
|
8
|
+
# ClientHelper class provides utility functions for API interactions
|
9
|
+
class ClientHelper
|
10
|
+
attr_reader :base_url
|
11
|
+
|
12
|
+
# Initialize with configuration settings
|
13
|
+
def initialize(config)
|
14
|
+
validate_config(config)
|
15
|
+
@region = fetch(:region, config)
|
16
|
+
@public_key_id = fetch(:public_key_id, config)
|
17
|
+
@private_key = fetch(:private_key, config)
|
18
|
+
@amazon_signature_algorithm = Constants::AMAZON_SIGNATURE_ALGORITHM
|
19
|
+
@salt_length = 32
|
20
|
+
environment = determine_environment(config)
|
21
|
+
@base_url = "https://#{endpoint}/#{environment}/#{Constants::API_VERSION}/"
|
22
|
+
end
|
23
|
+
|
24
|
+
# Determine the environment based on the public key or the config setting
|
25
|
+
def determine_environment(config)
|
26
|
+
@live = Constants::LIVE[0...-1].downcase
|
27
|
+
@sandbox = Constants::SANDBOX[0...-1].downcase
|
28
|
+
if @public_key_id.start_with?(Constants::LIVE)
|
29
|
+
:live
|
30
|
+
elsif @public_key_id.start_with?(Constants::SANDBOX)
|
31
|
+
:sandbox
|
32
|
+
else
|
33
|
+
fetch(:sandbox, config) ? :sandbox : :live
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Fetch value from config hash
|
38
|
+
def fetch(key, config)
|
39
|
+
config[key] || config[key.to_s]
|
40
|
+
end
|
41
|
+
|
42
|
+
# Get endpoint URL based on region
|
43
|
+
def endpoint
|
44
|
+
Constants::API_ENDPOINTS[@region] || raise(ArgumentError, "Unknown region: '#{@region}'. Valid regions are: #{Constants::API_ENDPOINTS.keys.join(', ')}.")
|
45
|
+
end
|
46
|
+
|
47
|
+
# Get HTTP method object based on method string
|
48
|
+
def http_method(method)
|
49
|
+
Constants::METHOD_TYPES[method] || raise(ArgumentError, "Unknown HTTP method: '#{method}'. Valid methods are: #{Constants::METHOD_TYPES.keys.join(', ')}.")
|
50
|
+
end
|
51
|
+
|
52
|
+
# Generate signed headers for the API request
|
53
|
+
def signed_headers(method, uri, payload, user_headers, query)
|
54
|
+
headers = prepare_headers(user_headers, uri, payload)
|
55
|
+
canonical_headers = canonicalize_headers(headers)
|
56
|
+
canonical_request = build_canonical_request(method, uri, query, canonical_headers, payload)
|
57
|
+
signed_headers = sign_headers(canonical_request, canonical_headers)
|
58
|
+
|
59
|
+
# Add authorization header
|
60
|
+
headers[Constants::AUTHORIZATION] = authorization_header(signed_headers)
|
61
|
+
headers
|
62
|
+
end
|
63
|
+
|
64
|
+
# Sign the given string using the private key
|
65
|
+
def sign(string_to_sign)
|
66
|
+
hashed_request = "#{@amazon_signature_algorithm}\n#{hex_and_hash(string_to_sign)}"
|
67
|
+
rsa = OpenSSL::PKey::RSA.new(@private_key)
|
68
|
+
Base64.strict_encode64(rsa.sign_pss(Constants::HASH_ALGORITHM, hashed_request, salt_length: @salt_length, mgf1_hash: Constants::HASH_ALGORITHM))
|
69
|
+
end
|
70
|
+
|
71
|
+
# Convert query parameters to URL query string
|
72
|
+
def to_query(query_params)
|
73
|
+
URI.encode_www_form(query_params.sort.to_h)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Prepare headers for the API request
|
77
|
+
def prepare_headers(user_headers, uri, payload)
|
78
|
+
headers = normalize_headers(user_headers)
|
79
|
+
headers[Constants::ACCEPT] = headers[Constants::CONTENT_TYPE] = Constants::APPLICATION_JSON
|
80
|
+
headers[Constants::X_AMZ_PAY_REGION] = @region
|
81
|
+
headers[Constants::X_AMZ_PAY_DATE] = formatted_timestamp
|
82
|
+
headers[Constants::X_AMZ_PAY_HOST] = uri.host
|
83
|
+
headers[Constants::CONTENT_LENGTH] = payload.bytesize.to_s unless payload.empty?
|
84
|
+
headers[Constants::X_AMZ_PAY_SDK_TYPE] = Constants::SDK_TYPE
|
85
|
+
headers[Constants::X_AMZ_PAY_SDK_VERSION] = Constants::SDK_VERSION
|
86
|
+
headers
|
87
|
+
end
|
88
|
+
|
89
|
+
# Normalize headers by converting keys to strings and stripping values
|
90
|
+
def normalize_headers(headers)
|
91
|
+
headers.transform_keys(&:to_s).transform_values(&:strip)
|
92
|
+
end
|
93
|
+
|
94
|
+
# Canonicalize headers by converting keys to lowercase and sorting them
|
95
|
+
def canonicalize_headers(headers)
|
96
|
+
headers.transform_keys(&:downcase).sort.to_h
|
97
|
+
end
|
98
|
+
|
99
|
+
# Build the canonical request string
|
100
|
+
def build_canonical_request(method, uri, query, canonical_headers, payload)
|
101
|
+
headers_string = canonical_headers.map { |k, v| "#{k}:#{v}" }.join("\n")
|
102
|
+
signed_headers = canonical_headers.keys.join(';')
|
103
|
+
hashed_payload = hex_and_hash(payload)
|
104
|
+
|
105
|
+
"#{method}\n#{uri.path}\n#{query}\n#{headers_string}\n\n#{signed_headers}\n#{hashed_payload}"
|
106
|
+
end
|
107
|
+
|
108
|
+
# Sign the canonical request headers
|
109
|
+
def sign_headers(canonical_request, canonical_headers)
|
110
|
+
hashed_request = "#{@amazon_signature_algorithm}\n#{hex_and_hash(canonical_request)}"
|
111
|
+
rsa = OpenSSL::PKey::RSA.new(@private_key)
|
112
|
+
signature = Base64.strict_encode64(rsa.sign_pss(Constants::HASH_ALGORITHM, hashed_request, salt_length: @salt_length, mgf1_hash: Constants::HASH_ALGORITHM))
|
113
|
+
"SignedHeaders=#{canonical_headers.keys.join(';')}, Signature=#{signature}"
|
114
|
+
end
|
115
|
+
|
116
|
+
# Build authorization header from signed headers
|
117
|
+
def authorization_header(signed_headers)
|
118
|
+
"#{@amazon_signature_algorithm} PublicKeyId=#{@public_key_id}, #{signed_headers}"
|
119
|
+
end
|
120
|
+
|
121
|
+
# Compute SHA256 hash of the given data
|
122
|
+
def hex_and_hash(data)
|
123
|
+
Digest::SHA256.hexdigest(data)
|
124
|
+
end
|
125
|
+
|
126
|
+
# Format the current timestamp
|
127
|
+
def formatted_timestamp
|
128
|
+
Time.now.utc.iso8601.delete(':-')
|
129
|
+
end
|
130
|
+
|
131
|
+
# URL encode the given value
|
132
|
+
def url_encode(value)
|
133
|
+
URI.encode_www_form_component(value).gsub('%7E', '~')
|
134
|
+
end
|
135
|
+
|
136
|
+
# AmazonPayClient dependency methods
|
137
|
+
# Create a new HTTP request
|
138
|
+
def create_request(method, uri, payload)
|
139
|
+
request = http_method(method).new(uri)
|
140
|
+
request.body = payload.is_a?(String) ? payload : JSON.generate(payload)
|
141
|
+
request
|
142
|
+
end
|
143
|
+
|
144
|
+
# Set headers for the HTTP request
|
145
|
+
def set_request_headers(request, signed_headers)
|
146
|
+
signed_headers.each { |k, v| request[k] = v }
|
147
|
+
end
|
148
|
+
|
149
|
+
# Send the HTTP request
|
150
|
+
def send_request(uri, request)
|
151
|
+
Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == Constants::HTTPS) do |http|
|
152
|
+
http.request(request)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
# Build the full URI for the API request
|
157
|
+
def build_uri(url_fragment, query)
|
158
|
+
URI.parse("#{@base_url}#{url_fragment}#{query.empty? ? '' : "?#{query}"}")
|
159
|
+
end
|
160
|
+
|
161
|
+
# This method checks if all required configuration keys are present in the given config.
|
162
|
+
# If any required key is missing, it raises a StandardError with a message listing the missing keys.
|
163
|
+
def validate_config(config)
|
164
|
+
# Define the list of required keys.
|
165
|
+
required_keys = %i[region public_key_id private_key]
|
166
|
+
|
167
|
+
# Identify which required keys are missing from the config hash.
|
168
|
+
missing_keys = required_keys.select { |key| config[key].nil? }
|
169
|
+
|
170
|
+
# If there are missing keys, raise an error with a descriptive message.
|
171
|
+
unless missing_keys.empty?
|
172
|
+
raise StandardError, "Missing required config keys: #{missing_keys.join(', ')}"
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
module Constants
|
4
|
+
SDK_TYPE = "MerchantOnboarding-Ruby".freeze
|
5
|
+
SDK_VERSION = "1.0.0".freeze
|
6
|
+
API_VERSION = "v2".freeze
|
7
|
+
API_ENDPOINTS = {
|
8
|
+
'na' => 'pay-api.amazon.com',
|
9
|
+
'eu' => 'pay-api.amazon.eu',
|
10
|
+
'jp' => 'pay-api.amazon.jp'
|
11
|
+
}.freeze
|
12
|
+
METHOD_TYPES = {
|
13
|
+
'GET' => Net::HTTP::Get,
|
14
|
+
'POST' => Net::HTTP::Post,
|
15
|
+
'PUT' => Net::HTTP::Put,
|
16
|
+
'PATCH' => Net::HTTP::Patch,
|
17
|
+
'DELETE' => Net::HTTP::Delete
|
18
|
+
}.freeze
|
19
|
+
HASH_ALGORITHM = "SHA256".freeze
|
20
|
+
HTTPS = 'https'.freeze
|
21
|
+
AMAZON_SIGNATURE_ALGORITHM = "AMZN-PAY-RSASSA-PSS-V2".freeze
|
22
|
+
AUTHORIZATION = 'authorization'.freeze
|
23
|
+
ACCEPT = 'accept'.freeze
|
24
|
+
CONTENT_TYPE = 'content-type'.freeze
|
25
|
+
APPLICATION_JSON = 'application/json'.freeze
|
26
|
+
X_AMZ_PAY_REGION = 'x-amz-pay-region'.freeze
|
27
|
+
X_AMZ_PAY_DATE = 'x-amz-pay-date'.freeze
|
28
|
+
X_AMZ_PAY_HOST = 'x-amz-pay-host'.freeze
|
29
|
+
CONTENT_LENGTH = 'content-length'.freeze
|
30
|
+
X_AMZ_PAY_SDK_TYPE = 'x-amz-pay-sdk-type'.freeze
|
31
|
+
X_AMZ_PAY_SDK_VERSION = 'x-amz-pay-sdk-version'.freeze
|
32
|
+
LIVE = 'LIVE-'
|
33
|
+
SANDBOX = 'SANDBOX-'
|
34
|
+
MERCHANT_ACCOUNTS_BASE_URL = 'merchantAccounts'.freeze
|
35
|
+
POST = 'POST'.freeze
|
36
|
+
PATCH = 'PATCH'.freeze
|
37
|
+
MAX_RETRIES = 3.freeze
|
38
|
+
BACKOFF_TIMES = [1, 2, 4].freeze # Define backoff times for retries
|
39
|
+
RETRYABLE_ERROR_CODES = [408, 429, 500, 502, 503, 504].freeze
|
40
|
+
HTTP_OK = '200'
|
41
|
+
HTTP_SERVER_ERROR = '500'
|
42
|
+
end
|
data/lib/amazon_pay.rb
ADDED
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: amazon-pay-api-sdk-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- AmazonPay
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-08-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: base64
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
description: AmazonPay Ruby SDK
|
42
|
+
email: amazon-pay-sdk@amazon.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- LICENSE
|
48
|
+
- README.md
|
49
|
+
- lib/amazon_pay.rb
|
50
|
+
- lib/amazon_pay/client.rb
|
51
|
+
- lib/amazon_pay/client_helper.rb
|
52
|
+
- lib/amazon_pay/constants.rb
|
53
|
+
homepage: https://rubygems.org/gems/amazon-pay-api-sdk-ruby
|
54
|
+
licenses:
|
55
|
+
- Apache-2.0
|
56
|
+
metadata:
|
57
|
+
source_code_uri: https://github.com/amzn/amazon-pay-api-sdk-ruby
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubygems_version: 3.4.10
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: This is an AmazonPay Ruby SDK
|
77
|
+
test_files: []
|