@sourceloop/ctrl-plane-subscription-service
Version:
Subscription management microservice for SaaS control plane.
66 lines (55 loc) • 2.13 kB
text/typescript
import {BindingScope, inject, injectable} from '@loopback/context';
import {PaymentSourceDto} from '../models';
import {BillingComponentBindings, IService} from 'loopback4-billing';
import {repository} from '@loopback/repository';
import {BillingCustomerRepository} from '../repositories';
({scope: BindingScope.TRANSIENT})
export class BillingPaymentSourceService {
constructor(
(BillingCustomerRepository)
private readonly billingCustomerRepository: BillingCustomerRepository,
(BillingComponentBindings.BillingProvider)
private readonly billingProvider: IService,
) {}
async createPaymentSource(
paymentSourceDto: PaymentSourceDto,
): Promise<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 PaymentSourceDto({
id: paymentSource.id,
customerId: paymentSource.customerId,
card: paymentSource.card,
});
}
async getPaymentSource(paymentSourceId: string): Promise<PaymentSourceDto> {
const paymentSource =
await this.billingProvider.retrievePaymentSource(paymentSourceId);
return new PaymentSourceDto({
id: paymentSource.id,
customerId: paymentSource.customerId,
card: paymentSource.card,
});
}
async deletePaymentSource(paymentSourceId: string): Promise<void> {
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,
});
}
}