viaklix.cpp

Back to Rude Socket Tutorials
// ProcessViaklix.cpp
// Example program to process ViaKlix credit card transaction
// Using Rudeserver Socket library
// COPYRIGHT 2007 Matt Flood
// All Rights Reserved

#include <rude/socket.h>

#include <string>
#include <iostream>

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 Viaklik account information
	//
	string viaklix_account_pin="SOMEPIN"; // Your VIAKLIX PIN
        string viaklix_account_merchant_id="SOMEMERCHANTID"; // Your VIAKLIX Merchant ID
	
	// ViaKlix Test Mode
	//  Set to uppercase TRUE for test mode
	//  Set to uppercase FALSE to go live
	//
	string testmode="TRUE"; 

	// Address/path of the viaklix.com services
	//
	string server_address="www.viaklix.com";
	string server_application_path="/process.asp";

		
	// 2. Build the query String
	//   In real-world app, you should url-encode anything that is not hard coded
	////////////////////////////

	string querystring = "ssl_pin=";
	querystring += viaklix_account_pin;
        querystring += "&ssl_merchant_id=";
        querystring += viaklix_account_merchant_id; 
        querystring += "&ssl_test_mode=";
        querystring += testmode;
        querystring += "&ssl_result_format=ASCII";
        querystring += "&ssl_show_form=FALSE";
	querystring += "&ssl_exp_date=";
	querystring += cardexpires;
	querystring += "&ssl_card_number=";
	querystring += cardnumber;
	querystring += "&ssl_description=";
	querystring += track;
	querystring += "&ssl_amount=";
	querystring += ordertotal;
	querystring += "&ssl_ship_to_first_name=";
	querystring += card_holder_firstname;
	querystring += "&ssl_ship_to_last_name=";
	querystring += card_holder_lastname;
	querystring += "&ssl_cvv2=present&ssl_cvv2cvc2=";
	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(server_address.c_str(), 443))
	{
		cerr << socket.getError() << "\n";
		return 1;
	}

	// Send the HTTP request
	//
	socket.sends("POST ");
	socket.sends(server_application_path.c_str());
	socket.sends(" HTTP/1.0\n");
	socket.sends("Host: ");
        socket.sends(server_address.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();
		
               
		int position = str_result.find("ssl_result=",0);
		if(position >= 0)
		{
			position += 11;
			if(str_result[position] == '0')
			{
				// YAY - Accepted
				cout << "Transaction Successful\n";
				return 0;
			}
			else
			{
				cerr << "Transaction Failed\n";

				position = str_result.find("ssl_result_message=", 0);
				if(position >=0 )
				{
					position += 19;
				
					string errorstr = str_result.substr(position, str_result.size());
					cerr << errorstr.c_str() << "\n";
					return 1;
				}
				else
				{
					cerr <<  "Could not determine error" << "\n";
					return 1;
				}
			}
		}
		else
		{
			cerr <<  result << "\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;
	}
}


Generated by GNU enscript 1.6.4.