AutoIt Requires Chilkat v11.0.0+
AutoIt
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 AutoIt Downloads
Local $bSuccess = 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.
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.AuthToken = "SANDBOX_ACCESS_TOKEN"
$oResp = ObjCreate("Chilkat.HttpResponse")
$oHttp.SetUrlVar("id","INVOICE_ID")
$bSuccess = $oHttp.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",$oResp)
If ($bSuccess <> True) Then
ConsoleWrite("No HTTP response: " & $oHttp.LastErrorText & @CRLF)
Exit
EndIf
If (($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
$oErrorInfo = ObjCreate("Chilkat.JsonObject")
$oErrorInfo.Load($oResp.BodyStr)
ConsoleWrite("HTTP " & $oResp.StatusCode & " " & $oErrorInfo.StringOf("name") & " " & $oErrorInfo.StringOf("details[0].issue") & @CRLF)
ConsoleWrite("PayPal debug ID: " & $oErrorInfo.StringOf("debug_id") & @CRLF)
Exit
EndIf
$oResult = ObjCreate("Chilkat.JsonObject")
Local $bParsedResult = $oResult.Load($oResp.BodyStr)
If ($bParsedResult <> True) Then
ConsoleWrite("Expected a JSON response; HTTP " & $oResp.StatusCode & @CRLF)
Exit
EndIf
If ($oResult.StringOfEquals("status","DRAFT",True) <> True) Then
ConsoleWrite("Select an unsent draft." & @CRLF)
Exit
EndIf
; Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
$oNow = ObjCreate("Chilkat.CkDateTime")
$oNow.SetFromCurrentSystemTime()
$oStamp = ObjCreate("Chilkat.StringBuilder")
$oStamp.Append($oResult.StringOf("detail.invoice_date"))
$oStamp.Append("T00:00:00Z")
$oIssue = ObjCreate("Chilkat.CkDateTime")
Local $bValidDate = $oIssue.SetFromTimestamp($oStamp.GetAsString())
If (($bValidDate <> True) Or ($oNow.DiffSeconds($oIssue) < 0)) Then
ConsoleWrite("Select a draft with an issue date of today or earlier." & @CRLF)
Exit
EndIf
$oBody = ObjCreate("Chilkat.JsonObject")
$oBody.UpdateBool("send_to_recipient",False)
$oBody.UpdateBool("send_to_invoicer",False)
$bSuccess = $oHttp.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send",$oBody,"application/json",$oResp)
If ($bSuccess <> True) Then
ConsoleWrite("No HTTP response: " & $oHttp.LastErrorText & @CRLF)
Exit
EndIf
If (($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
$oSendError = ObjCreate("Chilkat.JsonObject")
$oSendError.Load($oResp.BodyStr)
ConsoleWrite("HTTP " & $oResp.StatusCode & " " & $oSendError.StringOf("name") & " " & $oSendError.StringOf("details[0].issue") & @CRLF)
ConsoleWrite("PayPal debug ID: " & $oSendError.StringOf("debug_id") & @CRLF)
Exit
EndIf
; Read back the invoice: acceptance of the send alone does not guarantee it is payable.
$bSuccess = $oHttp.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",$oResp)
If ($bSuccess <> True) Then
ConsoleWrite("No HTTP response: " & $oHttp.LastErrorText & @CRLF)
Exit
EndIf
If (($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
$oInvoiceError = ObjCreate("Chilkat.JsonObject")
$oInvoiceError.Load($oResp.BodyStr)
ConsoleWrite("HTTP " & $oResp.StatusCode & " " & $oInvoiceError.StringOf("name") & " " & $oInvoiceError.StringOf("details[0].issue") & @CRLF)
ConsoleWrite("PayPal debug ID: " & $oInvoiceError.StringOf("debug_id") & @CRLF)
Exit
EndIf
$oInvoice = ObjCreate("Chilkat.JsonObject")
Local $bParsedInvoice = $oInvoice.Load($oResp.BodyStr)
If ($bParsedInvoice <> True) Then
ConsoleWrite("Expected a JSON response; HTTP " & $oResp.StatusCode & @CRLF)
Exit
EndIf
If ((($oInvoice.StringOfEquals("status","SENT",True) = True) Or ($oInvoice.StringOfEquals("status","UNPAID",True) = True) Or ($oInvoice.StringOfEquals("status","SHARED",True) = True)) And ($oInvoice.HasMember("detail.metadata.recipient_view_url") = True)) Then
ConsoleWrite("Customer payment page: " & $oInvoice.StringOf("detail.metadata.recipient_view_url") & @CRLF)
Exit
EndIf
ConsoleWrite("Invoice is " & $oInvoice.StringOf("status") & ". Retrieve it again when payable." & @CRLF)