in membership-attribute-service/app/services/PaymentFailureAlerter.scala [39:82]
def alertText(
accountSummary: AccountSummary,
subscription: Subscription,
paymentMethodGetter: PaymentMethodId => Future[Either[String, PaymentMethodResponse]],
catalog: Catalog,
)(implicit ec: ExecutionContext, logPrefix: LogPrefix): Future[Option[String]] = {
def expectedAlertText: Future[Option[String]] = {
val formatter = DateTimeFormat.forPattern("d MMMM yyyy").withLocale(Locale.ENGLISH)
val maybePaymentMethodLatestDate: Future[Option[DateTime]] = accountSummary.defaultPaymentMethod.map(_.id) match {
case Some(id) =>
val paymentMethod: Future[Either[String, PaymentMethodResponse]] =
paymentMethodGetter(id) fallbackTo Future.successful(Left("Failed to get payment method"))
paymentMethod.map(_.map(_.lastTransactionDateTime).toOption)
case None => Future.successful(None)
}
def getProductDescription(subscription: Subscription) =
if (subscription.ratePlans.head.product(catalog) == Membership) {
s"${subscription.plan(catalog).productName} membership"
} else if (subscription.ratePlans.head.product(catalog) == Contribution) {
"contribution"
} else {
subscription.plan(catalog).productName
}
maybePaymentMethodLatestDate map { maybeDate: Option[DateTime] =>
maybeDate map { latestDate: DateTime =>
val productDescription = getProductDescription(subscription)
logger.info(
s"Logging an alert for identityId: ${accountSummary.identityId} accountId: ${accountSummary.id}. Payment failed on ${latestDate.toString(formatter)}",
)
s"Our attempt to take payment for your $productDescription failed on ${latestDate.toString(formatter)}."
}
}
}
alertAvailableFor(accountObject(accountSummary), subscription, paymentMethodGetter, catalog) flatMap { shouldShowAlert: Boolean =>
expectedAlertText.map { someText => shouldShowAlert.option(someText).flatten }
}
}