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

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true

http:set_request_header("Authorization", "CHAT_2_PAY_API_KEY")
http:set_request_header("Accept", "application/json")
local now = chilkat.DateTime.new()
now:set_from_current_system_time()
local orderNumber = "ORD-1042"
local body = chilkat.JsonObject.new()
body:update_string("applicationId", "CHAT_2_PAY_APPLICATION_ID")
body:update_string("channel", "sms")
body:update_string("to", "PHONE_NUMBER")
body:update_string("orderNumber", orderNumber)
body:update_string("timestamp", now:get_as_timestamp(false))
body:update_string("currency", "USD")
body:update_number("totalAmount", "49.95")
local resp = chilkat.HttpResponse.new()
local sent = http:http_json("POST", "https://chat2pay-service.clickatell.com/api/v2/chat-2-pay", body, "application/json", resp)
if resp.status_code == 0 then
    print("No HTTP response. Check this order in Chat 2 Pay before retrying.")
    return
end

local reply = chilkat.JsonObject.new()
local parsed = reply:load(resp.body_str)
if (not sent) or (resp.status_code ~= 201) then
    print("Payment-link request returned HTTP " .. resp.status_code)
    if reply:has_member("message") then
        print(reply:string_of("message"))
    end

    return
end

local reference = reply:string_of("paymentLinkRef")
if (not parsed) or ((reply:json_type_of("paymentLinkRef") ~= 1) or (reference == "")) then
    print("Missing payment link reference. Check the transaction before retrying.")
    return
end

local record = chilkat.JsonObject.new()
record:update_string("orderNumber", orderNumber)
record:update_string("paymentLinkRef", reference)
if reply:json_type_of("transactionStatus") == 1 then
    record:update_string("transactionStatus", reply:string_of("transactionStatus"))
end

local saved = record:write_file("order-1042-payment-link.json")
if not saved then
    print("Link already created. Save this reference with the order: " .. reference)
    return
end

print("Saved the payment link reference for " .. orderNumber)