Ingenico Direct Support Site

Results for

icon-search-large No search results yet
Enter your search query above

Introduction

To understand how to use this SDK it is best to read the following documentation:

  • Server Introduction
    First read the Server Introduction to familiarize yourself with the various concepts.
  • Server API Reference
    This Server SDK wraps the Server API and (amongst other things) exposes the responses of the webservice calls as Ruby objects. Understanding the Server API will help you understanding these SDK objects as well.
  • This current document will help you understand the global flow when interacting with the Ingenico Direct platform using the Ruby SDK.

The Ruby SDK helps you to communicate with the Server API. More specifically, it offers a fluent Ruby API that provides access to all the functionality of the RESTful Server API. Below, we discuss the following topics in detail.

The source code of the SDK is available on Github . There you can find installation instructions.

The API documentation of the latest version of the SDK is available here . For a specific version, add the actual version to the URL.

Initialization of the Ruby SDK

All Ruby code snippets presented in the API reference assume you have initialized the Ruby SDK before using them in your Development Environment. This section details the initialization of the Ruby SDK.

Initializing is simple, and requires only one key task: use our Factory class to create an instance of Client, which contains the actual methods to communicate with the Server API.

The Factory needs the following input information to provide you with an initialized Client

  • A String to the configuration file with your connection configuration.
  • The secret_api_key and api_key_id. The secret_api_key is a key that is used to authenticate your API requests, and api_key_id identifies that key (as you can have multiple active keys). Both of these can be obtained from the Ingenico Direct setting tab in the BackOffice.

The property file should contain the following keys:

SDK: Ruby

  direct.api.integrator: <your company name>
  direct.api.endpoint.host: api.domain.com
# The properties below are optional and use the given values by default direct.api.endpoint.scheme: https direct.api.endpoint.port: -1 direct.api.authorizationType: v1HMAC direct.api.connectTimeout: 10 # use 0 for no timeout direct.api.socketTimeout: 10 # use 0 for no timeout direct.api.maxConnections: 10 # to support 10 concurrent connections

We recommend to keep the timeout values at these values. See API endpoints for the possible hosts.

If a proxy should be used, the property file should additionally contain the following key(s):

SDK: Ruby
  direct.api.proxy.uri: <URL to the proxy host including leading http:// or https://>
  # omit the following two lines if no proxy authentication is required
  direct.api.proxy.username: <username for the proxy>
  direct.api.proxy.password: <password for the proxy>

You can create an instance of Client using the Factory with this code snippet:

SDK: Ruby
client = Factory.create_client_from_file(configuration_file_name, "api_key_id", "secret_api_key")

This Client instance offers connection pooling and can be reused for multiple concurrent API calls. Once it is no longer used it should be closed.

Connection management

Connection pooling is configured with the max_connections setting in CommunicatorConfiguration or in the configuration file. Its value determines how many connections to the Server API are kept alive for re-use in a connection pool. If there are more requests in progress than this maximum number of connections, then a new connection will be temporarily opened.

Client meta information

Optionally, for BI and fraud prevention purposes, you can supply meta information about the client used by the customer. To do so, create a new instance of Client at the start of the customer's payment process as follows:

SDK: Ruby
consumer_specific_client = client.with_client_meta_info("consumer specific JSON meta info")

This consumer-specific instance will use the same connection pool as the Client from which it was created. As a result, closing a Client will close all Client instances created using the with_client_meta_info method. There is no need to close these separately.

This closing works both ways. If a Client created using the with_client_meta_info method is closed this will also close the Client it originated from. This will in turn close all other Client instances created using the with_client_meta_info method. This can be used if only a Client with client meta info is needed.

Do not use this consumer-specific instance for API calls for other consumers.

Example JSON meta information for a mobile app client:

SDK: Ruby
X-GCS-ClientMetaInfo: {
    "platformIdentifier": "Android/4.4",
    "appIdentifier": "Example mobile app/1.0",
    "sdkIdentifier": "AndroidClientSDK/v1.0",
    "screenSize": "800x600",
    "deviceBrand": "Samsung",
    "deviceType": "GT9300",
    "ipAddress": "123.123.123.123"
}

Example JSON meta information for the JavaScript SDK running in a browser:

