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

PayPal: Save a card during an approved checkout

See more PayPal Examples

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

For a first-time buyer who selected Save my card, request vaulting on a successful Card Fields checkout. Enable vaulting for your merchant. The create response returns an order ID; the payment token arrives after authorization or capture. If the buyer declines saving, omit the vault object.

PayPal documentation

Chilkat VB.NET Downloads

VB.NET
Dim success As Boolean = False

'  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.
Dim http As New Chilkat.Http
http.AuthToken = "SANDBOX_ACCESS_TOKEN"
Dim resp As New 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.
Dim crypt As New Chilkat.Crypt2
Dim requestId As String = crypt.GenerateUuid()
http.SetRequestHeader("PayPal-Request-Id",requestId)
http.SetRequestHeader("Prefer","return=representation")
Dim body As New Chilkat.JsonObject
body.UpdateString("intent","CAPTURE")
body.UpdateString("purchase_units[0].amount.currency_code","USD")
body.UpdateString("purchase_units[0].amount.value","30.00")
body.UpdateString("payment_source.card.attributes.verification.method","SCA_WHEN_REQUIRED")
body.UpdateString("payment_source.card.attributes.vault.store_in_vault","ON_SUCCESS")

success = http.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/checkout/orders",body,"application/json",resp)
If (success <> True) Then
    Debug.WriteLine("No HTTP response: " & http.LastErrorText)
    Exit Sub
End If

If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    Dim errorInfo As New Chilkat.JsonObject
    errorInfo.Load(resp.BodyStr)
    Debug.WriteLine("HTTP " & resp.StatusCode & " " & errorInfo.StringOf("name") & " " & errorInfo.StringOf("details[0].issue"))
    Debug.WriteLine("PayPal debug ID: " & errorInfo.StringOf("debug_id"))
    Exit Sub
End If


Dim result As New Chilkat.JsonObject
Dim parsed As Boolean = result.Load(resp.BodyStr)
If (parsed <> True) Then
    Debug.WriteLine("Expected a JSON response; HTTP " & resp.StatusCode)
    Exit Sub
End If

Debug.WriteLine("Order ID: " & result.StringOf("id"))
'  Return this order ID to the Card Fields createOrder callback. When PayPal signals
'  approval, your backend captures the corresponding order.
'  See Capture the approved order and read the
'  vault result.