Class: AmazonPayClient

Inherits:
Object
  • Object
show all
Includes:
PaymentServiceProviderClient
Defined in:
lib/amazon-pay-api-sdk-ruby/client.rb

Overview

AmazonPayClient class provides methods to interact with Amazon Pay API

Instance Method Summary collapse

Methods included from PaymentServiceProviderClient

#contest_dispute, #create_dispute, #get_dispute, #update_dispute, #upload_file

Constructor Details

#initialize(config) ⇒ AmazonPayClient

Initialize the client with configuration settings



10
11
12
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 10

def initialize(config)
  @helper = ClientHelper.new(config)
end

Instance Method Details

#api_call(url_fragment, method, payload: '', headers: {}, query_params: {}) ⇒ HTTPResponse

Perform an API call to Amazon Pay

Parameters:

  • url_fragment (String)

    The URL fragment for the API endpoint

  • method (String)

    The HTTP method for the API call (e.g., ‘POST’, ‘PATCH’)

  • payload (Hash) (defaults to: '')

    The payload for the API call, default is an empty string

  • headers (Hash) (defaults to: {})

    Optional headers for the API call, default is an empty hash

  • query_params (Hash) (defaults to: {})

    Optional query parameters for the API call, default is an empty hash

Returns:

  • (HTTPResponse)

    The response from the API call



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 21

def api_call(url_fragment, method, payload: '', headers: {}, query_params: {})
  # Convert query parameters into a URL-encoded query string
  query = @helper.to_query(query_params)

  # Build the full URI by combining the URL fragment and query string
  uri = @helper.build_uri(url_fragment, query)

  # Initialize the retry counter
  retries = 0

  begin
    # Create a new HTTP request with the specified method, URI, and payload
    request = @helper.create_request(method, uri, payload)

    # Generate signed headers for the request
    signed_headers = @helper.signed_headers(method, uri, request.body, headers, query)

    # Set the signed headers on the request
    @helper.set_request_headers(request, signed_headers)

    # Send the HTTP request and get the response
    response = @helper.send_request(uri, request)

    # Check if the response code indicates a retryable error and if we haven't exceeded the maximum retries
    if Constants::RETRYABLE_ERROR_CODES.include?(response.code.to_i) && retries < Constants::MAX_RETRIES
      # Wait for a specified backoff period before retrying
      sleep Constants::BACKOFF_TIMES[retries]

      # Increment the retry counter
      retries += 1
      # Raise an exception to force a retry
      raise "Transient error: #{response.code}" # Force retry
    end

    # Return the response if no retry is needed
    response 

    rescue => e
      # Catches any exceptions that occur during the request
      if retries < Constants::MAX_RETRIES

        # Increment the retry counter
        retries += 1

        # Wait for a specified backoff period before retrying
        sleep Constants::BACKOFF_TIMES[retries - 1] # Backoff before retry

        # Retry the request
        retry
      else
        # After maximum retries are exhausted, return the response from the last attempt
        response
      end
  end
end

#cancel_charge(charge_id, payload, headers: {}) ⇒ HTTPResponse

API to cancel a charge Cancels an existing charge, preventing it from being captured.

Parameters:

  • charge_id (String)
    • The unique ID of the charge to cancel.

  • payload (Object)
    • The payload containing cancellation details.

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

Returns:

  • (HTTPResponse)

    The response from the API call, which confirms the cancellation of the charge.

See Also:



257
258
259
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 257

def cancel_charge(charge_id, payload, headers: {})
  api_call("#{Constants::CHARGES_URL}/#{charge_id}/cancel", Constants::DELETE, payload: payload, headers: headers)
end

#cancel_report_schedule(report_schedule_id, headers: {}) ⇒ HTTPResponse

API to cancel an existing report schedule by ID Cancels a specific report schedule using its unique report schedule ID.

Parameters:

  • report_schedule_id (String)
    • The unique ID of the report schedule to cancel.

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes confirmation of the cancellation.

See Also:



357
358
359
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 357

