amzn/amazon-pay-sdk-ruby

Name: amazon-pay-sdk-ruby

Owner: Amazon

Description: Amazon Pay Ruby SDK

Created: 2015-01-26 18:56:59.0

Updated: 2018-05-23 20:33:10.0

Pushed: 2018-05-23 20:41:42.0

Homepage: https://pay.amazon.com/developer

Size: 74

Language: Ruby

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Amazon Pay Ruby SDK

Amazon Pay API Integration

Install

install amazon_pay

or add the following in your Gemfile:

'amazon_pay'
le install
Requirements
Quick Start

Instantiating the client:

ire 'amazon_pay'

ur Amazon Pay keys are
ailable in your Seller Central account
hant_id = 'YOUR_MERCHANT_ID'
ss_key = 'YOUR_ACCESS_KEY'
et_key = 'YOUR_SECRET_KEY'

nt = AmazonPay::Client.new(
rchant_id,
cess_key,
cret_key

Testing in Sandbox Mode

The sandbox parameter is defaulted to false if not specified:

ire 'amazon_pay'

ur Amazon Pay keys are
ailable in your Seller Central account
hant_id = 'YOUR_MERCHANT_ID'
ss_key = 'YOUR_ACCESS_KEY'
et_key = 'YOUR_SECRET_KEY'

nt = AmazonPay::Client.new(
rchant_id,
cess_key,
cret_key,
ndbox: true

Adjusting Region and Currency Code
ire 'amazon_pay'

ur Amazon Pay keys are
ailable in your Seller Central account
hant_id = 'YOUR_MERCHANT_ID'
ss_key = 'YOUR_ACCESS_KEY'
et_key = 'YOUR_SECRET_KEY'

nt = AmazonPay::Client.new(
rchant_id,
cess_key,
cret_key,
gion: :eu,
rrency_code: :gbp

Making an API Call

Below is an example on how to make the GetOrderReferenceDetails API call:

ire 'amazon_pay'

ur Amazon Pay keys are
ailable in your Seller Central account
hant_id = 'YOUR_MERCHANT_ID'
ss_key = 'YOUR_ACCESS_KEY'
et_key = 'YOUR_SECRET_KEY'

nt = AmazonPay::Client.new(
rchant_id,
cess_key,
cret_key,
ndbox: true


ese values are grabbed from the Amazon Pay
dress and Wallet widgets
on_order_reference_id = 'AMAZON_ORDER_REFERENCE_ID'
ess_consent_token = 'ADDRESS_CONSENT_TOKEN'

nt.get_order_reference_details(
azon_order_reference_id,
dress_consent_token: address_consent_token

Below is an example on how to query using the Seller Order ID using the ListOrderReference API call:

ire 'amazon_pay'

ur Amazon Pay keys are
ailable in your Seller Central account
hant_id = 'YOUR_MERCHANT_ID'
ss_key = 'YOUR_ACCESS_KEY'
et_key = 'YOUR_SECRET_KEY'

nt = AmazonPay::Client.new(
rchant_id,
cess_key,
cret_key


er_order_id = 'merchant_id:Test 4321'
ese are examples of input 
r the option values
y_id = '1234-example-order'
y_id_type = 'SellerOrderId'
ted_time_range_start_time = '2017-09-20T17:39:27.925Z'
ted_time_range_end_time = '2017-09-25T17:39:27.925Z'
_order = 'Descending'
r_reference_status_list_filter = ['canceled', 'open', 'closed']
_size = 1

onse = client.list_order_reference(
ery_id,
ery_id_type,
eated_time_range_start: created_time_range_start_time,
eated_time_range_end: created_time_range_end_time,
rt_order: sort_order,
ge_size: page_size,
der_reference_status_list_filter: order_reference_status_list_filter,
s_auth_token: nil

Below is an example on how to GetMerchantAccountStatus API call:

ire 'amazon_pay'

ur Amazon Pay keys are
ailable in your Seller Central account
hant_id = 'YOUR_MERCHANT_ID'
ss_key = 'YOUR_ACCESS_KEY'
et_key = 'YOUR_SECRET_KEY'

nt = AmazonPay::Client.new(
rchant_id,
cess_key,
cret_key


onse = client.client.get_merchant_account_status

Below is an example on how to query using the next token received from ListOrderReference using the ListOrderReferenceByNextToken API call:

ire 'amazon_pay'

ur Amazon Pay keys are
ailable in your Seller Central account
hant_id = 'YOUR_MERCHANT_ID'
ss_key = 'YOUR_ACCESS_KEY'
et_key = 'YOUR_SECRET_KEY'

nt = AmazonPay::Client.new(
rchant_id,
cess_key,
cret_key


onse = client.list_order_reference_by_next_token(
yJuZXh0UGFnZVRva2VuIjoiQUFBQUFBQUFBQUZXL0x3dE50TDBTUWhla29JVk5VNWgvbURhSWJXc1E2MnlVdzVuaURCTURFN2g4U0xja3EweWpCSlY4S2pVRHFJWC9wcD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TYrdkgrWG1Zb0V4ekM4K1pHWDVJcnlUUmVpRjBGMW1vRHNTUkowVW1kNGxyVldNNmNJTE0rOHFpb0IzMGF6dXdQNnZDb3VsVkZwWS9vK0o0Wmc9PSIsIm1hcmtldHBsYWNlSWQiOiJBM0JYQjBZTjNYSDE3SCJ9'

Below is an example on how to enable Logging for the SDK:

ire 'amazon_pay'

ur Amazon Pay keys are
ailable in your Seller Central account
hant_id = 'YOUR_MERCHANT_ID'
ss_key = 'YOUR_ACCESS_KEY'
et_key = 'YOUR_SECRET_KEY'

nt = AmazonPay::Client.new(
rchant_id,
cess_key,
cret_key,
g_enabled: true,
If you don't specify a log file like
the example below, logging will be
output to the standard out stream
g_file_name: 'log.txt',
Currently only the debug level has been
implemented in the SDK. This is done
by default.
g_level: :debug

Response Parsing
ese values are grabbed from the Amazon Pay
dress and Wallet widgets
on_order_reference_id = 'AMAZON_ORDER_REFERENCE_ID'
ess_consent_token = 'ADDRESS_CONSENT_TOKEN'

onse = client.get_order_reference_details(
azon_order_reference_id,
dress_consent_token: address_consent_token


is will return the original response body as a String
onse.body

is will return a REXML object
onse.to_xml

e 'get_element' method allows quick conversion to REXML
d parsing for the xml element. This will return a
ring value from the specified element.
h = 'XPath for the node you would like to extract'
ent = 'Node/Element name you would like to extract the value from'
onse.get_element(xpath, element)

is will return the status code of the response
onse.code

is will return true or false depending on the status code
onse.success
Instant Payment Notification Verification and Parsing
is can be placed in your controller for a method
at is configured to receive a "POST" IPN from Amazon.
ers = request.headers
 = request.body.read

= AmazonPay::IpnHandler.new(headers, body)

is will return "true" if the notification is a  
lid IPN from Amazon
authentic?

e following are methods used to extract the necessary
ta from the IPN
type
message_id
topic_arn
message
timestamp
signature
signature_version
signing_cert_url
unsubscribe_url
notification_type
seller_id
environment
version
notification_data
message_timestamp
Standard One Time Transaction API Flow
ire 'amazon_pay'

ur Amazon Pay keys are
ailable in your Seller Central account
hant_id = 'YOUR_MERCHANT_ID'
ss_key = 'YOUR_ACCESS_KEY'
et_key = 'YOUR_SECRET_KEY'

nt = AmazonPay::Client.new(
rchant_id,
cess_key,
cret_key,
ndbox: true


ese values are grabbed from the Amazon Pay
dress and Wallet widgets
on_order_reference_id = 'AMAZON_ORDER_REFERENCE_ID'
ess_consent_token = 'ADDRESS_CONSENT_TOKEN'

 get the buyers full address if shipping/tax
lculations are needed you can use the following
I call to obtain the order reference details.
nt.get_order_reference_details(
azon_order_reference_id,
dress_consent_token: address_consent_token


nt = '10.00'

ke the SetOrderReferenceDetails API call to
nfigure the Amazon Order Reference Id.
ere are additional optional parameters that
e not used below.
nt.set_order_reference_details(
azon_order_reference_id,
ount,
rrency_code: 'USD', # Default: USD
ller_note: 'Your Seller Note',
ller_order_id: 'Your Seller Order Id',
ore_name: 'Your Store Name'


ke the ConfirmOrderReference API call to
nfirm the details set in the API call
ove.
nt.confirm_order_reference(amazon_order_reference_id)

t a unique id for your current authorization
 this payment.
orization_reference_id = 'Your Unique Id'

ke the Authorize API call to authorize the
ansaction. You can also capture the amount
 this API call or make the Capture API call
parately. There are additional optional
rameters not used below.
onse = client.authorize(
azon_order_reference_id,
thorization_reference_id,
ount,
rrency_code: 'USD', # Default: USD
ller_authorization_note: 'Your Authorization Note',
ansaction_timeout: 0, # Set to 0 for synchronous mode
pture_now: true # Set this to true if you want to capture the amount in the same API call


u will need the Amazon Authorization Id from the
thorize API response if you decide to make the
pture API call separately.
on_authorization_id = response.get_element('AuthorizeResponse/AuthorizeResult/AuthorizationDetails','AmazonAuthorizationId')

t a unique id for your current capture of
is payment.
ure_reference_id = 'Your Unique Id'

ke the Capture API call if you did not set the
apture_now' parameter to 'true'. There are
ditional optional parameters that are not used
low.
nt.capture(
azon_authorization_id,
pture_reference_id,
ount,
rrency_code: 'USD', # Default: USD
ller_capture_note: 'Your Capture Note'


ose the order reference once your one time
yment transaction is complete.
nt.close_order_reference(amazon_order_reference_id)
Subscriptions API Flow
ire 'amazon_pay'

ur Amazon Pay keys are
ailable in your Seller Central account
hant_id = 'YOUR_MERCHANT_ID'
ss_key = 'YOUR_ACCESS_KEY'
et_key = 'YOUR_SECRET_KEY'

nt = AmazonPay::Client.new(
rchant_id,
cess_key,
cret_key,
ndbox: true


ese values are grabbed from the Amazon Pay
dress and Wallet widgets
on_billing_agreement_id = 'AMAZON_BILLING_AGREEMENT_ID'
ess_consent_token = 'ADDRESS_CONSENT_TOKEN'

 get the buyers full address if shipping/tax
lculations are needed you can use the following
I call to obtain the billing agreement details.
nt.get_billing_agreement_details(
azon_billing_agreement_id,
dress_consent_token


xt you will need to set the various details
r this subscription with the following API call.
ere are additional optional parameters that
e not used below.
nt.set_billing_agreement_details(
azon_billing_agreement_id,
ller_note: 'Your Seller Note',
ller_billing_agreement_id: 'Your Transaction Id',
ore_name: 'Your Store Name',
stom_information: 'Additional Information'


ke the ConfirmBillingAgreement API call to confirm
e Amazon Billing Agreement Id with the details set above.
 sure that everything is set correctly above before
nfirming.
nt.confirm_billing_agreement(
azon_billing_agreement_id


e following API call is not needed at this point, but
n be used in the future when you need to validate that
e payment method is still valid with the associated billing
reement id.
nt.validate_billing_agreement(
azon_billing_agreement_id


t the amount for your first authorization.
nt = '10.00'

t a unique authorization reference id for your
rst transaction on the billing agreement.
orization_reference_id = 'Your Unique Id'

w you can authorize your first transaction on the
lling agreement id. Every month you can make the
me API call to continue charging your buyer
th the 'capture_now' parameter set to true. You can
so make the Capture API call separately. There are
ditional optional parameters that are not used
low.
nt.authorize_on_billing_agreement(
azon_billing_agreement_id,
thorization_reference_id,
ount,
rrency_code: 'USD', # Default: USD
ller_authorization_note: 'Your Authorization Note',
ansaction_timeout: 0, # Set to 0 for synchronous mode
pture_now: true, # Set this to true if you want to capture the amount in the same API call
ller_note: 'Your Seller Note',
ller_order_id: 'Your Order Id',
ore_name: 'Your Store Name',
stom_information: 'Additional Information'


u will need the Amazon Authorization Id from the
thorizeOnBillingAgreement API response if you decide
 make the Capture API call separately.
on_authorization_id = res.get_element('AuthorizeOnBillingAgreementResponse/AuthorizeOnBillingAgreementResult/AuthorizationDetails','AmazonAuthorizationId')

t a unique id for your current capture of
is payment.
ure_reference_id = 'Your Unique Id'

ke the Capture API call if you did not set the
apture_now' parameter to 'true'. There are
ditional optional parameters that are not used
low.
nt.capture(
azon_authorization_id,
pture_reference_id,
ount,
rrency_code: 'USD', # Default: USD
ller_capture_note: 'Your Capture Note'


e following API call should not be made until you
e ready to terminate the billing agreement.
nt.close_billing_agreement(
azon_billing_agreement_id,
osure_reason: 'Reason For Closing'

Get Login Profile API

This API call allows you to obtain user profile information once a user has logged into your application using their Amazon credentials.

ire 'amazon_pay'

ur client id is located in your Seller
ntral account.
nt_id = 'Your Client Id'

n = AmazonPay::Login.new(
ient_id,
gion: :na, # Default: :na
ndbox: true # Default: false


e access token is available in the return URL
rameters after a user has logged in.
ss_token = 'User Access Token'

ke the 'get_user_info' api call.
ile = login.get_login_profile(access_token)

 = profile['name']
l = profile['email']
_id = profile['user_id']

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.