SDK: Ruby
X-GCS-ClientMetaInfo: {
    "platformIdentifier": "Mozilla/5.0 (Linux; U; Android 4.1.1; en-gb; Build/KLP) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30",
    "sdkIdentifier": "JavaScriptClientSDK/v1.0",
    "screenSize": "800x600"
}

Payments

As a merchant, your core interaction with Ingenico Direct typically starts when your customer clicks the checkout button in your application. The payment process usually has the following steps:

  1. Payment Product selection
  2. Setting of available information needed for selected payment product (e.g. amount of the order)
  3. Collection of missing customer information needed for selected payment product (e.g. creditcard number)
  4. Submitting the payment request to the Ingenico Direct platform
  5. Handling the response to the payment request (e.g. payment unsuccessful)

The Ingenico Direct platform offers the following ways of handling this payment process:

  • Use a hosted payment through the Hosted Checkout payment pages.
    In this case, you redirect the customer to our hosted payment pages. For you as a merchant, this is the easiest option as the Ingenico Direct platform can handle payment product selection and is responsible for the collection of sensitive data like a creditcard number. Through our Back Office, you still have a lot of control over the look and feel of the checkout.
  • Use a Server SDK to build a payment flow hosted on your server.
    In this case, you can use the Server SDK to obtain the payment products that are applicable to the payment, to obtain the fields that need to be collected from the customer for a selected payment product, and to submit the payment request itself.

In the next couple of paragraphs, we discuss each of these options in more detail.

Use a hosted payment through the Hosted Payment page (RPP)

The high-level flow of a hosted payment is described below, followed by a more detailed look at each of the steps.

  1. At this point, your customer has provided all relevant information regarding the order, e.g. a shopping cart of items and a shipping address.
  2. See the section on initialization. Use Factory to create an instance of Client if you hadn't done so yet, and set the meta data that you've collected about the client of the customer.
  3. Create a CreateHostedCheckoutRequest body and populate at least its order. See the relevant section of the full API reference for more details. You can specify an optional return_url, which is used to redirect your customer back to your website in step 9.
  4. The create hosted checkout SDK call returns a CreateHostedCheckoutResponse response. Store the hosted_checkout_id and RETURNMAC it contains, as well as any other relevant order information. You will need these when your customer returns from our hosted payment pages, or when the customer fails to return in a reasonable amount of time.This response also contains a partialRedirectURL. You have to concatenate the base URL https://payment. with partialRedirectURL according to the formula https://payment. + partialRedirectURL and perform a redirection of your customer to this URL.
  5. After completing the interactive payment process in the RPP, your customer is redirected back to the url you provided in step 3 as body.hosted_checkout_specific_input.return_url. The hosted_checkout_id and RETURNMAC you stored in step 5 are added to this URL as query parameters. Specifying a return_url is optional, however. As a result, your customer is only redirected back if you've provided a URL in step 3.
  6. If you cannot identify the customer based on e.g. the HTTP session, you can use the hosted_checkout_id for this purpose. If you do, you must check that the hosted_checkout_id and RETURNMAC from the return_url match those that you stored in step 3. Note that the RETURNMAC is used as a shared secret between the Ingenico Direct platform and your system that is specific for this hosted checkout.
  7. Retrieve the results of the customer's interaction with the Ingenico Direct platform.
  8. Check the GetHostedCheckoutResponse response returned in step 13. If response.status equals PAYMENT_CREATED, then the customer attempted a payment, the details of which can be found in response.created_payment_output. Depending on the payment product chosen and the status of the payment you can "deliver the goods" immediately, or set up a regular poll of the created payment to wait for the status. Such a poll is done using the SDK call client.merchant("merchant_id").payments().getPayment(payment_id), where payment_id is response.created_payment_output.payment.id. For details on the various payment products and their statuses, see Payment Products.

Additionally, it may be the case that the customer does not return in time (or at all), for example because the browser is closed or because you didn't provide a return_url. In this case, you need to retrieve the status of the hosted checkout (step 12) before the hosted checkout times out, which happens after 2 hours, and follow step 14 as well.

Use a Server SDK to build a payment flow hosted on your server

