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

C
#include <C_CkHttp.h>
#include <C_CkDateTime.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>

void ChilkatSample(void)
    {
    HCkHttp http;
    HCkDateTime now;
    const char *orderNumber;
    HCkJsonObject body;
    HCkHttpResponse resp;
    BOOL sent;
    HCkJsonObject reply;
    BOOL parsed;
    const char *reference;
    HCkJsonObject record;
    BOOL saved;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    http = CkHttp_Create();
    CkHttp_putRequireSslCertVerify(http,TRUE);

    CkHttp_SetRequestHeader(http,"Authorization","CHAT_2_PAY_API_KEY");
    CkHttp_SetRequestHeader(http,"Accept","application/json");
    now = CkDateTime_Create();
    CkDateTime_SetFromCurrentSystemTime(now);
    orderNumber = "ORD-1042";
    body = CkJsonObject_Create();
    CkJsonObject_UpdateString(body,"applicationId","CHAT_2_PAY_APPLICATION_ID");
    CkJsonObject_UpdateString(body,"channel","sms");
    CkJsonObject_UpdateString(body,"to","PHONE_NUMBER");
    CkJsonObject_UpdateString(body,"orderNumber",orderNumber);
    CkJsonObject_UpdateString(body,"timestamp",CkDateTime_getAsTimestamp(now,FALSE));
    CkJsonObject_UpdateString(body,"currency","USD");
    CkJsonObject_UpdateNumber(body,"totalAmount","49.95");
    resp = CkHttpResponse_Create();
    sent = CkHttp_HttpJson(http,"POST","https://chat2pay-service.clickatell.com/api/v2/chat-2-pay",body,"application/json",resp);
    if (CkHttpResponse_getStatusCode(resp) == 0) {
        printf("No HTTP response. Check this order in Chat 2 Pay before retrying.\n");
        CkHttp_Dispose(http);
        CkDateTime_Dispose(now);
        CkJsonObject_Dispose(body);
        CkHttpResponse_Dispose(resp);
        return;
    }

    reply = CkJsonObject_Create();
    parsed = CkJsonObject_Load(reply,CkHttpResponse_bodyStr(resp));
    if ((sent != TRUE) || (CkHttpResponse_getStatusCode(resp) != 201)) {
        printf("Payment-link request returned HTTP %d\n",CkHttpResponse_getStatusCode(resp));
        if (CkJsonObject_HasMember(reply,"message") == TRUE) {
            printf("%s\n",CkJsonObject_stringOf(reply,"message"));
        }

        CkHttp_Dispose(http);
        CkDateTime_Dispose(now);
        CkJsonObject_Dispose(body);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        return;
    }

    reference = CkJsonObject_stringOf(reply,"paymentLinkRef");
    if ((parsed != TRUE) || (CkJsonObject_JsonTypeOf(reply,"paymentLinkRef") != 1) || (reference == "")) {
        printf("Missing payment link reference. Check the transaction before retrying.\n");
        CkHttp_Dispose(http);
        CkDateTime_Dispose(now);
        CkJsonObject_Dispose(body);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        return;
    }

    record = CkJsonObject_Create();
    CkJsonObject_UpdateString(record,"orderNumber",orderNumber);
    CkJsonObject_UpdateString(record,"paymentLinkRef",reference);
    if (CkJsonObject_JsonTypeOf(reply,"transactionStatus") == 1) {
        CkJsonObject_UpdateString(record,"transactionStatus",CkJsonObject_stringOf(reply,"transactionStatus"));
    }

    saved = CkJsonObject_WriteFile(record,"order-1042-payment-link.json");
    if (saved != TRUE) {
        printf("Link already created. Save this reference with the order: %s\n",reference);
        CkHttp_Dispose(http);
        CkDateTime_Dispose(now);
        CkJsonObject_Dispose(body);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        CkJsonObject_Dispose(record);
        return;
    }

    printf("Saved the payment link reference for %s\n",orderNumber);


    CkHttp_Dispose(http);
    CkDateTime_Dispose(now);
    CkJsonObject_Dispose(body);
    CkHttpResponse_Dispose(resp);
    CkJsonObject_Dispose(reply);
    CkJsonObject_Dispose(record);

    }