Skip to content

Commit

Permalink
refactor(auth): refactor payment session data
Browse files Browse the repository at this point in the history
  • Loading branch information
giovanniberti committed Apr 12, 2024
1 parent 38b50bd commit 86b034b
Show file tree
Hide file tree
Showing 13 changed files with 848 additions and 566 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import it.pagopa.transactions.exceptions.InvalidRequestException;
import it.pagopa.transactions.utils.ConfidentialMailUtils;
import it.pagopa.transactions.utils.NpgBuildData;
import it.pagopa.transactions.utils.PaymentSessionData;
import it.pagopa.transactions.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -346,6 +347,20 @@ private Mono<Tuple2<String, FieldsDto>> requestNpgBuildSession(
return buildApiKey.fold(
Mono::error,
apiKey -> {
String contractId = switch (authorizationData.paymentSessionData()) {
case PaymentSessionData.WalletCardSessionData walletCardSessionData -> walletCardSessionData.contractId();
case PaymentSessionData.WalletPayPalSessionData walletPayPalSessionData -> walletPayPalSessionData.contractId();
default -> null;
};

// TODO: Remove `isWalletPayment` parameter and let types determine whether it's a payment with wallet
// this check is actually redundant, kept for backward compatibility with `isWalletPayment`
if (isWalletPayment && contractId == null) {
throw new InternalServerErrorException(
"Invalid request missing contractId"
);
}

if (isApmPayment) {
return npgClient.buildFormForPayment(
UUID.fromString(correlationId),
Expand All @@ -358,11 +373,7 @@ private Mono<Tuple2<String, FieldsDto>> requestNpgBuildSession(
NpgClient.PaymentMethod
.fromServiceName(authorizationData.paymentMethodName()),
apiKey,
isWalletPayment ? authorizationData.contractId().orElseThrow(
() -> new InternalServerErrorException(
"Invalid request missing contractId"
)
) : null,
contractId,
authorizationData.paymentNotices().stream()
.mapToInt(
paymentNotice -> paymentNotice.transactionAmount()
Expand All @@ -382,11 +393,7 @@ private Mono<Tuple2<String, FieldsDto>> requestNpgBuildSession(
NpgClient.PaymentMethod
.fromServiceName(authorizationData.paymentMethodName()),
apiKey,
authorizationData.contractId().orElseThrow(
() -> new InternalServerErrorException(
"Invalid request missing contractId"
)
)
contractId
).map(fieldsDto -> Tuples.of(orderId, fieldsDto));
}
}
Expand Down Expand Up @@ -477,22 +484,25 @@ public Mono<StateResponseDto> requestNpgCardsAuthorization(
.mapToInt(paymentNotice -> paymentNotice.transactionAmount().value()).sum())
+ authorizationData.fee()
);
if (authorizationData.sessionId().isEmpty()) {
return Mono.error(

Mono<String> sessionId = switch (authorizationData.paymentSessionData()) {
case PaymentSessionData.CardSessionData cardSessionData -> Mono.just(cardSessionData.sessionId());
default -> Mono.error(
new BadGatewayException(
"Missing sessionId for transactionId: "
+ authorizationData.transactionId(),
HttpStatus.BAD_GATEWAY
)
);
}
};

final var pspNpgApiKey = npgApiKeyConfiguration
.getApiKeyForPaymentMethod(NpgClient.PaymentMethod.CARDS, authorizationData.pspId());
return pspNpgApiKey.fold(
return sessionId.flatMap(sxId -> pspNpgApiKey.fold(
Mono::error,
apiKey -> npgClient.confirmPayment(
UUID.fromString(correlationId),
authorizationData.sessionId().get(),
sxId,
grandTotal,
apiKey
)
Expand Down Expand Up @@ -520,7 +530,7 @@ public Mono<StateResponseDto> requestNpgCardsAuthorization(
)
)
)
);
));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import it.pagopa.ecommerce.commons.domain.PaymentNotice;
import it.pagopa.ecommerce.commons.domain.TransactionId;
import it.pagopa.generated.transactions.server.model.RequestAuthorizationRequestDetailsDto;
import it.pagopa.transactions.utils.PaymentSessionData;

import java.util.List;
import java.util.Map;
Expand All @@ -28,9 +29,7 @@ public record AuthorizationRequestData(
String pspBusinessName,
Boolean pspOnUs,
String paymentGatewayId,
Optional<String> sessionId,
Optional<String> contractId,
String brand,
PaymentSessionData paymentSessionData,
RequestAuthorizationRequestDetailsDto authDetails,

String asset,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.pagopa.transactions.commands.data;

import it.pagopa.generated.ecommerce.paymentmethods.v1.dto.BundleDto;
import it.pagopa.transactions.utils.PaymentSessionData;

import java.util.Map;
import java.util.Optional;
Expand All @@ -13,20 +14,15 @@
* @param paymentMethodDescription the payment method description string
* @param bundle the gec bundle matching the authorization
* request psp
* @param npgSessionId the NPG session id
* @param brand the brand associated to the payment method
* @param npgContractId the NPG contract id
* @param paymentSessionData data about the specific payment method and
* authorization (e.g. wallet, card, apm)
*/
public record AuthorizationRequestSessionData(
String paymentMethodName,
String paymentMethodDescription,
Optional<BundleDto> bundle,
String brand,
Optional<String> npgSessionId,
Optional<String> npgContractId,

PaymentSessionData paymentSessionData,
String asset,

Map<String, String> brandAssets
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@
import it.pagopa.transactions.repositories.TransactionCacheInfo;
import it.pagopa.transactions.repositories.TransactionTemplateWrapper;
import it.pagopa.transactions.repositories.WalletPaymentInfo;
import it.pagopa.transactions.utils.PaymentSessionData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.http.HttpStatus;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@Slf4j
Expand Down Expand Up @@ -149,42 +153,54 @@ private Mono<AuthorizationOutput> walletNpgCardsPaymentFlow(
}
)
.flatMap(
orderIdAndFieldsDto -> invokeNpgConfirmPayment(
new AuthorizationRequestData(
authorizationData.transactionId(),
authorizationData.paymentNotices(),
authorizationData.email(),
authorizationData.fee(),
authorizationData.paymentInstrumentId(),
authorizationData.pspId(),
authorizationData.paymentTypeCode(),
authorizationData.brokerName(),
authorizationData.pspChannelCode(),
authorizationData.paymentMethodName(),
authorizationData.paymentMethodDescription(),
authorizationData.pspBusinessName(),
authorizationData.pspOnUs(),
authorizationData.paymentGatewayId(),
Optional.of(orderIdAndFieldsDto.getT2().getSessionId()),
authorizationData.contractId(),
authorizationData.brand(),
authorizationData.authDetails(),
authorizationData.asset(),
authorizationData.brandAssets()
),
orderIdAndFieldsDto.getT1(),
correlationId,
true
)
.map(
authorizationOutput -> new AuthorizationOutput(
authorizationOutput.authorizationId(),
authorizationOutput.authorizationUrl(),
Optional.of(orderIdAndFieldsDto.getT2().getSessionId()),
authorizationOutput.npgConfirmSessionId(),
authorizationOutput.authorizationTimeoutMillis()
)
)
orderIdAndFieldsDto -> {
PaymentSessionData paymentSessionData = switch (authorizationData.paymentSessionData()) {
case PaymentSessionData.WalletCardSessionData walletCardSessionData -> new PaymentSessionData.WalletCardSessionData(
walletCardSessionData.brand(),
Optional.of(Objects.requireNonNull(orderIdAndFieldsDto.getT2().getSessionId())),
walletCardSessionData.cardBin(),
walletCardSessionData.lastFourDigits(),
walletCardSessionData.contractId()
);
default ->
throw new IllegalStateException("Unexpected non wallet card payment session data of type: " + authorizationData.paymentSessionData().getClass());
};

return invokeNpgConfirmPayment(
new AuthorizationRequestData(
authorizationData.transactionId(),
authorizationData.paymentNotices(),
authorizationData.email(),
authorizationData.fee(),
authorizationData.paymentInstrumentId(),
authorizationData.pspId(),
authorizationData.paymentTypeCode(),
authorizationData.brokerName(),
authorizationData.pspChannelCode(),
authorizationData.paymentMethodName(),
authorizationData.paymentMethodDescription(),
authorizationData.pspBusinessName(),
authorizationData.pspOnUs(),
authorizationData.paymentGatewayId(),
paymentSessionData,
authorizationData.authDetails(),
authorizationData.asset(),
authorizationData.brandAssets()
),
orderIdAndFieldsDto.getT1(),
correlationId,
true
)
.map(
authorizationOutput -> new AuthorizationOutput(
authorizationOutput.authorizationId(),
authorizationOutput.authorizationUrl(),
Optional.of(orderIdAndFieldsDto.getT2().getSessionId()),
authorizationOutput.npgConfirmSessionId(),
authorizationOutput.authorizationTimeoutMillis()
)
);
}
);
}

Expand Down Expand Up @@ -343,11 +359,17 @@ private Mono<AuthorizationOutput> invokeNpgConfirmPayment(
HttpStatus.BAD_GATEWAY
);
};

Optional<String> sessionId = switch (authorizationData.paymentSessionData()) {
case PaymentSessionData.CardSessionData cardSessionData -> Optional.of(cardSessionData.sessionId());
default -> Optional.empty();
};

return Mono.just(
new AuthorizationOutput(
orderId,
authUrl,
authorizationData.sessionId(),
sessionId,
confirmPaymentSessionId,
Optional.empty()
)
Expand Down Expand Up @@ -385,7 +407,7 @@ private Mono<Either<BadGatewayException, StateResponseDto>> confirmPayment(

protected URI getLogo(AuthorizationRequestData authorizationRequestData) {
String paymentTypeCode = authorizationRequestData.paymentTypeCode();
String brand = authorizationRequestData.brand();
String brand = authorizationRequestData.paymentSessionData().brand();
String asset = authorizationRequestData.asset();
Optional<Map<String, String>> brandAssets = authorizationRequestData.brandAssets();
String logo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public Mono<RequestAuthorizationResponseDto> handle(TransactionRequestAuthorizat
AuthorizationOutput authorizationOutput = authorizationOutputAndGateway.getT1();
PaymentGateway paymentGateway = authorizationOutputAndGateway.getT2();
TransactionAuthorizationRequestData.CardBrand cardBrand = TransactionAuthorizationRequestData.CardBrand
.valueOf(authorizationRequestData.brand());
.valueOf(authorizationRequestData.paymentSessionData().brand());
TransactionAuthorizationRequestedEvent authorizationEvent = new TransactionAuthorizationRequestedEvent(
t.getTransactionId().value(),
new it.pagopa.ecommerce.commons.documents.v1.TransactionAuthorizationRequestData(
Expand Down
Loading

0 comments on commit 86b034b

Please sign in to comment.