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

feat: [IOBP-283] new wallet onboarding apis #310

Merged
merged 9 commits into from
Oct 24, 2023
22 changes: 20 additions & 2 deletions assets/wallet/wallet_onboarding.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script type="application/javascript">
<script type="text/javascript">
const outcomeList = [
["0", "SUCCESS"],
["1", "ERROR"]
];

const simulateOutcome = () => {
const container = document.getElementById("outcomeSelect");
window.location.href = `/outcomeView?outcome=${container.value}`;
if (container.value === "0") {
fetch('/wallets/mock', {
method: 'POST',
body: JSON.stringify({
paymentMethodId: "1",
}),
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json'
})
})
.then(response => response.json())
.then(response => {
window.location.href = `/outcomeView?outcome=${container.value}&walletId=${response.walletId}`;
})
.catch(err => alert(err))
} else {
window.location.href = `/outcomeView?outcome=${container.value}`;
}
}

function onLoad() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"api_pn": "https://raw.githubusercontent.com/pagopa/io-backend/v13.10.1-RELEASE/api_pn.yaml",
"api_idpay": "https://raw.githubusercontent.com/pagopa/cstar-infrastructure/v4.5.0/src/domains/idpay-app/api/idpay_appio_full/openapi.appio.full.yml",
"api_fast_login": "https://raw.githubusercontent.com/pagopa/io-backend/v13.10.1-RELEASE/openapi/generated/api_fast_login.yaml",
"api_pagopa_walletv3": "https://raw.githubusercontent.com/pagopa/pagopa-infra/ca0f61d6c764dcc8a8594c1593d436dcdae58a7c/src/domains/wallet-app/api/wallet-service/v1/_openapi.json.tpl",
"api_pagopa_walletv3": "https://raw.githubusercontent.com/pagopa/pagopa-infra/fde8d344d1eae9d16ca338d4f71095c805b99f75/src/domains/wallet-app/api/payment-wallet/v1/_openapi.json.tpl",
"author": "Matteo Boschi",
"license": "MIT",
"private": false,
Expand Down
46 changes: 46 additions & 0 deletions src/persistence/wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { faker } from "@faker-js/faker";
import { WalletInfo } from "../../generated/definitions/pagopa/walletv3/WalletInfo";
import { ServiceNameEnum } from "../../generated/definitions/pagopa/walletv3/ServiceName";
import { WalletStatusEnum } from "../../generated/definitions/pagopa/walletv3/WalletStatus";

const userWallets = new Map<string, WalletInfo>();

const addUserWallet = (walletId: string, wallet: WalletInfo) => {
userWallets.set(walletId, wallet);
};

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

const getUserWalletInfo = (walletId: string) => userWallets.get(walletId);

const generateUserWallet = (paymentMethodId: string) => {
const walletId = (getUserWallets().length + 1).toString();
const randomWallet: WalletInfo = {
contractId: "2",
creationDate: new Date(),
paymentMethodId,
services: [
{
name: ServiceNameEnum.PAGOPA
}
],
status: WalletStatusEnum.CREATED,
updateDate: new Date(),
userId: faker.datatype.uuid(),
walletId
};
addUserWallet(walletId, randomWallet);
return randomWallet;
};

const removeUserWallet = (walletId: string) => {
userWallets.delete(walletId);
};

export default {
addUserWallet,
getUserWallets,
getUserWalletInfo,
generateUserWallet,
removeUserWallet
};
19 changes: 0 additions & 19 deletions src/routers/features/walletV3/onboarding.ts

This file was deleted.

36 changes: 36 additions & 0 deletions src/routers/features/walletV3/onboarding/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { PaymentMethodStatusEnum } from "../../../../../generated/definitions/pagopa/walletv3/PaymentMethodStatus";
import { PaymentMethodsResponse } from "../../../../../generated/definitions/pagopa/walletv3/PaymentMethodsResponse";
import { Range } from "../../../../../generated/definitions/pagopa/walletv3/Range";

