UNPKG

paypal-rest-api

Version:

A typescript module for integrating with PayPal REST APIs.

156 lines (155 loc) 6.06 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const model_1 = require("../abstracts/model"); const api_1 = require("./api"); const schemas_1 = require("./schemas"); class InvoiceModel extends model_1.Model { constructor(model) { super(model, InvoiceModel.schema); this.model = model; } static get(id, options = {}) { return __awaiter(this, void 0, void 0, function* () { const response = yield this.api.get(id, options); return new this(response.body); }); } static search(search, options = {}) { return __awaiter(this, void 0, void 0, function* () { options.body = search; const response = yield this.api.search(options); return response.body.invoices.map((invoice) => { return new this(invoice); }); }); } static list(options = {}) { return __awaiter(this, void 0, void 0, function* () { const response = yield this.api.list(options); return response.body.invoices.map((invoice) => { return new this(invoice); }); }); } static generate(options = {}) { return __awaiter(this, void 0, void 0, function* () { const response = yield this.api.generateNumber(options); return new this(response); }); } static init(client) { const api = new api_1.InvoiceApi(client); InvoiceModel.prototype.api = api; InvoiceModel.api = api; } send(options = {}) { return __awaiter(this, void 0, void 0, function* () { if (!this.model.id) { throw new Error("Model id is invalid"); } if (this.model.status !== "DRAFT") { throw new Error("Invalid Status"); } const response = yield this.api.send(this.model.id, options); yield this.get(); return this; }); } remind(options = {}) { return __awaiter(this, void 0, void 0, function* () { if (!this.model.id) { throw new Error("Model id is invalid"); } const validStatuses = ["SENT"]; if (validStatuses.indexOf(this.model.status) === -1) { throw new Error("Invalid Status"); } const response = yield this.api.remind(this.model.id, options); return this; }); } cancel(options = {}) { return __awaiter(this, void 0, void 0, function* () { if (!this.model.id) { throw new Error("Model id is invalid"); } const validStatuses = ["SENT"]; if (validStatuses.indexOf(this.model.status) === -1) { throw new Error("Invalid Status"); } const response = yield this.api.cancel(this.model.id, options); return this; }); } update(payload = this.model, options) { const _super = name => super[name]; return __awaiter(this, void 0, void 0, function* () { const validStatuses = ["DRAFT", "UNPAID", "SENT"]; if (validStatuses.indexOf(this.model.status) === -1) { throw new Error("Invalid Status"); } return _super("update").call(this, payload, options); }); } delete(options = {}) { const _super = name => super[name]; return __awaiter(this, void 0, void 0, function* () { if (!this.model.id) { throw new Error("Model id is invalid"); } if (this.model.status !== "DRAFT") { throw new Error(`Invalid status`); } return _super("delete").call(this, options); }); } recordPayment(payment, options = {}) { return __awaiter(this, void 0, void 0, function* () { if (!this.model.id) { throw new Error("Model id is invalid"); } const validStatuses = ["UNPAID", "SENT"]; if (validStatuses.indexOf(this.model.status) === -1) { throw new Error("Invalid Status"); } options.body = payment; const response = yield this.api.recordPayment(this.model.id, options); yield this.get(); }); } recordRefund(refund = {}, options = {}) { return __awaiter(this, void 0, void 0, function* () { if (!this.model.id) { throw new Error("Model id is invalid"); } const validStatuses = ["UNPAID", "SENT"]; if (validStatuses.indexOf(this.model.status) === -1) { throw new Error("Invalid Status"); } options.body = refund; const response = yield this.api.recordRefund(this.model.id, options); yield this.get(); }); } qr(opts = {}, options = {}) { return __awaiter(this, void 0, void 0, function* () { if (!this.model.id) { throw new Error("Model id is invalid"); } options.qs = opts; const response = yield this.api.qr(this.model.id, options); this.qrImage = response.image; return this.qrImage; }); } } InvoiceModel.schema = schemas_1.invoiceSchema; exports.InvoiceModel = InvoiceModel;