Objective-C Requires Chilkat v11.0.0+
Objective-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 Objective-C Downloads
#import <CkoHttp.h>
#import <CkoDateTime.h>
#import <NSString.h>
#import <CkoJsonObject.h>
#import <CkoHttpResponse.h>
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
[http SetRequestHeader: @"Authorization" value: @"CHAT_2_PAY_API_KEY"];
[http SetRequestHeader: @"Accept" value: @"application/json"];
CkoDateTime *now = [[CkoDateTime alloc] init];
[now SetFromCurrentSystemTime];
NSString *orderNumber = @"ORD-1042";
CkoJsonObject *body = [[CkoJsonObject alloc] init];
[body UpdateString: @"applicationId" value: @"CHAT_2_PAY_APPLICATION_ID"];
[body UpdateString: @"channel" value: @"sms"];
[body UpdateString: @"to" value: @"PHONE_NUMBER"];
[body UpdateString: @"orderNumber" value: orderNumber];
[body UpdateString: @"timestamp" value: [now GetAsTimestamp: NO]];
[body UpdateString: @"currency" value: @"USD"];
[body UpdateNumber: @"totalAmount" numericStr: @"49.95"];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
BOOL sent = [http HttpJson: @"POST" url: @"https://chat2pay-service.clickatell.com/api/v2/chat-2-pay" json: body contentType: @"application/json" response: resp];
if ([resp.StatusCode intValue] == 0) {
NSLog(@"%@",@"No HTTP response. Check this order in Chat 2 Pay before retrying.");
return;
}
CkoJsonObject *reply = [[CkoJsonObject alloc] init];
BOOL parsed = [reply Load: resp.BodyStr];
if ((sent != YES) || ([resp.StatusCode intValue] != 201)) {
NSLog(@"%@%d",@"Payment-link request returned HTTP ",[resp.StatusCode intValue]);
if ([reply HasMember: @"message"] == YES) {
NSLog(@"%@",[reply StringOf: @"message"]);
}
return;
}
NSString *reference = [reply StringOf: @"paymentLinkRef"];
if ((parsed != YES) || ([[reply JsonTypeOf: @"paymentLinkRef"] intValue] != 1) || (reference == @"")) {
NSLog(@"%@",@"Missing payment link reference. Check the transaction before retrying.");
return;
}
CkoJsonObject *record = [[CkoJsonObject alloc] init];
[record UpdateString: @"orderNumber" value: orderNumber];
[record UpdateString: @"paymentLinkRef" value: reference];
if ([[reply JsonTypeOf: @"transactionStatus"] intValue] == 1) {
[record UpdateString: @"transactionStatus" value: [reply StringOf: @"transactionStatus"]];
}
BOOL saved = [record WriteFile: @"order-1042-payment-link.json"];
if (saved != YES) {
NSLog(@"%@%@",@"Link already created. Save this reference with the order: ",reference);
return;
}
NSLog(@"%@%@",@"Saved the payment link reference for ",orderNumber);