export const allPaymentMethods: PaymentMethodsResponse = {
paymentMethods: [
{
id: "1",
name: "Carta di credito",
description: "Pagamento con carta di credito",
asset: "creditCard",
status: PaymentMethodStatusEnum.ENABLED,
paymentTypeCode: "string",
ranges: [
{
min: 0,
max: 1000
} as Range
]
},
{
id: "2",
name: "BANCOMAT Pay",
description: "Pagamento con BANCOMAT Pay",
asset: "bancomatPay",
status: PaymentMethodStatusEnum.ENABLED,
paymentTypeCode: "string",
ranges: [
{
min: 0,
max: 500
} as Range
]
}
]
};
67 changes: 67 additions & 0 deletions src/routers/features/walletV3/onboarding/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { pipe } from "fp-ts/lib/function";
import * as E from "fp-ts/lib/Either";
import * as O from "fp-ts/lib/Option";

import WalletDB from "../../../../persistence/wallet";
import { WalletCreateRequest } from "../../../../../generated/definitions/pagopa/walletv3/WalletCreateRequest";
import { generateOnboardingWalletData } from "../../../../utils/wallet";
import { addPaymentMethodsHandler, addWalletV3Handler } from "../router";
import { generateOnboardablePaymentMethods } from "./utils";

/**
* This API is used to start an onboarding process for a new method of payment
*/
addWalletV3Handler("post", "/", (req, res) => {
pipe(
WalletCreateRequest.decode(req.body),
E.fold(
() => res.sendStatus(404),
() => res.status(201).json(generateOnboardingWalletData())
)
);
});

addWalletV3Handler("get", "/", (req, res) => {
res.json(WalletDB.getUserWallets());
});

addWalletV3Handler("get", "/:idWallet", (req, res) => {
const { idWallet } = req.params;
const result = WalletDB.getUserWalletInfo(idWallet);
pipe(
result,
O.fromNullable,
O.map(() => res.json(WalletDB.getUserWalletInfo(idWallet))),
O.getOrElse(() => res.sendStatus(400))
);
});

addWalletV3Handler("delete", "/:idWallet", (req, res) => {
const { idWallet } = req.params;
const result = WalletDB.getUserWalletInfo(idWallet);
pipe(
result,
O.fromNullable,
O.map(() => {
WalletDB.removeUserWallet(idWallet);
res.sendStatus(204);
}),
O.getOrElseW(() => res.status(400).json({ text: "Wallet id not present" }))
);
});

/**
* This API is used to retrieve a list of payment methods available for the onboarding process
*/
addPaymentMethodsHandler("get", "/", (req, res) => {
res.json(generateOnboardablePaymentMethods());
});

/**
* This API is used to start an onboarding process for a new method of payment
*/
addWalletV3Handler("post", "/mock", (req, res) => {
const { paymentMethodId } = req.body;
const generatedWallet = WalletDB.generateUserWallet(paymentMethodId);
res.json(generatedWallet);
});
5 changes: 5 additions & 0 deletions src/routers/features/walletV3/onboarding/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PaymentMethodsResponse } from "../../../../../generated/definitions/pagopa/walletv3/PaymentMethodsResponse";
import { allPaymentMethods } from "./data";

export const generateOnboardablePaymentMethods = (): PaymentMethodsResponse =>
allPaymentMethods;
14 changes: 14 additions & 0 deletions src/routers/features/walletV3/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@ import { addHandler, SupportedMethod } from "../../../payloads/response";
export const walletV3Router = Router();

export const addWalletV3Prefix = (path: string) => `/wallets${path}`;
export const addPaymentMethodsPrefix = (path: string) =>
`/payment-methods${path}`;

export const addWalletV3Handler = (
method: SupportedMethod,
path: string,
handleRequest: (request: Request, response: Response) => void
) => addHandler(walletV3Router, method, addWalletV3Prefix(path), handleRequest);

export const addPaymentMethodsHandler = (
method: SupportedMethod,
path: string,
handleRequest: (request: Request, response: Response) => void
) =>
addHandler(
walletV3Router,
method,
addPaymentMethodsPrefix(path),
handleRequest
);
1 change: 0 additions & 1 deletion src/utils/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ export const appendWalletV3Prefix = (path: string) => `${walletV3Path}${path}`;

export const WALLET_ONBOARDING_PATH = "/onboarding-wallet";
export const generateOnboardingWalletData = (): WalletCreateResponse => ({
walletId: faker.datatype.uuid(),
redirectUrl: `${serverUrl}${WALLET_ONBOARDING_PATH}#sessionToken=${faker.datatype.uuid()}`
});
Loading