UNPKG

@mirahi/vendure-adyen-dropin-plugin

Version:

A Vendure plugin to integrate the Adyen payment provider to your server. This plugin only handles the flow for a drop-in integration on your storefront.

83 lines 3.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.adyenPaymentHandler = void 0; const tslib_1 = require("tslib"); const core_1 = require("@vendure/core"); const adyen_service_1 = require("./adyen.service"); const constant_1 = require("./constant"); const loggerCtx = "AdyenHandler"; // eslint-disable-next-line @typescript-eslint/no-unused-vars let adyenService; exports.adyenPaymentHandler = new core_1.PaymentMethodHandler({ code: "payment-adyen", description: [ { languageCode: core_1.LanguageCode.en, value: "Adyen Payment Provider", }, ], args: { apiKey: { type: "string", label: [ { languageCode: core_1.LanguageCode.en, value: "API Key" }, { languageCode: core_1.LanguageCode.fr, value: "Clé de l'API" }, ], }, redirectUrl: { type: "string", label: [ { languageCode: core_1.LanguageCode.en, value: "Redirect URL" }, { languageCode: core_1.LanguageCode.fr, value: "URL de redirection" }, ], description: [ { languageCode: core_1.LanguageCode.en, value: "Redirect the client to this URL after payment" }, { languageCode: core_1.LanguageCode.fr, value: "Le client est redirigé vers cet URL quand il a fini de payer", }, ], }, }, init(injector) { adyenService = injector.get(adyen_service_1.AdyenService); }, /** This is called when the `addPaymentToOrder` mutation is executed. * It happens either as a GraphQL mutation (which we block when not admin), * or when the `orderService.addPaymentToOrder` method is called by the `AdyenService`. */ createPayment: (ctx, order, _amount /* Use `metadata.amount` */, args, _metadata) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const metadata = _metadata; const state = metadata.success === constant_1.Success.True ? "Authorized" : "Declined"; // #region Error handling and logging // Only Admins and internal calls should be allowed to settle and authorize payments if (ctx.apiType !== "admin") { throw Error(`CreatePayment is not allowed for apiType '${ctx.apiType}'`); } else { core_1.Logger.info(`Admin API requested "createPayment" for order ${order.code}`, loggerCtx); } if (metadata.eventCode !== constant_1.EventCode.Authorisation && metadata.success !== constant_1.Success.True) { throw Error(`Cannot create payment for eventCode ${metadata.eventCode} for order ${order.code} because "${metadata === null || metadata === void 0 ? void 0 : metadata.reason}"`); } if (!metadata.amount.value) { throw Error(`Metadata for Adyen transaction pspReference=${metadata.pspReference} has no amount provided!`); } core_1.Logger.info(`Payment for order ${order.code} created with state '${state}'`, loggerCtx); // #endregion return { amount: metadata.amount.value, state, transactionId: metadata.pspReference, metadata, // Stores all given metadata on a payment }; }), /** This is called when the `settlePayment` mutation or the `orderService.settlePayment` method are executed */ settlePayment: (ctx, order, payment, args) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { if (ctx.apiType !== "admin") { throw Error(`SettlePayment is not allowed for apiType '${ctx.apiType}'`); } return { success: true }; }), }); //# sourceMappingURL=adyen.handler.js.map