bananas-commerce
Version:
A client for bananas-commerce with support for TypeScript
73 lines (72 loc) • 2.66 kB
JavaScript
import { casedObjectKeys, } from "../util/casedObjectKeys.js";
import { formatVariableResult, } from "../util/formatVariableResult.js";
import { Extension } from "../extension.js";
export class Klarna extends Extension {
constructor() {
super(...arguments);
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: "klarna"
});
}
/**
* Initializes or updates a purchase with Klarna 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 Klarna widget.
*/
async begin(args) {
this.assertLoaded();
const { ...body } = args;
const response = await this.catchApiError(this.fetcher.endpoint("/api/v1/kco3/begin/").method("post")({
body: casedObjectKeys(body, "snake"),
}));
return formatVariableResult({ ...response, data: casedObjectKeys(response.data, "camel") }, (response) => {
switch (response.status) {
case 200:
case 201:
return "success";
case 400:
return "invalid-args";
case 409:
return "conflict";
case 422:
return "unprocessable";
case 503:
return "internal-error";
}
});
}
/**
* Confirms a purchase with Klarna as the PSP. If `shipping` is
* specified, it must be called after confirming the delivery.
*/
async confirm(args) {
this.assertLoaded();
const { merchantId, orderId } = args;
const response = await this.catchApiError(this.fetcher
.endpoint("/api/v1/kco3/{merchant_id}/confirm/{order_id}/")
.method("post")({
path: {
merchant_id: merchantId,
order_id: orderId,
},
}));
return formatVariableResult({ ...response, data: casedObjectKeys(response.data, "camel") }, (response) => {
switch (response.status) {
case 200:
case 201:
return "success";
case 400:
return "invalid-args";
case 409:
return "conflict";
case 422:
return "unprocessable-entity";
case 503:
return "internal-error";
}
});
}
}