@basel_mn/qv-api-client
Version:
TypeScript/JavaScript client for QV API, providing easy access to invoices, payments, and Swish integration
94 lines (93 loc) • 4 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SwishApi = void 0;
class SwishApi {
constructor(client) {
this.client = client;
}
getStatus(id, hash) {
return __awaiter(this, void 0, void 0, function* () {
try {
console.log('Fetching Swish payment status for ID:', id, 'and hash:', hash);
console.log('Fetching Swish payment status url:', `/invoices/${id}/${hash}/pay/swish`);
const { data } = yield this.client.get(`/invoices/${id}/${hash}/pay/swish`);
console.log('Swish payment status data:', data);
return data;
}
catch (error) {
console.error('Error fetching Swish payment status:', error);
const enhancedError = error;
enhancedError.context = { id, hash };
throw enhancedError;
}
});
}
initiatePayment(paymentData) {
return __awaiter(this, void 0, void 0, function* () {
try {
console.log('Initiating Swish payment with data:', paymentData);
const { id, hash } = paymentData, restData = __rest(paymentData, ["id", "hash"]);
if (!id) {
throw new Error('Invoice ID is required for payment');
}
const { data } = yield this.client.post(`/invoices/${id}/${hash}/pay/swish`, restData);
return data;
}
catch (error) {
const enhancedError = error;
enhancedError.context = { paymentData };
throw enhancedError;
}
});
}
// add cancelPayment method
cancelPayment(id, hash) {
return __awaiter(this, void 0, void 0, function* () {
try {
console.log('Cancelling Swish payment for ID:', id, 'and hash:', hash);
const { data } = yield this.client.delete(`/invoices/${id}/${hash}/pay/swish`);
return data;
}
catch (error) {
console.error('Error cancelling Swish payment:', error);
const enhancedError = error;
enhancedError.context = { id, hash };
throw enhancedError;
}
});
}
initiateRefund(refundData) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { data } = yield this.client.post('/swish/refund', refundData);
return data;
}
catch (error) {
const enhancedError = error;
enhancedError.context = { refundData };
throw enhancedError;
}
});
}
}
exports.SwishApi = SwishApi;