Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
PayPal: Suspend an active subscription
See more PayPal Examples
Pause future billing after checking that the subscription is active. Save the reason with the customer support record.
Chilkat Lazarus Pascal Downloads
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;
body: TJsonObject;
changeError: 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','SUBSCRIPTION_ID');
success := http.HttpNoBody('GET','https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$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.StringOfEquals('status','ACTIVE',True) <> True) then
begin
WriteLn('Subscription status is ' + result.StringOf('status') + '. No change made.');
Exit;
end;
body := TJsonObject.Create;
body.UpdateString('reason','Customer requested a billing pause.');
success := http.HttpJson('POST','https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}/suspend',body,'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
changeError := TJsonObject.Create;
changeError.Load(resp.BodyStr);
WriteLn('HTTP ' + resp.StatusCode + ' ' + changeError.StringOf('name') + ' ' + changeError.StringOf('details[0].issue'));
WriteLn('PayPal debug ID: ' + changeError.StringOf('debug_id'));
Exit;
end;
WriteLn('PayPal accepted the subscription change.');
http.Free;
resp.Free;
errorInfo.Free;
result.Free;
body.Free;
changeError.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.