首页 web前端 js教程 使用 Stripe Connect 创建市场:上线流程

使用 Stripe Connect 创建市场:上线流程

Nov 28, 2024 am 09:55 AM

介绍

考虑到没有多少支付处理商提供它,创建一个市场可能太难了,或者是不可能的,如果他们不提供它,那么你很可能会在他们听到风声的那一刻就被踢出平台,即使没有如果您没有坚实的基础来处理使用该平台的卖家的付款、退款和付款,那么创建一个市场是有风险的。

Stripe Connect 解决了这些问题,它将使我们能够创建一个基本的市场,您可以在其中注册成为卖家,并且客户可以轻松地从这些卖家那里购买商品。作为平台所有者,您还可以设置服务费,因此当用户 X 从商店 Y 购买商品时,我们将获得该交易的 X% 分成,但稍后会详细介绍。

Creating a marketplace with Stripe Connect: The onboard process

设置项目

为了处理数据库连接,我们使用 Prisma,身份验证由 remix-auth 处理,对于这部分,我们仅处理市场的卖家端。

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Store {
  id         String   @id // This will be the store's subdomain
  name       String
  updated_at DateTime @default(now()) @updatedAt
  seller     Seller?
}

model Seller {
  id           Int      @id @default(autoincrement())
  email        String
  password     String
  store        Store    @relation(fields: [store_id], references: [id])
  date_created DateTime @default(now())
  date_updated DateTime @updatedAt
  store_id     String   @unique
}
登录后复制
登录后复制
登录后复制

这就是我们的 schema.prisma 文件的样子,我们有一个卖家模型和一个与之相关的商店模型,“id”字段将用作子域,因此当我们到达买家一侧时,我将能够访问 store.localhost.com 并从那里的卖家那里购买产品。
我们还将添加一个 Stripe 模型,它将存储有关卖家 Connect 帐户的数据。

model Stripe {
  account_id String @id
  is_onboarded Boolean @default(false)
  user Users @relation(fields: [user_id], references: [discord_id])
  user_id String @unique
  created_at DateTime @default(now())
  updated_at DateTime @updatedAt
}

model Seller {
  id           Int      @id @default(autoincrement())
  email        String
  password     String
  store        Store    @relation(fields: [store_id], references: [id])
  date_created DateTime @default(now())
  date_updated DateTime @updatedAt
  store_id     String   @unique
  stripe       Stripe?
}
登录后复制
登录后复制
登录后复制

现在我们可以处理用户入门问题了,所以让我们在 .env 文件中定义另一个变量。

STRIPE_SK=your stripe secret key here
登录后复制
登录后复制

您可以通过在 Stripe 的开发页面中生成 Stripe 密钥来获取它,最好创建一个目前仅允许使用 Stripe Connect 的受限密钥。

然后您需要创建一个新文件来导出 Stripe 客户端,以便我们的路由可以使用它

// app/libs/stripe.server.ts
import Stripe from 'stripe';
export const stripe = new Stripe(process.env.STRIPE_SK)
登录后复制
登录后复制

我们将创建一条位于“/onboarding”的新路线

// app/routes/onboarding.tsx

export default function Onboarding() {
    const {stripe} = useLoaderData();

    return <div className={'text-center pt-[6%]'}>
    <h1 className={'text-xl'}>Account onboarded: {stripe?.is_onboarded ? stripe?.account_id : '? Not connected'}</h1>
        <div className={'flex items-center  text-white text-sm  mt-5 justify-center gap-3'}>
            {!stripe ? <>
                <Form method={'post'}>
                    <button type={'submit'} className={'bg-blue-600 hover:cursor-pointer  rounded-[6px] px-4 py-1.5'}>Setup your seller
                        account
                    </button>

                </Form>
            </> : <>
                <div className={'bg-blue-600 rounded-[6px] px-4 py-1.5'}>Seller dashboard</div>

            </>}
        </div>
    </div>
}
登录后复制
登录后复制

我们将添加一个加载器函数,该函数将传递有关卖家入职状态的数据

export async function loader({request}: LoaderFunctionArgs) {
    const user = await authenticator.isAuthenticated(request, {
        failureRedirect: '/login'
    })

    const seller = await prisma.seller.findFirst({
        where: {
            id: user.id
        }, include: {
            stripe: true
        }
    })

    return {
        stripe: seller?.stripe
    }
}
登录后复制
登录后复制

现在,如果您转到/onboarding,它会说您尚未连接,您将能够按下按钮进行注册,这就是我们的操作功能的用武之地

