UNPKG

bananas-commerce

Version:

A client for bananas-commerce with support for TypeScript

165 lines (164 loc) 6.53 kB
import { casedObjectKeys, } from "../util/casedObjectKeys.js"; import { formatVariableResult, } from "../util/formatVariableResult.js"; import { Extension } from "../extension.js"; /** Initializes, updates, or confirms a delivery with Ingrid as the shipping agent. */ export class Adyen extends 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: casedObjectKeys(body, "snake"), })); return formatVariableResult({ ...response, data: 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: casedObjectKeys(body, "snake"), })); return formatVariableResult({ ...response, data: 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: casedObjectKeys(body, "snake"), path: { site_code: siteCode }, })); return formatVariableResult({ ...response, data: 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: casedObjectKeys(body, "snake"), path: { site_code: siteCode }, })); return formatVariableResult({ ...response, data: 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: casedObjectKeys(body, "snake"), })); return formatVariableResult({ ...response, data: 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: casedObjectKeys(body, "snake"), })); return formatVariableResult({ ...response, data: 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"; } }); } }