Skip to content

Commit

Permalink
feat: [IOBP-687,IOBP-886] Add the recent used payment method when the…
Browse files Browse the repository at this point in the history
… user pays (#415)

* feat: Add the recent used payment method when the user pays

* chore: add code to test in local
  • Loading branch information
Hantex9 authored Oct 14, 2024
1 parent 94a89a6 commit f8549ca
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
2 changes: 1 addition & 1 deletion assets/wallet/wallet_onboarding.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
const paymentMethodId = urlParams.get('paymentMethodId');
const container = document.getElementById("outcomeSelect");
if (container.value === "0") {
fetch('/payment-wallet/v1/wallets/mock', {
fetch('/io-payment-wallet/v1/wallets/mock', {
method: 'POST',
body: JSON.stringify({
paymentMethodId,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"api_idpay": "https://raw.githubusercontent.com/pagopa/cstar-infrastructure/v8.25.1/src/domains/idpay-app/api/idpay_appio_full/openapi.appio.full.yml",
"api_fast_login": "https://raw.githubusercontent.com/pagopa/io-auth-n-identity-domain/[email protected]/apps/io-session-manager/api/fast-login.yaml",
"api_trial_system": "https://raw.githubusercontent.com/pagopa/io-backend/v14.3.0-RELEASE/api_trial_system.yaml",
"api_pagopa_walletv3": "https://raw.githubusercontent.com/pagopa/pagopa-infra/v1.64.0/src/domains/pay-wallet-app/api/io-payment-wallet/v1/_openapi.json.tpl",
"api_pagopa_ecommerce": "https://raw.githubusercontent.com/pagopa/pagopa-infra/v1.64.0/src/domains/ecommerce-app/api/ecommerce-io/v2/_openapi.json.tpl",
"api_pagopa_walletv3": "https://raw.githubusercontent.com/pagopa/pagopa-infra/v1.202.0/src/domains/pay-wallet-app/api/io-payment-wallet/v1/_openapi.json.tpl",
"api_pagopa_ecommerce": "https://raw.githubusercontent.com/pagopa/pagopa-infra/v1.202.0/src/domains/ecommerce-app/api/ecommerce-io/v2/_openapi.json.tpl",
"api_pagopa_biz_events": "https://raw.githubusercontent.com/pagopa/pagopa-biz-events-service/0.1.37/openapi/openapi_io_patch.json",
"api_pagopa_platform": "https://raw.githubusercontent.com/pagopa/pagopa-infra/v1.64.0/src/domains/shared-app/api/session-wallet/v1/_openapi.json.tpl",
"api_services": "https://raw.githubusercontent.com/pagopa/io-backend/master/api_services_app_backend.yaml",
Expand Down
35 changes: 34 additions & 1 deletion src/features/payments/persistence/userWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ import { WalletApplicationStatusEnum } from "../../../../generated/definitions/p
import { WalletClientStatusEnum } from "../../../../generated/definitions/pagopa/walletv3/WalletClientStatus";
import { WalletInfo } from "../../../../generated/definitions/pagopa/walletv3/WalletInfo";
import { WalletInfoDetails } from "../../../../generated/definitions/pagopa/walletv3/WalletInfoDetails";
import { UserLastPaymentMethodResponse } from "../../../../generated/definitions/pagopa/ecommerce/UserLastPaymentMethodResponse";
import { WalletStatusEnum } from "../../../../generated/definitions/pagopa/walletv3/WalletStatus";
import {
WalletLastUsageType,
WalletLastUsageTypeEnum
} from "../../../../generated/definitions/pagopa/ecommerce/WalletLastUsageType";
import { GuestMethodLastUsageType } from "../../../../generated/definitions/pagopa/ecommerce/GuestMethodLastUsageType";
import { uuidv4 } from "../../../utils/strings";
import { generateWalletDetailsByPaymentMethod } from "./paymentMethods";

const userWallets = new Map<WalletInfo["walletId"], WalletInfo>();

// eslint-disable-next-line functional/no-let, @typescript-eslint/no-unused-vars
let recentUsedPaymentMethod: UserLastPaymentMethodResponse | undefined;

const getUserWallets = () => Array.from(userWallets.values());

const getUserWalletInfo = (walletId: WalletInfo["walletId"]) =>
Expand All @@ -30,7 +40,7 @@ const generateUserWallet = (
paymentMethodId: number,
extraDetails: Partial<WalletInfoDetails> = {}
) => {
const walletId = (getUserWallets().length + 1).toString();
const walletId = uuidv4();
const { details, paymentMethodAsset } =
generateWalletDetailsByPaymentMethod(paymentMethodId);

Expand Down Expand Up @@ -87,6 +97,27 @@ const updateUserWalletApplication = (
return E.left("Wallet not found");
};

const setRecentUsedPaymentMethod = (
id: string,
type: WalletLastUsageType | GuestMethodLastUsageType
) => {
if (type === WalletLastUsageTypeEnum.wallet) {
recentUsedPaymentMethod = {
date: new Date(),
type,
walletId: id
};
return;
}
recentUsedPaymentMethod = {
date: new Date(),
type,
paymentMethodId: id
};
};

const getRecentusedPaymentMethod = () => recentUsedPaymentMethod;

// At server startup
generateWalletData();

Expand All @@ -96,5 +127,7 @@ export default {
getUserWalletInfo,
generateUserWallet,
removeUserWallet,
setRecentUsedPaymentMethod,
getRecentusedPaymentMethod,
updateUserWalletApplication
};
32 changes: 28 additions & 4 deletions src/features/payments/routers/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { FaultCategoryEnum } from "../../../../generated/definitions/pagopa/ecom
import { NewTransactionRequest } from "../../../../generated/definitions/pagopa/ecommerce/NewTransactionRequest";
import { RequestAuthorizationRequest } from "../../../../generated/definitions/pagopa/ecommerce/RequestAuthorizationRequest";
import { RequestAuthorizationResponse } from "../../../../generated/definitions/pagopa/ecommerce/RequestAuthorizationResponse";
import { GuestMethodLastUsageTypeEnum } from "../../../../generated/definitions/pagopa/ecommerce/GuestMethodLastUsageType";
import { WalletLastUsageTypeEnum } from "../../../../generated/definitions/pagopa/ecommerce/WalletLastUsageType";
import { WalletDetailTypeEnum } from "../../../../generated/definitions/pagopa/ecommerce/WalletDetailType";
import { RptId } from "../../../../generated/definitions/pagopa/ecommerce/RptId";
import { ioDevServerConfig } from "../../../config";
import { serverUrl } from "../../../utils/server";
Expand Down Expand Up @@ -163,16 +166,31 @@ addPaymentHandler(
}),
O.fold(
() => res.sendStatus(403),
({ transactionId }) =>
({ transactionId, requestAuthorization }) =>
pipe(
getTransactionInfoPayload(transactionId),
O.fold(
() => res.sendStatus(404),
() =>
res.status(200).json({
() => {
const usedPaymentMethodType =
requestAuthorization.details.detailType ===
WalletDetailTypeEnum.wallet
? WalletLastUsageTypeEnum.wallet
: GuestMethodLastUsageTypeEnum.guest;
const usedPaymentMethodId =
requestAuthorization.details.detailType ===
WalletDetailTypeEnum.wallet
? requestAuthorization.details.walletId
: requestAuthorization.details.paymentMethodId;
WalletDB.setRecentUsedPaymentMethod(
usedPaymentMethodId,
usedPaymentMethodType
);
return res.status(200).json({
authorizationUrl: `${serverUrl}${WALLET_PAYMENT_PATH}?transactionId=${transactionId}`,
authorizationRequestId: ulid()
} as RequestAuthorizationResponse)
} as RequestAuthorizationResponse);
}
)
)
)
Expand Down Expand Up @@ -207,6 +225,12 @@ addPaymentHandler("get", "/wallets", (req, res) => {
});
});

addPaymentHandler("get", "/user/lastPaymentMethodUsed", (req, res) => {
res.json({
...WalletDB.getRecentusedPaymentMethod()
});
});

/**
* This API is used to retrieve a list of payment methods available
*/
Expand Down

0 comments on commit f8549ca

Please sign in to comment.