def cancel_report_schedule(report_schedule_id, headers: {})
  api_call("#{Constants::REPORT_SCHEDULES}/#{report_schedule_id}", Constants::DELETE, headers: headers)
end

#capture_charge(charge_id, payload, headers: {}) ⇒ HTTPResponse

API to capture a charge Captures an authorized charge to collect the funds.

Parameters:

  • charge_id (String)
    • The unique ID of the charge to capture.

  • payload (Object)
    • The payload containing capture details, such as the amount to capture.

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes details of the captured charge.

See Also:



246
247
248
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 246

def capture_charge(charge_id, payload, headers: {})
  api_call("#{Constants::CHARGES_URL}/#{charge_id}/capture", Constants::POST, payload: payload, headers: headers)
end

#close_charge_permission(charge_permission_id, payload, headers: {}) ⇒ HTTPResponse

API to close a Charge Permission object Closes a Charge Permission, preventing further charges.

Parameters:

  • charge_permission_id (String)
    • The unique identifier for the Charge Permission.

  • payload (Object)
    • The payload for the request, typically including a reason for closure.

  • headers (Object) (defaults to: {})
    • Optional headers for the request.

Returns:

  • (HTTPResponse)

    The response from the API call, confirming the closure of the Charge Permission.

See Also:



203
204
205
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 203

def close_charge_permission(charge_permission_id, payload, headers: {})
  api_call("#{Constants::CHARGE_PERMISSIONS_URL}/#{charge_permission_id}/close", Constants::DELETE, payload: payload, headers: headers)
end

#complete_checkout_session(checkout_session_id, payload, headers: {}) ⇒ HTTPResponse

API to complete a Checkout Session Confirms the completion of buyer checkout.

Parameters:

  • checkout_session_id (String)
    • The unique identifier for the CheckoutSession.

  • payload (Object)
    • The payload for the request, typically including fields like chargeAmount, currencyCode, etc.

  • headers (Object) (defaults to: {})
    • Optional headers for the request.

Returns:

  • (HTTPResponse)

    The response from the API call, which confirms the completion of the checkout process.

See Also:



160
161
162
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 160

def complete_checkout_session(checkout_session_id, payload, headers: {})
  api_call("#{Constants::CHECKOUT_SESSION_URL}/#{checkout_session_id}/complete", Constants::POST, payload: payload, headers: headers)
end

#create_charge(payload, headers: {}) ⇒ HTTPResponse

API to create a new charge Initiates a new charge with the provided payment details.

Parameters:

  • payload (Object)
    • The payload containing payment details, such as the amount and currency.

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes details of the created charge.

See Also:



213
214
215
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 213

def create_charge(payload, headers: {})
  api_call(Constants::CHARGES_URL, Constants::POST, payload: payload, headers: headers)
end

#create_checkout_session(payload, headers: {}) ⇒ HTTPResponse

API to create a CheckoutSession object Creates a new CheckoutSession object.

Parameters:

  • payload (Object)
    • The payload for the request. This should include all the required fields such as chargeAmount, currencyCode, etc.

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes the details of the newly created CheckoutSession.

See Also:



128
129
130
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 128

def create_checkout_session(payload, headers: {})
  api_call(Constants::CHECKOUT_SESSION_URL, Constants::POST, payload: payload, headers: headers)
end

#create_merchant_account(payload, headers: {}) ⇒ HTTPResponse

Creates a merchant account

Parameters:

  • payload (Hash)

    The payload for the API call

  • headers (Hash) (defaults to: {})
    • Optional headers for the API call, default is an empty hash

Returns:

  • (HTTPResponse)

    The response from the API call



81
82
83
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 81

def (payload, headers: {})
  api_call(Constants::MERCHANT_ACCOUNTS_BASE_URL, Constants::POST, payload: payload, headers: headers)
end

#create_refund(payload, headers: {}) ⇒ HTTPResponse

API to create a refund Initiates a new refund for a previously captured charge.

Parameters:

  • payload (Object)
    • The payload containing refund details, such as the amount and currency.

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes details of the created refund.

See Also:



267
268
269
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 267

