Skip to content

Commit

Permalink
Merge pull request #905 from basedosdados/fix/accept-setup-intent-stripe
Browse files Browse the repository at this point in the history
feat: Add support for setup intents in PaymentForm
  • Loading branch information
AldemirLucas authored Aug 12, 2024
2 parents d9d0ccb + 94559a7 commit 4ecf97a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 35 deletions.
File renamed without changes.
3 changes: 2 additions & 1 deletion .github/workflows/release-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ on:
paths:
- ".github/workflows/release-dev.yaml"
- "next/**/*"
workflow_dispatch:

jobs:
release-docker-image-development:
release:
name: Release Docker Image (Development)
runs-on: ubuntu-latest
environment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ on:
branches:
- main
paths:
- ".github/workflows/release.yaml"
- ".github/workflows/release-prod.yaml"
- "next/**/*"
workflow_dispatch:

jobs:
release-docker-image-production:
release:
name: Release Docker Image (Production)
runs-on: ubuntu-latest
environment:
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/release-staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ on:
paths:
- ".github/workflows/release-staging.yaml"
- "next/**/*"
workflow_dispatch:

jobs:
release-docker-image-development:
release:
name: Release Docker Image (Staging)
runs-on: ubuntu-latest
environment:
Expand Down
58 changes: 32 additions & 26 deletions next/components/organisms/PaymentSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,35 @@ import {

const stripePromise = loadStripe(process.env.NEXT_PUBLIC_KEY_STRIPE)

const PaymentForm = ({ onSucess, onErro }) => {
const PaymentForm = ({ onSucess, onErro, clientSecret}) => {
const stripe = useStripe()
const elements = useElements()

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

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

if(data?.error?.code === "card_declined") return onErro()
if(data?.paymentIntent?.status === "succeeded") return onSucess()
const isSetupIntent = clientSecret.startsWith('seti_');
if (isSetupIntent) {
await elements.submit();

const data = await stripe.confirmSetup({
elements,
clientSecret: clientSecret,
redirect: 'if_required',
});

if (data?.error?.code === "card_declined") return onErro();
if (data?.setupIntent?.status === "succeeded") return onSucess();

} else {
const data = await stripe.confirmPayment({
elements,
redirect: 'if_required',
})

if(data?.error?.code === "card_declined") return onErro()
if(data?.paymentIntent?.status === "succeeded") return onSucess()
}
}

return (
Expand Down Expand Up @@ -89,26 +104,12 @@ export default function PaymentSystem({ userData, plan, onSucess, onErro }) {
}

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

const subscriptionCreate = await fetch(`/api/stripe/createSubscription?p=${btoa(id)}`, {method: "GET"})
const clientSecret = await fetch(`/api/stripe/createSubscription?p=${btoa(id)}`, {method: "GET"})
.then(res => res.json())

if(subscriptionCreate?.clientSecret) {
secret = subscriptionCreate?.clientSecret
if (clientSecret) {
setClientSecret(clientSecret)
}
if(secret !== "") return setClientSecret(secret)

const result = await fetch(`/api/stripe/createCustomer`, {method: "GET"})
.then(res => res.json())

if(result?.id) {
const subscriptionCreate = await fetch(`/api/stripe/createSubscription?p=${btoa(id)}`, {method: "GET"})
.then(res => res.json())
secret = subscriptionCreate?.clientSecret
}

return setClientSecret(secret)
}

async function customerCreat(plan) {
Expand Down Expand Up @@ -169,7 +170,12 @@ export default function PaymentSystem({ userData, plan, onSucess, onErro }) {

return (
<Elements options={options} stripe={stripePromise}>
<PaymentForm userData={userData} onSucess={onSucess} onErro={onErro}/>
<PaymentForm
clientSecret={options.clientSecret}
userData={userData}
onSucess={onSucess}
onErro={onErro}
/>
</Elements>
)
}
7 changes: 2 additions & 5 deletions next/pages/api/stripe/createSubscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ async function createSubscription(id, token) {
query: `
mutation {
createStripeSubscription (priceId: ${id}) {
subscription {
id
clientSecret
}
clientSecret
}
}
`
Expand All @@ -38,5 +35,5 @@ export default async function handler(req, res) {
if(result.errors) return res.status(500).json({error: result.errors})
if(result === "err") return res.status(500).json({error: "err"})

res.status(200).json(result?.data?.createStripeSubscription?.subscription)
res.status(200).json(result?.data?.createStripeSubscription?.clientSecret)
}

0 comments on commit 4ecf97a

Please sign in to comment.