Python Requires Chilkat v11.0.0+
Python
PayPal: Get a payable invoice link for your own email
See more PayPal Examples
Use a draft dated today or earlier. Make it payable with customer email suppressed, then retrieve the recipient view URL for the merchant's email. Future-dated invoices are scheduled for delivery and should use a different workflow.
Chilkat Python Downloads
import sys
import chilkat2
success = False
# See Unlock Chilkat before making requests.
# For the merchant's internal backoffice application. Load the sandbox token
# from the merchant's credential store. Keep it out of publicly distributed apps.
# See Get a PayPal access token.
http = chilkat2.Http()
http.AuthToken = "SANDBOX_ACCESS_TOKEN"
resp = chilkat2.HttpResponse()
http.SetUrlVar("id","INVOICE_ID")
success = http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp)
if (success != True):
print("No HTTP response: " + http.LastErrorText)
sys.exit()
if ((resp.StatusCode < 200) or (resp.StatusCode >= 300)):
errorInfo = chilkat2.JsonObject()
errorInfo.Load(resp.BodyStr)
print("HTTP " + str(resp.StatusCode) + " " + errorInfo.StringOf("name") + " " + errorInfo.StringOf("details[0].issue"))
print("PayPal debug ID: " + errorInfo.StringOf("debug_id"))
sys.exit()
result = chilkat2.JsonObject()
parsedResult = result.Load(resp.BodyStr)
if (parsedResult != True):
print("Expected a JSON response; HTTP " + str(resp.StatusCode))
sys.exit()
if (result.StringOfEquals("status","DRAFT",True) != True):
print("Select an unsent draft.")
sys.exit()
# Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
now = chilkat2.CkDateTime()
now.SetFromCurrentSystemTime()
stamp = chilkat2.StringBuilder()
stamp.Append(result.StringOf("detail.invoice_date"))
stamp.Append("T00:00:00Z")
issue = chilkat2.CkDateTime()
validDate = issue.SetFromTimestamp(stamp.GetAsString())
if ((validDate != True) or (now.DiffSeconds(issue) < 0)):
print("Select a draft with an issue date of today or earlier.")
sys.exit()
body = chilkat2.JsonObject()
body.UpdateBool("send_to_recipient",False)
body.UpdateBool("send_to_invoicer",False)
success = http.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send",body,"application/json",resp)
if (success != True):
print("No HTTP response: " + http.LastErrorText)
sys.exit()
if ((resp.StatusCode < 200) or (resp.StatusCode >= 300)):
sendError = chilkat2.JsonObject()
sendError.Load(resp.BodyStr)
print("HTTP " + str(resp.StatusCode) + " " + sendError.StringOf("name") + " " + sendError.StringOf("details[0].issue"))
print("PayPal debug ID: " + sendError.StringOf("debug_id"))
sys.exit()
# Read back the invoice: acceptance of the send alone does not guarantee it is payable.
success = http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp)
if (success != True):
print("No HTTP response: " + http.LastErrorText)
sys.exit()
if ((resp.StatusCode < 200) or (resp.StatusCode >= 300)):
invoiceError = chilkat2.JsonObject()
invoiceError.Load(resp.BodyStr)
print("HTTP " + str(resp.StatusCode) + " " + invoiceError.StringOf("name") + " " + invoiceError.StringOf("details[0].issue"))
print("PayPal debug ID: " + invoiceError.StringOf("debug_id"))
sys.exit()
invoice = chilkat2.JsonObject()
parsedInvoice = invoice.Load(resp.BodyStr)
if (parsedInvoice != True):
print("Expected a JSON response; HTTP " + str(resp.StatusCode))
sys.exit()
if (((invoice.StringOfEquals("status","SENT",True) == True) or (invoice.StringOfEquals("status","UNPAID",True) == True) or (invoice.StringOfEquals("status","SHARED",True) == True)) and (invoice.HasMember("detail.metadata.recipient_view_url") == True)):
print("Customer payment page: " + invoice.StringOf("detail.metadata.recipient_view_url"))
sys.exit()
print("Invoice is " + invoice.StringOf("status") + ". Retrieve it again when payable.")