UNPKG

@agoransson/klarna-payments

Version:

Typescript wrapper for Klarna Payments API.

71 lines 2.71 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CreateOrder = void 0; const axios_1 = __importDefault(require("axios")); const utils_1 = require("../utils"); const UnableToCreateOrder_1 = require("./UnableToCreateOrder"); const CommonErrors_1 = require("../CommonErrors"); /** * Use this API call to create a new order. Placing an order towards Klarna * means that the Klarna Payments session will be closed and that an order * will be created in Klarna's system. * * When you have received the authorization_token for a successful authorization * you can place the order. Among the other order details in this request, you * include a URL to the confirmation page for the customer. * * When the Order has been successfully placed at Klarna, you need to handle it * either through the Merchant Portal or using Klarna’s Order Management API. * * @param config * @param authorizationToken * @param order * @returns */ function CreateOrder(config, authorizationToken, order, recurring) { return new Promise((resolve, reject) => { const url = utils_1.URLS.API_URL(config) + utils_1.URLS.ORDER_API_URL(authorizationToken); axios_1.default.post(url, { headers: { "Authorization": (0, utils_1.generateAuth)(config.username, config.password), "content-type": "application/json" } }) .then(({ data }) => { if (!config.isLive) { console.log(data); } resolve(data); }) .catch((error) => { const { response } = error; if (response) { const { status } = response; switch (status) { case 400: reject(new UnableToCreateOrder_1.UnableToCreateOrder()); return; case 403: reject(new CommonErrors_1.NotAuthorized()); return; case 404: reject(new CommonErrors_1.ResourceMissing()); return; case 409: reject(new CommonErrors_1.DataMismatch()); return; default: reject(new CommonErrors_1.UnknownError()); } } else { reject(error); } }); }); } exports.CreateOrder = CreateOrder; //# sourceMappingURL=CreateOrder.js.map