Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
Clickatell Chat 2 Pay: Send a payment link for an order
See more Clickatell Examples
Use a configured Chat 2 Pay application and its own API key. Supply the application ID, an unpaid order, and a recipient on the configured SMS channel. Clickatell creates and sends the checkout link. HTTP 201 returns a paymentLinkRef that can be saved on the order. Keep the order unpaid pending confirmation of payment.
The example writes a local reference record for the sample order. After an uncertain network result, check the transaction in Clickatell before creating another link.
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.CkDateTime,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
http: THttp;
now: TCkDateTime;
orderNumber: string;
body: TJsonObject;
resp: THttpResponse;
sent: Boolean;
reply: TJsonObject;
parsed: Boolean;
reference: string;
record: TJsonObject;
saved: Boolean;
begin
// Assumes Chilkat is unlocked. See Global Unlock Sample.
http := THttp.Create;
http.RequireSslCertVerify := True;
http.SetRequestHeader('Authorization','CHAT_2_PAY_API_KEY');
http.SetRequestHeader('Accept','application/json');
now := TCkDateTime.Create;
now.SetFromCurrentSystemTime();
orderNumber := 'ORD-1042';
body := TJsonObject.Create;
body.UpdateString('applicationId','CHAT_2_PAY_APPLICATION_ID');
body.UpdateString('channel','sms');
body.UpdateString('to','PHONE_NUMBER');
body.UpdateString('orderNumber',orderNumber);
body.UpdateString('timestamp',now.GetAsTimestamp(False));
body.UpdateString('currency','USD');
body.UpdateNumber('totalAmount','49.95');
resp := THttpResponse.Create;
sent := http.HttpJson('POST','https://chat2pay-service.clickatell.com/api/v2/chat-2-pay',body,'application/json',resp);
if (resp.StatusCode = 0) then
begin
WriteLn('No HTTP response. Check this order in Chat 2 Pay before retrying.');
Exit;
end;
reply := TJsonObject.Create;
parsed := reply.Load(resp.BodyStr);
if ((sent <> True) or (resp.StatusCode <> 201)) then
begin
WriteLn('Payment-link request returned HTTP ' + resp.StatusCode);
if (reply.HasMember('message') = True) then
begin
WriteLn(reply.StringOf('message'));
end;
Exit;
end;
reference := reply.StringOf('paymentLinkRef');
if ((parsed <> True) or (reply.JsonTypeOf('paymentLinkRef') <> 1) or (reference = '')) then
begin
WriteLn('Missing payment link reference. Check the transaction before retrying.');
Exit;
end;
record := TJsonObject.Create;
record.UpdateString('orderNumber',orderNumber);
record.UpdateString('paymentLinkRef',reference);
if (reply.JsonTypeOf('transactionStatus') = 1) then
begin
record.UpdateString('transactionStatus',reply.StringOf('transactionStatus'));
end;
saved := record.WriteFile('order-1042-payment-link.json');
if (saved <> True) then
begin
WriteLn('Link already created. Save this reference with the order: ' + reference);
Exit;
end;
WriteLn('Saved the payment link reference for ' + orderNumber);
http.Free;
now.Free;
body.Free;
resp.Free;
reply.Free;
record.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.