Delphi ActiveX Requires Chilkat v11.0.0+
Delphi ActiveX
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.
Chilkat Delphi ActiveX Downloads
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');
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.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.
// See Capture the approved order and read the
// vault result.