def create_refund(payload, headers: {})
  api_call(Constants::REFUNDS_URL, Constants::POST, payload: payload, headers: headers)
end

#create_report(payload, headers: {}) ⇒ HTTPResponse

API to create a new report Creates a new report based on the provided payload.

Parameters:

  • payload (Object)
    • The payload containing data required to generate the report.

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes confirmation and details of the created report.

See Also:



307
308
309
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 307

def create_report(payload, headers: {})
  api_call(Constants::REPORTS, Constants::POST, payload: payload, headers: headers)
end

#create_report_schedule(payload, headers: {}, query_params: {}) ⇒ HTTPResponse

API to create a new report schedule Creates a new report schedule based on the provided payload.

Parameters:

  • payload (Object)
    • The payload containing data required to set up the report schedule.

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes confirmation and details of the created report schedule.

See Also:



347
348
349
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 347

def create_report_schedule(payload, headers: {}, query_params: {})
  api_call(Constants::REPORT_SCHEDULES, Constants::POST, payload: payload, headers: headers, query_params: query_params)
end

#finalize_checkout_session(checkout_session_id, payload, headers: {}) ⇒ HTTPResponse

API to finalize a Checkout Session Finalizes the checkout process by confirming the payment and completing the session.

Parameters:

  • checkout_session_id (String)
    • The unique ID of the checkout session that needs to be finalized.

  • payload (Object)
    • The payload for the request, typically including payment confirmation details.

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

Returns:

  • (HTTPResponse)

    The response from the API call, indicating the success or failure of the checkout finalization.

See Also:



171
172
173
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 171

def finalize_checkout_session(checkout_session_id, payload, headers: {})
  api_call("#{Constants::CHECKOUT_SESSION_URL}/#{checkout_session_id}/finalize", Constants::POST, payload: payload, headers: headers)
end

#generate_button_signature(payload) ⇒ String

Generates a signature for the given payload This method is used to generate a signature for an Amazon Pay button payload. The payload can be provided as either a String or a Hash. If a Hash is provided, it is converted to a JSON string before signing.

Parameters:

  • payload (Object|String)
    • The payload to be signed, which can be a JSON string or a Hash.

Returns:

  • (String)

    The signature for the provided payload.



108
109
110
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 108

def generate_button_signature(payload)
  @helper.sign(payload.is_a?(String) ? payload : JSON.generate(payload))
end

#get_buyer(buyer_token, headers: {}) ⇒ HTTPResponse

API to retrieve Buyer information Fetches details of a Buyer using the buyer token provided.

Parameters:

  • buyer_token (String)
    • The unique token associated with the Buyer.

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes the details of the Buyer, such as name, email, and address.

See Also:



118
119
120
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 118

def get_buyer(buyer_token, headers: {})
  api_call("#{Constants::BUYERS_URL}/#{buyer_token}", Constants::GET, headers: headers)
end

#get_charge(charge_id, headers: {}) ⇒ HTTPResponse

API to retrieve charge details Retrieves details of an existing charge using its unique charge ID.

Parameters:

  • charge_id (String)
    • The unique ID of the charge to retrieve.

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes details of the requested charge.

See Also:



223
224
225
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 223

def get_charge(charge_id, headers: {})
  api_call("#{Constants::CHARGES_URL}/#{charge_id}", Constants::GET, headers: headers)
end

#get_charge_permission(charge_permission_id, headers: {}) ⇒ HTTPResponse

API to retrieve a Charge Permission object Fetches details of a Charge Permission, which is associated with a Checkout Session.

Parameters:

  • charge_permission_id (String)
    • The unique identifier for the Charge Permission.

  • headers (Object) (defaults to: {})
    • Optional headers for the request.

Returns:

  • (HTTPResponse)

    The response from the API call, including the details of the Charge Permission.

See Also:



181
182
183
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 181

def get_charge_permission(charge_permission_id, headers: {})
  api_call("#{Constants::CHARGE_PERMISSIONS_URL}/#{charge_permission_id}", Constants::GET, headers: headers)
