Sample code for 30+ languages & platforms
Delphi ActiveX Requires Chilkat v11.0.0+

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.

PayPal documentation

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
crypt: TChilkatCrypt2;
requestId: WideString;
body: TChilkatJsonObject;
errorInfo: TChilkatJsonObject;
result: TChilkatJsonObject;
parsed: Integer;

begin
success := 0;

//  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.
http := TChilkatHttp.Create(Self);
http.AuthToken := 'SANDBOX_ACCESS_TOKEN';
resp := TChilkatHttpResponse.Create(Self);

//  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.
crypt := TChilkatCrypt2.Create(Self);
requestId := crypt.GenerateUuid();
http.SetRequestHeader('PayPal-Request-Id',requestId);
http.SetRequestHeader('Prefer','return=representation');
body := TChilkatJsonObject.Create(Self);
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.ControlInterface,'application/json',resp.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add('No HTTP response: ' + http.LastErrorText);
    Exit;
  end;
if ((resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
  begin
    errorInfo := TChilkatJsonObject.Create(Self);
    errorInfo.Load(resp.BodyStr);
    Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ' ' + errorInfo.StringOf('name') + ' ' + errorInfo.StringOf('details[0].issue'));
    Memo1.Lines.Add('PayPal debug ID: ' + errorInfo.StringOf('debug_id'));
    Exit;
  end;

result := TChilkatJsonObject.Create(Self);
parsed := result.Load(resp.BodyStr);
if (parsed <> 1) then
  begin
    Memo1.Lines.Add('Expected a JSON response; HTTP ' + IntToStr(resp.StatusCode));
    Exit;
  end;
Memo1.Lines.Add('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.