Merge branch 'AW-98-parameters-new-payment' into 'develop'

AW-98-parameters-new-payment

See merge request witapp/aura-webapp!162
This commit is contained in:
Daniil Chemerkin 2024-06-03 16:59:41 +00:00
commit cc40042ea4
7 changed files with 54 additions and 20 deletions

View File

@ -7,6 +7,8 @@ interface Payload {
export interface PayloadPost extends Payload { export interface PayloadPost extends Payload {
productId: string; productId: string;
placementId: string;
paywallId: string;
} }
interface ResponsePostSuccess { interface ResponsePostSuccess {
@ -37,10 +39,12 @@ interface ResponsePostError {
export type ResponsePost = ResponsePostSuccess | ResponsePostError; export type ResponsePost = ResponsePostSuccess | ResponsePostError;
export const createRequestPost = ({ token, productId }: PayloadPost): Request => { export const createRequestPost = ({ token, productId, placementId, paywallId }: PayloadPost): Request => {
const url = new URL(routes.server.makePayment()); const url = new URL(routes.server.makePayment());
const body = JSON.stringify({ const body = JSON.stringify({
productId productId,
placementId,
paywallId
}); });
return new Request(url, { method: "POST", headers: getAuthHeaders(token), body }); return new Request(url, { method: "POST", headers: getAuthHeaders(token), body });
}; };

View File

@ -17,7 +17,9 @@ export enum EPlacementKeys {
"aura.placement.palmistry.main" = "aura.placement.palmistry.main" "aura.placement.palmistry.main" = "aura.placement.palmistry.main"
} }
interface ResponseGetSuccess { export interface ResponseGetSuccess {
placementId: string,
paywallId: string,
paywall: IPaywall; paywall: IPaywall;
} }

View File

@ -40,6 +40,8 @@ function PaymentModal({
const activeProductFromStore = useSelector(selectors.selectActiveProduct); const activeProductFromStore = useSelector(selectors.selectActiveProduct);
const _activeProduct = activeProduct ? activeProduct : activeProductFromStore; const _activeProduct = activeProduct ? activeProduct : activeProductFromStore;
const { products, paywallId, placementId } = usePaywall({ placementKey });
const { const {
paymentIntentId, paymentIntentId,
clientSecret, clientSecret,
@ -50,8 +52,9 @@ function PaymentModal({
error, error,
} = useMakePayment({ } = useMakePayment({
productId: _activeProduct?._id || "", productId: _activeProduct?._id || "",
returnPaidUrl: paywallId,
returnUrl placementId,
returnPaidUrl: returnUrl,
}); });
if (checkoutUrl?.length) { if (checkoutUrl?.length) {
@ -66,8 +69,6 @@ function PaymentModal({
EPaymentMethod.PAYMENT_BUTTONS EPaymentMethod.PAYMENT_BUTTONS
); );
const { products } = usePaywall({ placementKey });
const onSelectPaymentMethod = (method: EPaymentMethod) => { const onSelectPaymentMethod = (method: EPaymentMethod) => {
setSelectedPaymentMethod(method); setSelectedPaymentMethod(method);
}; };

View File

@ -38,7 +38,7 @@ function PaymentModal({
const [stripePromise, setStripePromise] = const [stripePromise, setStripePromise] =
useState<Promise<Stripe | null> | null>(null); useState<Promise<Stripe | null> | null>(null);
const { products } = usePaywall({ const { products, placementId, paywallId } = usePaywall({
placementKey, placementKey,
}); });
const activeProductFromStore = useSelector(selectors.selectActiveProduct); const activeProductFromStore = useSelector(selectors.selectActiveProduct);
@ -54,6 +54,8 @@ function PaymentModal({
error, error,
} = useMakePayment({ } = useMakePayment({
productId: _activeProduct?._id || "", productId: _activeProduct?._id || "",
placementId,
paywallId,
returnPaidUrl: returnUrl, returnPaidUrl: returnUrl,
}); });

View File

@ -5,10 +5,17 @@ import { useSelector } from "react-redux";
interface IUseMakePaymentProps { interface IUseMakePaymentProps {
productId: string; productId: string;
placementId: string;
paywallId: string;
returnPaidUrl?: string; returnPaidUrl?: string;
} }
export const useMakePayment = ({ productId, returnPaidUrl = `https://${window.location.host}/payment/result/` }: IUseMakePaymentProps) => { export const useMakePayment = ({
productId,
placementId,
paywallId,
returnPaidUrl = `https://${window.location.host}/payment/result/`
}: IUseMakePaymentProps) => {
const api = useApi(); const api = useApi();
const token = useSelector(selectors.selectToken); const token = useSelector(selectors.selectToken);
const [paymentIntentId, setPaymentIntentId] = useState<string>(); const [paymentIntentId, setPaymentIntentId] = useState<string>();
@ -20,14 +27,16 @@ export const useMakePayment = ({ productId, returnPaidUrl = `https://${window.lo
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const makePayment = useCallback(async () => { const makePayment = useCallback(async () => {
if (!productId?.length) { if (!productId?.length || !placementId?.length || !paywallId?.length) {
return; return;
} }
try { try {
setIsLoading(true); setIsLoading(true);
const res = await api.makePayment({ const res = await api.makePayment({
token, token,
productId productId,
placementId,
paywallId
}); });
if (res.status === "paid") { if (res.status === "paid") {
@ -52,7 +61,7 @@ export const useMakePayment = ({ productId, returnPaidUrl = `https://${window.lo
} finally { } finally {
setIsLoading(false); setIsLoading(false);
} }
}, [api, productId, returnPaidUrl, token]) }, [api, paywallId, placementId, productId, returnPaidUrl, token])
useEffect(() => { useEffect(() => {
makePayment() makePayment()

View File

@ -33,6 +33,8 @@ export function usePaywall({ placementKey }: IUsePaywallProps) {
); );
const isMustUpdate = useSelector(selectors.selectPaywallsIsMustUpdate); const isMustUpdate = useSelector(selectors.selectPaywallsIsMustUpdate);
const paywalls = useSelector(selectors.selectPaywalls); const paywalls = useSelector(selectors.selectPaywalls);
const [placementId, setPlacementId] = useState<string>("");
const [paywallId, setPaywallId] = useState<string>("");
const getPaywallByPlacementKey = useCallback( const getPaywallByPlacementKey = useCallback(
async (placementKey: EPlacementKeys) => { async (placementKey: EPlacementKeys) => {
@ -45,9 +47,11 @@ export function usePaywall({ placementKey }: IUsePaywallProps) {
}); });
if ("paywall" in paywall && paywall.paywall) { if ("paywall" in paywall && paywall.paywall) {
setPaywall(paywall.paywall); setPaywall(paywall.paywall);
setPaywallId(paywall.paywallId || "");
setPlacementId(paywall.placementId || "");
dispatch( dispatch(
actions.paywalls.updatePaywall({ actions.paywalls.updatePaywall({
[placementKey]: paywall.paywall, [placementKey]: paywall,
}) })
); );
dispatch( dispatch(
@ -68,11 +72,12 @@ export function usePaywall({ placementKey }: IUsePaywallProps) {
); );
useEffect(() => { useEffect(() => {
if (isMustUpdate[placementKey] || !paywalls[placementKey]) { if (isMustUpdate[placementKey] || !paywalls[placementKey]?.paywall) {
getPaywallByPlacementKey(placementKey); getPaywallByPlacementKey(placementKey);
} else { } else {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion setPaywall(paywalls[placementKey]?.paywall);
setPaywall(paywalls[placementKey]!); setPaywallId(paywalls[placementKey]?.paywallId || "");
setPlacementId(paywalls[placementKey]?.placementId || "");
} }
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [getPaywallByPlacementKey, placementKey, isMustUpdate]); }, [getPaywallByPlacementKey, placementKey, isMustUpdate]);
@ -117,8 +122,19 @@ export function usePaywall({ placementKey }: IUsePaywallProps) {
error, error,
products, products,
properties, properties,
placementId,
paywallId,
getText, getText,
}), }),
[error, isLoading, paywall, products, properties, getText] [
paywall,
isLoading,
error,
products,
properties,
placementId,
paywallId,
getText,
]
); );
} }

View File

@ -1,4 +1,4 @@
import { EPlacementKeys, IPaywall } from '@/api/resources/Paywall' import { EPlacementKeys, ResponseGetSuccess } from '@/api/resources/Paywall'
import { createSlice, createSelector } from '@reduxjs/toolkit' import { createSlice, createSelector } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit' import type { PayloadAction } from '@reduxjs/toolkit'
@ -9,11 +9,11 @@ type TAdditionalParameters = {
} }
type TPaywalls = { type TPaywalls = {
[key in EPlacementKeys]: IPaywall | null; [key in EPlacementKeys]: ResponseGetSuccess | null;
} & TAdditionalParameters } & TAdditionalParameters
type IPayloadUpdatePaywall = { type IPayloadUpdatePaywall = {
[key in EPlacementKeys]: IPaywall [key in EPlacementKeys]: ResponseGetSuccess
} }
const initialState: TPaywalls = { const initialState: TPaywalls = {