export async function action({request}: ActionFunctionArgs) {
    const authenticated = await authenticator.isAuthenticated(request, {
        failureRedirect: '/login'
    })
    const seller = await prisma.seller.findFirst({
        where: {
            id: authenticated.id
        }, include: {
            stripe: true
        }
    })
    if (seller && seller.stripe?.is_onboarded) {
        return json({
            message: 'User is onboarded already',
            error: true
        }, {
            status: 400
        })
    }
    const account = seller?.stripe?.account_id ? {
        id: seller.stripe?.account_id
    } : await stripe.accounts.create({
        email: seller?.email,
        controller: {
            fees: {
                payer: 'application',
            },
            losses: {
                payments: 'application',
            },
            stripe_dashboard: {
                type: 'express',
            },
        },
    });
    if (!seller?.stripe?.account_id) {
        await prisma.seller.update({
            where: {
                id: authenticated.id
            },
            data: {
                stripe: {
                    create: {
                        account_id: account.id
                    }
                }
            }, include: {
                stripe: true
            }
        })
    }
    const accountLink = await stripe.accountLinks.create({
        account: account.id,
        refresh_url: 'http://localhost:5173/onboarding',
        return_url: 'http://localhost:5173/onboarding',
        type: 'account_onboarding',
        collection_options: {
            fields: 'eventually_due',
        },
    });
    console.debug(`[ACCOUNT ID = ${account.id}] CREATED ACCOUNT ONBOARDING LINK, REDIRECTING...`)

    return redirect(accountLink.url)
}
登录后复制
登录后复制

