Skip to content

Commit

Permalink
Merge pull request #730 from basedosdados/feat/stripe
Browse files Browse the repository at this point in the history
Feat/stripe
  • Loading branch information
AldemirLucas authored Dec 4, 2023
2 parents 11c70db + f9cd711 commit 9eca690
Show file tree
Hide file tree
Showing 20 changed files with 6,572 additions and 403 deletions.
56 changes: 28 additions & 28 deletions next/components/molecules/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { isMobileMod, useCheckMobile } from "../../hooks/useCheckMobile.hook"
import ControlledInput from "../atoms/ControlledInput";
import Link from "../atoms/Link";
import RoundedButton from "../atoms/RoundedButton";
import { getUserDataJson } from "../../utils";

import BDLogoProImage from "../../public/img/logos/bd_logo_pro";
import BDLogoEduImage from "../../public/img/logos/bd_logo_edu";
Expand All @@ -41,7 +42,7 @@ import SettingsIcon from "../../public/img/icons/settingsIcon";
import SignOutIcon from "../../public/img/icons/signOutIcon";

function MenuDrawer({ isOpen, onClose, links }) {
let userData = cookies.get("user") || null
let userData = getUserDataJson()

return (
<Drawer isOpen={isOpen} placement="left" onClose={onClose}>
Expand Down Expand Up @@ -161,8 +162,7 @@ function MenuDrawer({ isOpen, onClose, links }) {

function MenuDrawerUser({ isOpen, onClose}) {
const router = useRouter()
let userData = cookies.get("user") || null
if(userData !== null) userData = JSON.parse(cookies.get("user"))
let userData = getUserDataJson()

const links = [
{name: "Perfil público", value: "profile"},
Expand Down Expand Up @@ -196,7 +196,7 @@ function MenuDrawerUser({ isOpen, onClose}) {
alt=""
width="100%"
height="100%"
src={userData?.picture ? userData?.picture : "https://basedosdados-static.s3.us-east-2.amazonaws.com/equipe/sem_foto.png"}
src={userData?.picture ? userData?.picture : "https://storage.googleapis.com/basedosdados-website/equipe/sem_foto.png"}
/>
</Box>
<Text
Expand Down Expand Up @@ -274,7 +274,7 @@ function MenuDrawerUser({ isOpen, onClose}) {
padding="16px 0"
alignItems="center"
onClick={() => {
cookies.remove('user', { path: '/' })
cookies.remove('userBD', { path: '/' })
cookies.remove('token', { path: '/' })
window.open("/", "_self")
}}
Expand Down Expand Up @@ -334,7 +334,7 @@ function MenuUser ({ userData, onOpen, onClose }) {
alt=""
width="100%"
height="100%"
src={userData?.picture ? userData.picture : "https://basedosdados-static.s3.us-east-2.amazonaws.com/equipe/sem_foto.png"}
src={userData?.picture ? userData.picture : "https://storage.googleapis.com/basedosdados-website/equipe/sem_foto.png"}
/>
</Box>
)
Expand All @@ -359,7 +359,7 @@ function MenuUser ({ userData, onOpen, onClose }) {
alt=""
width="100%"
height="100%"
src={userData?.picture ? userData.picture : "https://basedosdados-static.s3.us-east-2.amazonaws.com/equipe/sem_foto.png"}
src={userData?.picture ? userData.picture : "https://storage.googleapis.com/basedosdados-website/equipe/sem_foto.png"}
/>
</Box>
</MenuButton>
Expand Down Expand Up @@ -393,7 +393,7 @@ function MenuUser ({ userData, onOpen, onClose }) {
width="100%"
height="100%"
display={{ base: "none", lg: "flex" }}
src={userData?.picture ? userData.picture : "https://basedosdados-static.s3.us-east-2.amazonaws.com/equipe/sem_foto.png"}
src={userData?.picture ? userData.picture : "https://storage.googleapis.com/basedosdados-website/equipe/sem_foto.png"}
/>
</Box>
<Text
Expand All @@ -404,7 +404,7 @@ function MenuUser ({ userData, onOpen, onClose }) {
lineHeight="16px"
letterSpacing="0.3px"
>
{userData?.username ? userData?.username : "dadinho"}
{userData?.username ? userData?.username : ""}
</Text>
<Text
color="#6F6F6F"
Expand All @@ -414,7 +414,7 @@ function MenuUser ({ userData, onOpen, onClose }) {
lineHeight="16px"
letterSpacing="0.3px"
>
{userData?.email ? userData?.email : "[email protected]"}
{userData?.email ? userData?.email : ""}
</Text>
</MenuItem>

Expand Down Expand Up @@ -448,7 +448,7 @@ function MenuUser ({ userData, onOpen, onClose }) {
padding="16px"
_hover={{ backgroundColor: "transparent", opacity: "0.6" }}
onClick={() => {
cookies.remove('user', { path: '/' })
cookies.remove('userBD', { path: '/' })
cookies.remove('token', { path: '/' })
window.open("/", "_self")}
}
Expand Down Expand Up @@ -642,8 +642,7 @@ function SearchInputUser () {
function DesktopLinks({ links, position = false, path, userTemplate = false }) {
const [statusSearch, setStatusSearch] = useState(false)

let userData = cookies.get("user") || null
if(userData !== null) userData = JSON.parse(cookies.get("user"))
let userData = getUserDataJson()

const searchStatus = (elm) => {
setStatusSearch(elm.status)
Expand Down Expand Up @@ -765,19 +764,21 @@ function DesktopLinks({ links, position = false, path, userTemplate = false }) {
<HStack spacing={8} display={{ base: "none", lg: "flex" }}>
{userData ? (
<HStack spacing="20px">
<RoundedButton
display={isMobileMod() ? "none" : "flex"}
backgroundColor="#FFF"
border="2px solid #42B0FF"
color="#42B0FF"
height="40px"
fontWeight="700"
borderRadius="30px"
fontSize="15px"
onClick={() => window.open("/precos", "_self")}
>
BD Pro
</RoundedButton>
{userData?.currentSubscriptionStatus[0] !== "active" &&
<RoundedButton
display={isMobileMod() ? "none" : "flex"}
backgroundColor="#FFF"
border="2px solid #42B0FF"
color="#42B0FF"
height="40px"
fontWeight="700"
borderRadius="30px"
fontSize="15px"
onClick={() => window.open(`/user/${userData.username}?plans_and_payment`, "_self")}
>
BD Pro
</RoundedButton>
}
<MenuUser userData={userData}/>
</HStack>
) : (
Expand Down Expand Up @@ -809,8 +810,7 @@ export default function MenuNav({ simpleTemplate = false, userTemplate = false }
const [menuMobileMargin, setMenuMobileMargin] = useState(0)
const [isMobile, setIsMobile] = useState(false)

let userData = cookies.get("user") || null
if(userData !== null) userData = JSON.parse(cookies.get("user"))
let userData = getUserDataJson()

const links = {
Dados: "/dataset",
Expand Down
5 changes: 2 additions & 3 deletions next/components/organisms/DatasetResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
} from "@chakra-ui/react";
import { useEffect, useState } from "react";
import { useRouter } from "next/router";
import cookies from "js-cookie";
import { isMobileMod } from "../../hooks/useCheckMobile.hook";
import { getUserDataJson } from "../../utils";

import SimpleButton from "../atoms/SimpleButton";
import { FilterAccordion } from "../atoms/FilterAccordion";
Expand All @@ -17,8 +17,7 @@ import InformationRequestPage from "./InformationRequestPage";
import CrossIcon from "../../public/img/icons/crossIcon";

function AdminButtons({ datasetId }) {
let userData = cookies.get("user") || null
if(userData !== null) userData = JSON.parse(cookies.get("user"))
let userData = getUserDataJson()

if(!userData?.isAdmin) return null

Expand Down
103 changes: 103 additions & 0 deletions next/components/organisms/paymentSystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import {
VStack,
Text,
} from "@chakra-ui/react"
import { useState, useEffect } from 'react';
import { loadStripe } from '@stripe/stripe-js';
import {
Elements,
PaymentElement,
AddressElement,
useElements,
useStripe,
} from "@stripe/react-stripe-js";
import Button from "../atoms/RoundedButton";

import {
getPrices,
createCustomer,
createSubscription
} from "../../pages/api/stripe";

const stripePromise = loadStripe(`${process.env.NEXT_PUBLIC_KEY_STRIPE}`)

const PaymentForm = () => {
const stripe = useStripe()
const elements = useElements()
const [messages, setMessages] = useState("")

const handlerSubmit = async (e) => {
e.preventDefault()

setMessages(`${messages}<br />Submitting payment...`)

const data = await stripe.confirmPayment({
elements,
// redirect: 'if_required',
confirmParams: {
return_url: "/"
}
})
}

return (
<VStack
spacing={0}
alignItems="start"
>
<form onSubmit={handlerSubmit}>
<PaymentElement />

<Button width="100%" type="submit" marginTop="20px !important">Iniciar inscrição</Button>
</form>

{messages && <Text marginTop="20px !important">{messages}</Text>}
</VStack>
)
}

export default function PaymentSystem() {
const [clientSecret, setClientSecret] = useState("")

const appearance = {
theme: "stripe",
}

const options = {
clientSecret,
appearance,
}

const customerCreatPost = async () => {
let secret = ""

const subscriptionCreate = await createSubscription("19")
if(subscriptionCreate?.clientSecret) {
secret = subscriptionCreate?.clientSecret
}
if(secret !== "") return setClientSecret(secret)

const result = await createCustomer()
if(result?.id) {
const subscriptionCreate = await createSubscription("19")
secret = subscriptionCreate?.clientSecret
}

return setClientSecret(secret)
}

useEffect(() => {
customerCreatPost()
}, [])

return (
<>
{clientSecret && (
<Elements options={options} stripe={stripePromise}>
<AddressElement options={{mode:'billing'}}/>
<PaymentForm />
</Elements>
)}
</>
)
}
2 changes: 1 addition & 1 deletion next/middlewares/authUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default async function authUser(context, destiny) {
// const invalidToken = true

if (invalidToken) {
cookies.remove('user', { path: '/' })
cookies.remove('userBD', { path: '/' })
cookies.remove('token', { path: '/' })

res.setHeader('Set-Cookie', [
Expand Down
Loading

0 comments on commit 9eca690

Please sign in to comment.