You can find full list of JavaScript/TypeScript modules, classes, interfaces, types, functions and variables available when creating your own plugin in our Builder plugin API reference. Please don't use undocumented functionality as your plugin might break if we make incompatible changes.
Every plugin consists of multiple files:
This file must contain a single JavaScript object with following structure:
true
if plugin uses store module functionality. This also forces store module to be published along with plugin
element even if website has no store or cart element on any page. Defaults to false
.true
if payment gateway added by plugin requires billing information to be entered by customer. Defaults to
false
.true
custom
element added by plugin will have a fixed height when publishing with auto-layout enabled. Defaults to
false
.require
function with the list of given modules to execute their code. This array may be
empty (or omitted) if you have no modules declared in main.js or main.min.js. This option is available since v3.7.301.true
if plugin should
load only when builder is opened in administration (template creation) mode. This can be used to prevent plugin
from getting loaded into production environment while it is being developed. Defaults to false
.Example of main.json
{
"type": "gateway",
"requireStoreModule": true,
"requireBillingInfo": true
}
This file should contain html code (with Mustache templating engine) that will be rendered to builder and final published site.
If you need different html for different cases (when in builder, on preview or on publish) there are variables added to template for that:
true
when plugin is rendered on published/previewed in builder website.true
when plugin is rendered on previewed in builder website.Template will also be populated with all properties from plugins element data object ElementDataDef.
This file must have the plugin logic, that registers and handles custom elements, payment gateways, or overrides UI functionality.
Plugin logic is loaded and executed always, even if that plugin is disabled. Disabling plugin only tells builder to ignore custom elements and payment gateways registered by the plugin.
Note.
Since v3.7.301 plugin logic is loaded from main.min.js instead of main.js if minified version is present and builder is not in admin (template creation) mode.
Sometimes it is not enough to use HTML/Javascript code for creating plugin. Often 3rd party plugins provide REST API for integration or manipulation of the system, for example:
To add PHP code into your plugin take the following steps:
{{{requireService}}}
. The marker will be replaced with PHP code in published websitephp
listLet's say you develop plugin which integrates some 3rd party system to your site. This system provides PHP SDK which you want to use.
The PHP SDK can be a folder which contains one or multiple .php files, other folders containing other files, etc.
To add all this PHP library to your plugin take the following steps:
After that you will have access to folder "site" within your PHP file "main.php".
Let's presume you have file in your plugin "myplugin/site/SomeLibrary.php". Then you can load this file with line require_once("SomeLibrary.php");
in your PHP file.
If you are using on-premises builder then you are given one more option of testing the plugin.
Instead of using built-in system you can work on plugin in your builder at once.
All plugins are stored in builder folder "plugins" and are distributed by folders.
To add your plugin to builder you need to take the following steps:
{"admin": true}
After adding plugin you can test it in admin builder mode ("Create custom templates" link in your on-premises brand).
To make plugin available for all your customers set parameter {"admin": false}
in file "main.json".
Plugin must use PluginWrapper.registerPlugin() method to register a custom element with its property dialog definition, saving/loading and other event handlers.
<iframe
id="{{id}}_vimeo"
src="http://player.vimeo.com/video/{{content.groupId}}?portrait={{content.portrait}}&title={{content.title}}&autoplay={{content.autoplay}}&color={{content.color}}"
width="{{width}}px"
height="{{height}}px"
frameborder="0"
webkitallowfullscreen
mozallowfullscreen
allowfullscreen
></iframe>
main.js:PluginWrapper.registerPlugin('vimeo', {
name: 'Vimeo',
element: {
minSize: {width: 100, height: 100},
defaultSize: {width: 480, height: 300},
resizable: true
},
propertyDialog: {
noScroll: true,
tabs: [
{children: [
{type: 'VerticalLayout', children: [
{type: 'Label', text: __('Vimeo ID or URL'),
helpText: __('Vimeo video id or url with format https://vimeo.com/{video_id}')
},
{type: 'TextField', id: 'groupId'}
]},
{type: 'HorizontalLayout', css: {marginTop: 15}, children: [
{type: 'VerticalLayout', children: [
{type: 'Label', text: __('Portrait'), helpText: __('Show the user’s portrait on the video')},
{type: 'RadioBox', id: 'portrait0', label: __('Yes'), group: 'portrait'},
{type: 'RadioBox', id: 'portrait1', label: __('No'), group: 'portrait'}
]},
{type: 'VerticalLayout', children: [
{type: 'Label', text: __('Title'), helpText: __('Show the title on the video.')},
{type: 'RadioBox', id: 'title0', label: __('Yes'), group: 'title'},
{type: 'RadioBox', id: 'title1', label: __('No'), group: 'title'}
]},
{type: 'VerticalLayout', children: [
{type: 'Label', text: __('Autoplay'), helpText: __('Play the video automatically on load.')},
{type: 'RadioBox', id: 'autoplay0', label: __('Yes'), group: 'autoplay'},
{type: 'RadioBox', id: 'autoplay1', label: __('No'), group: 'autoplay'}
]},
{type: 'VerticalLayout', children: [
{type: 'Label', text: __('Color'), helpText: __('Specify the color of the video controls.')},
{type: 'ColorSelector', id: 'color'}
]}
]}
]}
]
},
resizeTimeout: null,
resizeAction: function(data, elem) {
if (!this.resizeTimeout) {
var self = this;
this.resizeTimeout = setTimeout(function () {
self.resizeTimeout = null;
self.updateElement();
}, 1000);
}
},
openAction: function(fields, data, elem) {
fields.groupId.setText(data.content.groupId);
fields.portrait0.setValue(data.content.portrait === '1');
fields.portrait1.setValue(data.content.portrait === '0');
fields.title0.setValue(data.content.title === '1');
fields.title1.setValue(data.content.title === '0');
fields.autoplay0.setValue(data.content.autoplay === '1');
fields.autoplay1.setValue(data.content.autoplay === '0');
fields.color.setValue(data.content.color);
fields.groupId.on('change', function() {
var url = fields.groupId.getText();
var regExp = /https:\/\/(www\.)?vimeo.com\/(\d+)($|\/)/;
var match = url.match(regExp);
if (match) {
fields.groupId.setText(match[2]);
}
});
},
applyAction: function(fields, data, elem) {
if (fields.portrait0.getValue()) data.content.portrait = '1';
if (fields.portrait1.getValue()) data.content.portrait = '0';
if (fields.title0.getValue()) data.content.title = '1';
if (fields.title1.getValue()) data.content.title = '0';
if (fields.autoplay0.getValue()) data.content.autoplay = '1';
if (fields.autoplay1.getValue()) data.content.autoplay = '0';
data.content.color = fields.color.getValue().replace('#', '');
data.content.groupId = fields.groupId.getText();
},
loadAction: function (data) {
if (!data.content.groupId) data.content.groupId = '7976699';
if (!data.content.portrait) data.content.portrait = '1';
if (!data.content.title) data.content.title = '1';
if (!data.content.autoplay) data.content.autoplay = '0';
if (!data.content.color) data.content.color = '#00adef';
}
});
In this section e-commerce plugin is considered as a payment gateway which can be added to "Store Cart" widget in builder.
Let's say you need to have payment gateway in your Store which does not currently exist in site builder. Then creation of e-commerce plugin is a right choice.
The creation of e-commerce plugin can be started from creation of a simple payment plugin. Let's name simple payment plugin a button for which website owner can set some predefined product(s), amount, price, etc. (similar to Buy Now button of Paypal system). To deal with it you need to use documentation (API) of payment gateway system which you want to integrate — you will need to know what parameters and what endpoint you need to submit the form to.
Let's write the code for a simple payment plugin for a dummy payment gateway:
main.js:
PluginWrapper.registerPlugin('myplugin', {
name: 'myplugin',
element: {
minSize: {width: 50, height: 50},
defaultSize: {width: 100, height: 100},
resizable: true
},
propertyDialog: {
tabs: [
{children: [
{type: 'VerticalLayout', spacing: 15, children: [
{type: 'HorizontalLayout', columnWeights: [6, 6], css: {marginTop: 15}, children: [
{type: 'VerticalLayout', children: [
{type: 'Label', text: 'Merchant ID', helpText: 'Your identifier in payment system'},
{type: 'TextField', id: 'merchant'}
]},
{type: 'VerticalLayout', children: [
{type: 'Label', text: 'Password', helpText: 'Your password provided by payment system'},
{type: 'TextField', id: 'password'}
]}
]},
{type: 'HorizontalLayout', columnWeights: [4, 4, 4], css: {marginTop: 15}, children: [
{type: 'VerticalLayout', children: [
{type: 'Label', text: 'Product Name'},
{type: 'TextField', id: 'name'}
]},
{type: 'VerticalLayout', children: [
{type: 'Label', text: 'Price'},
{type: 'TextField', id: 'price'}
]},
{type: 'VerticalLayout', children: [
{type: 'Label', text: 'Currency'},
{type: 'DropdownBox', id: 'currency', options: [
{id: '#USD', name: 'USD', value: 'USD'},
{id: '#EUR', name: 'EUR', value: 'EUR'},
{id: '#GBP', name: 'GBP', value: 'GBP'}
]}
]}
]}
]}
]}
]
},
resizeAction: function (data, elem) {
if (!this.resizeTimeout) {
var self = this;
this.resizeTimeout = setTimeout(function () {
self.resizeTimeout = null;
self.updateElement();
}, 1000);
}
},
openAction: function(fields, data, elem) {
var itm;
itm = fields.currency.getItemById('#' + data.content.currency);
fields.currency.selectItem(itm);
fields.merchant.setText(data.content.merchant);
fields.password.setText(data.content.password);
fields.name.setText(data.content.name);
fields.price.setText(data.content.price);
},
applyAction: function(fields, data, elem) {
var itm;
itm = fields.currency.getSelectedItem();
data.content.currency = itm.getOriginal().value;
data.content.merchant = fields.merchant.getText();
data.content.password = fields.password.getText();
data.content.name = fields.name.getText();
data.content.price = fields.price.getText();
},
// set some default values
loadAction: function (data) {
if (!data.content.name)
data.content.name = 'Default product name';
if (!data.content.price)
data.content.amount = '10';
if (!data.content.currency)
data.content.currency = 'USD';
}
});
<form action="[payment gateway endpoint]" method="post" target="_blank">
<input type="hidden" name="d_merchantid" value="{{content.merchant}}" />
<input type="hidden" name="d_productname" value="{{content.name}}" />
<input type="hidden" name="d_productprice" value="{{content.price}}" />
<input type="hidden" name="d_currency" value="{{content.currency}}" />
{{{requireService}}}
<button type="submit">Pay</button>
</form>
<?php
$password = md5($pluginData->password);
echo '<input type="hidden" name="d_password" value="'.$password.'" />';
echo '<input type="hidden" name="d_transactionid" value="'.md5(microtime()).'" />';
{{{requireService}}}
string will be replaced with an output that PHP code generates.The plugin does not do much — it creates form which submits order information to payment system by clicking button "Pay". The payment system handles sent data and offers payment methods to a client.
The plugin properties dialog should look like this in builder interface:
Custom payment gateways that should appear in store cart must be registered using ElementRegister.registerPaymentGateway() method.
To integrate your plugin into store some changes must be made to a simple payment plugin:
The site builder should know that your plugin is a payment gateway and it has to be included to Store Cart widget as an option.
To let builder know about it we add the following code at the very beginning of "main.js" file:
ElementRegister.registerPaymentGateway({
name: "Dummy system", // (required) name of payment gateway inside Store Cart widget
id: "myplugin", // (required) must be the same as your plugin name
priceFieldId: "price", // (required) plugin price field ID in your plugin defined in property "propertyDialog"
keyFieldId: "merchant", // (required) field ID in your plugin defined in property "propertyDialog"
keyField2Id: "password", // (optional) another field ID in your plugin defined in property "propertyDialog"
keyFieldDef: {type: "HorizontalLayout", columnWeights: [6,6], noPadding: true, children: [
{type: "TextField", placeholder: "Merchant ID", id: "key"},
{type: "TextField", placeholder: "Password", id: "key2"}
]},
nameFieldId: "name", // (optional) plugin product name field ID in your plugin defined in property "propertyDialog"
globalVars: ["merchant","password"] // (optional) these variables will be available in PHP code
});
Let's take a closer look at these parameters (see PaymentGatewayItem):
name
— Name of payment gateway system inside Store Cart widget. This property is only informative and does not affect any plugin logic.id
— This property tells builder what plugin payment gateway will be associated with. The value for this property must be the same as your plugin name.priceFieldId
— Just specify field ID which stands for price in your property "propertyDialog" in "main.js". From the beginning the price is not known in Store and therefore it is replaced with a special placeholder {{price}}
in HTML form. When you submit the form this placeholder will be replaced with a real order price and sent to payment system.keyFieldId
— Specify merchant field ID here (or any other parameter which is related to payment system merchant identifier).keyField2Id
— Here we presume that our Dummy payment gateway specifies password which must be also used in the form (for example in signature calculation). So we add it as a second parameter.keyFieldDef
— This field accepts object which describes layout of properties specified above (keyFieldId, keyField2Id) in Store Cart properties dialog. The structure of this object is the same as of "propertyDialog". Developing your payment gateway you can see examples of this property used in other predefined payment gateways (e.g. in "plugins/assist/main.js").nameFieldId
— Just specify field ID which stands for product name/description in your property "propertyDialog" in "main.js".globalVars
— Specify array containing field IDs which you will need to have in PHP code. In example above we specified fields "merchant" and "password". We presume that after successful payment the payment system will send IPN (Instant payment notification) to website which must be handled properly. The information which payment system sends in this step can be encrypted and to decrypt it you might use your merchant ID or password values.After adding this part of code you should see something like this in Store Cart widget properties dialog:
When you integrate your plugin in Store widget then a special variable {{content.store}}
is available inside "main.html" file.
Let's see how the file should look like now:
<form action="[payment gateway endpoint]" method="post"
{{^content.store}} target="_blank"{{/content.store}}
{{#content.store}} data-gateway-id="DummySystem"{/content.store}}>
<input type="hidden" name="d_key" value="{{content.key}}" />
<input type="hidden" name="d_productname" value="{{content.name}}" />
<input type="hidden" name="d_productprice" value="{{content.price}}" />
<input type="hidden" name="d_currency" value="{{content.currency}}" />
{{#content.store}}
<input type="hidden" name="d_transactionid" value="{transactionId}" />
<input type="hidden" name="d_callbackurl" value="{callbackUrl}" />
<input type="hidden" name="d_returnurl" value="{returnUrl}" />
<input type="hidden" name="d_cancelurl" value="{cancelUrl}" />
<input type="hidden" name="d_verifyurl" value="{verifyUrl}" />
{{/content.store}}
{{{requireService}}}
<button type="submit">Pay</button>
</form>
The changes we made are:
target="_blank"
not used in form when the form is used in Store widget.data-gateway-id
(this is required for Store widget).{transactionId}
. This is a placeholder which will be replaced with a real transaction ID generated by Store widget when submitting the form.{callbackUrl}
, {returnUrl}
, {cancelUrl}
and {verifyUrl}
. These are placeholders which will be replaced by real URLs generated by Store widget when page is loaded. Let's take a closer look at these parameters:
{callbackUrl}
— Add this placeholder if your payment system supports IPN. The placeholder will be replaced with URL which must be used by payment system as IPN URL.{returnUrl}
— Add this placeholder if your payment system supports return URL. Usually payment systems let you specify URL where buyer will be redirected after successful payment.{cancelUrl}
— Add this placeholder if your payment system supports cancel URL. Usually payment systems let you specify URL where buyer will be redirected after canceling payment (or after payment was unsuccessful).{verifyUrl}
— Add this placeholder if your payment system supports payment verifying URL. Sometimes payment systems let you specify URL which will be called by payment system before callback URL in order to check if payment is not fake and really exists. Usually in this case payment systems require to output some message indicating of payment existence.We can see that parameter d_transactionid
is added twice to the form when using payment system from Store widget — first time in "main.html" file and the second time in "main.php" file. To fix this problem we need to edit "main.php" file:
<?php
$password = md5($pluginData->password);
echo '<input type="hidden" name="d_password" value="'.$password.'" />';
if (!$pluginData->store) {
echo '<input type="hidden" name="d_transactionid" value="'.md5(microtime()).'" />';
}
Also we can see that parameter d_password
is not added to the form at all. This parameter will be added to the form by PHP gateway payment class.
In previous sections we managed to submit form to payment system when the form is used in Store widget. Now the form can send dynamic price taking it from Store and pass extra parameters like IPN URL needed for payment system to inform website about payment status. After that we need write logic which will handle IPN call on website and send appropriate response (if needed).
1. Create file "plugins/myplugin/site/GatewayMyplugin.php".
Note: The name of the file should be constructed from concatenated words "Gateway" and your plugin name with capitalized first letter (e.g. if plugin name is "abc" then gateway class must be "GatewayAbc" or if plugin name is "abc_def" then gateway class must be "GatewayAbcDef").
2. Add the following contents to the file:
<?php
class GatewayMyplugin extends PaymentGateway {
/** @var boolean */
protected $returnAfterCallback = false;
public function init() {}
/** @return string */
public function getTransactionId() {}
/**
* @param array $formVars
* @return string[]
*/
public function createFormFields($formVars) {}
/**
* @param array $formVars
* @return string
*/
public function createRedirectUrl($formVars) {}
/**
* @param array $formVars
* @return string
*/
public function createRedirectUrl($formVars) {}
/**
* @param StoreModuleOrder $order
* @return boolean
*/
public function callback(StoreModuleOrder $order = null) {
return true;
}
/** @param StoreModuleOrder $order */
public function verify(StoreModuleOrder $order = null) {}
}
The class must extend abstract class "PaymentGateway" which contains one required method "getTransactionId". All other methods are optional and should be used only if needed by payment system you are developing payment gateway plugin for.
We left all methods empty because the logic will depend on payment system requirements. Let's take a closer look at these methods:
init
— There can be any code for class initialisation. The method is called after class contructor.true
then the payment will be set as successful. Otherwise the payment will be considered failed.
returnAfterCallback
— Property indicating whether website must redirect client to Return URL right after Callback URL (IPN) was called and callback logic was processed.getTransactionId
— Must return value from request (POST or GET depending on payment system) of property which stands for transaction ID passed as parameter "d_transactionid" in form. This method is important because Store on website must identify payment by returned value. If Store does not find payment by transaction ID returned by this method it will not be able to set appropriate status to payment (complete,pending,failed).createFormFields
— Returns array of HTML inputs which should be embedded to the form right after a buyer submitted the form but the form has not been sent to payment system yet. The method accepts argument $formVars which is associative array containing serialized form information.createRedirectUrl
— Returns URL which will be set to form "action" attribute. Some payment systems do not have static payment submiting URL. Intead they provide per merchant-basis URL or dynamic URL depending on form field values.callback
— The logic of callback. For example payment system might require to respond with some message which would indicate of successful IPN handling. In this case You can output any message like echo "OK";
(the message can be any depending on payment system). Note that you can also set your headers if payment system require (e.g. header('Content-Type: application/json; charset=utf-8');
.true
then the payment will be set as successful in Store. Otherwise the payment will be considered failed.
verify
— The logic of payment verification. For example payment system might require to respond with some message which would indicate of payment existance. Otherwise payment system will not process the payment considering it fake. You can output any message like echo "OK";
(the message can be any depending on payment system).
$this->config
in any method. This is a property of type "stdClass" contains information stored in file "main.js" in property globalVars
. For example in our Dummy payment gateway we can access our stored properties like this: $this->config->merchant
and $this->config->password
. Except our stored properties the object $config can also have these by default:
wbLang
— 2-letter language code the website is currently on (set only if "Languages" widget is added in website).wbDefLang
— 2-letter language code of default site language (set only if "Languages" widget is added in website).wbBaseLang
— 2-letter language code of builder language the client was on while building website (always set).Plugin must follow these minimum requirements to override default UI:
Since builder version 3.7.305 you are also required to open the Brand > Customize Builder dialog and select which UI plugin must be used in builder. In earlier versions builder was automatically initializing last loaded UI plugin.
{
"type": "ui"
}
main.js:require(["PluginApi/UIManager"], function(UIManager) {
var MyCustomUI = function(builder) {
UIManager.UIManager.call(this, builder);
var builderCanvas = $('<div id="builder_canvas"/>');
$(document.body).append(builderCanvas);
builder.init(builderCanvas);
}
MyCustomUI.prototype = Object.create(UIManager.UIManager.prototype);
MyCustomUI.prototype.constructor = MyCustomUI;
UIManager.registerUI(pluginData.id, MyCustomUI);
});
Please see BuilderAPI class for available builder, website and page control functions.