Web V3
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.
displayMode string
string
In SDK v3, there are two display Modes i.e grid
& column
.The Default displayMode
is column
. Here's an example of how Checkout.init
might be called to customize the displayMode
applePayInit object
object
The applePayInit
object enables users to modify the Apple Pay configurations used for generating payment sessions through Apple Pay. By default, all options are pre-configured. However users have the flexibility to customize these configurations using applePayInit
according to their requirements.
- buttonType
Users can change Apple Pay Button Type by using
buttonType
property. Values supported bybuttonType
are written here. - supportedNetworks
The payment networks that the merchant supports. Users can change
supportedNetworks
by usingsupportedNetworks
property.Values supported by supportedNetworks are written here. - merchantCapabilities
An array of the payment capabilities that the merchant supports, such as credit or debit.Users can change
merchantCapabilities
by usingmerchantCapabilities
property. Values supported bymerchantCapabilities
are written here.
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.
Example
To display only the Google Pay button, use the following code:
theme object
object
The SDK Theme Customization feature allows you to modify the appearance of elements within the SDK using a theme
object. This object contains specific CSS
properties that are applied to various components, giving you control over their styling. Theme
object consists of key-value pairs, where each key corresponds to a specific component, and the associated value is a set of CSS
properties to be applied to that component
Here are some example themes that you can use
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.
type
string
he type identifies the modal that should be displayed to the customer. Supported values areerror
,success
&redirect
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.pg_response
is only supported by typeerror
&success
Example
Checkout.showPopup("success","Please wait while we redirect you to merchants page")
Checkout.showPopup(‘error’,'Selected payment method failed. Try again.' , { "merchant":"009057332", "timeOfLastUpdate":"2023-08-01T14:19:00.510Z", "version":"65" })
Checkout.showPopup(‘redirect’,’Redirecting to payment page’')
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. This function will handle error as need and show error modal using Checkout.showPopup()
.
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. This function will handle cancellation as need and show error modal using Checkout.showPopup()
.
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 errorCallback, 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 the
session_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
URL
This field provides the URL to which the customer should be redirected in order to complete the payment process when they have selected this specific payment method. The developer implementing the SDK must ensure that the redirection process is smooth and secure, providing a seamless experience for the customer while maintaining the integrity of the payment process. It's important to note that the
redirect_url
option is only present in the successCallback. In other scenarios, especially with cancelCallback and errorCallback, it's absent. - order_no
string
The order number provided in the Checkout API. See Checkout API & order_no.
- form_of_payment
string
Enum:
apple_pay
,google_pay
,token_pay
,stc_pay
,redirect
Indicates the form of payment used to process the transaction. This can be one of several options, including
apple_pay
,google_pay
,token_pay
,stc_pay
, orredirect
. It's important to note that the redirect option is only present in theerrorCallback
. In other scenarios, especially withcancelCallback
andsuccessCallback
, it's absent. This is because, in the redirect flow, the customer is redirected to a different page where actions like payment cancellation or confirmation occur, not on the page where the SDK is displayed.apple_pay
- Apple Paygoogle_pay
- Google Paytoken_pay
- Token Paystc_pay
- STC Payredirect
- Redirect
- 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. - challenge_occurred
bool
Default: false This flag indicates if an additional verification, such as 3DS, OTP, PIN, etc., was initiated during the payment process. Use this flag in
cancelCallback
anderrorCallback
to control the presentation of error messages, especially for on-site payments undergoing a challenge flow. For example, after a failed 3DS verification, it's useful to show a custom popup informing the user of the payment failure. However, it's crucial to note that not all on-site failed payments need custom error messaging. In cases likeGooglePay
orApplePay
, error messages are inherently handled by the Payment Sheet, which remains open for the user to retry, making this distinction vital.
HTML
JS
JS
Checkout init function
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 theme 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 theme property by defining the following:
applePayInit.buttonType
: This determines the type of the Apple Pay button. For example, settingbuttonType
:plain
will render a plain Apple Pay button, while setting it tobuy
ordonate
will render buttons with the corresponding labels.theme.applePay
: This class sets the width,height, margin, and padding of the button.
By default, the width of the Apple Pay button is 100% of the Checkout SDK container width, gap of 8px between wallets buttons. 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 Apple Pay property, see the theme.
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 . 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.showPopup()
function to notify the user about the failed payment.
Last updated