The high-level flow of a payment performed from pages hosted on your server is described below, followed by a more detailed look at each of the steps. First, we describe the flow for payment products that do not require a redirect to a payment method hosted by a third party. Afterwards, the flow for payment methods that require a redirect is described.

  1. At this point, your customer has provided all relevant information regarding the order, e.g. a shopping cart of items and a shipping address.
  2. See the section on initialization. Use Factory to create an instance of Client if you hadn't done so yet, and set the meta data that you've collected about the client of the customer.
  3. Create GetPaymentProductsParams queryParams and request a list of relevant payment products. See the relevant section of the full API reference for more details.
  4. Show the relevant payment products to the customer such that they can select one.
  5. The customer selects one of the available payment products.
  6. Once the customer has decided which payment product should be used, you request the fields of this payment product. See the relevant section of the full API reference for more details.
  7. Based on the information retrieved in the previous step, you render a form that the customer can use to enter all relevant information for the selected payment product.
  8. The customer submits the form.
  9. Create a CreatePaymentRequest body, populate its order and other properties depending on the selected payment product, and submit it. See the relevant section of the full API reference for more details. Do not store the information provided by the customer. The payment_product_id can be used to determine whether this payment involves a redirect to a third party. For this flow, we assume that we're dealing with a payment that doesn't require a redirect.
  10. The create payment SDK call returns a CreatePaymentResponse response. The status of the payment is accessible via response.payment.status. Depending on the payment product chosen and the status of the payment you can "deliver the goods" immediately, or set up a regular poll of the created payment to wait for the status. Such a poll is done using the SDK call client.merchant("merchantId").payments().getPayment(payment_id), where paymentId is response.payment.id. For details on the various payment products and their statuses, see Payment Products.

The high-level flow of a payment performed from pages on your server is a little different if a redirect is involved. We only describe the steps that differ from the flow without a redirect.

  1. We assume that we're dealing with a payment that involves a redirect. As mentioned above, this can be determined using the payment_product_id. Create a CreatePaymentRequest body and populate at least its order. Additionally, populate its redirect_payment_method_specificInput by providing at least the desired return_url. The return_url defines the location to which the customer should be redirected after completing the payment process. See the relevant section of the full API reference for more details.
  2. The create payment SDK call returns a CreatePaymentResponse response. For payments involving a redirect, response.merchant_action.redirect_data.redirect_url defines the URL to which the customer should be redirected to complete the payment. You need to store the value of response.merchant_action.redirect_data.returnmac because it should be compared with theRETURNMAC returned by the third party at a later stage. Additionally, you need to store the value of response.payment.id. This payment_id is needed to check the status of the payment after the redirect.
  3. Redirect the customer to the redirectUrl.
  4. After the payment process is completed, your customer is redirected to the return_url specified previously. In the flow shown in the figure above we assume that this URL brings the customer back to your server.
  5. Retrieve the RETURNMAC provided by the third party from the return_url. You can use the RETURNMAC to identify the customer by comparing it with the one stored previously, and to validate that the customer was redirected to you by our systems.
  6. Use the payment_id stored previously to check the status of the payment. See the relevant section of the full API reference for more details. The retrieve payment SDK call returns a PaymentResponse response. The status of the payment is accessible via response.payment.status. Use this status to handle the order appropriately, as described above.

Idempotent requests

To execute a request as an idempotent request, you can call the same method as for a non-idempotent request, but with an extra CallContext argument with its idempotence_key property set. This will make sure the SDK will send an X-GCS-Idempotence-Key header with the idempotence key as its value.

If a subsequent request is sent with the same idempotence key, the response will contain an X-GCS-Idempotence-Request-Timestamp header, and the SDK will set the idempotence_request_timestamp property of the CallContext argument. If the first request has not finished yet, the RESTful Server API will return a 409 status code. If this occurs, the SDK will raise an IdempotenceException with the original idempotence key and the idempotence request timestamp.
For example:

SDK: Ruby
context = CallContext.new(idempotence_key)
begin
  reponse = client.merchants(merchant_id).payments().createPayment(request, context)
rescue IdempotenceException => e
  # a request with the same idempotence_key is still in progress, try again after a short pause
  # e.idempotence_request_timestamp contains the value of the
  # X-GCS-Idempotence-Request-Timestamp header
