in support-workers/src/main/scala/com/gu/support/workers/lambdas/PreparePaymentMethodForReuse.scala [25:120]
def this() = this(ServiceProvider)
override protected def servicesHandler(
state: PreparePaymentMethodForReuseState,
requestInfo: RequestInfo,
context: Context,
services: Services,
) = {
import com.gu.WithLoggingSugar._
val zuoraService = services.zuoraService
val accountId = state.paymentFields.billingAccountId
for {
account <- zuoraService.getAccount(accountId).withEventualLogging(s"getObjectAccount($accountId)")
accountIdentityId <- getOrFailWithMessage(
account.basicInfo.IdentityId__c,
s"Zuora account $accountId has no identityId",
)
_ <- ifFalseReturnError(
accountIdentityId == state.user.id,
s"Zuora account $accountId identity id: $accountIdentityId does not match ${state.user.id}",
)
paymentId <- getOrFailWithMessage(
account.billingAndPayment.defaultPaymentMethodId,
s"Zuora account $accountId has no default payment method",
)
getPaymentMethodResponse <- zuoraService
.getPaymentMethod(paymentId)
.withEventualLogging(s"getPaymentMethod($paymentId)")
_ <- ifFalseReturnError(
getPaymentMethodResponse.paymentMethodStatus == "Active",
s"Zuora account $accountId has a non active default payment method",
)
sfContactId <- getOrFailWithMessage(
account.basicInfo.sfContactId__c,
s"Zuora account $accountId has no sfContact",
)
paymentMethod <- toPaymentMethod(
getPaymentMethodResponse,
services.goCardlessService,
account.billingAndPayment.paymentGateway,
)
sfContact = SalesforceContactRecord(sfContactId, account.basicInfo.crmId)
(productState, productType) <- Future.fromTry(state.product match {
case c: Contribution =>
Success(
(
ContributionState(
product = c,
paymentMethod = paymentMethod,
salesForceContact = sfContact,
similarProductsConsent = None,
),
),
c,
)
case sp: SupporterPlus =>
Success(
(
SupporterPlusState(
product = sp,
paymentMethod = paymentMethod,
appliedPromotion = state.appliedPromotion,
salesForceContact = sfContact,
billingCountry = state.user.billingAddress.country,
similarProductsConsent = None,
),
),
sp,
)
case _ =>
Failure(
new RuntimeException(
"Reusing payment methods is not yet supported for products other than contributions or SupporterPlus",
),
)
})
} yield HandlerResult(
CreateZuoraSubscriptionState(
productSpecificState = productState,
requestId = state.requestId,
user = state.user,
product = productType,
analyticsInfo = state.analyticsInfo,
firstDeliveryDate = None,
appliedPromotion = state.appliedPromotion,
csrUsername = None,
salesforceCaseId = None,
acquisitionData = state.acquisitionData,
),
requestInfo
.appendMessage(s"Payment method is ${paymentMethod.toFriendlyString}")
.appendMessage(s"Product is ${state.product.describe}"),
)
}