Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.0.0+

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.

Clickatell API reference

Chilkat Delphi DLL Downloads

Delphi DLL
var
http: HCkHttp;
now: HCkDateTime;
orderNumber: PWideChar;
body: HCkJsonObject;
resp: HCkHttpResponse;
sent: Boolean;
reply: HCkJsonObject;
parsed: Boolean;
reference: PWideChar;
record: HCkJsonObject;
saved: Boolean;

begin
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);

CkHttp_SetRequestHeader(http,'Authorization','CHAT_2_PAY_API_KEY');
CkHttp_SetRequestHeader(http,'Accept','application/json');
now := CkDateTime_Create();
CkDateTime_SetFromCurrentSystemTime(now);
orderNumber := 'ORD-1042';
body := CkJsonObject_Create();
CkJsonObject_UpdateString(body,'applicationId','CHAT_2_PAY_APPLICATION_ID');
CkJsonObject_UpdateString(body,'channel','sms');
CkJsonObject_UpdateString(body,'to','PHONE_NUMBER');
CkJsonObject_UpdateString(body,'orderNumber',orderNumber);
CkJsonObject_UpdateString(body,'timestamp',CkDateTime__getAsTimestamp(now,False));
CkJsonObject_UpdateString(body,'currency','USD');
CkJsonObject_UpdateNumber(body,'totalAmount','49.95');
resp := CkHttpResponse_Create();
sent := CkHttp_HttpJson(http,'POST','https://chat2pay-service.clickatell.com/api/v2/chat-2-pay',body,'application/json',resp);
if (CkHttpResponse_getStatusCode(resp) = 0) then
  begin
    Memo1.Lines.Add('No HTTP response. Check this order in Chat 2 Pay before retrying.');
    Exit;
  end;
reply := CkJsonObject_Create();
parsed := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
if ((sent <> True) or (CkHttpResponse_getStatusCode(resp) <> 201)) then
  begin
    Memo1.Lines.Add('Payment-link request returned HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
    if (CkJsonObject_HasMember(reply,'message') = True) then
      begin
        Memo1.Lines.Add(CkJsonObject__stringOf(reply,'message'));
      end;
    Exit;
  end;
reference := CkJsonObject__stringOf(reply,'paymentLinkRef');
if ((parsed <> True) or (CkJsonObject_JsonTypeOf(reply,'paymentLinkRef') <> 1) or (reference = '')) then
  begin
    Memo1.Lines.Add('Missing payment link reference. Check the transaction before retrying.');
    Exit;
  end;
record := CkJsonObject_Create();
CkJsonObject_UpdateString(record,'orderNumber',orderNumber);
CkJsonObject_UpdateString(record,'paymentLinkRef',reference);
if (CkJsonObject_JsonTypeOf(reply,'transactionStatus') = 1) then
  begin
    CkJsonObject_UpdateString(record,'transactionStatus',CkJsonObject__stringOf(reply,'transactionStatus'));
  end;
saved := CkJsonObject_WriteFile(record,'order-1042-payment-link.json');
if (saved <> True) then
  begin
    Memo1.Lines.Add('Link already created. Save this reference with the order: ' + reference);
    Exit;
  end;
Memo1.Lines.Add('Saved the payment link reference for ' + orderNumber);

CkHttp_Dispose(http);
CkDateTime_Dispose(now);
CkJsonObject_Dispose(body);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
CkJsonObject_Dispose(record);