SQL Server
SQL Server
UniPin Game List
See more UniPin Examples
Demonstrates how to send a POST with the hash_hmac(sha256,partnerid+timestamp+path,secretkey) authentication.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
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Implements the following CURL command:
-- curl --request POST 'https://dev-api.unipin.com/in-game-topup/list' \
-- --header 'partnerid: 587e3675-e0ed-4e9e-9b39-099b11498fdc' \
-- --header 'timestamp: 1566552295' \
-- --header 'path: in-game-topup/list' \
-- --header 'auth: 1d9f5e7aca9f3c14da7c957d6977447739877cebfc10fcf3682bd32da47a2bda' \
-- --header 'Content-Type: application/json'
-- Use the following online tool to generate HTTP code from a CURL command
-- Convert a cURL Command to HTTP Source Code
DECLARE @dt int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @dt OUT
EXEC sp_OAMethod @dt, 'SetFromCurrentSystemTime', @success OUT
DECLARE @timestamp nvarchar(4000)
EXEC sp_OAMethod @dt, 'GetAsUnixTimeStr', @timestamp OUT, 0
-- Change this to your actual partner ID.
DECLARE @partnerId nvarchar(4000)
SELECT @partnerId = '587e3675-e0ed-4e9e-9b39-099b11498fdc'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'path', 'in-game-topup/list'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'timestamp', @timestamp
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'partnerid', @partnerId
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
-- Calculate the auth header using hash_hmac(sha256,partnerid+timestamp+path,secretkey)
DECLARE @crypt int
EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT
EXEC sp_OASetProperty @crypt, 'MacAlgorithm', 'hmac'
EXEC sp_OASetProperty @crypt, 'HashAlgorithm', 'sha256'
EXEC sp_OASetProperty @crypt, 'EncodingMode', 'hexlower'
-- Change this to your actual secret key..
EXEC sp_OAMethod @crypt, 'SetMacKeyEncoded', @success OUT, 'wabc123asljdgadlgd3', 'us-ascii'
DECLARE @sbMacData int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbMacData OUT
EXEC sp_OAMethod @sbMacData, 'Append', @success OUT, @partnerId
EXEC sp_OAMethod @sbMacData, 'Append', @success OUT, @timestamp
EXEC sp_OAMethod @sbMacData, 'Append', @success OUT, 'in-game-topup/list'
DECLARE @auth nvarchar(4000)
EXEC sp_OAMethod @sbMacData, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @crypt, 'MacStringENC', @auth OUT, @sTmp0
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'auth', @auth
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'POST', 'https://dev-api.unipin.com/in-game-topup/list', @resp
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @dt
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @sbMacData
EXEC @hr = sp_OADestroy @resp
RETURN
END
DECLARE @sbResponseBody int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT
EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody
DECLARE @jResp int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT
EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
EXEC sp_OASetProperty @jResp, 'EmitCompact', 0
PRINT 'Response Body:'
EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
PRINT @sTmp0
DECLARE @respStatusCode int
EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT
PRINT 'Response Status Code = ' + @respStatusCode
IF @respStatusCode >= 400
BEGIN
PRINT 'Response Header:'
EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @dt
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @sbMacData
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
RETURN
END
-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)
-- {
-- "game_list": [
-- {
-- "game_category": "MLBB_ID",
-- "game_code": "MLBBD_ID",
-- "game_name": "Mobile Legends Diamonds",
-- "icon_url": "http://dev-backoffice.unipin.com/images/icon_direct_topup_games/1565343343-icon-1548659712-icon-Mobile legend 300x300 px.png",
-- "game_status": "active",
-- "updated_at": "2019-08-09 16:35:43"
-- },
-- {
-- "game_category": "MLBB_ID",
-- "game_code": "MLBBS_ID",
-- "game_name": "Mobile Legends Starlight Member",
-- "icon_url": "http://dev-backoffice.unipin.com/images/icon_direct_topup_games/1565343258-icon-1548659712-icon-Mobile legend 300x300 px.png",
-- "game_status": "active",
-- "updated_at": "2019-08-09 16:34:18"
-- },
-- ...
-- ...
-- ],
-- "status": 1,
-- "reason": "Successful"
-- }
-- Sample code for parsing the JSON response...
-- Use the following online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
DECLARE @game_category nvarchar(4000)
DECLARE @game_code nvarchar(4000)
DECLARE @game_name nvarchar(4000)
DECLARE @icon_url nvarchar(4000)
DECLARE @game_status nvarchar(4000)
DECLARE @updated_at nvarchar(4000)
DECLARE @status int
EXEC sp_OAMethod @jResp, 'IntOf', @status OUT, 'status'
DECLARE @reason nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @reason OUT, 'reason'
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'game_list'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
EXEC sp_OAMethod @jResp, 'StringOf', @game_category OUT, 'game_list[i].game_category'
EXEC sp_OAMethod @jResp, 'StringOf', @game_code OUT, 'game_list[i].game_code'
EXEC sp_OAMethod @jResp, 'StringOf', @game_name OUT, 'game_list[i].game_name'
EXEC sp_OAMethod @jResp, 'StringOf', @icon_url OUT, 'game_list[i].icon_url'
EXEC sp_OAMethod @jResp, 'StringOf', @game_status OUT, 'game_list[i].game_status'
EXEC sp_OAMethod @jResp, 'StringOf', @updated_at OUT, 'game_list[i].updated_at'
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @dt
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @sbMacData
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
END
GO