B4X Requires Chilkat v11.0.0+
B4X
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 B4X Downloads
' Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim http As ChilkatHttp
http.Initialize("http")
http.RequireSslCertVerify = True
http.SetRequestHeader("Authorization", "CHAT_2_PAY_API_KEY")
http.SetRequestHeader("Accept", "application/json")
Dim now As ChilkatDateTime
now.Initialize
now.SetFromCurrentSystemTime
Dim orderNumber As String = "ORD-1042"
Dim body As ChilkatJsonObject
body.Initialize
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")
Dim resp As ChilkatHttpResponse
resp.Initialize
Dim sent As Boolean = http.HttpJson("POST", "https://chat2pay-service.clickatell.com/api/v2/chat-2-pay", body, "application/json", resp)
If resp.StatusCode = 0 Then
Log("No HTTP response. Check this order in Chat 2 Pay before retrying.")
Return
End If
Dim reply As ChilkatJsonObject
reply.Initialize
Dim parsed As Boolean = reply.Load(resp.BodyStr)
If (sent <> True) Or (resp.StatusCode <> 201) Then
Log("Payment-link request returned HTTP " & resp.StatusCode)
If reply.HasMember("message") = True Then
Log(reply.StringOf("message"))
End If
Return
End If
Dim reference As String = reply.StringOf("paymentLinkRef")
If (parsed <> True) Or (reply.JsonTypeOf("paymentLinkRef") <> 1) Or (reference = "") Then
Log("Missing payment link reference. Check the transaction before retrying.")
Return
End If
Dim record As ChilkatJsonObject
record.Initialize
record.UpdateString("orderNumber", orderNumber)
record.UpdateString("paymentLinkRef", reference)
If reply.JsonTypeOf("transactionStatus") = 1 Then
record.UpdateString("transactionStatus", reply.StringOf("transactionStatus"))
End If
Dim saved As Boolean = record.WriteFile("order-1042-payment-link.json")
If saved <> True Then
Log("Link already created. Save this reference with the order: " & reference)
Return
End If
Log("Saved the payment link reference for " & orderNumber)