Skip to content

Commit 35e6f67

Browse files
authored
Merge pull request #1593 from basedosdados/feat/payment-summary
Feat/payment summary
2 parents e5ddebe + ec8df7f commit 35e6f67

2 files changed

Lines changed: 70 additions & 9 deletions

File tree

next/components/organisms/PaymentSystem.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ export default function PaymentSystem({
8686
coupon,
8787
onSucess,
8888
onErro,
89-
isLoading
89+
isLoading,
90+
onClientSecretReady,
9091
}) {
9192
const [clientSecret, setClientSecret] = useState("")
9293

@@ -157,6 +158,7 @@ export default function PaymentSystem({
157158

158159
if (trial?.started) {
159160
setClientSecret(null)
161+
onClientSecretReady?.({ isSetupIntent: false, isTrialStarted: true })
160162
return isLoading(false)
161163
}
162164

@@ -165,13 +167,18 @@ export default function PaymentSystem({
165167

166168
if (clientSecret) {
167169
setClientSecret(clientSecret)
170+
onClientSecretReady?.({
171+
isSetupIntent: clientSecret.startsWith("seti_"),
172+
isTrialStarted: false,
173+
})
168174
return isLoading(false)
169175
}
170176
}
171177

172178
useEffect(() => {
173179
setClientSecret("")
174180
isLoading(true)
181+
onClientSecretReady?.({ isSetupIntent: false, isLoading: true })
175182
if(plan) {
176183
customerCreatPost(plan, coupon)
177184
}

next/components/organisms/componentsUserPage/PlansAndPayment.js

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export default function PlansAndPayment ({ userData }) {
149149
const [checkoutStep, setCheckoutStep] = useState("plan")
150150
const [isChatbotTrialSuccess, setIsChatbotTrialSuccess] = useState(false)
151151
const [isStartingChatbotTrial, setIsStartingChatbotTrial] = useState(false)
152+
const [isSetupIntentCheckout, setIsSetupIntentCheckout] = useState(false)
152153
const successCheckoutKindRef = useRef(null)
153154

154155
const internalSubscriptions = userData?.internalSubscription?.edges?.map((edge) => edge?.node) || []
@@ -346,6 +347,7 @@ export default function PlansAndPayment ({ userData }) {
346347
setCoupon("")
347348
setCouponInfos({})
348349
setPlan("")
350+
setIsSetupIntentCheckout(false)
349351
}
350352

351353
function openCheckoutPlanStep() {
@@ -356,6 +358,7 @@ export default function PlansAndPayment ({ userData }) {
356358
function openCheckoutPaymentStep() {
357359
setCheckoutStep("payment")
358360
setIsLoadingClientSecret(true)
361+
setIsSetupIntentCheckout(false)
359362
PaymentModal.onOpen()
360363
}
361364

@@ -672,14 +675,23 @@ export default function PlansAndPayment ({ userData }) {
672675
)
673676
}
674677

675-
const TotalToPayDisplay = () => {
676-
let value
677-
678-
if(couponInfos?.discountAmount) {
679-
value = (checkoutInfos?.amount-couponInfos?.discountAmount).toLocaleString('pt-BR', { style: 'currency', currency: 'BRL', minimumFractionDigits: 2 })
680-
} else {
681-
value = checkoutInfos?.amount?.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL', minimumFractionDigits: 2 })
678+
function getCheckoutTotalAmount() {
679+
if (couponInfos?.discountAmount) {
680+
return checkoutInfos?.amount - couponInfos.discountAmount
682681
}
682+
return checkoutInfos?.amount
683+
}
684+
685+
function formatCheckoutAmount(amount) {
686+
return amount?.toLocaleString("pt-BR", {
687+
style: "currency",
688+
currency: "BRL",
689+
minimumFractionDigits: 2,
690+
})
691+
}
692+
693+
const TotalToPayDisplay = () => {
694+
const value = formatCheckoutAmount(getCheckoutTotalAmount())
683695

684696
return (
685697
<>
@@ -693,6 +705,11 @@ export default function PlansAndPayment ({ userData }) {
693705
)
694706
}
695707

708+
const showPaymentSummary =
709+
checkoutStep === "payment" &&
710+
!isLoadingClientSecret &&
711+
(!isSetupIntentCheckout || !isChatbotCheckout)
712+
696713
async function handlerEmailGcp() {
697714
setErrEmailGCP(false)
698715
setIsLoadingEmailChange(true)
@@ -1019,7 +1036,11 @@ export default function PlansAndPayment ({ userData }) {
10191036
</Button>
10201037
<Button
10211038
width={{ base: "100%", lg: "fit-content" }}
1022-
onClick={() => setCheckoutStep("payment")}
1039+
onClick={() => {
1040+
setIsSetupIntentCheckout(false)
1041+
setIsLoadingClientSecret(true)
1042+
setCheckoutStep("payment")
1043+
}}
10231044
>
10241045
{t("username.next")}
10251046
</Button>
@@ -1032,6 +1053,32 @@ export default function PlansAndPayment ({ userData }) {
10321053
spacing="24px"
10331054
pointerEvents={isLoadingClientSecret ? "none" : "default"}
10341055
>
1056+
{showPaymentSummary && (
1057+
<Stack
1058+
spacing="8px"
1059+
padding="16px"
1060+
backgroundColor="#F7F7F7"
1061+
borderRadius="12px"
1062+
>
1063+
<Box
1064+
display="flex"
1065+
justifyContent="space-between"
1066+
alignItems="center"
1067+
gap="16px"
1068+
>
1069+
<LabelText textTransform="capitalize">
1070+
{checkoutInfos?.productName}
1071+
</LabelText>
1072+
<BodyText typography="small" color="#71757A">
1073+
{formattedPlanInterval(checkoutInfos?.interval)}
1074+
</BodyText>
1075+
</Box>
1076+
<TitleText typography="small">
1077+
{formatCheckoutAmount(getCheckoutTotalAmount())}/
1078+
{formattedPlanInterval(checkoutInfos?.interval, true)}
1079+
</TitleText>
1080+
</Stack>
1081+
)}
10351082
<LabelText>{t("username.paymentDetails")}</LabelText>
10361083
<PaymentSystem
10371084
userData={userData}
@@ -1040,6 +1087,13 @@ export default function PlansAndPayment ({ userData }) {
10401087
onSucess={(isTrial) => openModalSucess(isTrial)}
10411088
onErro={() => openModalErro()}
10421089
isLoading={(e) => setIsLoadingClientSecret(e)}
1090+
onClientSecretReady={({ isSetupIntent, isLoading: loadingSecret }) => {
1091+
if (loadingSecret) {
1092+
setIsSetupIntentCheckout(false)
1093+
return
1094+
}
1095+
setIsSetupIntentCheckout(Boolean(isSetupIntent))
1096+
}}
10431097
/>
10441098
</Stack>
10451099
)}

0 commit comments

Comments
 (0)