ensure
  idempotence_request_timestamp = context.idempotence_request_timestamp
  # idempotence_request_timestamp contains the value of the
  # X-GCS-Idempotence-Request-Timestamp header
  # if idempotence_request_timestamp is not None this was not the first request
end
If an idempotence key is sent for a call that does not support idempotence, the RESTful Server API will ignore the key and treat the request as a first request.

Exceptions

Payment exceptions

If a payment attempt is declined by the RESTful Server API, a DeclinedPaymentException is raised. This exception contains a reference to the payment result which can be inspected to find the reason why the payment attempt was declined. This payment result can also be used to later retrieve the payment attempt again.
For example:

SDK: Ruby
begin
  response = client.merchants(merchant_id).payments().createPayment(request)
rescue DeclinedPaymentException => e
  payment = e.payment_result.payment
  payment_id = payment.id
  payment_status = payment.status
  $stderr.puts "Payment " + payment_id + " was declined with status " + payment_status
end

Unlike direct payments, indirect payments like iDeal and PayPal usually will not cause a DeclinedPaymentException to be raised, but instead will result in a CreatePaymentResponse return value. To determine if the payment was successfully finished, declined or cancelled, you would need to retrieve the payment status and examine its contents, especially the status field. It is recommended to use shared code for handling errors.
For example:

SDK: Ruby
payment_id = nil
begin
  response = client.merchants(merchant_id).payments().createPayment(request)
  payment_id = respons.payment.id
rescue DeclinedPaymentException => e
  payment = e.create_payment_result.payment
  handle_payment_error(payment)
end
# other code
payment = client.merchants(merchant_id).payments().getPayment(payment_id)
if is_not_successful(payment)
  handle_payment_error(payment)
end

Payout exceptions

If a payout attempt is declined by the RESTful Server API a DeclinedPayoutException is raised. This exception contains a reference to the payout result which can be inspected to find the reason why the payout attempt was declined. This payout result can also be used to later retrieve the payout attempt again.
For example:

SDK: Ruby
begin
  response = client.merchants(merchant_id).payouts().createPayout(request)
rescue DeclinedPayoutException => e
  payout = e.payout_result
  payout_id = payout.id
  payout_status = payout.status
  $stderr.puts "Payout " + payout_id + " was declined with status " + payout_status
end

Refund exceptions

If a refund attempt is declined by the RESTful Server API, a DeclinedRefundException is raised. This exception contains a reference to the refund result which can be inspected to find the reason why the refund was declined. This refund result can also be used to later retrieve the refund attempt again.
For example:

SDK: Ruby
begin
  response = client.merchants(merchant_id).payments().refundPayment(payment_id, request)
rescue DeclinedRefundException => e
  refund = e.refund_result
  refund_id = refund.id
  refund _status = refund.status
  $stderr.puts "Refund " + refund_id + " was declined with status " + refund_status
end

Other exceptions

Besides from the above exceptions, all calls can raise one of the following runtime exceptions:

  • A ValidationException if the request was not correct and couldn't be processed (HTTP status code 400)
  • An AuthorizationException if the request was not allowed (HTTP status code 403)
  • An IdempotenceException if an idempotent request caused a conflict (HTTP status code 409)
  • A ReferenceException if an object was attempted to be referenced that doesn't exist or has been removed, or there was a conflict (HTTP status code 404, 409 or 410)
  • A DirectException if something went wrong on the Ingenico Direct platform. The Ingenico Direct platform was unable to process a message from a downstream partner/acquirer, or the service that you're trying to reach is temporary unavailable (HTTP status code 500, 502 or 503)
  • An ApiException if the RESTful Server API returned any other error

A payment attempt can now be handled as follows:

SDK: Ruby
begin
  response = client.merchants(merchant_id).payments().createPayment(request)
rescue DeclinedPaymentException => e
  payment = e.get_create_payment_result().payment
  payment_id = payment.id
  payment_status = payment.status
  $stderr.puts "Payment " + payment_id + " was declined with status " + payment_status
rescue ValidationException => e
  $stderr.puts "Input validation error:"
  e.errors.each do |error|
    if error.property_name.nil?
      $stderr.puts "- " + error.code + ": " + error.message
    else
      $stderr.puts "- " + error.property_name + ": " + error.code + ": " + error.message
    end
  end
