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

PayPal: Pay with a previously saved card

See more PayPal Examples

Buyer interaction. Your application uses a browser for buyer approval or authentication when PayPal requests it. Configure the merchant return and cancel URLs for this flow.

When a returning buyer selects their saved card, look up its vault ID under their authenticated local account. Send the order price and currency from your own cart. Creating the order can capture immediately; inspect each capture before fulfillment. Follow payer-action if further authentication is required.

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 lnLinkIndex
LOCAL i
LOCAL j

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

*  Resolve the saved token under this signed-in buyer. A vault ID from a different
*  customer must never be accepted from the browser.

*  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.vault_id","SAVED_CARD_TOKEN")
loBody.UpdateString("payment_source.card.stored_credential.payment_initiator","CUSTOMER")
loBody.UpdateString("payment_source.card.stored_credential.payment_type","ONE_TIME")
loBody.UpdateString("payment_source.card.stored_credential.usage","SUBSEQUENT")
loBody.UpdateString("payment_source.card.experience_context.return_url","https://merchant.example/paypal/return")
loBody.UpdateString("payment_source.card.experience_context.cancel_url","https://merchant.example/paypal/cancel")

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

? "ID: " + loResult.StringOf("id")
? "Status: " + loResult.StringOf("status")
*  Link order is not fixed. Send the buyer to the matching URL in a browser.
lnLinkIndex = 0
DO WHILE (lnLinkIndex < loResult.SizeOfArray("links"))
    loResult.I = lnLinkIndex
    IF (loResult.StringOfEquals("links[i].rel","payer-action",1)) THEN
        ? "Approval URL: " + loResult.StringOf("links[i].href")
    ENDIF

    lnLinkIndex = lnLinkIndex + 1
ENDDO
*  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

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