Ottu Web SDK
The Checkout SDK is a JavaScript library provided by Ottu that allows you to easily integrate an Ottu-powered checkout process into your web application. With the Checkout SDK, you can customize the look and feel of your checkout process, as well as which forms of payment are accepted.
To use the Checkout SDK, you'll need to include the library in your web application and initialize it with your Ottu merchant_id, session_id, and API key. You can also specify additional options such as, which forms of payment to accept, the css styling for the checkout interface, and more.
Please note that the Checkout SDK requires the implementation of the Checkout API in order to function properly.
For optimal security, call REST APIs from server-side implementations, not client-side applications such as mobile apps or web browsers.
Below is a demo of the Checkout SDK in action. This demo shows how the Checkout SDK can be used to create a streamlined checkout experience for customers, with support for multiple forms of payment and a customizable interface.
To install the Checkout SDK, you'll need to include the library in your web application by adding a script tag to your HTML section. You can do this by using the following code snippet:
Replace errorCallback, cancelCallback, successCallback, and beforeRedirect with the names of your error handling, cancel handling, success handling, and before-redirect handling functions, respectively.
You're all set! You can now use the Checkout SDK to create a checkout form on your web page and process payments through Ottu.
Is the function that initializes the checkout process and sets up the necessary configuration options for the Checkout SDK. It needs to be called once on your web page to initialize the checkout process, and it must be called with a configuration object that includes all the necessary options for the checkout process.
When you call Checkout.init
, the SDK will take care of setting up the necessary components for the checkout process, such as creating a form for the customer to enter their payment details, and handling communication with Ottu's servers to process the payment.
The Checkout.reload
function in the Checkout SDK is used to refresh the SDK. It's useful when you want to reload the content of the SDK after an error has occurred or when the content needs to be refreshed.
Here's an example of how Checkout.reload
might be called:
In this example, the Checkout.reload
function is called to refresh the content of the SDK. This can be useful when an error has occurred and the content needs to be reloaded or refreshed.
selector string
string
The selector
property in the Checkout SDK is used to specify the css
selector for the HTML element that will contain the checkout form. This is typically a <div>
element on your web page.
To specify the selector, you can add a <div>
element to your web page with a unique id
attribute, like this:
In this example, the id
attribute of the <div>
element is set to "checkout"
. This means that the selector
property in Checkout.init
should be set to "checkout"
.
It's important to note that the selector
property must be the ID of the HTML element that will contain the checkout form. This is because the Checkout SDK replaces the contents of the specified element with the checkout elements.
Here's an example of how Checkout.init
might be called with a selector
property:
In this example, the selector
property is set to "checkout"
, which means that the checkout form will be contained in the <div>
element with id="checkout"
.
merchant_id string
string
The merchant_id
specifies your Ottu merchant domain. This should be the root domain of your Ottu account, without the "https://" or "http://" prefix.
For example, if your Ottu URL is https://example.ottu.com
, then your merchant_id
is example.ottu.com. This property is used to identify which Ottu merchant account the checkout process should be linked to.
The apiKey
is your Ottu API public key. This key is used for authentication purposes when communicating with Ottu's servers during the checkout process.
According to the REST API documentation, the apiKey
property should be set to your Ottu API public key.
Ensure that you utilize the public key and refrain from using the private key. The private key should remain confidential at all times and must not be shared with any clients.
session_id string
string
The session_id
is the unique identifier for the payment transaction associated with the checkout process.
This unique identifier is automatically generated when the payment transaction is created. For more information on how to use the session_id
parameter in the Checkout API, see session_id.
lang string
string
The lang
property is used to specify the language in which the checkout elements should be displayed. This property can be set to either "en"
(for English) or "ar"
(for Arabic).
When lang
is set to "en"
, the checkout form will be displayed in English, and when it's set to "ar"
, the checkout elements will be displayed in Arabic. Additionally, when the lang
parameter is set to "ar"
, the layout will switch to right-to-left (RTL) to accommodate Arabic script.
For more information on how to use lang parameter in the Checkout API, see lang.
formsOfPayment array
array
formsOfPayment
allows you to customize which forms of payment will be displayed in your checkout process. By default, all forms of payment are configured.
The available options for formsOfPayment
are:
"applePay"
: The Apple Pay payment method that allows customers to make purchases using their Apple Pay-enabled devices."googlePay"
: The Google Pay payment method that allows customers to make purchases using their Google wallet cards linked in google accounts."ottuPG"
: A method that redirects customers to a page where customers enter their credit or debit card details to make a payment."tokenPay"
: A payment method that uses tokenization to securely store and process customers' payment information."redirect"
: A method where customers are redirected to a payment gateway or a third-party payment processor to complete their payment."stcPay"
: A method where customers enter their mobile number and provide an OTP send to their mobile number to complete their payment.
This property can be particularly useful when you want to customize the checkout process and display only specific forms of payment, such as only displaying the Apple Pay button and hiding the other payment options.
css object
object
css
can be used to override some of the elements rendered by the SDK to better integrate with your website.
There are several css
classes that can be overridden using the css property, including:
.ottu__sdk-main
: the class of the div which wraps all the elements of the container. It's useful to override this when you want to modify the size of the rendered content by the SDK..ottu__sdk-apple-pay-button-type
: specifies the button type of the Apple Pay button. This class defaults topay
and if there is no card in the wallet, it will automatically change tosetUp
. More information can be found on the ApplePay official documentation for ApplePay Button Type..apple-pay-button
: the class of the Apple Pay button, which is adiv
. It can be customized for width, padding, margin, and more. Useful to customize whenformsOfPayment
is configured to display only the Apple Pay button.
Here's an example of how Checkout.init might be called with a css property to customize the .ottu__sdk-main
class, .ottu__sdk-apple-pay-button-type
class, and .apple-pay-button
class:
In this example, the css property is set to override the .ottu__sdk-main
, .ottu__sdk-apple-pay-button-type
, and .apple-pay-button
classes with new styles. The updated styles for these classes will be applied to the checkout container when it's rendered by the SDK.
googlePayInit object
object
The googlePayInit
object enables users to modify the Google Pay configurations used for generating payment sessions through Google Pay. By default, all options are pre-configured. However, developers have the flexibility to customize these configurations using googlePayInit
according to their requirements by utilizing the options outlined in the documentation here.
Additionally, the buttonStyles
property allows users to customize the css
of the Google button in the SDK, making it easier to integrate with your website.
buttonStyles
object
ThebuttonStyle
is an object in the Google Pay API that enables merchants to customize the default styling properties of the Google Pay button. This object provides options to modify the width, margin-top, and margin-bottom of the button. By utilizing theButtonStyle
object, merchants can tailor the appearance of the Google Pay button to align with their website or app design. Defaultcss
is: width: 90%, margin-top: 12px, margin-bottom: 12px.
Example
To display only the Google Pay button, use the following code:
Example
HTML
Javascript
Is a function that shows an error message in a popup on the screen. The message parameter must be a string, and the optional pg_response
parameter is an object that displays key-value pairs representing object values within the popup.
message
string
The message for a failed payment can be displayed to the customer.pg_response
object
The raw response data that was received directly from the payment gateway after the transaction attempt. This typically includes transaction status, transaction identifier, and potentially error messages or additional data provided by the gateway.
Example
Checkout.showErrorPopup('Selected payment method failed. Try again.' , { "merchant":"009057332", "timeOfLastUpdate":"2023-08-01T14:19:00.510Z", "version":"65" })
Checkout.showErrorPopup('Selected payment method failed. Try again.' , cancel.payment_gateway_info.pg_response)
In the Checkout SDK, callback functions play a vital role in providing real-time updates on the status of payment transactions. Callbacks
enhance the user experience by enabling seamless and efficient handling of various payment scenarios, such as errors, successful payments, and cancellations.
Please note that due to technical constraints associated with off-site redirection during the payment process, the successCallback
and cancelCallback
functions are only called for on-site checkouts. However, the errorCallback
function is called for any kind of payments. On-site checkouts include options such as Apple Pay, Google Pay, payments with saved cards, and on-site card form transactions, which support callback functionality for a seamless user experience.
The errorCallback
is a callback function that is invoked when issues arise during a payment. It is important to handle errors appropriately to ensure a smooth user experience. The recommended best practice in case of an error is to restart the checkout process by creating a new session_id using the Checkout API.
To define the errorCallback
function, you can use the data-error
attribute on the Checkout script tag to specify a global function that will handle errors. If an error occurs during a payment, the errorCallback
function will be invoked with an error
object.
The error
object has the following properties:
status
: A string indicating the status of the payment transaction. The only value that will be passed to theerrorCallback
is"error"
.message
: A string containing a description of the error that occurred.
Here's an example of how errorCallback
might be defined:
In this example, the errorCallback
function is defined and passed as the value of the data-error
attribute on the Checkout script tag. If an error occurs during a payment, the function will be invoked with an error
object. The function will then handle the error as needed and redirect the customer to https://payment.example.com
.
errorCallback
function is not required to perform a redirection. It can handle errors in any way that is appropriate for your application.
The cancelCallback
in the Checkout SDK is a callback function that is invoked when a payment is canceled.
To define the cancelCallback
function, you can use the data-cancel
attribute on the Checkout script tag to specify a global function that will handle cancellations. If a customer cancels a payment, the cancelCallback
function will be invoked with a data object.
cancelCallback
receives a data object, where the data.status
is "canceled"
.
Here's an example of how cancelCallback
might be defined:
In this example, the cancelCallback
function is defined and passed as the value of the data-cancel
attribute on the Checkout script tag. If a customer cancels a payment, the function will be invoked with a data object containing information about the cancelled transaction. The function will then handle the cancellation as needed and refresh the Checkout SDK with Checkout.reload().
The successCallback
in the Checkout SDK is a callback
function that is invoked when the payment process has been completed successfully.
Here's an example of how successCallback
might be defined:
successCallback
receives a data object, where the data.status
is "success"
.
In this example, the successCallback
function is defined and passed as the value of the data-success
attribute on the Checkout script tag. If the payment process completes successfully, the function will be invoked with a data object containing information about the completed transaction. The function will then redirect the customer to the specified redirect_url
using window.location.href
.
Example
For redirect
checkout processes, you may want to freeze the customer's basket before the customer is redirected to the payment page. The Checkout SDK provides a beforeRedirect
callback that you can use to perform any necessary actions before the redirection occurs.
To define the beforeRedirect
callback, you can use the data-beforeredirect
attribute on the Checkout script tag to specify a global function that will handle the callback. This function should return a Promise that resolves when the necessary actions are complete.
Here's an example of how to define the beforeRedirect
callback:
In this example, the beforeRedirect
callback sends a request to an API endpoint to freeze the customer's basket while waiting for the redirection to occur. If the API response is successful, the Promise is resolved and the redirection proceeds. If the API request fails or the response is unsuccessful, the Promise is rejected and the redirection is cancelled.
The data object received by the cancelCallback and successCallback contains information related to the payment transaction, such as the status of the payment process, the session_id generated for the transaction, any error message associated with the payment, and more. This information can be used to handle the payment process and take appropriate actions based on the status of the transaction.
Data object child parameters
session_id
string
It is a unique identifier generated when a payment transaction is created. It is used to associate a payment transaction with the checkout process. You can find thesession_id
in the response of the Checkout API's session_id endpoint. This parameter is required to initialize the Checkout SDK.status
string
It is of the checkout process. Possible values are:
success
: The customer was charged successfully, and they can be redirected to a success page or display a success message.canceled
: The payment was either canceled by the customer or rejected by the payment gateway for some reason. When a payment is canceled, it's typically not necessary to create a new payment transaction, and the same session_id can be reused to initiate the Checkout SDK and allow the customer to try again. By reusing the same session_id, the customer can resume the checkout process without having to re-enter their payment information or start over from the beginning.error
: An error occurred during the payment process, This can happen for a variety of reasons, such as a network failure or a problem with the payment gateway's system. The recommended action is to create a new payment transaction using the Checkout API and restart the checkout process.
redirect_url
UR
L
The URL which is provided in Checkout API for redirect_url. See Checkout API & redirect_url.order_no
string
Checkout API
. See Checkout API & order_no.payment_gateway_info
object
Information about the payment gateway, accompanied by the response received from the payment gatewaypg_code
string
The unique identifier, or
pg_code
, for the payment gateway that was used to process the payment. This value corresponds to the specific payment method utilized by the customer, such ascredit-card
.pg_name
string
The name of the payment gateway, represented in all lowercase letters, that was used to perform the payment. This could be one of several values, such as
kpay
(for KNET),mpgs
, orcybersource
. These identifiers provide a human-readable way to understand the payment mechanism that was utilized.pg_response
object
The raw response data that was received directly from the payment gateway after the transaction attempt. This typically includes transaction status, transaction identifier, and potentially error messages or additional data provided by the gateway.
If you have completed the Apple Pay integration between Ottu and Apple, the Checkout SDK will automatically make the necessary checks to display the Apple Pay button.
When you initialize the Checkout SDK with your session_id and payment gateway codes, the SDK will automatically verify the following conditions:
When initializing the Checkout SDK, a session_id with a pg_codes that was associated with the Apple Pay Payment Service was supplied.
The customer has an Apple device that supports Apple Pay payments.
The browser being used supports Apple Pay.
The customer has a wallet configured on their Apple Pay device.
If all of these conditions are met, the Apple Pay button will be displayed and available for use in your checkout flow. If the wallet is not configured, the Apple Pay button will still appear, but with setUp type. Clicking on the setUp
button Apple Pay wallet on their device will open, allowing them to configure it and add payment cards.
By default, the type of the Apple Pay button is pay, which is used to initiate a payment. However, you can override the default button type using the css init property of the Checkout SDK.
If you're using only the Apple Pay button from the Checkout SDK and want to customize its appearance, it's important to follow the Apple Pay guidelines to ensure that your design is consistent with Apple's requirements. Failure to follow the guidelines could result in your app being rejected or your developer account being banned by Apple.
It's the responsibility of the merchant to ensure that their use of the Apple Pay button follows Apple's guidelines, and Ottu cannot be held responsible for any issues that arise from non-compliance. If you have any questions or concerns about using the Apple Pay button, please consult the Apple Pay guidelines or contact Apple directly for assistance.
If you only want to use Apple Pay with the Ottu Checkout SDK and control the other payment methods yourself, you can customize the Apple Pay button using the Checkout SDK's formsOfPayment and css properties.
To display only the Apple Pay button with default css, use the following code:
The formsOfPayment property tells the Checkout SDK to render only the Apple Pay button. If you don't include this property, the SDK will render all available payment options.
To customize the Apple Pay button's appearance, you can use the css property. The example below adjusts the size of the button and centers it within the Checkout SDK container:
The Apple Pay button inside the Checkout SDK container can be customized using the css property by defining the following css classes:
.ottu__sdk-apple-pay-button-type
: This class determines the type of the Apple Pay button. For example, setting-apple-pay-button-type: plain
will render a plain Apple Pay button, while setting it tobuy
ordonate
will render buttons with the corresponding labels..apple-pay-button
: This class sets the width, margin, and padding of the button.
By default, the width of the Apple Pay button is 90% of the Checkout SDK container width, with top and bottom margins of 12px. The Checkout SDK creates a containerized div
with the css class ottu__sdk-main
and places the Apple Pay button inside it. This container has no margin or padding added, as shown in below figure.
To learn more about the css
property, see the css.
If you have completed the Google Pay integration between Ottu and Google Pay, the Checkout SDK will handle the necessary checks to display the Google Pay button seamlessly.
When you initialize the Checkout SDK with your session_id and payment gateway codes pg_codes , the SDK will automatically verify the following conditions:
The
session_id
andpg_codes
provided during SDK initialization must be associated with the Google Pay Payment Service. This ensures that the Google Pay option is available for the customer to choose as a payment method.Web SDK checks if the merchant configuration for Google Pay is correct or not and then show Google Pay button based on it.
The Web SDK displays the Google Pay button irrespective of whether the customer's Google Pay wallet is configured. When the customer clicks the button, they are prompted to log in with their email and add their card if their wallet is not set up.
Google Pay configuration is controlled by using googlePayInit object.
Customize Google Pay button
You can customize the Google Pay button using the Checkout SDK's formsOfPayment and css properties. The formsOfPayment property tells the Checkout SDK to render only the Google Pay button. If you don't include this property, the SDK will render all available payment options.
Due to compliance requirements, KNET requires a popup displaying the payment result after each failed payment. This is available only on the cancelCallback when there is a response from the payment gateway. As a side effect, the user can not try again the payment without clicking on Apple Pay again.
The use of the popup notification described above is specific to the KNET payment gateway. Other payment gateways might have different requirements or notification mechanisms, so be sure to follow the respective documentation for each payment gateway integration.
To properly handle the popup notification for KNET, you need to implement the provided code snippet into your payment processing flow. The code looks like this:
The above code performs the following checks and actions:
It first verifies if the
cancel
object contains information about the payment gateway (payment_gateway_info
).Next, it checks if the
pg_name
property inpayment_gateway_info
is equal to"kpay"
, indicating that the payment gateway used is indeed KNET.If the above conditions are met, it retrieves the payment gateway's response from the
pg_response
property or, if not available, uses a default "Payment was cancelled." error message.Finally, it displays the error message in a popup using the
window.Checkout.showErrorPopup()
function to notify the user about the failed payment.
Last updated