Xbase++ Requires Chilkat v11.0.0+
Xbase++
PayPal: Partially capture an authorization
See more PayPal Examples
Capture USD 10 of the sample USD 30 authorization and leave the rest open. Multiple captures require merchant eligibility. Use a separate request ID for each intended capture.
Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oResp
LOCAL oCrypt
LOCAL cRequestId
LOCAL oBody
LOCAL oErrorInfo
LOCAL oResult
LOCAL nParsed
nSuccess := 0
// 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 := CreateObject("Chilkat.Http")
oHttp:AuthToken := "SANDBOX_ACCESS_TOKEN"
oResp := CreateObject("Chilkat.HttpResponse")
// Keep final_capture false if another shipment will be billed against this hold.
oHttp:SetUrlVar("id", "AUTHORIZATION_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 := CreateObject("Chilkat.Crypt2")
cRequestId := oCrypt:GenerateUuid()
oHttp:SetRequestHeader("PayPal-Request-Id", cRequestId)
oHttp:SetRequestHeader("Prefer", "return=representation")
oBody := CreateObject("Chilkat.JsonObject")
oBody:UpdateString("amount.currency_code", "USD")
oBody:UpdateString("amount.value", "10.00")
oBody:UpdateBool("final_capture", 0)
nSuccess := oHttp:HttpJson("POST", "https://api-m.sandbox.paypal.com/v2/payments/authorizations/{$id}/capture", oBody, "application/json", oResp)
IF (nSuccess != 1)
? "No HTTP response: " + oHttp:LastErrorText
oHttp:destroy()
oResp:destroy()
oCrypt:destroy()
oBody:destroy()
RETURN
ENDIF
IF ((oResp:StatusCode < 200) .OR. (oResp:StatusCode >= 300))
oErrorInfo := CreateObject("Chilkat.JsonObject")
oErrorInfo:Load(oResp:BodyStr)
? "HTTP " + Str(oResp:StatusCode) + " " + oErrorInfo:StringOf("name") + " " + oErrorInfo:StringOf("details[0].issue")
? "PayPal debug ID: " + oErrorInfo:StringOf("debug_id")
oHttp:destroy()
oResp:destroy()
oCrypt:destroy()
oBody:destroy()
oErrorInfo:destroy()
RETURN
ENDIF
oResult := CreateObject("Chilkat.JsonObject")
nParsed := oResult:Load(oResp:BodyStr)
IF (nParsed != 1)
? "Expected a JSON response; HTTP " + Str(oResp:StatusCode)
oHttp:destroy()
oResp:destroy()
oCrypt:destroy()
oBody:destroy()
oErrorInfo:destroy()
oResult:destroy()
RETURN
ENDIF
? "ID: " + oResult:StringOf("id")
? "Status: " + oResult:StringOf("status")
? "Amount: " + oResult:StringOf("amount.value")
? "Currency: " + oResult:StringOf("amount.currency_code")
oHttp:destroy()
oResp:destroy()
oCrypt:destroy()
oBody:destroy()
oErrorInfo:destroy()
oResult:destroy()