Java Requires Chilkat v11.0.0+
Java
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 Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
public class ChilkatExample {
public static void main(String[] args) {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Http http = new Http();
http.setRequireSslCertVerify(true);
http.setRequestHeader("Authorization", "CHAT_2_PAY_API_KEY");
http.setRequestHeader("Accept", "application/json");
DateTime now = new DateTime();
now.setFromCurrentSystemTime();
String orderNumber = "ORD-1042";
JsonObject body = new JsonObject();
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");
HttpResponse resp = new HttpResponse();
boolean sent;
try {
http.httpJson("POST", "https://chat2pay-service.clickatell.com/api/v2/chat-2-pay", body, "application/json", resp);
sent = true;
} catch (ChilkatException e) {
sent = false;
}
if (resp.getStatusCode() == 0) {
System.out.println("No HTTP response. Check this order in Chat 2 Pay before retrying.");
return;
}
JsonObject reply = new JsonObject();
boolean parsed;
try {
reply.load(resp.getBodyStr());
parsed = true;
} catch (ChilkatException e) {
parsed = false;
}
if ((!sent) || (resp.getStatusCode() != 201)) {
System.out.println("Payment-link request returned HTTP " + resp.getStatusCode());
if (reply.hasMember("message")) {
System.out.println(reply.stringOf("message"));
}
return;
}
String reference = reply.stringOf("paymentLinkRef");
if ((!parsed) || ((reply.jsonTypeOf("paymentLinkRef") != 1) || ("".equals(reference)))) {
System.out.println("Missing payment link reference. Check the transaction before retrying.");
return;
}
JsonObject record_ = new JsonObject();
record_.updateString("orderNumber", orderNumber);
record_.updateString("paymentLinkRef", reference);
if (reply.jsonTypeOf("transactionStatus") == 1) {
record_.updateString("transactionStatus", reply.stringOf("transactionStatus"));
}
try {
record_.writeFile("order-1042-payment-link.json");
} catch (ChilkatException e) {
System.out.println("Link already created. Save this reference with the order: " + reference);
return;
}
System.out.println("Saved the payment link reference for " + orderNumber);
}
}