end

#get_checkout_session(checkout_session_id, headers: {}) ⇒ HTTPResponse

API to get a CheckoutSession object Retrieves details of a previously created CheckoutSession object.

Parameters:

  • checkout_session_id (String)
    • The unique identifier for the CheckoutSession.

  • headers (Object) (defaults to: {})
    • Optional headers for the request.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes the details of the CheckoutSession object.Object [headers=null] - The headers for the request

See Also:



138
139
140
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 138

def get_checkout_session(checkout_session_id, headers: {})
  api_call("#{Constants::CHECKOUT_SESSION_URL}/#{checkout_session_id}", Constants::GET, headers: headers)
end

#get_disbursements(headers: {}, query_params: {}) ⇒ HTTPResponse

API to retrieve a list of disbursements Retrieves a list of disbursements based on the provided query parameters.

Parameters:

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

  • query_params (Object) (defaults to: {})
    • Query parameters to filter the disbursements, such as date range or status.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes a list of disbursements matching the criteria.

See Also:



367
368
369
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 367

def get_disbursements(headers: {}, query_params: {})
  api_call(Constants::DISBURSEMENTS, Constants::GET, headers: headers, query_params: query_params)
end

#get_refund(refund_id, headers: {}) ⇒ HTTPResponse

API to retrieve refund details Retrieves details of an existing refund using its unique refund ID.

Parameters:

  • refund_id (String)
    • The unique ID of the refund to retrieve.

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes details of the requested refund.

See Also:



277
278
279
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 277

def get_refund(refund_id, headers: {})
  api_call("#{Constants::REFUNDS_URL}/#{refund_id}", Constants::GET, headers: headers)
end

#get_report_by_id(report_id, headers: {}) ⇒ HTTPResponse

API to retrieve a specific report by ID Retrieves details of a specific report using its unique report ID.

Parameters:

  • report_id (String)
    • The unique ID of the report to retrieve.

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes details of the requested report.

See Also:



297
298
299
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 297

def get_report_by_id(report_id, headers: {})
  api_call("#{Constants::REPORTS}/#{report_id}", Constants::GET, headers: headers)
end

#get_report_document(report_document_id, headers: {}) ⇒ HTTPResponse

API to retrieve a specific report document by ID Retrieves the content of a specific report document using its unique report document ID.

Parameters:

  • report_document_id (String)
    • The unique ID of the report document to retrieve.

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes the content of the requested report document.

See Also:



317
318
319
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 317

def get_report_document(report_document_id, headers: {})
  api_call("#{Constants::REPORT_DOCUMENTS}/#{report_document_id}", Constants::GET, headers: headers)
end

#get_report_schedule_by_id(report_schedule_id, headers: {}) ⇒ HTTPResponse

API to retrieve a specific report schedule by ID Retrieves details of a specific report schedule using its unique report schedule ID.

Parameters:

  • report_schedule_id (String)
    • The unique ID of the report schedule to retrieve.

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes details of the requested report schedule.

See Also:



337
338
339
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 337

def get_report_schedule_by_id(report_schedule_id, headers: {})
  api_call("#{Constants::REPORT_SCHEDULES}/#{report_schedule_id}", Constants::GET, headers: headers)
end

#get_report_schedules(headers: {}, query_params: {}) ⇒ HTTPResponse

API to retrieve a list of report schedules Retrieves a list of report schedules based on the provided query parameters.

Parameters:

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

  • query_params (Object) (defaults to: {})
    • Query parameters to filter the report schedules, such as schedule type or status.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes a list of report schedules matching the criteria.

See Also:



327
328
329
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 327

def get_report_schedules(headers: {}, query_params: {})
  api_call(Constants::REPORT_SCHEDULES, Constants::GET, headers: headers, query_params: query_params)
end

#get_reports(headers: {}, query_params: {}) ⇒ HTTPResponse

API to retrieve a list of reports Retrieves a list of reports based on the provided query parameters.

Parameters:

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as authorization tokens or custom headers.

  • query_params (Object) (defaults to: {})
    • Query parameters to filter the reports, such as report type or processing status.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes a list of reports matching the criteria.

