들어가기전…

현재 Toss와 연동한 createLecturePaymentWithToss메서드의 코드이다.

  async createLecturePaymentWithToss(
    userId: number,
    dto: CreateLecturePaymentWithTossDto,
  ): Promise<PendingPaymentInfoDto> {
    const { lectureId, lectureSchedule } = dto;
    const { lecture, refundableDate } = await this.checkLectureValidity(
      lectureId,
      lectureSchedule,
    );

    await this.checkUserPaymentValidity(userId, dto.orderId);
    await this.checkApplicableCoupon(dto);

    // 강의 자리수 확인 및 쿠폰 비교
    const coupons: Coupons = await this.comparePrice(
      userId,
      lecture.price,
      dto,
    );

    await this.trxCreateLecturePaymentWithToss(
      userId,
      lecture,
      dto,
      coupons,
      refundableDate,
    );

    return new PendingPaymentInfoDto({
      orderId: dto.orderId,
      orderName: dto.orderName,
      value: dto.finalPrice,
    });
  }

이 중 쿠폰 적용 기능은 11월에 구현이 완료되어 약 4~5개월이 된 코드다.

Untitled

프로젝트를 8월에 시작했지만 해커톤 참여로인해 본격적인 프로젝트 시작은 9월부터라는 것을 감안하면 프로젝트를 시작한지 약 2달밖에 안된 시점이다. 그때는 “내가 이런 코드를 짜다니 대박인걸?” 이라는 생각을하면서 짯을것이다.

그로부터 약 4개월 뒤 24년 3월의 어느날.. 프로젝트가 대부분 구현이 끝나 코드를 되돌아보는 기회가 생겨 결제 로직들을 살펴보게 되었다.

Untitled

Untitled

Untitled

Untitled

Untitled

OMG “내가 이런 코드를 짰다니 대박이네”라는 생각이 절로 나와 바로 이슈를 추가하고 이 글을 쓰게 되었다.

https://github.com/connection-2023/backend/issues/163

간단한 쿠폰 적용 로직 설명

리팩터링을 하기 전 현재 적용된 할인 로직을 살펴보자!

Untitled

Untitled

첫 결제 요청이 들어오게 되면

checkApplicableCoupon메서드를 먼저 실행한다.

  1. checkApplicableCoupon 인수로 dto를 전달한다.