Python Stripe: Create a Refund

Back to Index

Creates a new refund to refund a charge that has previously been created but not yet refunded.

Documentation: https://stripe.com/docs/api/curl#create_refund

CURL Command

curl https://api.stripe.com/v1/refunds \
   -u STRIPE_SECRET_KEY: \
   -d charge=ch_1BnETKGswQrCoh0XE7kJI2wj
   -X POST

Python Example

import sys
import chilkat

rest = chilkat.CkRest()

#  URL: https://api.stripe.com/v1/refunds
bTls = True
port = 443
bAutoReconnect = True
success = rest.Connect("api.stripe.com",port,bTls,bAutoReconnect)
if (success != True):
    print("ConnectFailReason: " + str(rest.get_ConnectFailReason()))
    print(rest.lastErrorText())
    sys.exit()

rest.SetAuthBasic("STRIPE_SECRET_KEY","")

rest.AddQueryParam("charge","ch_1BnETKGswQrCoh0XE7kJI2wj")

strResponseBody = rest.fullRequestFormUrlEncoded("POST","/v1/refunds")
if (rest.get_LastMethodSuccess() != True):
    print(rest.lastErrorText())
    sys.exit()

jsonResponse = chilkat.CkJsonObject()
jsonResponse.Load(strResponseBody)

id = jsonResponse.stringOf("id")
object = jsonResponse.stringOf("object")
amount = jsonResponse.IntOf("amount")
balance_transaction = jsonResponse.IsNullOf("balance_transaction")
charge = jsonResponse.stringOf("charge")
created = jsonResponse.IntOf("created")
currency = jsonResponse.stringOf("currency")
reason = jsonResponse.IsNullOf("reason")
receipt_number = jsonResponse.IsNullOf("receipt_number")
status = jsonResponse.stringOf("status")

Sample JSON Response Body

{
  "id": "re_1BnETKGswQrCoh0XT2qLx7S0",
  "object": "refund",
  "amount": 100,
  "balance_transaction": null,
  "charge": "ch_1BnETKGswQrCoh0XE7kJI2wj",
  "created": 1516662782,
  "currency": "usd",
  "metadata": {},
  "reason": null,
  "receipt_number": null,
  "status": "succeeded"
}