hapi-paypal
Advanced tools
Comparing version 0.0.82 to 0.0.83
@@ -1,40 +0,2 @@ | ||
import * as hapi from "hapi"; | ||
import { IConfigureOptions, IWebhook } from "paypal-rest-api"; | ||
export interface IHapiPayPalOptions { | ||
sdk: IConfigureOptions; | ||
routes?: Array<Partial<IPayPalRouteConfiguration>>; | ||
webhook?: IWebhook; | ||
} | ||
export interface IPayPalRouteConfig extends hapi.RouteAdditionalConfigurationOptions { | ||
id: string; | ||
} | ||
export interface IPayPalRouteConfiguration extends hapi.RouteConfiguration { | ||
handler?: IPayPalRouteHandler; | ||
config: IPayPalRouteConfig; | ||
} | ||
export declare type IPayPalRouteHandler = (request: hapi.Request, reply: hapi.ReplyNoContinue, error: any, response: any) => void; | ||
export interface InternalRouteConfiguration extends hapi.RouteConfiguration { | ||
handler?: InternalRouteHandler; | ||
config: { | ||
id: string; | ||
payload?: any; | ||
}; | ||
} | ||
export declare type InternalRouteHandler = (request: hapi.Request, reply: hapi.ReplyNoContinue, ohandler: IPayPalRouteHandler) => void; | ||
export declare class HapiPayPal { | ||
private webhookEvents; | ||
private webhook; | ||
private routes; | ||
private server; | ||
private paypal; | ||
constructor(); | ||
register: hapi.PluginFunction<any>; | ||
private setupRoutes(); | ||
private defaultResponseHandler(ohandler, request, reply, error, response); | ||
private buildRoutes(routes); | ||
private enableWebhooks(webhook); | ||
private getWebhookEventTypes(); | ||
private getAccountWebhooks(); | ||
private createWebhook(webhook); | ||
private replaceWebhook(webhook); | ||
} | ||
export * from "./plugin"; | ||
export * from "./glue"; |
333
lib/index.js
"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()); | ||
}); | ||
}; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const boom = require("boom"); | ||
const Joi = require("joi"); | ||
const paypal_rest_api_1 = require("paypal-rest-api"); | ||
const pkg = require("../package.json"); | ||
class HapiPayPal { | ||
constructor() { | ||
this.routes = new Map(); | ||
this.register = (server, options, next) => { | ||
this.server = server; | ||
let webhookPromise = Promise.resolve(); | ||
this.paypal = new paypal_rest_api_1.PayPalRestApi(options.sdk); | ||
if (!this.paypal.config.requestOptions.headers["PayPal-Partner-Attribution-Id"]) { | ||
this.paypal.config.requestOptions.headers["PayPal-Partner-Attribution-Id"] = "Hapi-PayPal"; | ||
} | ||
this.server.expose("paypal", this.paypal); | ||
if (options.webhook) { | ||
const webhooksSchema = Joi.object().keys({ | ||
event_types: Joi.array().min(1).required(), | ||
url: Joi.string().uri({ scheme: ["https"] }).required(), | ||
}); | ||
const validate = Joi.validate(options.webhook, webhooksSchema); | ||
if (validate.error) { | ||
throw validate.error; | ||
} | ||
webhookPromise = this.enableWebhooks(options.webhook); | ||
} | ||
webhookPromise | ||
.then(() => { | ||
this.setupRoutes(); | ||
if (options.routes && options.routes.length > 0) { | ||
this.buildRoutes(options.routes); | ||
} | ||
}) | ||
.then(() => next()); | ||
}; | ||
this.register.attributes = { | ||
pkg, | ||
}; | ||
} | ||
setupRoutes() { | ||
this.routes.set("paypal_payment_create", { | ||
config: { | ||
id: "paypal_payment_create", | ||
}, | ||
handler: (request, reply, ohandler) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const response = yield this.paypal.payment.api.create({ body: request.payload }); | ||
this.defaultResponseHandler(ohandler, request, reply, null, response); | ||
} | ||
catch (err) { | ||
this.defaultResponseHandler(ohandler, request, reply, err, null); | ||
} | ||
}), | ||
method: "POST", | ||
path: "/paypal/payment/create", | ||
}); | ||
this.routes.set("paypal_webhooks_listen", { | ||
config: { | ||
id: "paypal_webhooks_listen", | ||
payload: { | ||
parse: false, | ||
}, | ||
}, | ||
handler: (request, reply, ohandler) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const response = yield this.paypal.webhookEvent.verify(this.webhook.model.id, request.headers, request.payload.toString()); | ||
if (response.verification_status !== "SUCCESS") { | ||
throw new Error("Webhook Verification Error"); | ||
} | ||
request.payload = JSON.parse(request.payload.toString()); | ||
this.defaultResponseHandler(ohandler, request, reply, null, response); | ||
} | ||
catch (err) { | ||
this.defaultResponseHandler(ohandler, request, reply, err, null); | ||
} | ||
}), | ||
method: "POST", | ||
path: "/paypal/webhooks/listen", | ||
}); | ||
this.routes.set("paypal_webhooks_test", { | ||
config: { | ||
id: "paypal_webhooks_test", | ||
}, | ||
handler: (request, reply, ohandler) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const response = yield this.paypal.webhookEvent.api.get(request.params.webhookid); | ||
this.defaultResponseHandler(ohandler, request, reply, null, response); | ||
} | ||
catch (err) { | ||
this.defaultResponseHandler(ohandler, request, reply, err, null); | ||
} | ||
}), | ||
method: "GET", | ||
path: "/paypal/webhooks/test/{webhookid}", | ||
}); | ||
this.routes.set("paypal_invoice_search", { | ||
config: { | ||
id: "paypal_invoice_search", | ||
}, | ||
handler: (request, reply, ohandler) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const response = yield this.paypal.invoice.api.search({ body: request.payload }); | ||
this.defaultResponseHandler(ohandler, request, reply, null, response); | ||
} | ||
catch (err) { | ||
this.defaultResponseHandler(ohandler, request, reply, err, null); | ||
} | ||
}), | ||
method: "POST", | ||
path: "/paypal/invoice/search", | ||
}); | ||
this.routes.set("paypal_invoice_create", { | ||
config: { | ||
id: "paypal_invoice_create", | ||
}, | ||
handler: (request, reply, ohandler) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const response = yield this.paypal.invoice.api.create({ body: request.payload }); | ||
this.defaultResponseHandler(ohandler, request, reply, null, response); | ||
} | ||
catch (err) { | ||
this.defaultResponseHandler(ohandler, request, reply, err, null); | ||
} | ||
}), | ||
method: "POST", | ||
path: "/paypal/invoice", | ||
}); | ||
this.routes.set("paypal_invoice_send", { | ||
config: { | ||
id: "paypal_invoice_send", | ||
}, | ||
handler: (request, reply, ohandler) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const response = yield this.paypal.invoice.api.send(request.params.invoiceid, { | ||
body: request.payload, | ||
}); | ||
this.defaultResponseHandler(ohandler, request, reply, null, response); | ||
} | ||
catch (err) { | ||
this.defaultResponseHandler(ohandler, request, reply, err, null); | ||
} | ||
}), | ||
method: "POST", | ||
path: "/paypal/invoice/{invoiceid}/send", | ||
}); | ||
this.routes.set("paypal_invoice_get", { | ||
config: { | ||
id: "paypal_invoice_get", | ||
}, | ||
handler: (request, reply, ohandler) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const response = yield this.paypal.invoice.api.get(request.params.invoiceid); | ||
this.defaultResponseHandler(ohandler, request, reply, null, response); | ||
} | ||
catch (err) { | ||
this.defaultResponseHandler(ohandler, request, reply, err, null); | ||
} | ||
}), | ||
method: "GET", | ||
path: "/paypal/invoice/{invoiceid}", | ||
}); | ||
this.routes.set("paypal_invoice_cancel", { | ||
config: { | ||
id: "paypal_invoice_cancel", | ||
}, | ||
handler: (request, reply, ohandler) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const response = yield this.paypal.invoice.api.cancel(request.params.invoiceid, { | ||
body: request.payload, | ||
}); | ||
this.defaultResponseHandler(ohandler, request, reply, null, response); | ||
} | ||
catch (err) { | ||
this.defaultResponseHandler(ohandler, request, reply, err, null); | ||
} | ||
}), | ||
method: "POST", | ||
path: "/paypal/invoice/{invoiceid}/cancel", | ||
}); | ||
this.routes.set("paypal_invoice_update", { | ||
config: { | ||
id: "paypal_invoice_update", | ||
}, | ||
handler: (request, reply, ohandler) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const response = yield this.paypal.invoice.api.update(request.params.invoiceid, { | ||
body: request.payload, | ||
}); | ||
this.defaultResponseHandler(ohandler, request, reply, null, response); | ||
} | ||
catch (err) { | ||
this.defaultResponseHandler(ohandler, request, reply, err, null); | ||
} | ||
}), | ||
method: "PUT", | ||
path: "/paypal/invoice/{invoiceid}", | ||
}); | ||
this.routes.set("paypal_invoice_remind", { | ||
config: { | ||
id: "paypal_invoice_remind", | ||
}, | ||
handler: (request, reply, ohandler) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const response = yield this.paypal.invoice.api.remind(request.params.invoiceid, { | ||
body: request.payload, | ||
}); | ||
this.defaultResponseHandler(ohandler, request, reply, null, response); | ||
} | ||
catch (err) { | ||
this.defaultResponseHandler(ohandler, request, reply, err, null); | ||
} | ||
}), | ||
method: "POST", | ||
path: "/paypal/invoice/{invoiceid}/remind", | ||
}); | ||
this.routes.set("paypal_sale_refund", { | ||
config: { | ||
id: "paypal_sale_refund", | ||
}, | ||
handler: (request, reply, ohandler) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const response = yield this.paypal.sale.api.refund(request.params.transactionid, { | ||
body: request.payload, | ||
}); | ||
this.defaultResponseHandler(ohandler, request, reply, null, response); | ||
} | ||
catch (err) { | ||
this.defaultResponseHandler(ohandler, request, reply, err, null); | ||
} | ||
}), | ||
method: "POST", | ||
path: "/paypal/sale/{transactionid}/refund", | ||
}); | ||
} | ||
defaultResponseHandler(ohandler, request, reply, error, response) { | ||
if (ohandler) { | ||
ohandler(request, reply, error, response); | ||
} | ||
else { | ||
if (error) { | ||
const bError = boom.badRequest(error.message); | ||
bError.reformat(); | ||
return reply(bError); | ||
} | ||
return reply(response.body); | ||
} | ||
} | ||
buildRoutes(routes) { | ||
routes.forEach((route) => { | ||
const dRoute = this.routes.get(route.config.id); | ||
const nRoute = { | ||
config: Object.assign({}, dRoute.config, route.config), | ||
handler: (request, reply) => { | ||
dRoute.handler(request, reply, route.handler); | ||
}, | ||
method: route.method || dRoute.method, | ||
path: route.path || dRoute.path, | ||
}; | ||
this.server.route(Object.assign({}, route, nRoute)); | ||
}); | ||
} | ||
enableWebhooks(webhook) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const w = yield Promise.all([this.getWebhookEventTypes(), this.getAccountWebhooks()]); | ||
this.webhookEvents = w[0]; | ||
const accountWebHooks = w[1]; | ||
const twebhook = accountWebHooks.filter((hook) => hook.url === webhook.url)[0]; | ||
!twebhook ? yield this.createWebhook(webhook) : yield this.replaceWebhook(twebhook); | ||
} | ||
catch (err) { | ||
try { | ||
if (err.message) { | ||
const error = JSON.parse(err.message); | ||
if (error.name !== "WEBHOOK_PATCH_REQUEST_NO_CHANGE") { | ||
throw err; | ||
} | ||
} | ||
} | ||
catch (err) { | ||
throw err; | ||
} | ||
} | ||
}); | ||
} | ||
getWebhookEventTypes() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const response = yield this.paypal.webhook.api.types(); | ||
this.webhookEvents = response.body.event_types; | ||
return this.webhookEvents; | ||
}); | ||
} | ||
getAccountWebhooks() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const response = yield this.paypal.webhook.api.list(); | ||
return response.body.webhooks; | ||
}); | ||
} | ||
createWebhook(webhook) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const webhookmodel = new this.paypal.webhook(webhook); | ||
this.webhook = webhookmodel; | ||
yield webhookmodel.create(); | ||
}); | ||
} | ||
replaceWebhook(webhook) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const webhookmodel = new this.paypal.webhook(webhook); | ||
this.webhook = webhookmodel; | ||
yield webhookmodel.update([ | ||
{ | ||
op: "replace", | ||
path: "/event_types", | ||
value: webhook.event_types, | ||
}, | ||
]); | ||
}); | ||
} | ||
} | ||
exports.HapiPayPal = HapiPayPal; | ||
__export(require("./plugin")); | ||
__export(require("./glue")); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "hapi-paypal", | ||
"version": "0.0.82", | ||
"version": "0.0.83", | ||
"description": "A hapi plugin to interface with PayPal Rest API's and webhooks.", | ||
@@ -32,6 +32,7 @@ "license": "MIT", | ||
"postversion": "git push --follow-tags", | ||
"reinstall": "rimraf node_modules yarn.lock && yarn install" | ||
"reinstall": "rimraf node_modules yarn.lock && yarn install", | ||
"start": "ts-node -r dotenv/config example/server.ts" | ||
}, | ||
"dependencies": { | ||
"joi": "^11.0.3", | ||
"joi": "^11.1.1", | ||
"paypal-rest-api": "^0.0.51", | ||
@@ -45,3 +46,3 @@ "us": "^2.0.0" | ||
"@types/joi": "^10.4.0", | ||
"@types/node": "^8.0.17", | ||
"@types/node": "^8.0.31", | ||
"@types/sinon": "^2.3.3", | ||
@@ -55,6 +56,6 @@ "blue-tape": "^1.0.0", | ||
"good-squeeze": "^5.0.2", | ||
"hapi": "^16.4.3", | ||
"hapi": "^16.6.2", | ||
"nyc": "^11.1.0", | ||
"rimraf": "^2.0.0", | ||
"sinon": "^3.3.0", | ||
"sinon": "^4.0.0", | ||
"tap-spec": "^4.1.1", | ||
@@ -61,0 +62,0 @@ "ts-node": "^3.3.0", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 4 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40144
12
536
5
1
Updatedjoi@^11.1.1