Sample code for 30+ languages & platforms
Visual FoxPro 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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loHttp
LOCAL loResp
LOCAL loCrypt
LOCAL lcRequestId
LOCAL loBody
LOCAL loErrorInfo
LOCAL loResult
LOCAL lnParsed
LOCAL i
LOCAL j
LOCAL lcCardToken
LOCAL lcCustomerId

lnSuccess = 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.
loHttp = CreateObject('Chilkat.Http')
loHttp.AuthToken = "SANDBOX_ACCESS_TOKEN"
loResp = 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.
loHttp.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.
loCrypt = CreateObject('Chilkat.Crypt2')
lcRequestId = loCrypt.GenerateUuid()
loHttp.SetRequestHeader("PayPal-Request-Id",lcRequestId)
loHttp.SetRequestHeader("Prefer","return=representation")
loBody = CreateObject('Chilkat.JsonObject')

lnSuccess = loHttp.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/checkout/orders/{$id}/capture",loBody,"application/json",loResp)
IF (lnSuccess <> 1) THEN
    ? "No HTTP response: " + loHttp.LastErrorText
    RELEASE loHttp
    RELEASE loResp
    RELEASE loCrypt
    RELEASE loBody
    CANCEL
ENDIF

IF ((loResp.StatusCode < 200) OR (loResp.StatusCode >= 300)) THEN
    loErrorInfo = CreateObject('Chilkat.JsonObject')
    loErrorInfo.Load(loResp.BodyStr)
    ? "HTTP " + STR(loResp.StatusCode) + " " + loErrorInfo.StringOf("name") + " " + loErrorInfo.StringOf("details[0].issue")
    ? "PayPal debug ID: " + loErrorInfo.StringOf("debug_id")
    RELEASE loHttp
    RELEASE loResp
    RELEASE loCrypt
    RELEASE loBody
    RELEASE loErrorInfo
    CANCEL
ENDIF

loResult = CreateObject('Chilkat.JsonObject')
lnParsed = loResult.Load(loResp.BodyStr)
IF (lnParsed <> 1) THEN
    ? "Expected a JSON response; HTTP " + STR(loResp.StatusCode)
    RELEASE loHttp
    RELEASE loResp
    RELEASE loCrypt
    RELEASE loBody
    RELEASE loErrorInfo
    RELEASE loResult
    CANCEL
ENDIF

? "ID: " + loResult.StringOf("id")
? "Status: " + loResult.StringOf("status")
*  A purchase unit may have no captures yet, or more than one.
i = 0
DO WHILE (i < loResult.SizeOfArray("purchase_units"))
    loResult.I = i
    j = 0
    DO WHILE (j < loResult.SizeOfArray("purchase_units[i].payments.captures"))
        loResult.J = j
        ? "Unit: " + loResult.StringOf("purchase_units[i].reference_id") + "  capture: " + loResult.StringOf("purchase_units[i].payments.captures[j].id")
        ? "  " + loResult.StringOf("purchase_units[i].payments.captures[j].status") + "  " + loResult.StringOf("purchase_units[i].payments.captures[j].amount.value") + " " + loResult.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 (loResult.StringOfEquals("payment_source.card.attributes.vault.status","VAULTED",1)) THEN
    lcCardToken = loResult.StringOf("payment_source.card.attributes.vault.id")
    lcCustomerId = loResult.StringOf("payment_source.card.attributes.vault.customer.id")
    *  Store cardToken and customerId with this buyer in the merchant credential store.
ENDIF

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

RELEASE loHttp
RELEASE loResp
RELEASE loCrypt
RELEASE loBody
RELEASE loErrorInfo
RELEASE loResult