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

PayPal: Create an order for PayPal-hosted Card Fields

See more PayPal Examples

Web server required. Run this code in the merchant checkout backend. The browser calls that backend through PayPal Card Fields.

This is the server half of a card checkout. Render eligible PayPal Card Fields in the browser and have createOrder call your server. Derive the price from your cart; return only the order ID. The SDK handles card entry and any required buyer authentication before onApprove.

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

lnSuccess = 0

*  See Unlock Chilkat before making requests.
*  Web checkout backend: load the merchant token on the web server that serves the Card Fields checkout.
*  See Get a PayPal access token.
loHttp = CreateObject('Chilkat.Http')
loHttp.AuthToken = "SANDBOX_ACCESS_TOKEN"
loResp = CreateObject('Chilkat.HttpResponse')

*  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')
loBody.UpdateString("intent","CAPTURE")
loBody.UpdateString("purchase_units[0].amount.currency_code","USD")
loBody.UpdateString("purchase_units[0].amount.value","30.00")
loBody.UpdateString("payment_source.card.attributes.verification.method","SCA_WHEN_REQUIRED")

lnSuccess = loHttp.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/checkout/orders",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

? "Order ID: " + loResult.StringOf("id")
*  Return this order ID to the Card Fields createOrder callback. When PayPal signals
*  approval, your backend captures the corresponding order.

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