def paymentDetails()

in membership-attribute-service/app/services/zuora/payment/PaymentService.scala [25:50]


  def paymentDetails(
      sub: Subscription,
      defaultMandateIdIfApplicable: Option[String] = None,
      catalog: Catalog,
  )(implicit logPrefix: LogPrefix): Future[PaymentDetails] = {
    val currency = sub.plan(catalog).chargesPrice.currencies.head
    // I am not convinced this function is very safe, hence the option
    val eventualMaybeLastPaymentDate = zuoraService
      .getPaymentSummary(sub.subscriptionNumber, currency)
      .map(_.current.serviceStartDate.some)
      .recover { case _ => None }
    eventualMaybeLastPaymentDate.withLogging(s"lastPaymentDate for $sub")

    for {
      account <- zuoraService.getAccount(sub.accountId)
      eventualBills = getNextBill(sub.id, account, 15).withLogging(s"next bill for $sub")
      eventualMaybePaymentMethod = getPaymentMethod(account.defaultPaymentMethodId, defaultMandateIdIfApplicable) // kick off async
      bills <- eventualBills
      maybePaymentMethod <- eventualMaybePaymentMethod
      lpd <- eventualMaybeLastPaymentDate
    } yield {
      val maybePayment = bills.find(_.amount > 0).map(bill => Payment(Price(bill.amount, currency), bill.date))
      val maybeFirstInvoiceDate = bills.headOption.map(_.date)
      PaymentDetails.fromSubAndPaymentData(sub, maybePaymentMethod, maybePayment, maybeFirstInvoiceDate, lpd, catalog)
    }
  }