Sample code for 30+ languages & platforms
AutoIt Requires Chilkat v11.0.0+

PayPal: Create an itemized draft invoice

See more PayPal Examples

Bill a sandbox recipient for two inspections at USD 45 each, due in 30 days. Request the invoice representation and save its ID for sending. Creating the draft does not notify the customer.

PayPal documentation

Chilkat AutoIt Downloads

AutoIt
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")

;  Replace buyer@example.com with your sandbox buyer. PayPal uses the authenticated
;  merchant as the invoicer.
$oHttp.SetRequestHeader "Prefer","return=representation"
$oBody = ObjCreate("Chilkat.JsonObject")
$oBody.UpdateString("detail.currency_code","USD")
$oBody.UpdateString("detail.payment_term.term_type","NET_30")
$oBody.UpdateString("primary_recipients[0].billing_info.email_address","buyer@example.com")
$oBody.UpdateString("items[0].name","Equipment inspection")
$oBody.UpdateString("items[0].quantity","2")
$oBody.UpdateString("items[0].unit_amount.currency_code","USD")
$oBody.UpdateString("items[0].unit_amount.value","45.00")

$bSuccess = $oHttp.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/invoicing/invoices",$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
    $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 $bParsed = $oResult.Load($oResp.BodyStr)
If ($bParsed <> True) Then
    ConsoleWrite("Expected a JSON response; HTTP " & $oResp.StatusCode & @CRLF)
    Exit
EndIf

ConsoleWrite("ID: " & $oResult.StringOf("id") & @CRLF)
ConsoleWrite("Status: " & $oResult.StringOf("status") & @CRLF)
ConsoleWrite("Invoice number: " & $oResult.StringOf("detail.invoice_number") & @CRLF)
ConsoleWrite("Total: " & $oResult.StringOf("amount.value") & @CRLF)