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

Node.js
NODEJS_PRELUDE

function chilkatExample() {

    var success = 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.
    var http = new chilkat.Http();
    http.AuthToken = "SANDBOX_ACCESS_TOKEN";
    var resp = 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.
    var crypt = new chilkat.Crypt2();
    var requestId = crypt.GenerateUuid();
    http.SetRequestHeader("PayPal-Request-Id",requestId);
    http.SetRequestHeader("Prefer","return=representation");
    var body = 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) {
        console.log("No HTTP response: " + http.LastErrorText);
        return;
    }

    if ((resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
        var errorInfo = new chilkat.JsonObject();
        errorInfo.Load(resp.BodyStr);
        console.log("HTTP " + resp.StatusCode + " " + errorInfo.StringOf("name") + " " + errorInfo.StringOf("details[0].issue"));
        console.log("PayPal debug ID: " + errorInfo.StringOf("debug_id"));
        return;
    }

    var result = new chilkat.JsonObject();
    var parsed = result.Load(resp.BodyStr);
    if (parsed !== true) {
        console.log("Expected a JSON response; HTTP " + resp.StatusCode);
        return;
    }

    console.log("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.

}

chilkatExample();