Node.js Requires Chilkat v11.0.0+
Node.js
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.
Chilkat Node.js Downloads
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");
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.
}
chilkatExample();