@lskjs/billing
Version:
LSK.js module for adding billing in cabinet
231 lines (230 loc) • 11 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _err = _interopRequireDefault(require("@lskjs/err"));
var _serverApi = _interopRequireDefault(require("@lskjs/server-api"));
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
var CoingateBillingApi = /*#__PURE__*/function (_Api) {
(0, _inherits2["default"])(CoingateBillingApi, _Api);
var _super = _createSuper(CoingateBillingApi);
function CoingateBillingApi() {
(0, _classCallCheck2["default"])(this, CoingateBillingApi);
return _super.apply(this, arguments);
}
(0, _createClass2["default"])(CoingateBillingApi, [{
key: "getRoutes",
value: function getRoutes() {
return _objectSpread(_objectSpread({}, (0, _get2["default"])((0, _getPrototypeOf2["default"])(CoingateBillingApi.prototype), "getRoutes", this).call(this)), {}, {
'/create': this.create.bind(this),
'/callback': this.callback.bind(this)
});
}
}, {
key: "url",
value: function url() {
var _this$app;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return (_this$app = this.app).url.apply(_this$app, ["/api/billing/coingate/".concat(args[0])].concat((0, _toConsumableArray2["default"])(args.slice(1))));
}
}, {
key: "create",
value: function () {
var _create = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(req) {
var _yield$this$app$modul, billing, coingate, BillingTransactionModel, amount, transaction, transactionToken, callbackUrl, data, res;
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return this.app.module(['crypto', 'billing']);
case 2:
_yield$this$app$modul = _context.sent;
billing = _yield$this$app$modul.billing;
coingate = billing.providers.coingate;
if (coingate) {
_context.next = 7;
break;
}
throw new _err["default"]('!coingate');
case 7:
BillingTransactionModel = this.models.BillingTransactionModel;
amount = req.data.amount;
_context.next = 11;
return BillingTransactionModel.createPayment({
type: BillingTransactionModel.TYPE_IN,
amount: amount,
provider: 'coingate',
userId: req.user._id
});
case 11:
transaction = _context.sent;
transactionToken = coingate.encode(transaction._id);
callbackUrl = this.app.url("/api/billing/coingate/callback", {
transactionToken: transactionToken
});
data = {
order_id: "transaction_".concat(transaction._id),
price_amount: transaction.amount / 100,
price_currency: 'USD',
receive_currency: 'BTC',
callback_url: callbackUrl,
success_url: this.app.url("/cabinet/billing/".concat(transaction._id)),
cancel_url: this.app.url("/cabinet/billing/".concat(transaction._id)),
description: "Top up #".concat(transaction._id)
};
this.log.debug('create', data);
_context.prev = 16;
_context.next = 19;
return coingate.createOrder(data);
case 19:
res = _context.sent;
if (!res.message) {
_context.next = 23;
break;
}
this.log.error('billing/coingate res', res);
throw [res.message, (res.errors || []).join(', ')].join(': ');
case 23:
transaction.addEvent({
type: 'createOrder',
data: res
});
transaction.meta.continueUrl = res.payment_url;
transaction.markModified('meta');
_context.next = 28;
return transaction.save();
case 28:
return _context.abrupt("return", transaction);
case 31:
_context.prev = 31;
_context.t0 = _context["catch"](16);
transaction.addEvent({
type: 'callback',
data: _context.t0
});
_context.next = 36;
return transaction.save();
case 36:
throw _context.t0;
case 37:
case "end":
return _context.stop();
}
}, _callee, this, [[16, 31]]);
}));
function create(_x) {
return _create.apply(this, arguments);
}
return create;
}() // TODO:
// / pending vs progress
}, {
key: "callback",
value: function () {
var _callback = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(req) {
var BillingTransactionModel, _yield$this$app$modul2, billing, coingate, transactionToken, id, transaction, data, status;
return _regenerator["default"].wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
BillingTransactionModel = this.models.BillingTransactionModel;
_context2.next = 3;
return this.app.module(['billing']);
case 3:
_yield$this$app$modul2 = _context2.sent;
billing = _yield$this$app$modul2.billing;
coingate = billing.providers.coingate;
if (coingate) {
_context2.next = 8;
break;
}
throw new _err["default"]('!coingate');
case 8:
transactionToken = req.query.transactionToken;
id = coingate.decode(transactionToken);
_context2.next = 12;
return BillingTransactionModel.findById(id);
case 12:
transaction = _context2.sent;
if (transaction) {
_context2.next = 15;
break;
}
throw new _err["default"]('billing.transaction.notFound', {
status: 404,
data: {
id: id
}
});
case 15:
if (!(transaction.status !== BillingTransactionModel.STATUS_PENDING)) {
_context2.next = 17;
break;
}
throw new _err["default"]('transaction.notPending', {
status: 400,
data: {
id: id
}
});
case 17:
data = req.body;
this.log.trace('callback', data);
status = data.status;
transaction.addEvent({
type: status,
data: data
});
if (!(status === 'paid')) {
_context2.next = 26;
break;
}
_context2.next = 24;
return transaction.changeStatus(BillingTransactionModel.STATUS_SUCCESS);
case 24:
_context2.next = 29;
break;
case 26:
if (!(status === 'expired' || status === 'invalid' || status === 'canceled')) {
_context2.next = 29;
break;
}
_context2.next = 29;
return transaction.changeStatus(BillingTransactionModel.STATUS_CANCELED);
case 29:
_context2.next = 31;
return transaction.save();
case 31:
return _context2.abrupt("return", 'ok');
case 32:
case "end":
return _context2.stop();
}
}, _callee2, this);
}));
function callback(_x2) {
return _callback.apply(this, arguments);
}
return callback;
}()
}]);
return CoingateBillingApi;
}(_serverApi["default"]);
exports["default"] = CoingateBillingApi;
//# sourceMappingURL=CoingateBillingApi.js.map