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

PowerShell
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
$http = New-Object Chilkat.Http
$http.RequireSslCertVerify = $true

$http.SetRequestHeader("Authorization","CHAT_2_PAY_API_KEY")
$http.SetRequestHeader("Accept","application/json")
$now = New-Object Chilkat.CkDateTime
$now.SetFromCurrentSystemTime()
$orderNumber = "ORD-1042"
$body = New-Object Chilkat.JsonObject
$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 = New-Object Chilkat.HttpResponse
$sent = $http.HttpJson("POST","https://chat2pay-service.clickatell.com/api/v2/chat-2-pay",$body,"application/json",$resp)
if ($resp.StatusCode -eq 0) {
    $("No HTTP response. Check this order in Chat 2 Pay before retrying.")
    exit
}

$reply = New-Object Chilkat.JsonObject
$parsed = $reply.Load($resp.BodyStr)
if (($sent -ne $true) -or ($resp.StatusCode -ne 201)) {
    $("Payment-link request returned HTTP " + $resp.StatusCode)
    if ($reply.HasMember("message") -eq $true) {
        $($reply.StringOf("message"))
    }

    exit
}

$reference = $reply.StringOf("paymentLinkRef")
if (($parsed -ne $true) -or ($reply.JsonTypeOf("paymentLinkRef") -ne 1) -or ($reference -eq "")) {
    $("Missing payment link reference. Check the transaction before retrying.")
    exit
}

$record = New-Object Chilkat.JsonObject
$record.UpdateString("orderNumber",$orderNumber)
$record.UpdateString("paymentLinkRef",$reference)
if ($reply.JsonTypeOf("transactionStatus") -eq 1) {
    $record.UpdateString("transactionStatus",$reply.StringOf("transactionStatus"))
}

$saved = $record.WriteFile("order-1042-payment-link.json")
if ($saved -ne $true) {
    $("Link already created. Save this reference with the order: " + $reference)
    exit
}

$("Saved the payment link reference for " + $orderNumber)