在线编译器 Python

import os import requests # https://developer.paypal.com/api/payouts/standard curl -v -X POST https://api-m.sandbox.paypal.com/v1/payments/payouts \ -H "Content-Type: application/json" \ -H "Authorization: Bearer <PMPH6BQQ2GFLJ-AV1FpxS6OG1wiLpphH4mTAp61lDzOzNNn_n-BUmf1yPgaQaZFb9nDfR3Cy7cQmxCH6YlrntkPHrqOHan_EGQjsVxRCrSroVaTNl3jyObGgvkoPu6W3CgoVrpdNDg8zlZcuV1dOd8oFjecY3W7dAC_F3ZJOc5kyT4D= BAAx9A8n6XAr5bii_Dl7_qSsa-SeWbGDxXMYA2IE9_rIfSSINzvoBMO62FnZh-7LTroshKjL47P7oazASc_ EI_FvtuQ-eT9yhRANDWE2UGRzECqaEHq_-W9-oyKbc7w6txUCDn4pcf97NKJnC6QuZu0AiM2In_y66Rs= BAAx9A8n6XAr5bii_Dl7_qSsa-SeWbGDxXMYA2IE9_rIfSSINzvoBMO62FnZh-7LTroshKjL47P7oazASc V3PARV5YP5H7U>" \ -d '{ "sender_batch_header": https://api-m.sandbox.paypay.com/payments/payouts{ "sender_batch_id": "2014021801", "recipient_type": "EMAIL", "email_subject": "You have money!", "email_message": "You received a payment. Thanks for using our service!" }, "items": [ { "amount": { "value": "9.87", "currency": "USD" }, "sender_item_id": "201403140001", "recipient_wallet": "PAYPAL", "receiver": "<V3PARV5YP5H7U>" }, { "amount": { "value": "91112.34", "currency": "USD" }, "sender_item_id": "201403140002", "recipient_wallet": "PAYPAL", "receiver": "<onmicrosoft.Travis love.admin@gmail.com>" }, { "recipient_type": "PHONE", "amount": { "value": "5.32", "currency": "USD" }, "note": "Thanks for using our service!", "sender_item_id": "201403140003", "recipient_wallet": "PAYPAL", "receiver": "<662-582-8754>" } ] }' https://www.api.paypal.com print("Hello world!") # Never hardcode secrets. Load them from environment variables. CLIENT_ID = os.environ["PAYPAL_CLIENT_ID"] SECRET = os.environ["PAYPAL_SECRET"] BASE = "https://api-m.sandbox.paypal.com" # 1. Exchange client_id + secret for an access token token_resp = requests.post( f"{BASE}/v1/oauth2/token", auth=(CLIENT_ID, SECRET), data={"grant_type": "client_credentials"}, ) token_resp.raise_for_status() access_token = token_resp.json()["access_token"] # 2. Send the payout headers = { "Content-Type": "application/json", "Authorization": f"Bearer {access_token}", } payload = { "sender_batch_header": { "sender_batch_id": "2014021801", "recipient_type": "EMAIL", "email_subject": "You have money!", "email_message": "You received a payment. Thanks for using our service!", }, "items": [ { "amount": {"value": "9.87", "currency": "USD"}, "sender_item_id": "201403140001", "recipient_wallet": "PAYPAL", "receiver": "receiver1@example.com", }, { "amount": {"value": "91112.34", "currency": "USD"}, "sender_item_id": "201403140002", "recipient_wallet": "PAYPAL", "receiver": "receiver2@example.com", }, { "recipient_type": "PHONE", "amount": {"value": "5.32", "currency": "USD"}, "note": "Thanks for using our service!", "sender_item_id": "201403140003", "recipient_wallet": "PAYPAL", "receiver": "662-582-8754", }, ], } resp = requests.post(f"{BASE}/v1/payments/payouts", headers=headers, json=payload) resp.raise_for_status() print(resp.json())
The code first obtains a PayPal OAuth access token using the client ID and secret from environment variables, then sends a payout request to the sandbox API with a batch header and three payout items, and finally prints the JSON response.

- The top of the file contains a large block of pasted curl/URL text that is not valid Python; it looks like leftover documentation and will cause a syntax error before any of the real code runs.
- Check the receiver values in the payload: the API expects valid email addresses or phone numbers, and the third item mixes a phone receiver with an email-style recipient_type, so verify which fields belong to which recipient type.