-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: [IOBP-544] Add
usePagoPaPayment
custom hook (#5503)
## Short description This PR introduces the `usePagoPaPayment` custom hook, which centralizes the initialization and start of a pagoPA payment flow. ## List of changes proposed in this pull request - Added `usePagoPaPayment` custom hook - Replaced payment initialization inside playgrounds screen - Small refactorings for types ## How to test Within new wallet Payment playgrounds, you should be able to start a payment flow. --------- Co-authored-by: Martino Cesari Tomba <[email protected]>
- Loading branch information
Showing
14 changed files
with
185 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { | ||
RptId as PagoPaRptId, | ||
RptIdFromString as PagoPaRptIdFromString, | ||
PaymentNoticeNumberFromString | ||
} from "@pagopa/io-pagopa-commons/lib/pagopa"; | ||
import { OrganizationFiscalCode } from "@pagopa/ts-commons/lib/strings"; | ||
import { NavigatorScreenParams, useRoute } from "@react-navigation/native"; | ||
import { sequenceS } from "fp-ts/lib/Apply"; | ||
import * as E from "fp-ts/lib/Either"; | ||
import * as O from "fp-ts/lib/Option"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
import { RptId } from "../../../../../definitions/pagopa/ecommerce/RptId"; | ||
import { | ||
AppParamsList, | ||
useIONavigation | ||
} from "../../../../navigation/params/AppParamsList"; | ||
import { useIODispatch } from "../../../../store/hooks"; | ||
import { WalletPaymentRoutes } from "../navigation/routes"; | ||
import { | ||
PaymentInitStateParams, | ||
walletPaymentInitState | ||
} from "../store/actions/orchestration"; | ||
import { PaymentStartRoute } from "../types"; | ||
|
||
type PagoPaPaymentParams = Omit<PaymentInitStateParams, "startRoute">; | ||
|
||
/** | ||
* A hook for initiating a PagoPA payment flow. | ||
* This hook provides functions to start a payment flow using various input methods. | ||
* @returns An object containing functions to start different types of payment flows. | ||
*/ | ||
const usePagoPaPayment = () => { | ||
const route = useRoute(); | ||
const dispatch = useIODispatch(); | ||
const navigation = useIONavigation(); | ||
|
||
/** | ||
* Initializes the payment state based on the provided parameters. | ||
* The initialization includes the store of the current route which allows the app to | ||
* return to it when the payment flow is finished. | ||
* @param {PagoPaPaymentParams} params - Parameters for initializing the payment state. | ||
*/ | ||
const initPaymentState = ({ startOrigin }: PagoPaPaymentParams) => { | ||
const startRoute: PaymentStartRoute = { | ||
routeName: route.name as keyof AppParamsList, | ||
routeKey: | ||
route.key as keyof NavigatorScreenParams<AppParamsList>["screen"] | ||
}; | ||
dispatch( | ||
walletPaymentInitState({ | ||
startRoute, | ||
startOrigin | ||
}) | ||
); | ||
}; | ||
|
||
/** | ||
* Initiates the payment flow using the provided RptId string and additional parameters. | ||
* @param {RptId} rptId - The RptId for the payment flow. | ||
* @param {PagoPaPaymentParams} params - Additional parameters for the payment flow. | ||
*/ | ||
const startPaymentFlow = (rptId: RptId, params: PagoPaPaymentParams = {}) => { | ||
initPaymentState(params); | ||
navigation.navigate(WalletPaymentRoutes.WALLET_PAYMENT_MAIN, { | ||
screen: WalletPaymentRoutes.WALLET_PAYMENT_DETAIL, | ||
params: { | ||
rptId | ||
} | ||
}); | ||
}; | ||
|
||
/** | ||
* Initiates the payment flow using the provided PagoPA RptId and additional parameters. | ||
* @param {PagoPaRptId} rptId - The PagoPA RptId for the payment flow. | ||
* @param {PagoPaPaymentParams} params - Additional parameters for the payment flow. | ||
*/ | ||
const startPaymentFlowWithRptId = ( | ||
rptId: PagoPaRptId, | ||
params: PagoPaPaymentParams = {} | ||
) => { | ||
pipe( | ||
O.fromNullable(rptId), | ||
O.map(PagoPaRptIdFromString.encode), | ||
O.map(RptId.decode), | ||
O.chain(O.fromEither), | ||
O.map(rptIdString => startPaymentFlow(rptIdString, params)) | ||
); | ||
}; | ||
|
||
/** | ||
* Initiates the payment flow using the provided payment data and additional parameters. | ||
* @param {Object} data - Payment data containing the payment notice number and an organization fiscal code. | ||
* @param {PagoPaPaymentParams} params - Additional parameters for the payment flow. | ||
*/ | ||
const startPaymentFlowWithData = ( | ||
data: { | ||
paymentNoticeNumber: string; | ||
organizationFiscalCode: string; | ||
}, | ||
params: PagoPaPaymentParams = {} | ||
) => { | ||
pipe( | ||
sequenceS(E.Monad)({ | ||
paymentNoticeNumber: PaymentNoticeNumberFromString.decode( | ||
data.paymentNoticeNumber | ||
), | ||
organizationFiscalCode: OrganizationFiscalCode.decode( | ||
data.organizationFiscalCode | ||
) | ||
}), | ||
E.map(PagoPaRptIdFromString.encode), | ||
E.chain(RptId.decode), | ||
E.map(rptIdString => startPaymentFlow(rptIdString, params)) | ||
); | ||
}; | ||
|
||
return { | ||
startPaymentFlow, | ||
startPaymentFlowWithRptId, | ||
startPaymentFlowWithData | ||
}; | ||
}; | ||
|
||
export { usePagoPaPayment }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export type PaymentStartOrigin = | ||
| "message" | ||
| "qrcode_scan" | ||
| "poste_datamatrix_scan" | ||
| "manual_insertion" | ||
| "donation"; | ||
|
||
export type PaymentHistory = { | ||
startOrigin?: PaymentStartOrigin; | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type WalletPaymentPspSortType = "default" | "name" | "amount"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
export type WalletPaymentPspSortType = "default" | "name" | "amount"; | ||
import { NavigatorScreenParams } from "@react-navigation/native"; | ||
import { AppParamsList } from "../../../../navigation/params/AppParamsList"; | ||
|
||
export type { WalletPaymentPspSortType } from "./WalletPaymentPspSortType"; | ||
export type { PaymentStartOrigin, PaymentHistory } from "./PaymentHistory"; | ||
|
||
export type PaymentStartRoute = { | ||
routeName: keyof AppParamsList; | ||
routeKey: NavigatorScreenParams<AppParamsList>["screen"]; | ||
}; |
Oops, something went wrong.