Helcim Logo Support & Tutorials

© Copyright 2006-2018 Helcim Inc.

All Rights Reserved.

Legacy Platforms » Helcim Virtual Terminal » Helcim Gateway API » Code Samples »

Android (Java)



 ////////////////////////////////////////////////////////////
// PRIVATE FUNCTION - SEND POST DATA TO URL
////////////////////////////////////////////////////////////
private void sendPost() {

	// SETTINGS
	final String[] responseArray = new String[1];
	final CountDownLatch latch = new CountDownLatch(1);

	// THREAD TO SEND POST
	new Thread(new Runnable() {
		@Override
		public void run() {

			// SETTINGS
			String postString = "merchantId=CHANGEME"
					+ "&token=CHANGEME"
					+ "&type=purchase"
					+ "&test=1"
					+ "&amount=66.00"
					+ "&cardNumber=CHANGEME"
					+ "&cardExpiry=CHANGEME"
					+ "&cardCVV=CHANGEME";

			// CHECK FOR +
			while (postString.contains("+")){

				// REPLACE TO CODE
				postString = postString.replace("+","%2B");

			}

			try {

				// SET URL AND POST STRING
				URL toUrl = new URL("https://gateway.helcim.com/");

				// CREATE CONNECTION
				HttpURLConnection connection = (HttpURLConnection) toUrl.openConnection();

				// SET CONNECTION TIMEOUT
				connection.setConnectTimeout(5000);

				// SET PROPERTIES
				connection.setRequestMethod("POST");
				connection.setRequestProperty("USER-AGENT", "Mozilla/5.0");
				connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");
				connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

				// SET OUTPUT
				connection.setDoOutput(true);
				DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());

				// GET RESPONSE
				dStream.writeBytes(postString);

				// CLOSE
				dStream.close();

				// CREATE READER AND READ DATA
				BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
				String line = null;
				StringBuilder responseOutput = new StringBuilder();
				while ((line = br.readLine()) != null) {
					responseOutput.append(line);
				}

				// CLOSE READER
				br.close();

				// SET RESPONSE AND ALLOW RETURN
				responseArray[0] = responseOutput.toString();
				latch.countDown();

			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (SocketTimeoutException e) {

				// CONNECTION TIMED OUT
				responseArray[0] = "Connection timed out. Please try again.";
				latch.countDown();

			} catch (IOException e) {

				// ERROR WITH CONNECTION
				responseArray[0] = e.getLocalizedMessage();
				latch.countDown();
			}
		}
	}).start();


	try {

		// WAIT TO MAKE SURE WE HAVE DATA
		latch.await();

	} catch (InterruptedException e) {
		e.printStackTrace();
	}

	// RETURN ANSWER
	return responseArray[0];

} // END FUNCTION