UNPKG

bananas-commerce

Version:

A client for bananas-commerce with support for TypeScript

169 lines (168 loc) 7.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Adyen = void 0; const casedObjectKeys_js_1 = require("../util/casedObjectKeys.js"); const formatVariableResult_js_1 = require("../util/formatVariableResult.js"); const extension_js_1 = require("../extension.js"); /** Initializes, updates, or confirms a delivery with Ingrid as the shipping agent. */ class Adyen extends extension_js_1.Extension { constructor() { super(...arguments); Object.defineProperty(this, "name", { enumerable: true, configurable: true, writable: true, value: "adyen" }); } /** * Initializes or updates a purchase with Adyen as the PSP. If `shipping` is specified, a shipping * reference must be retrieved before initializing the purchase. The data and session identifier * included in the response is used to initialize the Adyen widget. */ async begin(args) { this.assertLoaded(); const { ...body } = args; const response = await this.catchApiError(this.fetcher.endpoint("/api/v1/aco/begin/").method("post")({ body: (0, casedObjectKeys_js_1.casedObjectKeys)(body, "snake"), })); return (0, formatVariableResult_js_1.formatVariableResult)({ ...response, data: (0, casedObjectKeys_js_1.casedObjectKeys)(response.data, "camel") }, (response) => { switch (response.status) { case 201: return "success"; case 400: return "invalid-args"; case 404: return "not-found"; case 422: return "unprocessable"; case 503: return "internal-error"; } }); } /** * * Seals a purchase with Adyen as the PSP. If `shipping` is specified, it must be called after * * confirming the delivery. */ async seal(args) { this.assertLoaded(); const { ...body } = args; const response = await this.catchApiError(this.fetcher.endpoint("/api/v1/aco/seal/").method("post")({ body: (0, casedObjectKeys_js_1.casedObjectKeys)(body, "snake"), })); return (0, formatVariableResult_js_1.formatVariableResult)({ ...response, data: (0, casedObjectKeys_js_1.casedObjectKeys)(response.data, "camel") }, (response) => { switch (response.status) { case 200: return "success"; case 307: return "redirect"; case 400: return "invalid-args"; case 404: return "not-found"; case 503: return "internal-error"; } }); } /** * Initializes or updates a purchase with Adyen as the PSP. If `shipping` is specified, a shipping * reference must be retrieved before initializing the purchase. The data and session identifier * included in the response is used to initialize the Adyen widget. */ async siteBegin(args) { this.assertLoaded(); const { siteCode = this.defaults.siteCode, ...body } = args; if (!siteCode) { return { _type: "internal-error", detail: "A site code must be provided, either by load or in the arguments passed to Adyen.begin", }; } const response = await this.catchApiError(this.fetcher.endpoint("/api/v1/{site_code}/aco/begin/").method("post")({ body: (0, casedObjectKeys_js_1.casedObjectKeys)(body, "snake"), path: { site_code: siteCode }, })); return (0, formatVariableResult_js_1.formatVariableResult)({ ...response, data: (0, casedObjectKeys_js_1.casedObjectKeys)(response.data, "camel") }, (response) => { switch (response.status) { case 201: return "success"; case 400: return "invalid-args"; case 404: return "not-found"; case 422: return "unprocessable"; case 503: return "internal-error"; } }); } /** * Seals a purchase with Adyen as the PSP. If `shipping` is specified, it must be called after * confirming the delivery. */ async siteSeal(args) { this.assertLoaded(); const { siteCode = this.defaults.siteCode, ...body } = args; if (!siteCode) { return { _type: "internal-error", detail: "A site code must be specified either in the constructor of BananasCommerce " + "or in the arguments passed to BananasCommerce.adyen.seal.", }; } const response = await this.catchApiError(this.fetcher.endpoint("/api/v1/{site_code}/aco/seal/").method("post")({ body: (0, casedObjectKeys_js_1.casedObjectKeys)(body, "snake"), path: { site_code: siteCode }, })); return (0, formatVariableResult_js_1.formatVariableResult)({ ...response, data: (0, casedObjectKeys_js_1.casedObjectKeys)(response.data, "camel") }, (response) => { switch (response.status) { case 200: return "success"; case 307: return "redirect"; case 400: return "invalid-args"; case 404: return "not-found"; case 503: return "internal-error"; } }); } async paymentMethods(body) { this.assertLoaded(); const response = await this.catchApiError(this.fetcher.endpoint("/api/v1/aco/pay/").method("options")({ query: (0, casedObjectKeys_js_1.casedObjectKeys)(body, "snake"), })); return (0, formatVariableResult_js_1.formatVariableResult)({ ...response, data: (0, casedObjectKeys_js_1.casedObjectKeys)(response.data, "camel") }, (response) => { switch (response.status) { case 200: return "success"; case 400: return "invalid-args"; } }); } async paymentRequest(body) { this.assertLoaded(); const response = await this.catchApiError(this.fetcher.endpoint("/api/v1/aco/pay/").method("post")({ body: (0, casedObjectKeys_js_1.casedObjectKeys)(body, "snake"), })); return (0, formatVariableResult_js_1.formatVariableResult)({ ...response, data: (0, casedObjectKeys_js_1.casedObjectKeys)(response.data, "camel") }, (response) => { switch (response.status) { case 200: return "success"; case 400: return "invalid-args"; case 404: return "not-found"; case 422: return "unprocessable"; } }); } } exports.Adyen = Adyen;