@sourceloop/ctrl-plane-subscription-service
Version:
Subscription management microservice for SaaS control plane.
60 lines • 2.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BillingPaymentSourceService = void 0;
const tslib_1 = require("tslib");
const context_1 = require("@loopback/context");
const models_1 = require("../models");
const loopback4_billing_1 = require("loopback4-billing");
const repository_1 = require("@loopback/repository");
const repositories_1 = require("../repositories");
let BillingPaymentSourceService = class BillingPaymentSourceService {
constructor(billingCustomerRepository, billingProvider) {
this.billingCustomerRepository = billingCustomerRepository;
this.billingProvider = billingProvider;
}
async createPaymentSource(paymentSourceDto) {
const customer = await this.billingCustomerRepository.find({
where: { customerId: paymentSourceDto.customerId },
});
if (!customer.length) {
throw new Error('Customer with tenantId is not present');
}
const paymentSource = await this.billingProvider.createPaymentSource(paymentSourceDto);
await this.billingCustomerRepository.updateById(customer[0].id, {
paymentSourceId: paymentSource.id,
});
return new models_1.PaymentSourceDto({
id: paymentSource.id,
customerId: paymentSource.customerId,
card: paymentSource.card,
});
}
async getPaymentSource(paymentSourceId) {
const paymentSource = await this.billingProvider.retrievePaymentSource(paymentSourceId);
return new models_1.PaymentSourceDto({
id: paymentSource.id,
customerId: paymentSource.customerId,
card: paymentSource.card,
});
}
async deletePaymentSource(paymentSourceId) {
const customer = await this.billingCustomerRepository.find({
where: { paymentSourceId: paymentSourceId },
});
if (!customer.length) {
throw new Error('Customer with tenantId is not present');
}
await this.billingProvider.deletePaymentSource(paymentSourceId);
await this.billingCustomerRepository.updateById(customer[0].id, {
paymentSourceId: undefined,
});
}
};
exports.BillingPaymentSourceService = BillingPaymentSourceService;
exports.BillingPaymentSourceService = BillingPaymentSourceService = tslib_1.__decorate([
(0, context_1.injectable)({ scope: context_1.BindingScope.TRANSIENT }),
tslib_1.__param(0, (0, repository_1.repository)(repositories_1.BillingCustomerRepository)),
tslib_1.__param(1, (0, context_1.inject)(loopback4_billing_1.BillingComponentBindings.BillingProvider)),
tslib_1.__metadata("design:paramtypes", [repositories_1.BillingCustomerRepository, Object])
], BillingPaymentSourceService);
//# sourceMappingURL=billing-payment-source.service.js.map