我正在尝试在 svelte-kit 上设置 stripe connect 付款,但是 stripe 不断返回错误:
提供的 client_secret 与此账户上任何关联的 PaymentIntent 都不匹配。确保使用的可发布密钥属于创建 PaymentIntent 的同一帐户。
我已经三次检查客户端密钥是否是使用与我的公钥匹配的密钥创建的,我还尝试尝试沙盒帐户而不是测试环境,但仍然遇到相同的错误。
这是生成client_secret的服务器代码:
import { SECRET_STRIPE_KEY } from '$env/static/private';
const stripe = new Stripe(SECRET_STRIPE_KEY, {
apiVersion: "2024-11-20.acacia" as "2024-10-28.acacia", // This is a type assertion
});
export async function GET() {
const ticket_price = 45;
const application_fee = 1;
const paymentIntent = await stripe.paymentIntents.create({
amount: ticket_price,
currency: 'gbp',
application_fee_amount: application_fee,
automatic_payment_methods: {
enabled: true
}
}, {
stripeAccount: 'acct_1QWcgoDArWfcz7re',
});
return json({
clientSecret: paymentIntent.client_secret
}, { status: 200 });
}
这是客户端使用 svelte-stipe 和 stripe-js 创建付款表单的部分:
<script lang="ts">
import { onMount } from 'svelte';
import { loadStripe } from '@stripe/stripe-js'
import type { Stripe, StripeElements } from '@stripe/stripe-js'
import { Elements, PaymentElement, LinkAuthenticationElement, Address } from 'svelte-stripe'
import { PUBLIC_STRIPE_KEY } from '$env/static/public';
let stripe: null | Stripe = null
let clientSecret : null | string = null
let elements: StripeElements | null | undefined
let processing = false
onMount(async () => {
stripe = await loadStripe(PUBLIC_STRIPE_KEY, { apiVersion: "2024-11-20.acacia" as "2024-10-28.acacia" })
clientSecret = await createPaymentIntent()
})
async function createPaymentIntent() {
const response = await fetch('/api/stripe/payment', {
method: 'GET',
headers: {
'content-type': 'application/json'
}
})
const { clientSecret } = await response.json()
return clientSecret
}
async function submit() {
// ...
}
</script>
{#if clientSecret !== null}
<Elements
{stripe}
{clientSecret}
theme="flat"
labels="floating"
bind:elements
>
<form on:submit|preventDefault={submit}>
<LinkAuthenticationElement />
<PaymentElement />
<Address mode="billing" />
<button disabled={processing}>
{#if processing}
Processing...
{:else}
Pay
{/if}
</button>
</form>
</Elements>
{:else}
Loading...
{/if}
起初,我认为这可能是 stripe-svelte 的问题,但是在查看网络请求时,所有参数都是正确的。我对密钥和 client_secret 进行了三次检查,所以这只是 Stripe 的问题吗?
https://api.stripe.com/v1/elements/sessions?client_secret=pi_3QWcxADArWfcz7re023mkEqp_secret_mHQ6ErNxsy61qqAAp57FD1rIT&key=pk_test_69buoGS6vl82TpptRDix1WEr00LnbLWpxw&_stripe_version=2024-11-20.acacia&type=payment_intent&locale=en-US&referrer_host=localhost:5173&expand[0]=payment_method_preference.payment_intent.payment_method&stripe_js_id=bb0bb448-9ebf-46c9-b6e5-77e60c7f4016
client_secret=pi_3QWcxADArWfcz7re023mkEqp_secret_mHQ6ErNxsy61qqAAp57FD1rIT
key=pk_test_69buoGS6vl82TpptRDix1WEr00LnbLWpxw
这是来自 stripe 的错误响应:
{
"error": {
"message": "The client_secret provided does not match any associated PaymentIntent on this account. Ensure the publishable key used belongs to the same account that created the PaymentIntent.",
"param": "client_secret",
"request_log_url": "https://dashboard.stripe.com/test/logs/req_Gf29tN9FjPAYJe?t=1734350353",
"type": "invalid_request_error"
}
}
检查Stripe日志,您可以看到失败的请求,但看不到创建客户端密钥的请求。
更新
我从 paymentIntent 创建中删除了“stripeAccount”,它现在不显示错误,但它现在不能作为连接付款使用,所以我假设我必须使用连接的 stripe 帐户的秘密和公开?
问题出在客户端,我忘记将“stripeAccount”添加到“loadStripe”的选项中