SQL Server Requires Chilkat v11.0.0+
SQL Server
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 SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Authorization', 'CHAT_2_PAY_API_KEY'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept', 'application/json'
DECLARE @now int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @now OUT
DECLARE @success int
EXEC sp_OAMethod @now, 'SetFromCurrentSystemTime', @success OUT
DECLARE @orderNumber nvarchar(4000)
SELECT @orderNumber = 'ORD-1042'
DECLARE @body int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @body OUT
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'applicationId', 'CHAT_2_PAY_APPLICATION_ID'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'channel', 'sms'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'to', 'PHONE_NUMBER'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'orderNumber', @orderNumber
EXEC sp_OAMethod @now, 'GetAsTimestamp', @sTmp0 OUT, 0
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'timestamp', @sTmp0
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'currency', 'USD'
EXEC sp_OAMethod @body, 'UpdateNumber', @success OUT, 'totalAmount', '49.95'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
DECLARE @sent int
EXEC sp_OAMethod @http, 'HttpJson', @sent OUT, 'POST', 'https://chat2pay-service.clickatell.com/api/v2/chat-2-pay', @body, 'application/json', @resp
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
PRINT 'No HTTP response. Check this order in Chat 2 Pay before retrying.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @resp
RETURN
END
DECLARE @reply int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @reply OUT
DECLARE @parsed int
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @reply, 'Load', @parsed OUT, @sTmp0
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF (@sent <> 1) or (@iTmp0 <> 201)
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'Payment-link request returned HTTP ' + @iTmp0
EXEC sp_OAMethod @reply, 'HasMember', @iTmp0 OUT, 'message'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'message'
PRINT @sTmp0
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
RETURN
END
DECLARE @reference nvarchar(4000)
EXEC sp_OAMethod @reply, 'StringOf', @reference OUT, 'paymentLinkRef'
EXEC sp_OAMethod @reply, 'JsonTypeOf', @iTmp0 OUT, 'paymentLinkRef'
IF (@parsed <> 1) or (@iTmp0 <> 1) or (@reference = '')
BEGIN
PRINT 'Missing payment link reference. Check the transaction before retrying.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
RETURN
END
DECLARE @record int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @record OUT
EXEC sp_OAMethod @record, 'UpdateString', @success OUT, 'orderNumber', @orderNumber
EXEC sp_OAMethod @record, 'UpdateString', @success OUT, 'paymentLinkRef', @reference
EXEC sp_OAMethod @reply, 'JsonTypeOf', @iTmp0 OUT, 'transactionStatus'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'transactionStatus'
EXEC sp_OAMethod @record, 'UpdateString', @success OUT, 'transactionStatus', @sTmp0
END
DECLARE @saved int
EXEC sp_OAMethod @record, 'WriteFile', @saved OUT, 'order-1042-payment-link.json'
IF @saved <> 1
BEGIN
PRINT 'Link already created. Save this reference with the order: ' + @reference
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
EXEC @hr = sp_OADestroy @record
RETURN
END
PRINT 'Saved the payment link reference for ' + @orderNumber
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
EXEC @hr = sp_OADestroy @record
END
GO