UNPKG

@apihawk/billia-sdk

Version:

The ApiHawk Billia SDK

147 lines (146 loc) 5.66 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const billia_sdk_service_base_1 = require("../lib/billia-sdk-service-base"); // tslint:disable-next-line const markdown = require('markdown').markdown; class BilliaSDKPayment extends billia_sdk_service_base_1.BilliaSDKServiceBase { /** * Get available payment methods. * * @param session user session * @param queryOptions methods listing options */ getPaymentMethods(session, queryOptions = {}) { return __awaiter(this, void 0, void 0, function* () { const { addFunds, images, purchaseId, customerProfileId } = queryOptions; const headers = { Accept: 'application/hal+json', 'Content-Type': 'application/json', 'Accept-Response': 'Advanced' }; if (images) { headers.Accept = 'application/vnd.image+json'; } const response = yield this.api.call({ url: '/payment/method?available_methods=true' + (purchaseId ? '&purchase_id=' + purchaseId : '') + (addFunds ? '&add-funds=true' : '') + (customerProfileId ? `&customer_profile_id=${customerProfileId}` : ''), method: 'GET', session, headers }); const paymentMethods = response._embedded.payment_method; if (images) { return paymentMethods.map(this.extractPaymentMethodImage); } else { return paymentMethods; } }); } /** * Get payment method by ID. * * @param session user session * @param methodId the payment method ID */ getPaymentMethodById(session, methodId) { return __awaiter(this, void 0, void 0, function* () { const headers = { Accept: 'application/vnd.image+json' }; const response = yield this.api.call({ url: `/payment/method/${methodId}`, method: 'GET', session, headers }); const paymentMethod = this.extractPaymentMethodImage(response); if (paymentMethod._language && paymentMethod._language.translations && paymentMethod._language.translations.description) { for (const lang in paymentMethod._language.translations.description) { if (paymentMethod._language.translations.description.hasOwnProperty(lang)) { paymentMethod._language.translations.description[lang].value = markdown.toHTML(paymentMethod._language.translations.description[lang].value || '', 'Maruku'); } } } return paymentMethod; }); } /** * Prepares an order for customer payment. * * @param session user session * @param purchaseId order ID * * @returns URL which redirects to a payment page */ preparePurchase(session, purchaseId) { return __awaiter(this, void 0, void 0, function* () { const response = yield this.api.call({ url: `/payment/prepare-purchase/${purchaseId}`, method: 'GET', session, headers: { Accept: 'application/hal+json' } }); return response; }); } /** * Changes the payment method of an order. * * @param session user session * @param purchaseId purchase ID * @param paymentMethodId payment method ID */ changePurchasePaymentMethod(session, purchaseId, paymentMethodId) { return __awaiter(this, void 0, void 0, function* () { const response = yield this.api.call({ url: `/database/order-purchase/${purchaseId}`, method: 'PATCH', body: { payment_method_id: paymentMethodId }, session, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json', 'Accept-Response': 'Advanced' } }); return response; }); } extractPaymentMethodImage(method) { const imgs = method._images || {}; const result = { logo: undefined, icon: undefined }; if (method._images) { result.logo = imgs.payment_logo && imgs.payment_logo.href ? imgs.payment_logo.href : undefined; result.icon = imgs.payment_icon && imgs.payment_icon.href ? imgs.payment_icon.href : undefined; } method._images = result; return method; } } exports.BilliaSDKPayment = BilliaSDKPayment;