php小编鱼仔编辑了一篇简洁明了的文章,解释了在使用SetupIntent过程中可能遇到的问题。其中,一种可能的错误是“提供的client_secret与此帐户上的任何关联的SetupIntent不匹配”,这可能导致操作失败。文章通过简单明了的语言解释了这个错误的原因和解决办法,帮助读者快速解决问题,提高使用SetupIntent的效率。
问题内容
我正在尝试将外部银行帐户链接到 Stripe 连接帐户。帐户类型是自定义的。我成功创建了与已连接帐户关联的 SetupIntent(如下所示)并收到了客户端密钥:
params := &stripe.SetupIntentParams{
AttachToSelf: stripe.Bool(true),
FlowDirections: stripe.StringSlice([]string{
*stripe.String(string(stripe.SetupIntentFlowDirectionInbound)),
*stripe.String(string(stripe.SetupIntentFlowDirectionOutbound)),
}),
PaymentMethodOptions: &stripe.SetupIntentPaymentMethodOptionsParams{
USBankAccount: &stripe.SetupIntentPaymentMethodOptionsUSBankAccountParams{
FinancialConnections: &stripe.SetupIntentPaymentMethodOptionsUSBankAccountFinancialConnectionsParams{
Permissions: stripe.StringSlice([]string{*stripe.String("balances"), *stripe.String("payment_method")}),
},
VerificationMethod: stripe.String("instant"),
},
},
PaymentMethodTypes: stripe.StringSlice([]string{
*stripe.String(string(stripe.PaymentMethodTypeUSBankAccount)),
}),
}
params.SetStripeAccount(connectedId)
resp, err := setupintent.New(params)
if err != nil {
return nil, fmt.Errorf("failed to setup intent: %w", err)
}
return resp, nil
client_secret 被传递到前端,我们在前端启动授权流程以收集银行信息以便能够链接外部帐户。
const publishableKey = process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY || ''
if (!publishableKey) {
throw new Error('missing stripe publishable key')
}
export default function Page() {
const [clientSecret, setClientSecret] = useState('')
const [showModal, setShowModal] = useState(false)
const [stripeInstance, setStripeInstance] = useState<Stripe | null>(null)
const [linkingBankLoading, setLinkingBankLoading] = useState(false)
useEffect(() => {
const loadAndSetStripe = async () => {
try {
const loadedStripe = await loadStripe(publishableKey)
setStripeInstance(loadedStripe)
} catch (err) {
console.error('stripe publishable key failed to load', err)
}
}
loadAndSetStripe()
}, [])
const linkExternalBank = async () => {
setLinkingBankLoading(true)
try {
const { data } = await linkExternalBankMutation({
context: {
headers: { authorization: `Bearer ${accessToken}` },
},
})
if (data?.linkExternalBank.clientSecret) {
setClientSecret(data.linkExternalBank.clientSecret)
setShowStripeModal(true)
}
} catch (err) {
console.error('error linking external bank', err)
} finally {
setLinkingBankLoading(false)
}
}
{showModal && stripeInstance && clientSecret && (
<Modal
isOpen={showModal}
onClose={() => setShowModal(false)}
clientSecret={clientSecret}
stripe={stripeInstance}
/>
)}
}
最后在模态中:
interface ModalProps {
isOpen: boolean
onClose: () => void
clientSecret: string
stripe: Stripe | null
}
export const Modal = ({ isOpen, onClose, clientSecret, stripe }: ModalProps) => {
if (!isOpen || !stripe || !clientSecret) {
return null
}
return (
<button onClick={onClose}>Close</button>
<Elements stripe={stripe} options={{ clientSecret }}>
<BankDetailsForm clientSecret={clientSecret} />
</Elements>
)
}
它尝试加载元素并在崩溃之前快速显示一秒钟。当我查看网络选项卡时,我们收到错误:提供的 client_secret 与此帐户上的任何关联的 SetupIntent 不匹配。
而且当我运行curl命令来检索SetupIntent时,我可以成功地看到它是为该帐户创建的。我很困惑为什么会发生这个错误。
此外,我还仔细检查了生成的 API 密钥,并且使用了正确的密钥。
我已经阅读了 Stripe 文档,但仍然遇到了这个问题。还尝试对客户端密钥进行硬编码,并在通过 Curl 生成时可发布,但仍然遇到相同的错误。
将客户端密钥传递给 Element 组件时会失败。
解决方法
您正在代表连接的帐户进行服务器端调用:
params.SetStripeAccount(connectedId)
但在前端,你不是:
const loadedStripe = await loadStripe(publishableKey)
因此,Stripe 正在寻找您帐户上的意图进行确认,而不是连接的帐户。
您必须以相同的方式对所有内容进行身份验证 - 在您的情况下,这意味着代表您连接的帐户 ID 对您的前端进行身份验证:
const stripePromise = loadStripe('{{PLATFORM_PUBLISHABLE_KEY}}', {
stripeAccount: '{{CONNECTED_STRIPE_ACCOUNT_ID}}',
});
相关Stripe 文档。