Swift Requires Chilkat v11.0.0+
Swift
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 Swift Downloads
func chilkatTest() {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let http = CkoHttp()!
http.requireSslCertVerify = true
http.setRequestHeader(name: "Authorization", value: "CHAT_2_PAY_API_KEY")
http.setRequestHeader(name: "Accept", value: "application/json")
let now = CkoDateTime()!
now.setFromCurrentSystemTime()
var orderNumber: String? = "ORD-1042"
let body = CkoJsonObject()!
body.updateString(jsonPath: "applicationId", value: "CHAT_2_PAY_APPLICATION_ID")
body.updateString(jsonPath: "channel", value: "sms")
body.updateString(jsonPath: "to", value: "PHONE_NUMBER")
body.updateString(jsonPath: "orderNumber", value: orderNumber)
body.updateString(jsonPath: "timestamp", value: now.get(asTimestamp: false))
body.updateString(jsonPath: "currency", value: "USD")
body.updateNumber(jsonPath: "totalAmount", numericStr: "49.95")
let resp = CkoHttpResponse()!
var sent: Bool = http.httpJson(verb: "POST", url: "https://chat2pay-service.clickatell.com/api/v2/chat-2-pay", json: body, contentType: "application/json", response: resp)
if resp.statusCode.intValue == 0 {
print("No HTTP response. Check this order in Chat 2 Pay before retrying.")
return
}
let reply = CkoJsonObject()!
var parsed: Bool = reply.load(json: resp.bodyStr)
if (sent != true) || (resp.statusCode.intValue != 201) {
print("Payment-link request returned HTTP \(resp.statusCode.intValue)")
if reply.hasMember(jsonPath: "message") == true {
print("\(reply.string(of: "message")!)")
}
return
}
var reference: String? = reply.string(of: "paymentLinkRef")
if (parsed != true) || (reply.jsonType(of: "paymentLinkRef").intValue != 1) || (reference == "") {
print("Missing payment link reference. Check the transaction before retrying.")
return
}
let record = CkoJsonObject()!
record.updateString(jsonPath: "orderNumber", value: orderNumber)
record.updateString(jsonPath: "paymentLinkRef", value: reference)
if reply.jsonType(of: "transactionStatus").intValue == 1 {
record.updateString(jsonPath: "transactionStatus", value: reply.string(of: "transactionStatus"))
}
var saved: Bool = record.writeFile(path: "order-1042-payment-link.json")
if saved != true {
print("Link already created. Save this reference with the order: \(reference!)")
return
}
print("Saved the payment link reference for \(orderNumber!)")
}