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

AutoIt
Local $bSuccess = False

;  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 = ObjCreate("Chilkat.Http")
$oHttp.AuthToken = "SANDBOX_ACCESS_TOKEN"
$oResp = ObjCreate("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.
$oCrypt = ObjCreate("Chilkat.Crypt2")
Local $sRequestId = $oCrypt.GenerateUuid()
$oHttp.SetRequestHeader "PayPal-Request-Id",$sRequestId
$oHttp.SetRequestHeader "Prefer","return=representation"
$oBody = ObjCreate("Chilkat.JsonObject")
$oBody.UpdateString("intent","CAPTURE")
$oBody.UpdateString("purchase_units[0].amount.currency_code","USD")
$oBody.UpdateString("purchase_units[0].amount.value","30.00")
$oBody.UpdateString("payment_source.card.vault_id","SAVED_CARD_TOKEN")
$oBody.UpdateString("payment_source.card.stored_credential.payment_initiator","CUSTOMER")
$oBody.UpdateString("payment_source.card.stored_credential.payment_type","ONE_TIME")
$oBody.UpdateString("payment_source.card.stored_credential.usage","SUBSEQUENT")
$oBody.UpdateString("payment_source.card.experience_context.return_url","https://merchant.example/paypal/return")
$oBody.UpdateString("payment_source.card.experience_context.cancel_url","https://merchant.example/paypal/cancel")

$bSuccess = $oHttp.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/checkout/orders",$oBody,"application/json",$oResp)
If ($bSuccess <> True) Then
    ConsoleWrite("No HTTP response: " & $oHttp.LastErrorText & @CRLF)
    Exit
EndIf

If (($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
    $oErrorInfo = ObjCreate("Chilkat.JsonObject")
    $oErrorInfo.Load($oResp.BodyStr)
    ConsoleWrite("HTTP " & $oResp.StatusCode & " " & $oErrorInfo.StringOf("name") & " " & $oErrorInfo.StringOf("details[0].issue") & @CRLF)
    ConsoleWrite("PayPal debug ID: " & $oErrorInfo.StringOf("debug_id") & @CRLF)
    Exit
EndIf

$oResult = ObjCreate("Chilkat.JsonObject")
Local $bParsed = $oResult.Load($oResp.BodyStr)
If ($bParsed <> True) Then
    ConsoleWrite("Expected a JSON response; HTTP " & $oResp.StatusCode & @CRLF)
    Exit
EndIf

ConsoleWrite("ID: " & $oResult.StringOf("id") & @CRLF)
ConsoleWrite("Status: " & $oResult.StringOf("status") & @CRLF)
;  Link order is not fixed. Send the buyer to the matching URL in a browser.
Local $iLinkIndex = 0
While ($iLinkIndex < $oResult.SizeOfArray("links"))
    $oResult.I = $iLinkIndex
    If ($oResult.StringOfEquals("links[i].rel","payer-action",True)) Then
        ConsoleWrite("Approval URL: " & $oResult.StringOf("links[i].href") & @CRLF)
    EndIf

    $iLinkIndex = $iLinkIndex + 1
Wend
;  A purchase unit may have no captures yet, or more than one.
Local $i = 0
While ($i < $oResult.SizeOfArray("purchase_units"))
    $oResult.I = $i
Local $iJ = 0
    While ($iJ < $oResult.SizeOfArray("purchase_units[i].payments.captures"))
        $oResult.J = $iJ
        ConsoleWrite("Unit: " & $oResult.StringOf("purchase_units[i].reference_id") & "  capture: " & $oResult.StringOf("purchase_units[i].payments.captures[j].id") & @CRLF)
        ConsoleWrite("  " & $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") & @CRLF)
        $iJ = $iJ + 1
    Wend
    $i = $i + 1
Wend