当卖家按下按钮时,我们将使用他们注册时使用的电子邮件创建一个帐户,然后我们将创建一个帐户链接,将他们重定向到入职页面(如果卖家已经附加了 Stripe 帐户,但尚未加入,那么我们也会将他们重定向到加入链接。

Creating a marketplace with Stripe Connect: The onboard process

从那里卖家将输入他的电子邮件/电话号码,然后入职流程将开始,Stripe 通常会询问卖家企业位置、企业详细信息、银行账户等...

现在我们可以监听 Stripe Connect 事件的 webhook,因此当卖家成功加入后,我们会将这些属性添加到数据库中的卖家记录中。

为了进行测试,您可以简单地下载 Stripe CLI,然后您可以将任何事件转发到我们现在将创建的新路由 /api/notifications

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Store {
  id         String   @id // This will be the store's subdomain
  name       String
  updated_at DateTime @default(now()) @updatedAt
  seller     Seller?
}

model Seller {
  id           Int      @id @default(autoincrement())
  email        String
  password     String
  store        Store    @relation(fields: [store_id], references: [id])
  date_created DateTime @default(now())
  date_updated DateTime @updatedAt
  store_id     String   @unique
}
登录后复制
登录后复制
登录后复制

当您运行该命令时,您将获得一个 Webhook 签名,以便我们可以验证 Stripe 发送给我们的每个 Webhook 的完整性,同样,如果您在 Stripe 上的开发人员门户上创建一个 Webhook,您将拥有一个秘密.

model Stripe {
  account_id String @id
  is_onboarded Boolean @default(false)
  user Users @relation(fields: [user_id], references: [discord_id])
  user_id String @unique
  created_at DateTime @default(now())
  updated_at DateTime @updatedAt
}

model Seller {
  id           Int      @id @default(autoincrement())
  email        String
  password     String
  store        Store    @relation(fields: [store_id], references: [id])
  date_created DateTime @default(now())
  date_updated DateTime @updatedAt
  store_id     String   @unique
  stripe       Stripe?
}
登录后复制
登录后复制
登录后复制

我们还将在 .env 文件中添加一个新变量

STRIPE_SK=your stripe secret key here
登录后复制
登录后复制

现在我们可以编写代码来处理 Stripe 发送给我们的这些事件

// app/libs/stripe.server.ts
import Stripe from 'stripe';
export const stripe = new Stripe(process.env.STRIPE_SK)
登录后复制
登录后复制

我们验证是否是 Stripe 发送了请求,如果是,那么我们继续,现在我们要关注的事件是 account.updated,该事件与我们在重定向卖家之前创建的帐户相关。

当卖家开始入职流程、添加电话号码、输入电子邮件或最终完成入职流程时,我们将收到“account.updated”事件,并且会发送此数组

account.requirements.currently_due

当“currently_due”数组的长度为零时,我们知道用户已完全注册,能够接受付款,因此从我们这边我们可以更新数据库并允许用户创建产品,但在此之前让我们添加'/api/notifications' 操作中的逻辑

// app/routes/onboarding.tsx

export default function Onboarding() {
    const {stripe} = useLoaderData();

    return <div className={'text-center pt-[6%]'}>
    <h1 className={'text-xl'}>Account onboarded: {stripe?.is_onboarded ? stripe?.account_id : '? Not connected'}</h1>
        <div className={'flex items-center  text-white text-sm  mt-5 justify-center gap-3'}>
            {!stripe ? <>
                <Form method={'post'}>
                    <button type={'submit'} className={'bg-blue-600 hover:cursor-pointer  rounded-[6px] px-4 py-1.5'}>Setup your seller
                        account
                    </button>

                </Form>
            </> : <>
                <div className={'bg-blue-600 rounded-[6px] px-4 py-1.5'}>Seller dashboard</div>

            </>}
        </div>
    </div>
}
登录后复制
登录后复制

一旦到位,我们就可以尝试加入并看看它是否有效。例如,一旦您输入地址,您就会在项目的控制台中看到一条消息,例如

export async function loader({request}: LoaderFunctionArgs) {
    const user = await authenticator.isAuthenticated(request, {
        failureRedirect: '/login'
    })

    const seller = await prisma.seller.findFirst({
        where: {
            id: user.id
        }, include: {
            stripe: true
        }
    })

    return {
        stripe: seller?.stripe
    }
}
登录后复制
登录后复制

这意味着主体已经过验证,并且我们已成功接收来自 Stripe 的事件,但让我们看看加入是否有效。

一旦你到达最后一步,它可能会说你的帐户详细信息不完整,最后一步是身份验证,因为这是测试模式,我们可以模拟

Creating a marketplace with Stripe Connect: The onboard process

好的,一旦我们完成了,我们将返回到上一页,我们可以按提交,按提交,我们将进入控制台

export async function action({request}: ActionFunctionArgs) {
    const authenticated = await authenticator.isAuthenticated(request, {
        failureRedirect: '/login'
    })
    const seller = await prisma.seller.findFirst({
        where: {
            id: authenticated.id
        }, include: {
            stripe: true
        }
    })
    if (seller && seller.stripe?.is_onboarded) {
        return json({
            message: 'User is onboarded already',
            error: true
        }, {
            status: 400
        })
    }
    const account = seller?.stripe?.account_id ? {
        id: seller.stripe?.account_id
    } : await stripe.accounts.create({
        email: seller?.email,
        controller: {
            fees: {
                payer: 'application',
            },
            losses: {
                payments: 'application',
            },
            stripe_dashboard: {
                type: 'express',
            },
        },
    });
    if (!seller?.stripe?.account_id) {
        await prisma.seller.update({
            where: {
                id: authenticated.id
            },
            data: {
                stripe: {
                    create: {
                        account_id: account.id
                    }
                }
            }, include: {
                stripe: true
            }
        })
    }
    const accountLink = await stripe.accountLinks.create({
        account: account.id,
        refresh_url: 'http://localhost:5173/onboarding',
        return_url: 'http://localhost:5173/onboarding',
        type: 'account_onboarding',
        collection_options: {
            fields: 'eventually_due',
        },
    });
    console.debug(`[ACCOUNT ID = ${account.id}] CREATED ACCOUNT ONBOARDING LINK, REDIRECTING...`)

    return redirect(accountLink.url)
}
登录后复制
登录后复制

成功了,现在 Stripe 将使我们返回到入门页面,它会向我们显示我们的帐户 ID,这意味着我们已成功入门,我们可以开始创建产品。

Creating a marketplace with Stripe Connect: The onboard process

好吧,让我们先让卖家仪表板按钮发挥作用,然后再继续讨论产品,创建一条位于 /portal 的新路线

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Store {
  id         String   @id // This will be the store's subdomain
  name       String
  updated_at DateTime @default(now()) @updatedAt
  seller     Seller?
}

model Seller {
  id           Int      @id @default(autoincrement())
  email        String
  password     String
  store        Store    @relation(fields: [store_id], references: [id])
  date_created DateTime @default(now())
  date_updated DateTime @updatedAt
  store_id     String   @unique
}
登录后复制
登录后复制
登录后复制

非常基本的功能,因此现在当您登录 /portal 时,您将被重定向到我们为 Stripe 帐户生成的一次性链接。

在入职路线中,我们将使用链接包裹卖家仪表板 div。

model Stripe {
  account_id String @id
  is_onboarded Boolean @default(false)
  user Users @relation(fields: [user_id], references: [discord_id])
  user_id String @unique
  created_at DateTime @default(now())
  updated_at DateTime @updatedAt
}

model Seller {
  id           Int      @id @default(autoincrement())
  email        String
  password     String
  store        Store    @relation(fields: [store_id], references: [id])
  date_created DateTime @default(now())
  date_updated DateTime @updatedAt
  store_id     String   @unique
  stripe       Stripe?
}
登录后复制
登录后复制
登录后复制

当我们访问 /portal 或按下按钮时,我们将被重定向到 Stripe 的 Connect 帐户门户,用户可以在那里看到他的分析、付款等...

Creating a marketplace with Stripe Connect: The onboard process

这标志着我们使用 Stripe Connect 创建市场的第一部分的结束,第二部分将处理产品、付款和支出,第三部分将是最终部分,我们将处理项目面向客户的方面.

您可以在https://github.com/ddm50/stripe-connect-howto-seller查看该项目的源代码

以上是使用 Stripe Connect 创建市场:上线流程的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

前端热敏纸小票打印遇到乱码问题怎么办? 前端热敏纸小票打印遇到乱码问题怎么办? Apr 04, 2025 pm 02:42 PM

前端热敏纸小票打印的常见问题与解决方案在前端开发中,小票打印是一个常见的需求。然而,很多开发者在实...

神秘的JavaScript:它的作用以及为什么重要 神秘的JavaScript:它的作用以及为什么重要 Apr 09, 2025 am 12:07 AM

JavaScript是现代Web开发的基石,它的主要功能包括事件驱动编程、动态内容生成和异步编程。1)事件驱动编程允许网页根据用户操作动态变化。2)动态内容生成使得页面内容可以根据条件调整。3)异步编程确保用户界面不被阻塞。JavaScript广泛应用于网页交互、单页面应用和服务器端开发,极大地提升了用户体验和跨平台开发的灵活性。

