Android™ Requires Chilkat v11.0.0+
Android™
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 Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkHttp http = new CkHttp();
http.put_RequireSslCertVerify(true);
http.SetRequestHeader("Authorization","CHAT_2_PAY_API_KEY");
http.SetRequestHeader("Accept","application/json");
CkDateTime now = new CkDateTime();
now.SetFromCurrentSystemTime();
String orderNumber = "ORD-1042";
CkJsonObject body = new CkJsonObject();
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");
CkHttpResponse resp = new CkHttpResponse();
boolean sent = http.HttpJson("POST","https://chat2pay-service.clickatell.com/api/v2/chat-2-pay",body,"application/json",resp);
if (resp.get_StatusCode() == 0) {
Log.i(TAG, "No HTTP response. Check this order in Chat 2 Pay before retrying.");
return;
}
CkJsonObject reply = new CkJsonObject();
boolean parsed = reply.Load(resp.bodyStr());
if ((sent != true) or (resp.get_StatusCode() != 201)) {
Log.i(TAG, "Payment-link request returned HTTP " + String.valueOf(resp.get_StatusCode()));
if (reply.HasMember("message") == true) {
Log.i(TAG, reply.stringOf("message"));
}
return;
}
String reference = reply.stringOf("paymentLinkRef");
if ((parsed != true) or (reply.JsonTypeOf("paymentLinkRef") != 1) or (reference == "")) {
Log.i(TAG, "Missing payment link reference. Check the transaction before retrying.");
return;
}
CkJsonObject record = new CkJsonObject();
record.UpdateString("orderNumber",orderNumber);
record.UpdateString("paymentLinkRef",reference);
if (reply.JsonTypeOf("transactionStatus") == 1) {
record.UpdateString("transactionStatus",reply.stringOf("transactionStatus"));
}
boolean saved = record.WriteFile("order-1042-payment-link.json");
if (saved != true) {
Log.i(TAG, "Link already created. Save this reference with the order: " + reference);
return;
}
Log.i(TAG, "Saved the payment link reference for " + orderNumber);
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}