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

Rust
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);

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

let reply = chilkat::JsonObject::new();
let parsed = reply.load(&resp.body_str()).is_ok();
if (!sent) || (resp.status_code() != 201) {
    println!("Payment-link request returned HTTP {}", resp.status_code());
    if reply.has_member("message") {
        println!("{}", reply.string_of("message").unwrap_or_default());
    }

    return;
}

let reference = reply.string_of("paymentLinkRef").unwrap_or_default();
if (!parsed) || ((reply.json_type_of("paymentLinkRef") != 1) || (reference == "")) {
    println!("Missing payment link reference. Check the transaction before retrying.");
    return;
}

let record = chilkat::JsonObject::new();
let _ = record.update_string("orderNumber", &order_number);
let _ = record.update_string("paymentLinkRef", &reference);
if reply.json_type_of("transactionStatus") == 1 {
    let _ = record.update_string("transactionStatus", &reply.string_of("transactionStatus").unwrap_or_default());
}

if record.write_file("order-1042-payment-link.json").is_err() {
    println!("Link already created. Save this reference with the order: {}", reference);
    return;
}

println!("Saved the payment link reference for {}", order_number);