Stripe技术问题:如何从Charge对象获取Payout对象
Let me break down how to connect your Charge to the right Payout, since you're hitting that "No such payout" error because you're using a Transfer ID (tr_xxx) instead of a Payout ID (po_xxx).
Stripe's flow here is a bit layered—Charges don't directly link to Payouts. Instead, they connect through either Transfers (if you're sending funds directly to a connected account) or Balance Transactions (if funds are first deposited into your platform's balance). Here's how to get the correct Payout ID in both scenarios:
Scenario 1: Charge was sent directly to a connected account (using the destination parameter)
If you created the Charge with a destination pointing to a connected account, the funds go through a Transfer first:
- Start by retrieving your Charge object. Look for the
transferfield—it will hold the Transfer ID (tr_xxx). - Retrieve that Transfer object. Inside it, you'll find a
destination_payoutfield—this is thepo_xxxPayout ID you need. Use this ID to call the Payout API, and it will work correctly.
Example code snippet (using Stripe's Python SDK):
import stripe stripe.api_key = "your_api_key" # Get the Charge charge = stripe.Charge.retrieve("ch_your_charge_id") # Extract the Transfer ID transfer_id = charge.transfer # Get the Transfer to find the linked Payout transfer = stripe.Transfer.retrieve(transfer_id) # Now you have the valid Payout ID payout_id = transfer.destination_payout # Retrieve the Payout payout = stripe.Payout.retrieve(payout_id)
Scenario 2: Charge funds went to your platform's balance first
If the Charge's funds were added to your platform's balance before being included in a Payout:
- Retrieve the Charge object and grab the
balance_transactionfield (it will be atxn_xxxID). - Retrieve that Balance Transaction. Look for the
payoutfield—this will be thepo_xxxPayout ID once the funds have been included in a payout.
Note: If the funds are still in pending status (not yet paid out), the payout field will be null. It will populate once Stripe processes the payout for those funds.
Example code snippet:
import stripe stripe.api_key = "your_api_key" # Get the Charge charge = stripe.Charge.retrieve("ch_your_charge_id") # Extract the Balance Transaction ID balance_txn_id = charge.balance_transaction # Get the Balance Transaction balance_txn = stripe.BalanceTransaction.retrieve(balance_txn_id) # Get the linked Payout ID (if available) payout_id = balance_txn.payout if payout_id: payout = stripe.Payout.retrieve(payout_id) else: print("Funds are still pending and not yet included in a payout")
To recap: You can't use a Transfer ID (tr_xxx) to look up a Payout directly—you need to go through either the Transfer's destination_payout (for direct connected account transfers) or the Balance Transaction's payout (for platform balance funds) to get the valid po_xxx Payout ID.
内容的提问来源于stack exchange,提问作者Loïc Bellemare-Alford

