Sample code for 30+ languages & platforms
AutoIt 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 AutoIt Downloads

AutoIt
;  Assumes Chilkat is unlocked. See Global Unlock Sample.
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True

$oHttp.SetRequestHeader "Authorization","CHAT_2_PAY_API_KEY"
$oHttp.SetRequestHeader "Accept","application/json"
$oNow = ObjCreate("Chilkat.CkDateTime")
$oNow.SetFromCurrentSystemTime()
Local $sOrderNumber = "ORD-1042"
$oBody = ObjCreate("Chilkat.JsonObject")
$oBody.UpdateString("applicationId","CHAT_2_PAY_APPLICATION_ID")
$oBody.UpdateString("channel","sms")
$oBody.UpdateString("to","PHONE_NUMBER")
$oBody.UpdateString("orderNumber",$sOrderNumber)
$oBody.UpdateString("timestamp",$oNow.GetAsTimestamp(False))
$oBody.UpdateString("currency","USD")
$oBody.UpdateNumber("totalAmount","49.95")
$oResp = ObjCreate("Chilkat.HttpResponse")
Local $bSent = $oHttp.HttpJson("POST","https://chat2pay-service.clickatell.com/api/v2/chat-2-pay",$oBody,"application/json",$oResp)
If ($oResp.StatusCode = 0) Then
    ConsoleWrite("No HTTP response. Check this order in Chat 2 Pay before retrying." & @CRLF)
    Exit
EndIf

$oReply = ObjCreate("Chilkat.JsonObject")
Local $bParsed = $oReply.Load($oResp.BodyStr)
If (($bSent <> True) Or ($oResp.StatusCode <> 201)) Then
    ConsoleWrite("Payment-link request returned HTTP " & $oResp.StatusCode & @CRLF)
    If ($oReply.HasMember("message") = True) Then
        ConsoleWrite($oReply.StringOf("message") & @CRLF)
    EndIf

    Exit
EndIf

Local $sReference = $oReply.StringOf("paymentLinkRef")
If (($bParsed <> True) Or ($oReply.JsonTypeOf("paymentLinkRef") <> 1) Or ($sReference = "")) Then
    ConsoleWrite("Missing payment link reference. Check the transaction before retrying." & @CRLF)
    Exit
EndIf

$oRecord = ObjCreate("Chilkat.JsonObject")
$oRecord.UpdateString("orderNumber",$sOrderNumber)
$oRecord.UpdateString("paymentLinkRef",$sReference)
If ($oReply.JsonTypeOf("transactionStatus") = 1) Then
    $oRecord.UpdateString("transactionStatus",$oReply.StringOf("transactionStatus"))
EndIf

Local $bSaved = $oRecord.WriteFile("order-1042-payment-link.json")
If ($bSaved <> True) Then
    ConsoleWrite("Link already created. Save this reference with the order: " & $sReference & @CRLF)
    Exit
EndIf

ConsoleWrite("Saved the payment link reference for " & $sOrderNumber & @CRLF)