rescue AuthorizationException => e
  $stderr.puts "Authorization error:"
  e.errors.each do |error|
    $stderr.puts "- " + error.code + ": " + error.message
  end
rescue ReferenceException => e
  $stderr.puts  "Incorrect object reference:"
  e.errors.each do |error|
    $stderr.puts "- " + error.code + ": " + error.message
  end
rescue DirectException => e
  $stderr.puts "Error occurred at Ingenico Direct or a downstream partner/acquirer:"
  e.errors.each do |error|
    $stderr.puts "- " + error.code + ": " + error.message
  end
rescue ApiException => e
  $stderr.puts "Ingenico Direct error:"
  e.errors.each do |error|
    $stderr.puts "- " + error.code + ": " + error.message
end

Exception overview

The following table is a summary that shows when each of these exceptions will be raised:

HTTP status code Meaning Description Exception Type
200 Successful Your request was processed correctly N/A
201 Created Your request was processed correctly and a new resource was created.
The URI of this created resource is returned in the Location header of the response.
N/A
204 No Content Your request was processed correctly N/A
various; CreatePaymentResult is present in the response Payment Rejected Your request was rejected either by the Ingenico Direct platform or one of our downstream partners/acquirers. DeclinedPaymentException
various; PayoutResult is present in the response Payout Rejected Your request was rejected either by the Ingenico Direct platform or one of our downstream partners/acquirers. DeclinedPayoutException
various; RefundResult is present in the response Refund Rejected Your request was rejected either by the Ingenico Direct platform or one of our downstream partners/acquirers. DeclinedRefundException
400 Bad Request Your request is not correct and can't be processed. Please correct the mistake and try again. ValidationException
403 Not Authorized You're trying to do something that is not allowed or that you're not authorized to do. AuthorizationException
404 Not Found The object you were trying to access could not be found on the server. ReferenceException
409 Conflict Your idempotent request resulted in a conflict. The first request has not finished yet. IdempotenceException
409 Conflict Your request resulted in a conflict. Either you submitted a duplicate request or you're trying to create something with a duplicate key. ReferenceException
410 Gone The object that you are trying to reach has been removed. ReferenceException
500 Internal Server Error Something went wrong on the Ingenico Direct platform. DirectException
502 Bad Gateway The Ingenico Direct platform was unable to process a message from a downstream partner/acquirer. DirectException
503 Service Unavailable The service that you're trying to reach is temporary unavailable.
Please try again later.
DirectException
other Unexpected error An unexpected error has occurred ApiException

Logging

The Ruby SDK supports logging of requests, responses and exceptions of the API communication.

In order to start using the logging feature, an implementation of the CommunicatorLogger class should be provided. The SDK provides two example implementations for logging to $stdout (StdoutCommunicatorLogger) and logging to a logger (RubyCommunicatorLogger).

Logging can be enabled by calling the enable_logging method on a Client object, and providing the logger as an argument. The logger can subsequently be disabled by calling the disable_logging method.

When logged messages contain sensitive data, this data is obfuscated.

The following code exemplifies the use of adding a logger:

SDK: Ruby
client = Factory.create_client_from_file(configuration_file_name, "api_key_id", "secret_api_key")
logger = RubyCommunicatorLogger.new(Logger.new('logfile.log'), Logger::INFO)
client.enable_logging(logger)
# ... Do some calls
client.disable_logging()

Advanced use: Connection pooling

A Client created using the Factory class from a properties file or CommunicatorConfiguration object will use its own connection pool. If multiple clients should share a single connection pool, the Factory class should be used to first create a shared Communicator, then to create Client instances with that Communicator:

SDK: Ruby
communicator = Factory.create_communicator(configuration_file_name, "api_key_id", "secret_api_key")
client = Factory.create_client_from_communicator(communicator)

Instead of closing these Client instances, you should instead close the Communicator when it is no longer needed. This will close all Client instances that use the Communicator.

If instead one of the Client instances is closed, the Communicator will be closed as well. As a result, all other Client instances that use the Communicator instance will also be closed. Attempting to use a closed Client or Communicator will result in an error.

Connection management

