// TestEftApi.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
//

#include "stdafx.h"

#include <stdio.h>
#ifdef WIN32
#include <conio.h>
#include <windows.h>
#include <process.h>
#endif
#include <string.h>
#include "eftapi.h"
#include "errors.h"
#include "DeviceControlCodes.h"

int _tmain(int argc, _TCHAR* argv[])
{
	int ch;
	int r;
	long nCount;
	long readerState = -1, newReaderState;
	long deviceState = -1, newDeviceState;
	long newActivationState;
	long cardsRetained;
	long ecrSequence;
	long lEftvalue;
	char sEftvalue[128];
	char msg[1000];
	char receipt[4000];
	char * transType;
	long amount;
	EFT_HANDLE h;

	r = EFT_CreateSession(&h);
	if (r != 0)
	{
		EFT_GetExceptionMessage(h, msg, sizeof(msg));
		printf("EFT_CreateSession returned error %d: %s\n", r, msg);
		goto by;
	}

	/* Connect to be shure, the driver is runnung */
	r = EFT_Connect(h);
	if (r != 0)
	{
		EFT_GetExceptionMessage(h, msg, sizeof(msg));
		printf("EFT_Connect returned error %d: %s\n", r, msg);
		goto by;
	}
	printf("EFT_Connected\n\n");

retryread0:
	printf(
	"MPD EFTAPI Test Programm\n"
	"o) Open\n"
	"t) Transaction with SalesItems\n"
	"z) Preauthorisation with Infos\n"
	"u) Transaction with proceed\n"
	"0) Exit\n"
	"\n"
	);

retryread:
	printf ("\nOperation ? \n");

retryread2:
	ch = _getch();
	if (ch == 10) goto retryread2;
	printf ("\n");

	switch(ch)
	{
	case 'o': /* open */
		printf("opening EFT\n");
		r = EFT_Open(h);
		if (r != 0)
		{
			/* in the language, set in the mpd */
			EFT_GetExceptionMessage(h, msg, sizeof(msg));
			printf("EFT_Open returned error %d: %s\n", r, msg);

			/* in the language, set here */
			const char * t = 0;
			EFT_GetErrorText(h, "de", r, &t);
			printf("EFT_Open gibt Fehler %d: %s\n", r, t);

			break;
		}
		EFT_GetECRSequence(h, &ecrSequence);
		printf("EFT is open, ecr-sequence: %d\n", ecrSequence);
		break;

	case 't':/* transaction  */
		printf("start transaction with SalesItems\n");

		/* ProductCodeScheme */
		EFT_WriteProperty(h, ptIFSF, peIFSFProductCodeScheme, "99");

		/* SalesItem 100 */
		EFT_WriteProperty(h, ptIFSF, peIFSFSalesItemId, "a001");
		EFT_WriteProperty(h, ptIFSF, peIFSFProductCode, "100");
		EFT_WriteProperty(h, ptIFSF, peIFSFSalesItemAmount, "98.99");
		EFT_WriteProperty(h, ptIFSF, peIFSFUnitMeasure, "L");
		EFT_WriteProperty(h, ptIFSF, peIFSFUnitPrice, "1.62");
		EFT_WriteProperty(h, ptIFSF, peIFSFQuantity, "61.11");
		EFT_WriteProperty(h, ptIFSF, peIFSFAdditionalProductCode, "Fuel");

		/* SalesItem 200 */
		EFT_WriteProperty(h, ptIFSF, peIFSFSalesItemId, "a002");
		EFT_WriteProperty(h, ptIFSF, peIFSFProductCode, "200");
		EFT_WriteProperty(h, ptIFSF, peIFSFSalesItemAmount, "2.20");
		EFT_WriteProperty(h, ptIFSF, peIFSFUnitPrice, "2.20");
		EFT_WriteProperty(h, ptIFSF, peIFSFQuantity, "1");
		EFT_WriteProperty(h, ptIFSF, peIFSFTaxCode, "2");
		EFT_WriteProperty(h, ptIFSF, peIFSFAdditionalProductCode, "Service/Waren");

		EFT_PutProceedNeeded(h, 0);  /* to clear an older setting */
		r = EFT_Transaction(h, "debit", 200, 0);
		if (r != 0)
		{
			EFT_GetExceptionMessage(h, msg, sizeof(msg));
			printf("EFT_Transaction returned error %d: %s\n", r, msg);
			if (r != EFT_E_AUTH_DENIED)
			{
				/* see if restriction codes */
				r = EFT_ReadProperty(h, ptIFSF, peIFSFRestrictionCodes, msg, sizeof(msg));
				if (r == 0)
				{
					if (strlen(msg) > 0)
						printf("RestrictionCodes: %s\n", msg);
				}
			}
			break;
		}
		printf("Trx is ok -> Commit\n");
		r = EFT_Commit(h, true);
		if (r != 0)
		{
			EFT_GetExceptionMessage(h, msg, sizeof(msg));
			printf("EFT_Commit returned error %d: %s\n", r, msg);
			break;
		}
		break;

	case 'z':/* preauthorization  */
		printf("start preauthorization\n");

		EFT_PutProceedNeeded(h, 0);  /* to clear an older setting */
		r = EFT_Transaction(h, "preauthorization", 10000, 0);
		if (r != 0)
		{
			EFT_GetExceptionMessage(h, msg, sizeof(msg));
			printf("EFT_Transaction returned error %d: %s\n", r, msg);
			break;
		}
		printf("Trx is ok\n");
		/* see if restriction codes */
		r = EFT_ReadProperty(h, ptIFSF, peIFSFRestrictionCodes, msg, sizeof(msg));
		if (r == 0)
		{
			if (strlen(msg) > 0)
				printf("RestrictionCodes: %s\n", msg);
		}
		/* see if driver number */
		r = EFT_ReadProperty(h, ptIFSF, peIFSFDriverNumber, msg, sizeof(msg));
		if (r == 0)
		{
			if (strlen(msg) > 0)
				printf("DriverNumber: %s\n", msg);
		}
		/* see if mileage */
		r = EFT_ReadProperty(h, ptIFSF, peIFSFMileage, msg, sizeof(msg));
		if (r == 0)
		{
			if (strlen(msg) > 0)
				printf("Mileage: %s\n", msg);
		}

		/* see if FleetID */
		r = EFT_ReadProperty(h, ptIFSF, peIFSFFleetID, msg, sizeof(msg));
		if (r == 0)
		{
			if (strlen(msg) > 0)
				printf("FleetID: %s\n", msg);
		}
		printf(" -> Commit\n");
		r = EFT_Commit(h, true);
		if (r != 0)
		{
			EFT_GetExceptionMessage(h, msg, sizeof(msg));
			printf("EFT_Commit returned error %d: %s\n", r, msg);
			break;
		}
		break;
	case 'u':/* transaction with proceed  */
		printf("start transaction with proceed\n");

		EFT_PutAsync(h, 1);  /* to be able to read the card event */
		EFT_PutProceedNeeded(h, 1);  /* start phase 1 */
		r = EFT_Transaction(h, "debit", 200, 0);
		if (r != 0)
		{
			EFT_GetExceptionMessage(h, msg, sizeof(msg));
			printf("EFT_Transaction returned error %d: %s\n", r, msg);
			EFT_PutAsync(h, 0);
			break;
		}

		/* see card events */
		r = EFT_E_TIMEOUT;
		while (r == EFT_E_TIMEOUT)
		{
			/* wait for events from driver for 500ms */
			r = EFT_Complete(h, 500);

            /* check ReaderStatus */
			EFT_GetDeviceEventCode(h, &readerState);
			if (readerState == readerLoaded)
			{
					printf("Readerstate: Card Loaded\n");
					break;
			}
		}
		EFT_PutAsync(h, 0);
		if (r == EFT_E_TIMEOUT) /* the while loop was breaked because of readerLoaded */
		{
			/* check card data and information 
			.
			.
			.
			*/
			
			/* in this demo we start the phase 2 */
			/* put the sales items 
			.
			.
			.
			*/
			EFT_PutProceedNeeded(h, 2);  /* phase 2 */
			r = EFT_Transaction(h, "debit", 200, 0);
		}
		if (r != 0)
		{
			EFT_GetExceptionMessage(h, msg, sizeof(msg));
			printf("EFT_Transaction returned error %d: %s\n", r, msg);
			/* test if restriction codes
			.
			.
			*/
			break;
		}
		printf("Trx is ok\n");
		/* see if driver number */
		r = EFT_ReadProperty(h, ptIFSF, peIFSFDriverNumber, msg, sizeof(msg));
		if (r == 0)
		{
			if (strlen(msg) > 0)
				printf("DriverNumber: %s\n", msg);
		}
		/* see if mileage */
		r = EFT_ReadProperty(h, ptIFSF, peIFSFMileage, msg, sizeof(msg));
		if (r == 0)
		{
			if (strlen(msg) > 0)
				printf("Mileage: %s\n", msg);
		}

		/* see if FleetID */
		r = EFT_ReadProperty(h, ptIFSF, peIFSFFleetID, msg, sizeof(msg));
		if (r == 0)
		{
			if (strlen(msg) > 0)
				printf("FleetID: %s\n", msg);
		}
		
		/* Trx is ok -> Commit */
		printf(" -> Commit\n");
		r = EFT_Commit(h, true);
		if (r != 0)
		{
			EFT_GetExceptionMessage(h, msg, sizeof(msg));
			printf("EFT_Commit returned error %d: %s\n", r, msg);
			break;
		}
		break;

	case '0': /* exit */
		goto by;
		break;

	default:
		goto retryread0;
	}
	goto retryread;

by:
	r = EFT_DestroySession(h);
	printf("*** Session Destroyed ***\n\npress any key to exit\n");
by2:	
	ch = _getch();
	if (ch == 10) goto by2;

	return 0;

}

