Unicode C++ Requires Chilkat v11.0.0+
Unicode C++
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 Unicode C++ Downloads
#include <CkHttpW.h>
#include <CkDateTimeW.h>
#include <CkJsonObjectW.h>
#include <CkHttpResponseW.h>
void ChilkatSample(void)
{
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkHttpW http;
http.put_RequireSslCertVerify(true);
http.SetRequestHeader(L"Authorization",L"CHAT_2_PAY_API_KEY");
http.SetRequestHeader(L"Accept",L"application/json");
CkDateTimeW now;
now.SetFromCurrentSystemTime();
const wchar_t *orderNumber = L"ORD-1042";
CkJsonObjectW body;
body.UpdateString(L"applicationId",L"CHAT_2_PAY_APPLICATION_ID");
body.UpdateString(L"channel",L"sms");
body.UpdateString(L"to",L"PHONE_NUMBER");
body.UpdateString(L"orderNumber",orderNumber);
body.UpdateString(L"timestamp",now.getAsTimestamp(false));
body.UpdateString(L"currency",L"USD");
body.UpdateNumber(L"totalAmount",L"49.95");
CkHttpResponseW resp;
bool sent = http.HttpJson(L"POST",L"https://chat2pay-service.clickatell.com/api/v2/chat-2-pay",body,L"application/json",resp);
if (resp.get_StatusCode() == 0) {
wprintf(L"No HTTP response. Check this order in Chat 2 Pay before retrying.\n");
return;
}
CkJsonObjectW reply;
bool parsed = reply.Load(resp.bodyStr());
if ((sent != true) || (resp.get_StatusCode() != 201)) {
wprintf(L"Payment-link request returned HTTP %d\n",resp.get_StatusCode());
if (reply.HasMember(L"message") == true) {
wprintf(L"%s\n",reply.stringOf(L"message"));
}
return;
}
const wchar_t *reference = reply.stringOf(L"paymentLinkRef");
if ((parsed != true) || (reply.JsonTypeOf(L"paymentLinkRef") != 1) || (reference == L"")) {
wprintf(L"Missing payment link reference. Check the transaction before retrying.\n");
return;
}
CkJsonObjectW record;
record.UpdateString(L"orderNumber",orderNumber);
record.UpdateString(L"paymentLinkRef",reference);
if (reply.JsonTypeOf(L"transactionStatus") == 1) {
record.UpdateString(L"transactionStatus",reply.stringOf(L"transactionStatus"));
}
bool saved = record.WriteFile(L"order-1042-payment-link.json");
if (saved != true) {
wprintf(L"Link already created. Save this reference with the order: %s\n",reference);
return;
}
wprintf(L"Saved the payment link reference for %s\n",orderNumber);
}