AutoIt Requires Chilkat v11.0.0+
AutoIt
PayPal: Authorize a buyer-approved order
See more PayPal Examples
Authorize the stored order after the buyer approves it. Save the returned authorization ID and amount for capture or void. An authorization reserves funds; it is not a capture.
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")
; Use the approved order created with intent AUTHORIZE.
$oHttp.SetUrlVar("id","ORDER_ID")
; Create and persist this key once per business operation. On a timeout,
; retry the same request with its saved key, not a newly generated one.
$oCrypt = ObjCreate("Chilkat.Crypt2")
Local $sRequestId = $oCrypt.GenerateUuid()
$oHttp.SetRequestHeader "PayPal-Request-Id",$sRequestId
$oHttp.SetRequestHeader "Prefer","return=representation"
$oBody = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oHttp.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/checkout/orders/{$id}/authorize",$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)
; A purchase unit may have no authorizations yet, or more than one.
Local $i = 0
While ($i < $oResult.SizeOfArray("purchase_units"))
$oResult.I = $i
Local $iJ = 0
While ($iJ < $oResult.SizeOfArray("purchase_units[i].payments.authorizations"))
$oResult.J = $iJ
ConsoleWrite("Unit: " & $oResult.StringOf("purchase_units[i].reference_id") & " authorization: " & $oResult.StringOf("purchase_units[i].payments.authorizations[j].id") & @CRLF)
ConsoleWrite(" " & $oResult.StringOf("purchase_units[i].payments.authorizations[j].status") & " " & $oResult.StringOf("purchase_units[i].payments.authorizations[j].amount.value") _
& " " & $oResult.StringOf("purchase_units[i].payments.authorizations[j].amount.currency_code") & @CRLF)
$iJ = $iJ + 1
Wend
$i = $i + 1
Wend