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

PayPal: Capture a buyer-approved order

See more PayPal Examples

After approval, match the order to the checkout in your merchant records. Read each capture ID and status. Fulfill only when the expected amount and currency are captured and the capture is COMPLETED.

PayPal documentation

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oResp
LOCAL oCrypt
LOCAL cRequestId
LOCAL oBody
LOCAL oErrorInfo
LOCAL oResult
LOCAL nParsed
LOCAL i
LOCAL j
LOCAL cCardToken
LOCAL cCustomerId

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

//  Use the order ID you stored before redirecting the buyer, associated with this cart
//  and session.

//  Use the approved order created with intent CAPTURE.
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 := CreateObject("Chilkat.Crypt2")
cRequestId := oCrypt:GenerateUuid()
oHttp:SetRequestHeader("PayPal-Request-Id", cRequestId)
oHttp:SetRequestHeader("Prefer", "return=representation")
oBody := CreateObject("Chilkat.JsonObject")

nSuccess := oHttp:HttpJson("POST", "https://api-m.sandbox.paypal.com/v2/checkout/orders/{$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")
//  A purchase unit may have no captures yet, or more than one.
i := 0
DO WHILE (i < oResult:SizeOfArray("purchase_units"))
    oResult:I := i
    j := 0
    DO WHILE (j < oResult:SizeOfArray("purchase_units[i].payments.captures"))
        oResult:J := j
        ? "Unit: " + oResult:StringOf("purchase_units[i].reference_id") + "  capture: " + oResult:StringOf("purchase_units[i].payments.captures[j].id")
        ? "  " + oResult:StringOf("purchase_units[i].payments.captures[j].status") + "  " + oResult:StringOf("purchase_units[i].payments.captures[j].amount.value") + " " + oResult:StringOf("purchase_units[i].payments.captures[j].amount.currency_code")
        j := j + 1
    ENDDO
    i := i + 1
ENDDO
//  When a buyer opted to save a card, the token may arrive separately from capture.
IF (oResult:StringOfEquals("payment_source.card.attributes.vault.status", "VAULTED", 1))
    cCardToken := oResult:StringOf("payment_source.card.attributes.vault.id")
    cCustomerId := oResult:StringOf("payment_source.card.attributes.vault.customer.id")
    //  Store cardToken and customerId with this buyer in the merchant credential store.
ENDIF

IF (oResult:StringOfEquals("payment_source.card.attributes.vault.status", "APPROVED", 1))
    ? "Await the verified VAULT.PAYMENT-TOKEN.CREATED webhook for the token."
ENDIF

oHttp:destroy()
oResp:destroy()
oCrypt:destroy()
oBody:destroy()
oErrorInfo:destroy()
oResult:destroy()