Dart Requires Chilkat v11.0.0+
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
final http = CkHttp();
http.requireSslCertVerify = true;
http.setRequestHeader('Authorization', 'CHAT_2_PAY_API_KEY');
http.setRequestHeader('Accept', 'application/json');
final now = CkDateTime();
now.setFromCurrentSystemTime();
final orderNumber = 'ORD-1042';
final body = 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');
final resp = CkHttpResponse();
var sent = true;
try {
http.httpJson('POST', 'https://chat2pay-service.clickatell.com/api/v2/chat-2-pay', body, 'application/json', resp);
} on ChilkatException {
sent = false;
}
if (resp.statusCode == 0) {
print('No HTTP response. Check this order in Chat 2 Pay before retrying.');
return;
}
final reply = CkJsonObject();
var parsed = true;
try {
reply.load(resp.bodyStr);
} on ChilkatException {
parsed = false;
}
if ((!sent) || (resp.statusCode != 201)) {
print('Payment-link request returned HTTP ${resp.statusCode}');
if (reply.hasMember('message')) {
print(reply.stringOf('message'));
}
return;
}
final reference = reply.stringOf('paymentLinkRef');
if ((!parsed) || ((reply.jsonTypeOf('paymentLinkRef') != 1) || (reference == ''))) {
print('Missing payment link reference. Check the transaction before retrying.');
return;
}
final record = CkJsonObject();
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');
} on ChilkatException {
print('Link already created. Save this reference with the order: $reference');
return;
}
print('Saved the payment link reference for $orderNumber');
}