From 7b5429957c10d38f30857934b8c095ba3494aa3b Mon Sep 17 00:00:00 2001 From: cballevre Date: Tue, 12 Dec 2023 17:14:04 +0100 Subject: [PATCH] feat(Paywall): Add IAP support BREAKING CHANGE: To check if IAP is available, you need to wrap your Paywall into a `WebviewContext` component ``` import { WebviewIntentProvider } from 'cozy-intent' ``` --- react/Paywall/Paywall.jsx | 28 +++++++-- react/Paywall/Paywall.spec.jsx | 66 ++++++++++++++------ react/Paywall/Readme.md | 15 ++++- react/Paywall/locales/en.json | 5 +- react/Paywall/locales/fr.json | 5 +- react/Paywall/locales/withPaywallLocales.jsx | 2 +- 6 files changed, 91 insertions(+), 30 deletions(-) diff --git a/react/Paywall/Paywall.jsx b/react/Paywall/Paywall.jsx index 86288e5b27..8db6cf5cac 100644 --- a/react/Paywall/Paywall.jsx +++ b/react/Paywall/Paywall.jsx @@ -1,4 +1,4 @@ -import React from 'react' +import React, { useEffect, useState } from 'react' import ReactMarkdown from 'react-markdown' import PropTypes from 'prop-types' @@ -6,6 +6,7 @@ import { isFlagshipApp } from 'cozy-device-helper' import { useInstanceInfo } from 'cozy-client' import { buildPremiumLink } from 'cozy-client/dist/models/instance' import flag from 'cozy-flags' +import { useWebviewIntent } from 'cozy-intent' import Spinner from '../Spinner' import { IllustrationDialog } from '../CozyDialogs' @@ -24,6 +25,19 @@ const Paywall = ({ variant, onClose, isPublic, contentInterpolation }) => { const instanceInfo = useInstanceInfo() const { t } = useI18n() + const webviewIntent = useWebviewIntent() + const [isFlagshipAppIapAvailable, setFlagshipAppIapAvailable] = useState(null) + + useEffect(() => { + const fetchIapAvailability = async () => { + const isAvailable = + (await webviewIntent?.call('isAvailable', 'iap')) ?? false + setFlagshipAppIapAvailable(isAvailable) + } + + fetchIapAvailability() + }, [webviewIntent]) + if (!instanceInfo.isLoaded) return ( { ) const canOpenPremiumLink = - !isFlagshipApp() || (isFlagshipApp() && !!flag('flagship.iap.enabled')) + !isFlagshipApp() || + (isFlagshipApp() && + !!flag('flagship.iap.enabled') && + isFlagshipAppIapAvailable) const link = buildPremiumLink(instanceInfo) const type = makeType(instanceInfo, isPublic, link) @@ -67,10 +84,13 @@ const Paywall = ({ variant, onClose, isPublic, contentInterpolation }) => {