Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
PayPal: Look up a subscription and its recent payments
See more PayPal Examples
Start with the PayPal subscription ID saved with the local customer. Display its status and next billing time, then list transactions for the previous 30 days with each gross amount and currency.
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,
Chilkat.CkDateTime;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
resp: THttpResponse;
errorInfo: TJsonObject;
result: TJsonObject;
parsedResult: Boolean;
now: TCkDateTime;
query: TJsonObject;
historyError: TJsonObject;
history: TJsonObject;
parsedHistory: Boolean;
i: Integer;
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;
WriteLn('Status: ' + result.StringOf('status'));
WriteLn('Next billing: ' + result.StringOf('billing_info.next_billing_time'));
now := TCkDateTime.Create;
now.SetFromCurrentSystemTime();
query := TJsonObject.Create;
query.UpdateString('end_time',now.GetAsTimestamp(False));
now.AddDays(-30);
query.UpdateString('start_time',now.GetAsTimestamp(False));
success := http.HttpParams('GET','https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}/transactions',query,resp);
if (success <> True) then
begin
WriteLn('No HTTP response: ' + http.LastErrorText);
Exit;
end;
if ((resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
begin
historyError := TJsonObject.Create;
historyError.Load(resp.BodyStr);
WriteLn('HTTP ' + resp.StatusCode + ' ' + historyError.StringOf('name') + ' ' + historyError.StringOf('details[0].issue'));
WriteLn('PayPal debug ID: ' + historyError.StringOf('debug_id'));
Exit;
end;
history := TJsonObject.Create;
parsedHistory := history.Load(resp.BodyStr);
if (parsedHistory <> True) then
begin
WriteLn('Expected a JSON response; HTTP ' + resp.StatusCode);
Exit;
end;
i := 0;
while (i < history.SizeOfArray('transactions')) do
begin
history.I := i;
WriteLn(history.StringOf('transactions[i].time') + ' ' + history.StringOf('transactions[i].id') + ' ' + history.StringOf('transactions[i].status') + ' '
+ history.StringOf('transactions[i].amount_with_breakdown.gross_amount.value') + ' ' + history.StringOf('transactions[i].amount_with_breakdown.gross_amount.currency_code'));
i := i + 1;
end;
http.Free;
resp.Free;
errorInfo.Free;
result.Free;
now.Free;
query.Free;
historyError.Free;
history.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.