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

Handle successful local Crypto Payment #1976

Merged
merged 23 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
66b68df
close dialog once payment tx has been mined
FSM1 Feb 24, 2022
c7a1bad
Merge branch 'dev' into feat/crypto-payment-after-transaction-is-conf…
FSM1 Feb 25, 2022
72896e5
remove console logs
FSM1 Feb 25, 2022
757ff9e
Merge branch 'feat/crypto-payment-after-transaction-is-confirmed-1974…
FSM1 Feb 25, 2022
3cfc57d
add success screen
FSM1 Feb 28, 2022
995cfd6
add success modal
FSM1 Mar 1, 2022
3b74273
Merge branch 'dev' into feat/crypto-payment-after-transaction-is-conf…
Tbaut Mar 1, 2022
2d37517
lingui extract
actions-user Mar 1, 2022
e8916e7
Apply suggestions from code review
FSM1 Mar 1, 2022
3a7b703
lingui extract
actions-user Mar 1, 2022
c8725b4
missing brace
FSM1 Mar 1, 2022
cd4e390
whitespace
FSM1 Mar 1, 2022
969c08c
Merge branch 'dev' into feat/crypto-payment-after-transaction-is-conf…
tanmoyAtb Mar 1, 2022
37ff052
make ConfirmPlan methods nullable
FSM1 Mar 1, 2022
f49401b
Merge branch 'feat/crypto-payment-after-transaction-is-confirmed-1974…
FSM1 Mar 1, 2022
d7f8866
Merge branch 'dev' into feat/crypto-payment-after-transaction-is-conf…
FSM1 Mar 2, 2022
2c8fe92
update success text for crypto
FSM1 Mar 2, 2022
4ac42ce
Merge branch 'feat/crypto-payment-after-transaction-is-confirmed-1974…
FSM1 Mar 2, 2022
7a67bfd
lingui extract
actions-user Mar 2, 2022
8b11deb
disable payment confirmation if no card is available
FSM1 Mar 2, 2022
4f2ddec
Merge branch 'feat/crypto-payment-after-transaction-is-confirmed-1974…
FSM1 Mar 2, 2022
59ba708
Merge branch 'dev' into feat/crypto-payment-after-transaction-is-conf…
FSM1 Mar 3, 2022
42462e9
Merge branch 'dev' into feat/crypto-payment-after-transaction-is-conf…
FSM1 Mar 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import PaymentMethodSelector from "./PaymentMethodSelector"
import ConfirmPlan from "../Common/ConfirmPlan"
import { useBilling } from "../../../../../Contexts/BillingContext"
import { Product, ProductPrice, ProductPriceRecurringInterval } from "@chainsafe/files-api-client"
import PlanSuccess from "./PlanSuccess"
import PlanSuccess from "../Common/PlanSuccess"
import DowngradeDetails from "./DowngradeDetails"
import { PaymentMethod } from "../../../../../Contexts/BillingContext"
import CryptoPayment from "../Common/CryptoPayment"
Expand Down Expand Up @@ -156,7 +156,7 @@ const ChangeProductModal = ({ onClose }: IChangeProductModal) => {
plans={plans}
/>
)}
{ slide === "downgradeDetails" && selectedPlan && (
{slide === "downgradeDetails" && selectedPlan && (
<DowngradeDetails
goBack={() => {setSlide("select")}}
goToPlanDetails={() => setSlide("planDetails")}
Expand Down Expand Up @@ -209,12 +209,14 @@ const ChangeProductModal = ({ onClose }: IChangeProductModal) => {
/>}
{slide === "cryptoPayment" && <CryptoPayment
planPrice={selectedPrice}
onSuccess={() => setSlide("planSuccess")}
onClose={onClose}
/>}
{slide === "planSuccess" && selectedPlan && selectedPrice && <PlanSuccess
{slide === "planSuccess" && selectedPlan && selectedPrice && selectedPaymentMethod && <PlanSuccess
onClose={onClose}
plan={selectedPlan}
planPrice={selectedPrice}
paymentMethod={selectedPaymentMethod}
/>}
</Modal>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ const useStyles = makeStyles(({ constants, palette }: CSFTheme) =>
interface IConfirmPlan {
plan: Product
planPrice: ProductPrice
goToSelectPlan: () => void
goToPaymentMethod: () => void
goToSelectPlan?: () => void
goToPaymentMethod?: () => void
onChangeSubscription: () => void
loadingChangeSubscription: boolean
paymentMethod: PaymentMethod
Expand Down Expand Up @@ -176,6 +176,7 @@ const ConfirmPlan = ({
component="p"
className={classes.textButton}
onClick={goToSelectPlan}
disabled={!goToSelectPlan}
data-cy="link-edit-plan"
>
<Trans>Edit plan</Trans>
Expand Down Expand Up @@ -218,6 +219,7 @@ const ConfirmPlan = ({
component="p"
className={classes.textButton}
onClick={goToPaymentMethod}
disabled={!goToPaymentMethod}
data-cy="link-edit-payment-method"
>
<Trans>Edit payment method</Trans>
Expand Down Expand Up @@ -452,7 +454,7 @@ const ConfirmPlan = ({
<Button
variant="primary"
loading={loadingChangeSubscription || !checkSubscriptionUpdate}
disabled={loadingChangeSubscription || !checkSubscriptionUpdate}
disabled={loadingChangeSubscription || !checkSubscriptionUpdate || (paymentMethod === "creditCard" && !defaultCard)}
onClick={onChangeSubscription}
testId="confirm-plan-change"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ const useStyles = makeStyles(({ constants, palette, zIndex, animation, breakpoin
interface ICryptoPayment {
planPrice?: ProductPrice
onClose: () => void
onSuccess: () => void
}

const iconMap: { [key: string]: React.FC<any> } = {
Expand All @@ -242,7 +243,7 @@ const symbolMap: { [key: string]: string } = {
usdc: "USDC"
}

const CryptoPayment = ({ planPrice, onClose }: ICryptoPayment) => {
const CryptoPayment = ({ planPrice, onClose, onSuccess }: ICryptoPayment) => {
const classes = useStyles()
const { selectWallet } = useFilesApi()
const { isReady, network, provider, wallet, tokens, switchNetwork, checkIsReady, ethBalance } = useWeb3()
Expand Down Expand Up @@ -358,27 +359,32 @@ const CryptoPayment = ({ planPrice, onClose }: ICryptoPayment) => {
if (!provider || !selectedCurrency || !selectedPaymentMethod) return

const signer = provider.getSigner()
let transferSuccess = false
try {
setTransferActive(true)
if (selectedCurrency === "ethereum") {
await (await signer.sendTransaction({
to: selectedPaymentMethod.address,
value: utils.parseEther(selectedPaymentMethod.amount)
})).wait(1)
})).wait(5)
} else {
const token = Object.values(tokens).find(t => t.symbol?.toLowerCase() === selectedCurrency)
if (!token || !token.transfer) return
await (await token.transfer(
selectedPaymentMethod.address,
utils.parseUnits(selectedPaymentMethod.amount, token.decimals)
)).wait(1)
)).wait(5)
}
await fetchCurrentSubscription()
transferSuccess = true
onSuccess()
} catch (error) {
console.error(error)
} finally {
setTransferActive(false)
transferSuccess && onClose()
}
}, [provider, selectedCurrency, selectedPaymentMethod, tokens])
}, [fetchCurrentSubscription, onClose, onSuccess, provider, selectedCurrency, selectedPaymentMethod, tokens])

const handleSwitchNetwork = useCallback(async () => {
await switchNetwork(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Button, CheckCircleIcon, CheckIcon, Divider, formatBytes, Link, Typogr
import { Trans } from "@lingui/macro"
import { ROUTE_LINKS } from "../../../../FilesRoutes"
import clsx from "clsx"
import { PaymentMethod } from "../../../../../Contexts/BillingContext"

const useStyles = makeStyles(({ constants, palette }: CSFTheme) =>
createStyles({
Expand Down Expand Up @@ -84,10 +85,11 @@ const useStyles = makeStyles(({ constants, palette }: CSFTheme) =>
interface IPlanSuccess {
plan: Product
planPrice: ProductPrice
paymentMethod: PaymentMethod
onClose: () => void
}

const PlanSuccess = ({ plan, onClose, planPrice }: IPlanSuccess) => {
const PlanSuccess = ({ plan, onClose, planPrice, paymentMethod }: IPlanSuccess) => {
const classes = useStyles()
const newPlanCapacity = formatBytes(Number(planPrice?.metadata?.storage_size_bytes), 2)

Expand Down Expand Up @@ -164,6 +166,16 @@ const PlanSuccess = ({ plan, onClose, planPrice }: IPlanSuccess) => {
</Link>
</Typography>
</div>
{paymentMethod === "crypto" &&
<div className={classes.rowBox}>
<Typography>
<Trans>
Crypto payments may take a few minutes to be processed.
The subscription update will reflect on next login.
</Trans>
</Typography>
</div>
}
<section className={classes.bottomSection}>
<div className={classes.buttons}>
<Button
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useMemo, useState } from "react"
import React, { useCallback, useEffect, useMemo, useState } from "react"
import { makeStyles, createStyles, useThemeSwitcher } from "@chainsafe/common-theme"
import { CSFTheme } from "../../../../../Themes/types"
import { Modal } from "@chainsafe/common-components"
Expand All @@ -7,6 +7,7 @@ import { useBilling } from "../../../../../Contexts/BillingContext"
import { useFilesApi } from "../../../../../Contexts/FilesApiContext"
import ConfirmPlan from "../Common/ConfirmPlan"
import { formatSubscriptionError } from "../utils/formatSubscriptionError"
import PlanSuccess from "../Common/PlanSuccess"

const useStyles = makeStyles(({ constants, breakpoints }: CSFTheme) =>
createStyles({
Expand All @@ -27,6 +28,8 @@ const useStyles = makeStyles(({ constants, breakpoints }: CSFTheme) =>
})
)

type PayInvoiceModalSlides = "confirmPlan" | "planSuccess" | "cryptoPayment"

interface IChangeProductModal {
invoiceId: string
onClose: () => void
Expand All @@ -40,14 +43,19 @@ const PayInvoiceModal = ({ onClose, invoiceId }: IChangeProductModal) => {
const invoiceToPay = useMemo(() => invoices?.find(i => i.uuid === invoiceId), [invoices, invoiceId])
const [payingInvoice, setPayingInvoice] = useState(false)
const [errorMessage, setErrorMessage] = useState<string | undefined>()
const [slide, setSlide] = useState<PayInvoiceModalSlides | undefined>()

const payInvoice = useCallback(() => {
if (!invoiceToPay) return

try {
setPayingInvoice(true)
setErrorMessage(undefined)
filesApiClient.payInvoice(invoiceToPay.uuid).then(refreshInvoices)
filesApiClient.payInvoice(invoiceToPay.uuid)
.then(() => {
setSlide("planSuccess")
refreshInvoices()
})
} catch (error: any) {
const errorMessage = formatSubscriptionError(error)
setErrorMessage(errorMessage)
Expand All @@ -56,6 +64,15 @@ const PayInvoiceModal = ({ onClose, invoiceId }: IChangeProductModal) => {
}
}, [filesApiClient, invoiceToPay, refreshInvoices])

useEffect(() => {
if (!slide) {
setSlide(invoiceToPay?.payment_method === "crypto"
? "cryptoPayment"
: "confirmPlan"
FSM1 marked this conversation as resolved.
Show resolved Hide resolved
)
}
}, [invoiceToPay, slide])

if (!invoiceToPay) return null

return (
Expand All @@ -71,18 +88,23 @@ const PayInvoiceModal = ({ onClose, invoiceId }: IChangeProductModal) => {
testId="cryptoPayment"
onClose={onClose}
>
{invoiceToPay?.payment_method === "crypto"
? <CryptoPayment onClose={onClose}/>
: <ConfirmPlan
plan={{ ...invoiceToPay.product, prices: [invoiceToPay?.product.price] }}
planPrice={invoiceToPay?.product.price}
goToSelectPlan={() => undefined }
goToPaymentMethod={() => undefined }
onChangeSubscription={payInvoice}
loadingChangeSubscription={payingInvoice}
subscriptionErrorMessage={errorMessage}
paymentMethod={invoiceToPay.payment_method === "stripe" ? "creditCard" : "crypto"}
/>
{slide === "cryptoPayment" && <CryptoPayment
onClose={onClose}
onSuccess={() => setSlide("planSuccess")}
/>}
{slide === "confirmPlan" && <ConfirmPlan
plan={{ ...invoiceToPay.product, prices: [invoiceToPay.product.price] }}
planPrice={invoiceToPay.product.price}
onChangeSubscription={payInvoice}
loadingChangeSubscription={payingInvoice}
subscriptionErrorMessage={errorMessage}
paymentMethod={invoiceToPay.payment_method === "stripe" ? "creditCard" : "crypto"}
/>}
{slide === "planSuccess" && <PlanSuccess
plan={{ ...invoiceToPay.product, prices: [invoiceToPay.product.price] }}
planPrice={invoiceToPay.product.price}
onClose={onClose}
paymentMethod={invoiceToPay.payment_method === "stripe" ? "creditCard" : "crypto"}/>
}
</Modal>
)
Expand Down
3 changes: 3 additions & 0 deletions packages/files-ui/src/locales/de/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ msgstr ""
msgid "Credit Card is expiring soon"
msgstr ""

msgid "Crypto payments may take a few minutes to be processed. The subscription update will reflect on next login."
msgstr ""

msgid "DAI, USDC, ETH or BTC"
msgstr ""

Expand Down
3 changes: 3 additions & 0 deletions packages/files-ui/src/locales/en/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ msgstr "Create your public username in <0>Settings</0>!"
msgid "Credit Card is expiring soon"
msgstr "Credit Card is expiring soon"

msgid "Crypto payments may take a few minutes to be processed. The subscription update will reflect on next login."
msgstr "Crypto payments may take a few minutes to be processed. The subscription update will reflect on next login."

msgid "DAI, USDC, ETH or BTC"
msgstr "DAI, USDC, ETH or BTC"

Expand Down
3 changes: 3 additions & 0 deletions packages/files-ui/src/locales/es/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ msgstr ""
msgid "Credit Card is expiring soon"
msgstr ""

msgid "Crypto payments may take a few minutes to be processed. The subscription update will reflect on next login."
msgstr ""

msgid "DAI, USDC, ETH or BTC"
msgstr ""

Expand Down
3 changes: 3 additions & 0 deletions packages/files-ui/src/locales/fr/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ msgstr "Créez votre nom d'utilisateur public dans <0>Paramètres</0> !"
msgid "Credit Card is expiring soon"
msgstr "La carte de crédit va bientôt expirer"

msgid "Crypto payments may take a few minutes to be processed. The subscription update will reflect on next login."
msgstr ""

msgid "DAI, USDC, ETH or BTC"
msgstr "DAI, USDC, ETH ou BTC"

Expand Down
3 changes: 3 additions & 0 deletions packages/files-ui/src/locales/no/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ msgstr ""
msgid "Credit Card is expiring soon"
msgstr ""

msgid "Crypto payments may take a few minutes to be processed. The subscription update will reflect on next login."
msgstr ""

msgid "DAI, USDC, ETH or BTC"
msgstr ""

Expand Down