Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHK-2736] feat(NPG): handle new fields from GET auth-data wallet api #475

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>https://raw.githubusercontent.com/pagopa/pagopa-infra/main/src/domains/wallet-app/api/payment-wallet-for-ecommerce/v1/_openapi.json.tpl</inputSpec>
<inputSpec>https://raw.githubusercontent.com/pagopa/pagopa-infra/CHK-2736-wallet-auth-data/src/domains/wallet-app/api/payment-wallet-for-ecommerce/v1/_openapi.json.tpl</inputSpec>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<inputSpec>https://raw.githubusercontent.com/pagopa/pagopa-infra/CHK-2736-wallet-auth-data/src/domains/wallet-app/api/payment-wallet-for-ecommerce/v1/_openapi.json.tpl</inputSpec>
<inputSpec>https://raw.githubusercontent.com/pagopa/pagopa-infra/main/src/domains/wallet-app/api/payment-wallet-for-ecommerce/v1/_openapi.json.tpl</inputSpec>

TODO: to be replaced after dependent pr merge

<generatorName>java</generatorName>
<library>webclient</library>
<generateApiDocumentation>false</generateApiDocumentation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import it.pagopa.transactions.exceptions.NpgNotRetryableErrorException;
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 @@ -359,6 +360,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 @@ -371,11 +386,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 @@ -395,11 +406,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 @@ -512,22 +519,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 @@ -583,7 +593,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.v2.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 @@ -18,6 +18,7 @@
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;
Expand All @@ -29,10 +30,7 @@
import javax.crypto.SecretKey;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
Expand Down Expand Up @@ -178,43 +176,55 @@ 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,
clientId,
Optional.of(userId)
)
.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,
clientId,
Optional.of(userId)
)
.map(
authorizationOutput -> new AuthorizationOutput(
authorizationOutput.authorizationId(),
authorizationOutput.authorizationUrl(),
Optional.of(orderIdAndFieldsDto.getT2().getSessionId()),
authorizationOutput.npgConfirmSessionId(),
authorizationOutput.authorizationTimeoutMillis()
)
);
}
);
}

Expand Down Expand Up @@ -421,11 +431,18 @@ private Mono<AuthorizationOutput> invokeNpgConfirmPayment(
);
};

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

default -> Optional.empty();
};

return authUrl.map(
url -> new AuthorizationOutput(
orderId,
url,
authorizationData.sessionId(),
sessionId,
confirmPaymentSessionId,
Optional.empty()
)
Expand Down Expand Up @@ -463,7 +480,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 @@ -133,7 +133,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
Loading