简体   繁体   English

似乎无法使用python检索条带电荷

[英]Can't seem to retrieve stripe charge using python

I have the following python code to create a charge in stripe. 我有以下python代码来创建条带电荷。

a_charge = stripe.Charge.create(
amount=cents, 
currency="usd",
source=token,
description="my_description",
application_fee=application_fee,
stripe_account=teacher_stripe_id
)

This succeeds (I think), because it shows up in my dashboard with a charge_id. 这成功(我认为),因为它显示在我的仪表板中,带有charge_id。 But then, immediately after in the code, the following fails: 但是,在代码之后,紧接着失败:

stripe.Charge.retrieve(a_charge.id)

With error: No such charge: somelongstringhere 有错误:没有这样的费用:某些字符串

However, somelongstringhere is indeed the ID under charge details on my stripe dashboard. 然而,某些字符串确实是我的条带仪表板上的ID详细信息。 So how come Stripe can't retrieve this charge? 那么为什么Stripe无法收回这笔费用呢? Is this not the correct id? 这不是正确的ID吗?

The reason the charge retrieval call fails is because the charge was created on a different account. 费用检索呼叫失败的原因是因为费用是在另一个帐户上创建的。

When you create the charge: 当您创建费用时:

a_charge = stripe.Charge.create(
  amount=cents, 
  currency="usd",
  source=token,
  description="my_description",
  application_fee=application_fee,
  stripe_account=teacher_stripe_id
)

you use the stripe_account parameter, which instructs the library to use the Stripe-Account in the request. 您使用stripe_account参数,该参数指示库在请求中使用Stripe-Account This is used to tell Stripe's API that the request is issued by your platform on behalf of one of its connected accounts. 这用于告知Stripe的API,您的平台代表其中一个连接的帐户发出请求。

So in order to retrieve the charge, you need to use the same stripe_account parameter: 因此,为了检索费用,您需要使用相同的stripe_account参数:

the_same_charge = stripe.Charge.retrieve(a_charge.id, stripe_account= teacher_stripe_id)

That said, there is little use to do this in practice. 也就是说,在实践中没有什么用处。 You already have the charge object in a_charge . 你已经在a_charge拥有了充电对象。 If you execute the code above, you'd find that a_charge == the_same_charge . 如果你执行上面的代码,你会发现a_charge == the_same_charge

More generally, when you already have an instance of a Stripe object and want to get the latest state from the API, you can use the refresh() method: 更一般地说,当您已经拥有Stripe对象的实例并希望从API获取最新状态时,您可以使用refresh()方法:

a_charge.refresh()

This will query the API (without you having to worry about the stripe_account parameter -- the instance "remembers" about it and will use it in the background) and refresh the instance's attributes with the values retrieved from the API. 这将查询API(您无需担心stripe_account参数 - 实例“记住”它并将在后台使用它)并使用从API检索的值刷新实例的属性。

Why do you need to do stripe.Charge.retrieve(a.charge.id) immediately after creating the charge while you already have the data within a_charge ? 为什么你需要在创建电荷后立即执行stripe.Charge.retrieve(a.charge.id) ,而你已经拥有a_charge的数据?

It is possible that data are cached and/or broadcasted to multiple servers/databases. 数据可以缓存和/或广播到多个服务器/数据库。 Newly created data may then take a few seconds to be available for reading. 然后,新创建的数据可能需要几秒钟才能读取。

The code you're using here is creating a charge directly on a connected Stripe account. 您在此处使用的代码是直接在已连接的Stripe帐户上创建费用。 This is covered in Stripe's documentation here . 这是条纹的文档中涵盖这里

Since the charge is on a connected account, it doesn't exist on your own Stripe account and it's expected you can't retrieve it directly. 由于费用是在一个已连接的帐户上,因此您自己的Stripe帐户中不存在该费用,并且预计您无法直接检索该帐户。 To do this, you need to pass the Stripe-Account header again as documented here . 为此,您需要再次传递Stripe-Account标头,如此处所述

Your code should instead be 你的代码应该是

the_charge = stripe.Charge.retrieve(
    id=a_charge.id,
    stripe_account=teacher_stripe_id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM