Instantly share code, notes, and snippets.
Created
May 26, 2011 21:28
-
Save gsherman/994126 to your computer and use it in GitHub Desktop.
ClearBasic code that demonstrates use of credit card authorization via Authorize.net using their AIM method (http://developer.authorize.net)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
Const DEBUGGER = False | |
Const DELIMITER = "|" | |
Const API_KEY = "9gmCxBG6P8Us" | |
Const TRANSACTION_KEY = "7Knw35u9L5HFj427" | |
Dim keyNames as List | |
Dim keyValues as List | |
Declare Function GetHTTPResponse (url As String, method As String, data As String) as String | |
Declare Sub PrettyPrint(explanation as String, data as String) | |
Declare Sub AddToDictionary(key, value) | |
Declare Function BuildNameValueString(keyNames as List, keyValues as List) as String | |
Declare Function ParseResponseCode(code) as String | |
Declare Sub SetupData | |
Declare Function ParseResponseCode(code) as String | |
Declare Function parse_string(str_input as String, str_delim as String, int_count as Integer, lst_words as List) as Integer | |
Declare Sub PrintList(ListToPrint as List) | |
Sub main() | |
Dim response as String | |
Dim postData as String | |
Dim url as String | |
Dim functionResult as Integer | |
Dim num_response_data as Integer | |
Dim responseList as New List | |
Set keyNames = New List | |
Set keyValues = New List | |
url = "https://test.authorize.net/gateway/transact.dll" | |
Call SetupData | |
postData = BuildNameValueString(keyNames, keyValues) | |
If DEBUGGER Then Call PrettyPrint("postData", postData) | |
response = GetHTTPResponse (url,"POST", postData) | |
If DEBUGGER Then Call PrettyPrint("response", response) | |
functionResult = parse_string(response, DELIMITER, num_response_data, responseList) | |
If DEBUGGER Then Debug.Print "responseList:" | |
If DEBUGGER Then PrintList responseList | |
Debug.Print | |
Debug.Print "Result of Credit Card Transaction: " & ParseResponseCode(responseList.ItemByIndex(0)) | |
Debug.Print "Reason Text: " & responseList.ItemByIndex(3) | |
Debug.Print "Authorization Code: " & responseList.ItemByIndex(4) | |
Debug.Print "Amount: " & responseList.ItemByIndex(9) | |
Debug.Print "Card Type: " & responseList.ItemByIndex(51) | |
Debug.Print "Last 4 digits of Card: " & responseList.ItemByIndex(50) | |
Debug.Print | |
End Sub | |
Sub SetupData | |
AddToDictionary "x_login", API_KEY | |
AddToDictionary "x_tran_key", TRANSACTION_KEY | |
AddToDictionary "x_delim_data", "TRUE" | |
AddToDictionary "x_delim_char", DELIMITER | |
AddToDictionary "x_relay_response", "FALSE" | |
AddToDictionary "x_type", "AUTH_CAPTURE" | |
AddToDictionary "x_method", "CC" | |
AddToDictionary "x_card_num", "4007000000027" | |
AddToDictionary "x_exp_date", "0115" | |
AddToDictionary "x_amount", "29.97" | |
AddToDictionary "x_description", "Sample Transaction" | |
AddToDictionary "x_first_name", "John" | |
AddToDictionary "x_last_name", "Doe" | |
AddToDictionary "x_address", "1234 Street" | |
AddToDictionary "x_state", "WA" | |
AddToDictionary "x_zip", "98004" | |
End Sub | |
Function GetHTTPResponse (url As String, method As String, data As String) as String | |
Dim oHttp as Object | |
Set oHttp = CreateObject("microsoft.XmlHttp") | |
oHttp.open method, url, False | |
oHttp.send ByVal data | |
GetHTTPResponse = oHttp.ResponseText | |
End Function | |
'Print with blank lines on either side, for easier reading | |
Sub PrettyPrint(explanation as String, data as String) | |
Debug.Print | |
Debug.Print explanation & ":" | |
Debug.Print data | |
Debug.Print | |
End Sub | |
Sub AddToDictionary(key, value) | |
keyNames.AppendItem key | |
keyValues.AppendItem value | |
End Sub | |
Function BuildNameValueString(keyNames as List, keyValues as List) as String | |
Dim postData as String | |
Dim index as Integer | |
For index = 0 to keyNames.Count - 1 | |
postData = postData & keyNames.ItemByIndex(index) & "=" & keyValues.ItemByIndex(index) & "&" | |
Next index | |
postData = Left(postData,Len(postData)-1) 'remove the last ampersand | |
BuildNameValueString = postData | |
End Function | |
Function ParseResponseCode(code) as String | |
Dim s as String | |
Select Case code | |
Case 1 | |
s = "Approved" | |
Case 2 | |
s = "Declined" | |
Case 3 | |
s = "Error" | |
Case 4 | |
s = "Held for Review" | |
Case Else | |
s = "Unknown response code of " & cstr(code) | |
End Select | |
ParseResponseCode = s | |
End Function | |
Function parse_string(str_input as String, str_delim as String, int_count as Integer, lst_words as List) as Integer | |
Dim int_itemCnt as Integer | |
Dim str_word as String | |
Dim i as Integer | |
int_count = 0 | |
str_input = Trim$(str_input) | |
int_itemCnt = ItemCount(str_input, str_delim) | |
If Len(str_input) = 0 Then | |
parse_string = -1 | |
Else | |
parse_string = 0 | |
End If | |
For i = 1 to int_itemCnt | |
str_word = Trim$(Item$(str_input, i, i, str_delim)) | |
lst_words.AppendItem str_word | |
int_count = int_count + 1 | |
Next i | |
End Function | |
Sub PrintList(ListToPrint as List) | |
Dim index as Integer | |
For index = 0 to ListToPrint.Count - 1 | |
Debug.Print Cstr(index) & "=" & ListToPrint.ItemByIndex(index) | |
Next index | |
End Sub | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment