paypal-rest-api
Version:
A typescript module for integrating with PayPal REST APIs.
86 lines (85 loc) • 3.98 kB
JavaScript
;
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 abstracts_1 = require("../abstracts");
const schemas_1 = require("./schemas");
class WebhookEventApi extends abstracts_1.Api {
constructor(client) {
super(client, WebhookEventApi.schemas, WebhookEventApi.paths);
}
resend(id, options = {}) {
if (this.schemas.id) {
id = this.schemaValidate(id, WebhookEventApi.schemas.id);
}
options.uri = this.parsePath(WebhookEventApi.paths.resend, { id });
options = this.schemaValidate(options, WebhookEventApi.schemas.resend);
return this.client.request(options);
}
verify(options = {}) {
return __awaiter(this, void 0, void 0, function* () {
options.uri = WebhookEventApi.paths.verify;
options = this.schemaValidate(options, WebhookEventApi.schemas.verify);
let parsedEvent;
try {
parsedEvent = JSON.parse(options.body.webhook_event);
}
catch (err) {
throw new Error("Webhook event is malformed");
}
const stringy = JSON.stringify({
auth_algo: options.body.auth_algo,
cert_url: options.body.cert_url,
transmission_id: options.body.transmission_id,
transmission_sig: options.body.transmission_sig,
transmission_time: options.body.transmission_time,
webhook_id: options.body.webhook_id,
});
options.body = stringy.slice(0, -1) + `,"webhook_event":${options.body.webhook_event}` + "}";
try {
const response = yield this.client.request(options);
const parsedResponse = JSON.parse(response.body);
if (parsedResponse.verification_status !== "SUCCESS") {
throw parsedResponse;
}
return parsedResponse;
}
catch (err) {
this.logResend(parsedEvent);
throw err;
}
});
}
simulate(options = {}) {
options.uri = WebhookEventApi.paths.simulate;
options = this.schemaValidate(options, WebhookEventApi.schemas.simulate);
return this.client.request(options);
}
logResend(webhookEvent) {
if (process.env.NODE_ENV === "development") {
console.log(`Webhook Resend: curl -X POST -v -H "Content-Type:application/json" -H "Authorization: Bearer ${this.client.oauth.token.access_token}" -d '{}' ${this.client.config.requestOptions.baseUrl + this.parsePath(this.paths.resend, { id: webhookEvent.id })}`);
}
}
}
WebhookEventApi.paths = {
get: `/v1/notifications/webhooks-events/{id}`,
list: `/v1/notifications/webhooks-events`,
resend: `/v1/notifications/webhooks-events/{id}/resend`,
simulate: `/v1/notifications/simulate-event`,
verify: `/v1/notifications/verify-webhook-signature`,
};
WebhookEventApi.schemas = {
get: schemas_1.webhookEventGetRequestSchema,
id: schemas_1.webhookEventIdSchema,
list: schemas_1.webhookEventListRequestSchema,
resend: schemas_1.webhookEventResendRequestSchema,
simulate: schemas_1.webhookEventSimulateRequestSchema,
verify: schemas_1.webhookEventVerifyRequestSchema,
};
exports.WebhookEventApi = WebhookEventApi;