kite-publisher
Version:
The Kite Publisher Javascript plugin lets you add one-click trade buttons to your webpage. It works like a basket combined with a payment gateway, where an inline popup opens on your webpage, guides the user through a trade, and lands the user back on you
102 lines (101 loc) • 3.95 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var KiteConnect = /** @class */ (function () {
function KiteConnect(apiKey, redirect_url) {
this.basket = [];
this.finishedCallback = null;
this.options = { api_key: apiKey, redirect_url: redirect_url !== null && redirect_url !== void 0 ? redirect_url : "#" };
}
// Add item to basket
KiteConnect.prototype.add = function (item) {
if (this.basket.length >= 50)
return false;
var cleanItem = {};
var fields = [
"variety",
"exchange",
"tradingsymbol",
"transaction_type",
"quantity",
"order_type",
"price",
"trigger_price",
"product",
"validity",
"readonly",
"tag",
"stoploss",
"squareoff",
"trailing_stoploss",
"disclosed_quantity",
];
fields.forEach(function (field) {
if (item[field] !== undefined) {
cleanItem[field] =
item[field];
}
});
this.basket.push(cleanItem);
return true;
};
// Connect to Kite for basket execution
KiteConnect.prototype.connect = function () {
if (this.basket.length === 0)
return;
var fields = __assign({ data: JSON.stringify(this.basket) }, this.options);
var form = this.createForm(fields, "https://kite.zerodha.com/connect/basket", "post");
this.showPopup(form);
};
// Render button
KiteConnect.prototype.renderButton = function (target) {
var _this = this;
var _a;
var $target = typeof target === "string" ? jQuery(target) : jQuery(target);
var button = jQuery("<button>")
.addClass("kite-trade-button")
.text("Trade with Kite");
if (this.basket.length === 1) {
var item = this.basket[0];
button.addClass("kite-".concat((_a = item === null || item === void 0 ? void 0 : item.transaction_type) === null || _a === void 0 ? void 0 : _a.toLowerCase()));
button.attr("title", "".concat(item.transaction_type, " ").concat(item.tradingsymbol));
}
button.on("click", function (e) {
e.preventDefault();
_this.connect();
});
$target.append(button);
};
// Create form for payload
KiteConnect.prototype.createForm = function (fields, url, method) {
var form = jQuery("<form>").attr({ method: method, action: url });
Object.entries(fields).forEach(function (_a) {
var key = _a[0], value = _a[1];
return form.append(jQuery("<input>").attr({ name: key, type: "hidden", value: value }));
});
return form[0];
};
// Show popup
KiteConnect.prototype.showPopup = function (content) {
var popupWidth = Math.min(window.screen.width / 2, 500);
var popupHeight = Math.min(window.screen.height / 1.5, 500);
var popupLeft = (window.screen.width - popupWidth) / 2;
var popupTop = (window.screen.height - popupHeight) / 2;
var popup = window.open("", "Kite", "width=".concat(popupWidth, ",height=").concat(popupHeight, ",left=").concat(popupLeft, ",top=").concat(popupTop));
if (!popup)
throw new Error("Popup blocked");
var body = popup.document.body;
jQuery(body).empty().append(content);
setTimeout(function () { return content.submit(); }, 500);
};
return KiteConnect;
}());
export { KiteConnect };