谁得到更多的Python或JavaScript? 谁得到更多的Python或JavaScript? Apr 04, 2025 am 12:09 AM

Python和JavaScript开发者的薪资没有绝对的高低,具体取决于技能和行业需求。1.Python在数据科学和机器学习领域可能薪资更高。2.JavaScript在前端和全栈开发中需求大,薪资也可观。3.影响因素包括经验、地理位置、公司规模和特定技能。

如何实现视差滚动和元素动画效果,像资生堂官网那样?
或者:
怎样才能像资生堂官网一样,实现页面滚动伴随的动画效果? 如何实现视差滚动和元素动画效果,像资生堂官网那样? 或者: 怎样才能像资生堂官网一样,实现页面滚动伴随的动画效果? Apr 04, 2025 pm 05:36 PM

实现视差滚动和元素动画效果的探讨本文将探讨如何实现类似资生堂官网(https://www.shiseido.co.jp/sb/wonderland/)中�...

JavaScript难以学习吗? JavaScript难以学习吗? Apr 03, 2025 am 12:20 AM

学习JavaScript不难,但有挑战。1)理解基础概念如变量、数据类型、函数等。2)掌握异步编程,通过事件循环实现。3)使用DOM操作和Promise处理异步请求。4)避免常见错误,使用调试技巧。5)优化性能,遵循最佳实践。

JavaScript的演变:当前的趋势和未来前景 JavaScript的演变:当前的趋势和未来前景 Apr 10, 2025 am 09:33 AM

JavaScript的最新趋势包括TypeScript的崛起、现代框架和库的流行以及WebAssembly的应用。未来前景涵盖更强大的类型系统、服务器端JavaScript的发展、人工智能和机器学习的扩展以及物联网和边缘计算的潜力。

如何使用JavaScript将具有相同ID的数组元素合并到一个对象中? 如何使用JavaScript将具有相同ID的数组元素合并到一个对象中? Apr 04, 2025 pm 05:09 PM

如何在JavaScript中将具有相同ID的数组元素合并到一个对象中?在处理数据时,我们常常会遇到需要将具有相同ID�...

Zustand异步操作:如何确保useStore获取的最新状态? Zustand异步操作:如何确保useStore获取的最新状态? Apr 04, 2025 pm 02:09 PM

zustand异步操作中的数据更新问题在使用zustand状态管理库时,经常会遇到异步操作导致数据更新不及时的问题。�...

See all articles