machinomy
Version:
Micropayments powered by Ethereum
61 lines • 2.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = require("events");
const logger_1 = require("@machinomy/logger");
const accept_payment_request_1 = require("./accept_payment_request");
const accept_payment_response_1 = require("./accept_payment_response");
const accept_token_request_1 = require("./accept_token_request");
const accept_token_response_1 = require("./accept_token_response");
const PaymentRequiredRequest_1 = require("./PaymentRequiredRequest");
const LOG = new logger_1.default('client');
class ClientImpl extends events_1.EventEmitter {
constructor(transport, channelManager) {
super();
this.transport = transport;
this.channelManager = channelManager;
}
async doPreflight(sender, gateway, datetime) {
this.emit('willPreflight');
const request = new PaymentRequiredRequest_1.PaymentRequiredRequest(sender, datetime);
const deres = await this.transport.paymentRequired(request, gateway);
this.emit('didPreflight');
return deres;
}
async doPayment(payment, gateway, purchaseMeta) {
this.emit('willSendPayment');
LOG.info(`Attempting to send payment to ${gateway}. Sender: ${payment.sender} / Receiver: ${payment.receiver} / Amount: ${payment.price.toString()}`);
const request = new accept_payment_request_1.AcceptPaymentRequest(payment, purchaseMeta);
const deres = this.transport.doPayment(request, gateway);
LOG.info(`Successfully sent payment to ${gateway}.`);
this.emit('didSendPayment');
return deres;
}
async acceptPayment(req) {
const payment = req.payment;
LOG.info(`Received payment request. Sender: ${payment.sender} / Receiver: ${payment.receiver}`);
let token = await this.channelManager.acceptPayment(payment);
LOG.info(`Accepted payment request. Sender: ${payment.sender} / Receiver: ${payment.receiver}`);
return new accept_payment_response_1.AcceptPaymentResponse(token);
}
async doVerify(token, gateway) {
this.emit('willVerifyToken');
LOG.info(`Attempting to verify token with ${gateway}.`);
const request = new accept_token_request_1.AcceptTokenRequest(token);
try {
const deres = this.transport.doVerify(request, gateway);
LOG.info(`Successfully verified token with ${gateway}.`);
this.emit('didVerifyToken');
return deres;
}
catch (e) {
return new accept_token_response_1.AcceptTokenResponse(false);
}
}
acceptVerify(req) {
return this.channelManager.verifyToken(req.token)
.then((res) => new accept_token_response_1.AcceptTokenResponse(res))
.catch(() => new accept_token_response_1.AcceptTokenResponse(false));
}
}
exports.ClientImpl = ClientImpl;
//# sourceMappingURL=client.js.map