Skip to content

Commit d50e120

Browse files
committed
Amazon Pay API SDK (Ruby) 2.0.0
1 parent 0351281 commit d50e120

File tree

7 files changed

+142
-12
lines changed

7 files changed

+142
-12
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
amazon_pay (0.1.0)
4+
amazon_pay (2.0.0)
55

66
GEM
77
remote: https://rubygems.org/

lib/amazon_pay.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
require 'amazon_pay/version'
22
require 'amazon_pay/amazon_pay_client'
33
require 'amazon_pay/amazon_web_store_client'
4+
require 'amazon_pay/amazon_in_store_client'
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module AmazonPay
2+
# Class for Amazon Web Store
3+
class AmazonInStoreClient < AmazonPayClient
4+
def initialize(config_args)
5+
super(config_args)
6+
end
7+
8+
# API to initiate a purchase with a merchant
9+
# - Initiates a purchase with a merchant.
10+
# @see //TODO Update Live URL
11+
# @param {Object} payload - The payload for the request
12+
# @param {Object} [headers=null] - The headers for the request
13+
#
14+
def merchant_scan(payload: nil, headers: nil)
15+
api_call(options: {
16+
method: 'POST',
17+
url_fragment: 'in-store/merchantScan',
18+
payload: payload,
19+
headers: headers
20+
})
21+
end
22+
23+
# API to create Charge to the buyer
24+
# - Creates a charge to the buyer with the requested amount.
25+
# @see //TODO Update Live URL
26+
# @param {Object} payload - The payload for the request
27+
# @param {Object} [headers=null] - The headers for the request
28+
#
29+
def charge(payload: nil, headers: nil)
30+
api_call(options: {
31+
method: 'POST',
32+
url_fragment: 'in-store/charge',
33+
payload: payload,
34+
headers: headers
35+
})
36+
end
37+
38+
# API to create a Refund to the buyer
39+
# - Refunds an amount that was previously charged to the buyer.
40+
# @see //TODO Update Live URL
41+
# @param {Object} payload - The payload for the request
42+
# @param {Object} [headers=null] - The headers for the request
43+
#
44+
def refund(payload: nil, headers: nil)
45+
api_call(options: {
46+
method: 'POST',
47+
url_fragment: 'in-store/refund',
48+
payload: payload,
49+
headers: headers
50+
})
51+
end
52+
end
53+
end
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
headers = {}
2+
headers['x-amz-pay-idempotency-key'] = SecureRandom.uuid.to_s.gsub(/-/, '')
3+
4+
config = Config.get_config
5+
6+
client = AmazonPay::AmazonInStoreClient.new(config)
7+
charge_permission_id = '' # Enter Charge Permission ID
8+
charge_id = ''
9+
10+
merchant_scan_payload = {
11+
scanData: 'UKhrmatMeKdlfY6b',
12+
scanReferenceId: SecureRandom.uuid.to_s.gsub(/-/, ''),
13+
merchantCOE: config[:country_code],
14+
ledgerCurrency: config[:currency_code],
15+
storeLocation: {
16+
countryCode: config[:country_code]
17+
},
18+
metadata: {
19+
merchantNote: 'Software Purchase',
20+
customInformation: 'in-store Software Purchase',
21+
communicationContext: {
22+
merchantStoreName: 'TESTSTORE',
23+
merchantOrderId: '789123'
24+
}
25+
}
26+
}
27+
28+
merchant_scan_expected_response = {
29+
chargePermissionId: ''
30+
}
31+
32+
charge_expected_response = {
33+
chargeId: '',
34+
chargeStatus: {
35+
state: 'Completed'
36+
}
37+
}
38+
39+
refund_expected_response = {
40+
refundId: '',
41+
refundStatus: {
42+
state: 'Pending'
43+
}
44+
}
45+
46+
RSpec.describe 'InStore Client Test Cases' do
47+
it 'Validating Merchant Scan API' do
48+
result = client.merchant_scan(payload: merchant_scan_payload, headers: headers)
49+
charge_permission_id = result[:chargePermissionId]
50+
result = result.transform_keys(&:to_sym)
51+
expect(result.keys).to eq(merchant_scan_expected_response.keys)
52+
end
53+
54+
it 'Validating Charge API' do
55+
charge_payload = {
56+
chargePermissionId: charge_permission_id,
57+
chargeReferenceId: SecureRandom.uuid.to_s.gsub(/-/, ''),
58+
chargeTotal: {
59+
currencyCode: config[:currency_code],
60+
amount: 2
61+
},
62+
softDescriptor: 'amzn-store'
63+
}
64+
result = client.charge(payload: charge_payload)
65+
charge_id = result[:chargeId]
66+
result = result.transform_keys(&:to_sym)
67+
expect(result.keys).to eq(charge_expected_response.keys)
68+
end
69+
70+
it 'Validating Refund API' do
71+
refund_payload = {
72+
chargeId: charge_id,
73+
refundReferenceId: SecureRandom.uuid.to_s.gsub(/-/, ''),
74+
refundTotal: {
75+
currencyCode: config[:currency_code],
76+
amount: 2
77+
},
78+
softDescriptor: 'amzn-store'
79+
}
80+
result = client.refund(payload: refund_payload)
81+
82+
result = result.transform_keys(&:to_sym)
83+
expect(result.keys).to eq(refund_expected_response.keys)
84+
end
85+
end

spec/amazon_pay_client_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def verify(signature, data)
4141
RSpec.describe 'AmazonPay Client Test Cases - Get Authorization Token' do
4242
before do
4343
if mws_auth_token.empty? || merchant_id.empty?
44-
skip 'Enter your mws_auth_token and merchant_id babovebefore running this test'
44+
skip 'Please provide your mws_auth_token and merchant_id before executing these test cases'
4545
end
4646
end
4747

spec/amazon_pay_spec.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.

spec/config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Config
22
def self.get_config
33
{
44
'public_key_id': '', # Enter your Public Key ID
5-
'private_key': File.read(File.expand_path('/test/private.pem', __dir__)), # Path to your private key file
5+
'private_key': File.read(File.expand_path('amzn.pem', __dir__)), # Path to your private key file
66
'region': 'us',
77
'sandbox': true,
88
'currency_code': 'USD',

0 commit comments

Comments
 (0)