Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

PayPal: Update the price on a reusable payment link

See more PayPal Examples

Retrieve the simple USD inspection link from the create example. Check its item and currency, retain its configuration, and replace the price with USD 95. PUT sends the complete editable definition.

PayPal documentation

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  resp: THttpResponse;
  errorInfo: TJsonObject;
  result: TJsonObject;
  parsedResult: Boolean;
  updateError: TJsonObject;

begin
  success := False;

  //  See Unlock Chilkat before making requests.
  //  For the merchant's internal backoffice application. Load the sandbox token
  //  from the merchant's credential store. Keep it out of publicly distributed apps.
  //  See Get a PayPal access token.
  http := THttp.Create;
  http.AuthToken := 'SANDBOX_ACCESS_TOKEN';
  resp := THttpResponse.Create;

  http.SetUrlVar('id','PAYMENT_RESOURCE_ID');

  success := http.HttpNoBody('GET','https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{$id}',resp);
  if (success <> True) then
    begin
      WriteLn('No HTTP response: ' + http.LastErrorText);
      Exit;
    end;
  if ((resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
    begin
      errorInfo := TJsonObject.Create;
      errorInfo.Load(resp.BodyStr);
      WriteLn('HTTP ' + resp.StatusCode + ' ' + errorInfo.StringOf('name') + ' ' + errorInfo.StringOf('details[0].issue'));
      WriteLn('PayPal debug ID: ' + errorInfo.StringOf('debug_id'));
      Exit;
    end;

  result := TJsonObject.Create;
  parsedResult := result.Load(resp.BodyStr);
  if (parsedResult <> True) then
    begin
      WriteLn('Expected a JSON response; HTTP ' + resp.StatusCode);
      Exit;
    end;
  if ((result.SizeOfArray('line_items') <> 1) or (result.StringOfEquals('line_items[0].name','Annual inspection',True) <> True) or (result.StringOfEquals('line_items[0].unit_amount.currency_code','USD',True) <> True)) then
    begin
      WriteLn('Select the USD inspection link from your catalog.');
      Exit;
    end;
  //  Preserve line items, taxes, shipping and checkout settings from the retrieved link.
  //  Remove the resource metadata PayPal added to the GET response.
  if (result.HasMember('id') = True) then
    begin
      result.Delete('id');
    end;
  if (result.HasMember('create_time') = True) then
    begin
      result.Delete('create_time');
    end;
  if (result.HasMember('update_time') = True) then
    begin
      result.Delete('update_time');
    end;
  if (result.HasMember('status') = True) then
    begin
      result.Delete('status');
    end;
  if (result.HasMember('links') = True) then
    begin
      result.Delete('links');
    end;
  if (result.HasMember('payment_link') = True) then
    begin
      result.Delete('payment_link');
    end;
  result.UpdateString('line_items[0].unit_amount.value','95.00');
  success := http.HttpJson('PUT','https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{$id}',result,'application/json',resp);
  if (success <> True) then
    begin
      WriteLn('No HTTP response: ' + http.LastErrorText);
      Exit;
    end;
  if ((resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
    begin
      updateError := TJsonObject.Create;
      updateError.Load(resp.BodyStr);
      WriteLn('HTTP ' + resp.StatusCode + ' ' + updateError.StringOf('name') + ' ' + updateError.StringOf('details[0].issue'));
      WriteLn('PayPal debug ID: ' + updateError.StringOf('debug_id'));
      Exit;
    end;
  WriteLn('The inspection price is now USD 95.00.');


  http.Free;
  resp.Free;
      errorInfo.Free;
  result.Free;
      updateError.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.