Just like Client, Communicator also has method close_expired_connections that can be used to evict expired HTTP connections. You can call this method on the Communicator instead of on any of the Client instances. The effect will be the same.

Advanced use: Customization of the communication

A Client uses a Communicator to communicate with the RESTful Server API. A Communicator contains all the logic to transform a request object to a HTTP request and a HTTP response to a response object. If needed, you can extend this class. To instantiate a Client that uses your own implementation of Communicator you can use the following code snippet:

SDK: Ruby
communicator = YourCommunicator.new
client = Factory.create_client_from_communicator(communicator)

However, for most customizations you do not have to extend Communicator. The functionality of the Communicator is built on classes Authenticater, Connection, Marshaller and MetaDataProvider, the implementation of which can easily be extended or replaced. Marshaller is used to marshal and unmarshal request and response objects to and from JSON. It is unlikely that you will want to change this module. The other modules are needed to communicate with the Ingenico Direct platform:

  • The RESTful Server API endpoint URI.
  • A Connection, which represents one or more HTTP connections to the Ingenico Direct server.
  • An Authenticator, which is used to sign your requests.
  • A MetaDataProvider, which constructs the header with meta data of your server that is sent in requests for BI and fraud prevention purposes.

For your convenience, the Factory.create_communicator_from_configuration and Factory.create_communicator_from_file functions can take optional arguments to set the Connection, Authenticator or MetaDataProvider modules. For example, you can use the following code snippet to use your own implementation of Connection:

SDK: Ruby
connection = YourConnection.new
communicator = Factory.create_communicator_from_file(configuration_file_name,
                                           "api_key_id",
                                           "secret_api_key",
                                           connection: connection)
client = Factory.create_client_from_communicator(communicator)

Connection management

Calling close_expired_connections on a Client or a Communicator object only works if the Connection extends PooledConnection, otherwise these methods do nothing. If you write a custom Connection that uses a pool of HTTP connections, extend PooledConnection instead.

Logging

To facilitate implementing logging in a custom Connection, the SDK provides utility classes RequestLogMessageBuilder and ResponseLogMessageBuilder. These can be used to easily construct request and response messages. For instance:

SDK: Ruby
# In the below code, logger is the CommunicatorLogger set using enable_logging.
# Note that it may be nil if enable_logging is not called.
request_id = SecureRandom.uuid
request_log_message_builder = RequestLogMessageBuilder.new(request_id, method, uri)
# add request headers to request_log_message_builder
# if present, set the request body on request_log_message_builder
logger.log(request_log_message_builder.get_message)
start_time = Time.now * 1000
# send the request
end_time = Time.now * 1000
duration = end_time - start_time
status_code = ...
# note: duration is optional
response_log_message_builder = ResponseLogMessageBuilder.new(request_id, status_code, duration)
# add response headers to response_log_message_builder
# if present, set the response body on response_log_message_builder
logger.log(response_log_message_builder.get_message)

Webhooks

The part of the SDK that handles the webhooks support is called the webhooks helper. It transparently handles both validation of signatures against the event bodies sent by the webhooks system (including finding the secret key for key ids), and unmarshalling of these bodies to objects. This allows you to focus on the essentials, and not the additional overhead.

Providing secret keys

Secret keys are provided to the webhooks helper using implementations of mixin SecretKeyStore. The Ruby SDK provides one implementation: InMemorySecretKeyStore. This will store secret keys in-memory. If more advanced storage is required, e.g. using a database or file system, then you should write your own implementation.

Initialization of the webhooks helper

Using an implementation of SecretKeyStore, create an instance of WebhooksHelper using our Webhooks class:

SDK: Ruby
helper = Webhooks::Webhooks.create_helper(secret_key_store)

Use the webhooks helper

From an entrypoint that you should write yourself, call the unmarshal method of the WebhooksHelper object. It takes the following arguments:

  • The body, as a string. This should be the raw body as received from the webhooks system.
  • A list of request headers as received from the webhooks system.

In code:

SDK: Ruby
event = helper.unmarshal(body, request_headers)

Notes

Renaming of properties

The Ruby SDK uses Ruby naming conventions. As a result, properties have been turned into so-called Snake case . For example, hostedCheckoutSpecificInput is called hosted_checkout_specific_input, and RETURNMAC is called returnmac.