Skip to main content
Version: 4.0.2

Mounting the Charge SDK

Introduction

To implement the Charge SDK into a custom application, use the boilerplate below to assist you in the setting up and configuration to match your site.

Creating a Payment Configuration

The merchant_id and config_id used to mount the SDK come from a payment configuration in the Merchant Dashboard:

  1. Navigate to the Merchant Dashboard and select Payment Configuration, then Add Config.
  2. Enter a name for the configuration.
  3. Set the allowed origins for the Charge SDK (for development you can use *; lock this down for production).
  4. Select the payment method types to enable.
  5. Set the redirect URL for both approved and declined payments — see Redirect and Popup Flows.
  6. Save, then note the Config ID (your config_id) and Token (your merchant_id).

Create a separate configuration per environment — the redirect URLs and origins differ between staging and production.

Note: for optimal performance, it is more ideal to place this code at the bottom of the body tag.

JavaScript Boilerplate

<head>
<script src="https://{domain-name}/form/v1/payment/dmg-ecomm-sdk.js"></script>
</head>
<body>
...

<script>
window._DataMeshAPICallback = function () {
window.Datamesh.load({
merchant_id: '<your-merchant-id',
config_id: 'your-intended-config-id',
order_id: 'order-id',
locale: 'en-US',
public_key: 'consumer-public-key',
container: '#sdk-fe-wrapper',
payment_flow_method: 'payment',
callback: () => {
window.Datamesh.on("payment.completed", response => {
// merchant payment response logic
});
window.Datamesh.on("payment.failed", response => {
// merchant payment failure logic
});
}
})
}
</script>

...
<div id="sdk-fe-wrapper"></div>
</body>
  1. Replace domain-name with one of the following for the respective environment.
  2. You can also replace <#sdk-fe-wrapper> with your own div element name.
EnvironmentDomain Name
Staginghttps://dmg-ecommerce-backend.ecomm-stg.dmgsecure.io
Production (IN)https://dmg-ecommerce-backend.ecomm-prd.dmgsecure.io
Production (AU)https://dmg-ecommerce-backend.ecomm-au.dmgsecure.io
Production (ID)https://dmg-ecommerce-backend.ecomm-id.dmgsecure.io

Methods Available

The following methods are available

load()
on()
off()
destroy()

load() Method

This method is used to initialise and load the SDK iframe by passing the required parameters.

The Datamesh object is your entry point to the Client SDK; calling this method will instantiate the object.

Syntax: load(merchant_id,config_id,order_id,locale,public_key,container,callback)

Example

window.Datamesh.load({
merchant_id: '<your-merchant-id',
config_id: 'your-intended-config-id',
order_id: 'order-id',
locale: 'en-US',
public_key: 'consumer-public-key',
container: '#sdk-fe-wrapper',
callback: () => {
window.Datamesh.on("payment.completed", response => {
// merchant payment response logic
});
}
})

Options

NameDescriptionType
merchant_idREQUIRED - Unique identifier assigned by DMG to uniquely identify you. This is called service_id in the Charge API schema, and is shown as Token on the Merchant Dashboard's Payment Configuration page.string
config_idOPTIONAL - Unique identifier of custom configuration you created on the Merchant Dashboard (shown as Config ID). The configuration binds environment-specific settings including allowed origins, enabled payment methods, and the result-page redirect URLs — use the correct value for each environment.string
order_idUsed as the order intent which your application back-end creates prior to launching the Charge SDK, via POST /order_token.string
localeOPTIONAL - Used to retrieve locale setting of your environment. Default to 'en-US'string
public_keyREQUIRED - Consumer public key provided by DMG used to authenticate your request to launch the Charge SDK.string
containerREQUIRED - Name of the element of your page that will contain the Charge SDK iFrame.string
payment_flow_methodOPTIONAL - The flow to initiate. Set to payment for the standard end-to-end payment flow. See Payment Flows.string
callbackOPTIONAL - Used to attach listens to DMG object once initiated.string

on() Method

This method subscribes an event onto an eventHandler

Syntax: on(eventName, eventHandler)

NameDescriptionType
eventNameREQUIRED - The event code to refer to, to receive updates on. Refer to page Handle Client SDK ObserverString
eventHandlerREQUIRED - A function for the merchant to implement which allows merchant to implement logic on events being triggered by the SDKFunction(Event eventObject)

Object eventObject

{
data: {
type: 'STATUS',
status: {
status: 'payment.completed'
}
}
}

For example: by implementing the following code, the merchant will be able to run merchant logic for when successful payments have been created:

window.Datamesh.on('payment.completed', (response) => {
// merchant logic
})

off() Method

This method removes an event handler.

Syntax: off(eventName, eventHandler)

NameDescriptionType
eventNameREQUIRED - The event to unsubscribeString
eventHandlerREQUIRED - A handler function previously attached for the eventFunction(Event eventObject)

destroy() Method

This method unloads and destroys the iFrame.

Syntax: destroy()

  • Use the method load() OR
  • Refresh the browser to load the iFrame again