// ProcessAuthorizeNet.cpp // Example program to process AUthorize.net credit card transaction // Using Rudeserver Socket library // COPYRIGHT 2007 Matt Flood // All Rights Reserved #include #include #include using namespace std; using namespace rude; int main(void) { // 1. Configure the data we need to form the request // In real-world applications, you will want to untaint and url-encode // this information if it is obtained from another source (eg. from a CGI application) // ///////////////////////// // Order information // string track = "1000"; // Internal tracking number eg. the invoice number string ordertotal = "6.34"; // Total to charge the credit card // Credit Card information // string cardnumber = "5123456789012346"; // Credit Card Number string cardexpires = "0307"; // Credit Card Expiration Date (MMYY) string card_securitycode="123"; // Credit Card CVV2 Code string card_holder_firstname="Buck"; // string card_holder_lastname="Rogers"; // You Authorize.net account information // string authnet_login="SOMELOGIN"; // Your Authorize.net login information string authnet_passwd="SOMEPASSWD"; // Your Authorize.net password // Authorize.net Test Mode // Set to uppercase TRUE for test mode // Set to uppercase FALSE to go live // string testmode="TRUE"; // Address/path of the authorize.net services // string authnet_server="secure.authorize.net"; string authnet_path="/gateway/transact.dll"; // 2. Build the query String // In real-world app, you should url-encode anything that is not hard coded //////////////////////////// string querystring = "x_version=3.1"; querystring += "&x_delim_data=True"; querystring += "&x_login="; querystring += authnet_login; querystring += "&x_password="; querystring += authnet_passwd; querystring += "&x_type=AUTH_CAPTURE"; querystring += "&X_TEST_REQUEST=TRUE"; querystring += "&x_exp_date="; querystring += cardexpires; querystring += "&x_card_num="; querystring += cardnumber; querystring += "&x_cust_id="; querystring += track; querystring += "&x_amount="; querystring += ordertotal; querystring += "&x_first_name="; querystring += card_holder_firstname; querystring += "&x_last_name="; querystring += card_holder_lastname; querystring += "&x_card_code="; querystring += card_securitycode; // We will need the size of the querystring as a string (not an int) // when building the HTTP request // char querystring_length[20]; sprintf(querystring_length, "%d", querystring.size()); // 3. Use the Rude Socket Object to send the secure request and receive the response ////////////////////////// // Create socket object // Socket socket; // Connect to remote server using SSL // if(!socket.connectSSL(authnet_server.c_str(), 443)) { cerr << socket.getError() << "\n"; return 1; } // Send the HTTP request // socket.sends("POST "); socket.sends(authnet_path.c_str()); socket.sends(" HTTP/1.0\n"); socket.sends("Host: "); socket.sends(authnet_server.c_str()); socket.sends("\n"); socket.sends("Content-Type: application/x-www-form-urlencoded\n"); socket.sends("Content-Length: "); socket.sends(querystring_length); socket.sends("\n\n"); socket.sends(querystring.c_str()); // Read the Response // const char *result = socket.reads(); // NOTE: The response includes the HTTP response header if(result) { // convert result to a string so we can use c++ string functions to parse it // string str_result = result; // close the socket // socket.close(); // The existance of the following substring indicates that the card was accepted // int position = str_result.find("1,1,1",0); if(position >= 0) { cout << "Transaction was successful\n"; return 0; } else { // We need to get to the end of the headers, // Content-Length is usually the last one, so we look for it // position = str_result.find("Content-Length:", 0); if(position < 0) { // We received an unexpected response // cerr << "Server Response did not include Content-Length field:\n"; cerr << result; cerr << "\n"; } // Reason for failure is in position 4 of a comma separated list // Look for 3 commas if( position = str_result.find(",", position) ) { if( position = str_result.find(",", (position+ 1)) ) { if ( position =str_result.find(",", (position + 1)) ) { // We are at the beginning of the error message // Find the end of the message by finding the comma that follows it // int end_position = str_result.find(",", (position + 1)); if(!end_position) { // Could not find trailing comma, use the end of the entire result instead // end_position = str_result.size() -1; } // Extract the error message // string reason = str_result.substr((position + 1), end_position - position - 1); // Display error message // cerr << reason << "\n"; return 1; } } } } } else { // report the error // cerr << socket.getError() << "\n"; // close the socket // socket.close(); // exit program with failure (non-zero) status // return 1; } }