@vigo.technology/payment
Version:
184 lines (183 loc) • 7.26 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Layer2 = void 0;
const api_1 = require("./api");
const config_1 = __importDefault(require("./config"));
const v_engine_1 = require("@vigo.technology/v-engine");
const PayStatus = {
processing: 'Processing',
completed: 'Completed',
failure: 'Failure',
};
class Layer2 {
constructor(options) {
this.timer = null;
const envMode = options.envMode || config_1.default.defaultEnvMode;
const baseURL = options.apiHost || config_1.default.host[envMode];
this.api = new api_1.Api({
baseURL,
timeout: options.timeout,
token: options.token,
});
}
transfer(transfer) {
return __awaiter(this, void 0, void 0, function* () {
const transferTx = yield this.doTransfer(transfer);
console.log("transferTx:", transferTx);
const txReceipt = yield transferTx.awaitReceipt();
console.log("txReceipt:", txReceipt);
this.putHashCount = 0;
yield this.putHash(transfer.payId, transferTx);
return txReceipt;
});
}
doTransfer(transfer) {
return __awaiter(this, void 0, void 0, function* () {
const { jsrpc, ethWallet, toAccount, token, amount, } = transfer;
// 1. Provider
const syncHttpProvider = yield v_engine_1.Provider.newHttpProvider(jsrpc);
// 2. syncWallet
const syncWallet = yield v_engine_1.Wallet.fromEthSigner(ethWallet, syncHttpProvider);
// Get transaction fee
const txFee = yield syncHttpProvider.getTransactionFee("Transfer", toAccount, token);
console.log("txFee:", txFee);
// Do transfer
const transferTx = yield syncWallet.syncTransfer({
to: toAccount,
token,
amount,
fee: txFee.totalFee,
});
console.log("transferTx:", transferTx);
return transferTx;
});
}
putHash(payId, transferTx) {
return __awaiter(this, void 0, void 0, function* () {
// Retry 3 times
return yield this.api.request({
url: `/channels/layer2/records/${payId}`,
method: "put",
data: {
tx_hash: transferTx.txHash,
}
}).then(res => true).catch(e => {
this.putHashCount++;
if (this.putHashCount > 3) {
return false;
}
else {
return this.putHash(payId, transferTx);
}
});
});
}
cancelRecord(id) {
return __awaiter(this, void 0, void 0, function* () {
return this.api.request({
url: `channels/layer2/records/${id}/cancel`,
method: "put",
});
});
}
pollingPayStatus(payId) {
return __awaiter(this, void 0, void 0, function* () {
let count = 0;
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
const finished = yield this.fetchPayStatus(payId).then(status => {
console.log("status1:", status);
// Response status: Processing, Completed, Failure
if (status === PayStatus.completed || status === PayStatus.failure) {
resolve(status);
return true;
}
return false;
}).catch(e => null);
if (finished)
return;
// Polling
this.timer = setInterval(() => {
count++;
if (count > config_1.default.payStatusPollingTimes) {
this.clearInterval();
reject("Time out");
}
this.fetchPayStatus(payId).then(status => {
console.log("status2:", status);
// Response status: Processing, Completed, Failure
if (status === PayStatus.completed || status === PayStatus.failure) {
this.clearInterval();
resolve(status);
}
}).catch(e => { });
}, config_1.default.payStatusPollingInterval);
}));
});
}
createRecord(order) {
return __awaiter(this, void 0, void 0, function* () {
return this.api.request({
url: "/channels/layer2/records",
method: "post",
data: order
}).then(res => res === null || res === void 0 ? void 0 : res.data);
});
}
fetchPayStatus(payId) {
return __awaiter(this, void 0, void 0, function* () {
return this.api.request({
url: `/records/${payId}`,
method: "get",
timeout: config_1.default.payStatusPollingInterval - 100,
}).then(res => { var _a; return (_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.status; });
});
}
clearInterval() {
clearInterval(this.timer);
}
checkParams(options) {
if (!options) {
throw Error("options is required!");
}
if (!options.memberId) {
throw Error("memberId is required!");
}
if (!options.businessId) {
throw Error("businessId is required!");
}
if (!options.chainId) {
throw Error("chainId is required!");
}
if (!options.crypto) {
throw Error("crypto is required!");
}
if (!options.cryptoAmount) {
throw Error("cryptoAmount is required!");
}
if (!options.merchantOrderNo) {
throw Error("merchantOrderNo is required!");
}
if (!options.payMethodId) {
throw Error("payMethodId is required!");
}
if (!options.payChannelId) {
throw Error("payChannelId is required!");
}
if (!options.payerAddress) {
throw Error("payerAddress is required!");
}
}
}
exports.Layer2 = Layer2;