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

Perl
use strict;
use warnings;
use Chilkat2;

# Assumes Chilkat is unlocked. See Global Unlock Sample.
my $http = Chilkat2::Http->new;
$http->RequireSslCertVerify(1);

$http->SetRequestHeader('Authorization', 'CHAT_2_PAY_API_KEY');
$http->SetRequestHeader('Accept', 'application/json');
my $now = Chilkat2::DateTime->new;
$now->SetFromCurrentSystemTime();
my $orderNumber = 'ORD-1042';
my $body = Chilkat2::JsonObject->new;
$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(0));
$body->UpdateString('currency', 'USD');
$body->UpdateNumber('totalAmount', '49.95');
my $resp = Chilkat2::HttpResponse->new;
my $sent = $http->HttpJson('POST', 'https://chat2pay-service.clickatell.com/api/v2/chat-2-pay', $body, 'application/json', $resp);
if ($resp->StatusCode == 0) {
    print "No HTTP response. Check this order in Chat 2 Pay before retrying.\n";
    exit;
}

my $reply = Chilkat2::JsonObject->new;
my $parsed = $reply->Load($resp->BodyStr);
if ((!$sent) || ($resp->StatusCode != 201)) {
    print 'Payment-link request returned HTTP ', $resp->StatusCode, "\n";
    if ($reply->HasMember('message')) {
        print $reply->StringOf('message'), "\n";
    }

    exit;
}

my $reference = $reply->StringOf('paymentLinkRef');
if ((!$parsed) || (($reply->JsonTypeOf('paymentLinkRef') != 1) || ($reference eq ''))) {
    print "Missing payment link reference. Check the transaction before retrying.\n";
    exit;
}

my $record = Chilkat2::JsonObject->new;
$record->UpdateString('orderNumber', $orderNumber);
$record->UpdateString('paymentLinkRef', $reference);
if ($reply->JsonTypeOf('transactionStatus') == 1) {
    $record->UpdateString('transactionStatus', $reply->StringOf('transactionStatus'));
}

my $saved = $record->WriteFile('order-1042-payment-link.json');
if (!$saved) {
    print 'Link already created. Save this reference with the order: ', $reference, "\n";
    exit;
}

print 'Saved the payment link reference for ', $orderNumber, "\n";