See Also:



287
288
289
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 287

def get_reports(headers: {}, query_params: {})
  api_call(Constants::REPORTS, Constants::GET, headers: headers, query_params: query_params)
end

#merchant_account_claim(merchant_account_id, payload, headers: {}) ⇒ HTTPResponse

Claims a merchant account

Parameters:

  • merchant_account_id (String)

    The ID of the merchant account to claim

  • payload (Hash)

    The payload for the API call

  • headers (Hash) (defaults to: {})

    Optional headers for the API call, default is an empty hash

Returns:

  • (HTTPResponse)

    The response from the API call



99
100
101
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 99

def (, payload, headers: {})
  api_call("#{Constants::MERCHANT_ACCOUNTS_BASE_URL}/#{}/claim", Constants::POST, payload: payload, headers: headers)
end

#update_charge(charge_id, payload, headers: {}) ⇒ HTTPResponse

API to retrieve charge details. The updateCharge operation is used to update the charge status of any PSP (Payment Service Provider) processed payment method (PPM) transactions. Please note that is API is supported only for PSPs (Payment Service Provider)

Parameters:

  • charge_id (String)
    • The unique ID of the charge to update.

  • payload (Object)
    • The payload containing statusDetails.

  • headers (Object) (defaults to: {})
    • Optional headers for the request, such as x-amz-pay-idempotency-key, authorization tokens or custom headers.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes details of the requested charge.

See Also:



235
236
237
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 235

def update_charge(charge_id, payload, headers: {})
  api_call("#{Constants::CHARGES_URL}/#{charge_id}", Constants::PATCH, payload: payload, headers: headers)
end

#update_charge_permission(charge_permission_id, payload, headers: {}) ⇒ HTTPResponse

API to update a Charge Permission object Updates a previously created Charge Permission with new information.

Parameters:

  • charge_permission_id (String)
    • The unique identifier for the Charge Permission.

  • payload (Object)
    • The payload for the request. This should include the fields that need to be updated.

  • headers (Object) (defaults to: {})
    • Optional headers for the request.

Returns:

  • (HTTPResponse)

    The response from the API call, including the updated details of the Charge Permission.

See Also:



192
193
194
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 192

def update_charge_permission(charge_permission_id, payload, headers: {})
  api_call("#{Constants::CHARGE_PERMISSIONS_URL}/#{charge_permission_id}", Constants::PATCH, payload: payload, headers: headers)
end

#update_checkout_session(checkout_session_id, payload, headers: {}) ⇒ HTTPResponse

API to update a CheckoutSession object Updates a previously created CheckoutSession object with new information.

Parameters:

  • checkout_session_id (String)
    • The unique identifier for the CheckoutSession.

  • payload (Object)
    • The payload for the request. This should include the fields that need to be updated.

  • headers (Object) (defaults to: {})
    • Optional headers for the request.

Returns:

  • (HTTPResponse)

    The response from the API call, which includes the updated details of the CheckoutSession.

See Also:



149
150
151
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 149

def update_checkout_session(checkout_session_id, payload, headers: {})
  api_call("#{Constants::CHECKOUT_SESSION_URL}/#{checkout_session_id}", Constants::PATCH, payload: payload, headers: headers)
end

#update_merchant_account(merchant_account_id, payload, headers: {}) ⇒ HTTPResponse

Updates a merchant account

Parameters:

  • merchant_account_id (String)

    The ID of the merchant account to update

  • payload (Hash)

    The payload for the API call

  • headers (Hash) (defaults to: {})
    • Optional headers for the API call but requires x-amz-pay-authToken header for this API, default is an empty hash

Returns:

  • (HTTPResponse)

    The response from the API call



90
91
92
# File 'lib/amazon-pay-api-sdk-ruby/client.rb', line 90

def (, payload, headers: {})
  api_call("#{Constants::MERCHANT_ACCOUNTS_BASE_URL}/#{}", Constants::PATCH, payload: payload, headers: headers)
end