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

PowerBuilder
integer li_rc
oleobject loo_Http
oleobject loo_Now
string ls_OrderNumber
oleobject loo_Body
oleobject loo_Resp
long li_Sent
oleobject loo_Reply
long li_Parsed
string ls_Reference
oleobject loo_Record
long li_Saved

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Http.RequireSslCertVerify = 1

loo_Http.SetRequestHeader("Authorization","CHAT_2_PAY_API_KEY")
loo_Http.SetRequestHeader("Accept","application/json")
loo_Now = create oleobject
li_rc = loo_Now.ConnectToNewObject("Chilkat.CkDateTime")

loo_Now.SetFromCurrentSystemTime()
ls_OrderNumber = "ORD-1042"
loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")

loo_Body.UpdateString("applicationId","CHAT_2_PAY_APPLICATION_ID")
loo_Body.UpdateString("channel","sms")
loo_Body.UpdateString("to","PHONE_NUMBER")
loo_Body.UpdateString("orderNumber",ls_OrderNumber)
loo_Body.UpdateString("timestamp",loo_Now.GetAsTimestamp(0))
loo_Body.UpdateString("currency","USD")
loo_Body.UpdateNumber("totalAmount","49.95")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Sent = loo_Http.HttpJson("POST","https://chat2pay-service.clickatell.com/api/v2/chat-2-pay",loo_Body,"application/json",loo_Resp)
if loo_Resp.StatusCode = 0 then
    Write-Debug "No HTTP response. Check this order in Chat 2 Pay before retrying."
    destroy loo_Http
    destroy loo_Now
    destroy loo_Body
    destroy loo_Resp
    return
end if

loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")

li_Parsed = loo_Reply.Load(loo_Resp.BodyStr)
if (li_Sent <> 1) OR (loo_Resp.StatusCode <> 201) then
    Write-Debug "Payment-link request returned HTTP " + string(loo_Resp.StatusCode)
    if loo_Reply.HasMember("message") = 1 then
        Write-Debug loo_Reply.StringOf("message")
    end if

    destroy loo_Http
    destroy loo_Now
    destroy loo_Body
    destroy loo_Resp
    destroy loo_Reply
    return
end if

ls_Reference = loo_Reply.StringOf("paymentLinkRef")
if (li_Parsed <> 1) OR (loo_Reply.JsonTypeOf("paymentLinkRef") <> 1) OR (ls_Reference = "") then
    Write-Debug "Missing payment link reference. Check the transaction before retrying."
    destroy loo_Http
    destroy loo_Now
    destroy loo_Body
    destroy loo_Resp
    destroy loo_Reply
    return
end if

loo_Record = create oleobject
li_rc = loo_Record.ConnectToNewObject("Chilkat.JsonObject")

loo_Record.UpdateString("orderNumber",ls_OrderNumber)
loo_Record.UpdateString("paymentLinkRef",ls_Reference)
if loo_Reply.JsonTypeOf("transactionStatus") = 1 then
    loo_Record.UpdateString("transactionStatus",loo_Reply.StringOf("transactionStatus"))
end if

li_Saved = loo_Record.WriteFile("order-1042-payment-link.json")
if li_Saved <> 1 then
    Write-Debug "Link already created. Save this reference with the order: " + ls_Reference
    destroy loo_Http
    destroy loo_Now
    destroy loo_Body
    destroy loo_Resp
    destroy loo_Reply
    destroy loo_Record
    return
end if

Write-Debug "Saved the payment link reference for " + ls_OrderNumber


destroy loo_Http
destroy loo_Now
destroy loo_Body
destroy loo_Resp
destroy loo_Reply
destroy loo_Record