@atomixdesign/nodepay-pay-way
Version:
Nodepay strategy for the WestPac PayWay payment gateway.
268 lines (267 loc) • 12.8 kB
JavaScript
"use strict";
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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
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 __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PaywayAPI = void 0;
var typedi_1 = require("typedi");
var uuid_1 = require("uuid");
var qs_1 = __importDefault(require("qs"));
var types_1 = require("../types");
var network_1 = require("@atomixdesign/nodepay-core/build/network");
var PaywayAPI = /** @class */ (function () {
function PaywayAPI(config) {
this.config = config;
var httpClientFactory = new network_1.HttpClientFactory();
this.idempotencyKey = uuid_1.v4();
this.secretAuthHeader = "Basic " + this.encodeKey(config.secretKey);
this.publicAuthHeader = "Basic " + this.encodeKey(config.publishableKey);
this.httpClient = httpClientFactory.create({
baseURL: config.apiRoot,
headers: {
Authorization: this.secretAuthHeader,
Accept: "application/" + config.responseType,
'Idempotency-Key': this.idempotencyKey,
},
});
}
PaywayAPI.prototype.encodeKey = function (key) {
return Buffer.from(key, 'binary').toString('base64');
};
PaywayAPI.prototype._process = function (requestConfig) {
return __awaiter(this, void 0, void 0, function () {
var response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.httpClient.request(requestConfig)];
case 1:
response = _a.sent();
return [2 /*return*/, {
status: response.status,
statusText: response.statusText,
data: response.data,
originalResponse: response,
}];
}
});
});
};
// Verify key expiry/validity
PaywayAPI.prototype.verifyKey = function () {
return __awaiter(this, void 0, void 0, function () {
var response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.httpClient.request({
url: '/api-keys/latest',
})];
case 1:
response = _a.sent();
if (response.status === 200 && (response === null || response === void 0 ? void 0 : response.data.key) !== this.config.secretKey) {
console.error('Payway API key is about to expire. Please log in to your Payway admin and generate a new api key.');
}
return [2 /*return*/, {
status: response.status,
statusText: response.statusText,
data: response.data,
originalResponse: response,
}];
}
});
});
};
PaywayAPI.prototype.getCCtoken = function (creditCard) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._process({
method: 'post',
url: '/single-use-tokens',
data: qs_1.default.stringify(__assign({}, creditCard)),
headers: {
Authorization: this.publicAuthHeader,
},
})];
});
});
};
PaywayAPI.prototype.getBankAccountToken = function (bankAccount) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._process({
method: 'post',
url: '/single-use-tokens',
data: qs_1.default.stringify(__assign({}, bankAccount)),
headers: {
Authorization: this.publicAuthHeader,
},
})];
});
});
};
PaywayAPI.prototype.addCustomer = function (customer) {
return __awaiter(this, void 0, void 0, function () {
var payload;
return __generator(this, function (_a) {
payload = __assign({}, customer);
delete payload.customerNumber;
return [2 /*return*/, this._process({
method: 'put',
url: "/customers/" + customer.customerNumber,
data: qs_1.default.stringify(__assign({}, payload)),
})];
});
});
};
PaywayAPI.prototype.updateCustomerDetails = function (reference, customerAddress) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._process({
method: 'put',
url: "/customers/" + reference + "/contact",
data: qs_1.default.stringify(__assign({}, customerAddress)),
})];
});
});
};
PaywayAPI.prototype.updateCustomerPayment = function (reference, customerPaymentDetails) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._process({
method: 'put',
url: "/customers/" + reference + "/payment-setup",
data: qs_1.default.stringify(__assign({}, customerPaymentDetails)),
})];
});
});
};
PaywayAPI.prototype.stopCustomerPayments = function (customerId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._process({
method: 'patch',
url: "/customers/" + customerId + "/payment-setup",
data: qs_1.default.stringify({ stopped: true }),
})];
});
});
};
PaywayAPI.prototype.deleteCustomer = function (customerId) {
return __awaiter(this, void 0, void 0, function () {
var response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.stopCustomerPayments(customerId)];
case 1:
_a.sent();
return [4 /*yield*/, this.httpClient.request({
method: 'delete',
url: "/customers/" + customerId,
})];
case 2:
response = _a.sent();
return [2 /*return*/, {
status: response.status,
statusText: response.statusText,
data: response.data,
originalResponse: response,
}];
}
});
});
};
PaywayAPI.prototype.placeCharge = function (singleUseTokenId, charge) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._process({
method: 'post',
url: '/transactions',
data: qs_1.default.stringify(__assign({ singleUseTokenId: singleUseTokenId }, charge)),
})];
});
});
};
PaywayAPI.prototype.placeDirectCharge = function (charge) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._process({
method: 'post',
url: '/transactions',
data: qs_1.default.stringify(__assign({}, charge)),
})];
});
});
};
PaywayAPI.prototype.schedulePayment = function (customerId, schedule) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._process({
method: 'put',
url: "/customers/" + customerId + "/schedule",
data: qs_1.default.stringify(__assign({}, schedule)),
})];
});
});
};
PaywayAPI = __decorate([
typedi_1.Service(),
__metadata("design:paramtypes", [types_1.PaywayConfig])
], PaywayAPI);
return PaywayAPI;
}());
exports.PaywayAPI = PaywayAPI;