Void Create Sample
Take a look at the things you can do with our APIs

This code sample is used to create Void of a prior Capture or Purchase in OpenAPI. For more information on Void, why you would use it, and a complete service parameter breakdown, see the documentation here: [+Void (Create)]

At the beginning of the method, we create a number of instance variables. Most of them normally would be supplied to this method in another way, usually from your user interface. The endPoint variable is pointing at the sandbox, so you would need to change this value when you are ready to use production.

To void a capture or purchase, you need to record the OrderId from the Capture/Purchase response. You can record this when you create the Capture/Purchase (see Capture Create Sample or Purchase Create Sample), or you could save your Capture/Purchase's TransactionId and perform an Capture/Purchase query using that TransactionId (see Capture Query Sample or Purchase Query Sample) and extract the OrderId.

/*
 *  Creates a Void in OpenAPI.
 */
public void createVoid() {
	final String voidEndPoint = "https://sandbox.api.mastercard.com/payments/v1/void?Format=XML";

	// Most of these values would normally be passed into the method.
	final String companyId = "changeme";
	final String companyPassword = "changeme";
	final String messageId = "changeme";
	final String settlementId = "changme";

	final String orderId = "12345"; // This orderId must match the orderId from an approved Capture or Purchase.



This next bit of code creates the XML needed to be passed in the body of an Authorization request. We are showing you raw XML, since it documents the process best. However, we recommend using a more formal technique for generating XML, such as using Jersey JAX-RS as documented here: [openapi:/2010/09/30/Accessing the Payment API using JAX\-RS]

		String request =
		"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
		"<VoidRequest>" +
			"<MerchantIdentity>" +
				"<CompanyId>" + companyId + "</CompanyId>" +
				"<CompanyPassword>" + companyPassword + "</CompanyPassword>" +
			"</MerchantIdentity>" +
			"<Reference>" +
				"<MessageId>" +	messageId +	"</MessageId>" +
				"<SettlementId>" + settlementId + "</SettlementId>" +
				"<OrderId>" + orderId + "</OrderId>" +
			"</Reference>" +
		"</VoidRequest>";



This section of code establishes the connection to OpenAPI, and processes the result. This code depends on the code for the createOpenAPIConnection method found here: Java OAuth Sample Code

	try {
		HttpsURLConnection conn = createOpenAPIConnection(voidEndPoint, request);

		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
		Document voidDoc = docBuilder.parse(conn.getInputStream());

		if (voidDoc.getFirstChild().getNodeName().equals("Void")) {
			// Test for approval.
			if (voidDoc.getElementsByTagName("TransactionResponse").item(0).getTextContent().equals("APPROVED")) {
				System.out.println("Void was successful.");
			}
		} else {
			if (voidDoc.getFirstChild().getNodeName().equals("Errors")) {
				System.out.println("Error: " + voidDoc.getElementsByTagName("Description").item(0).getTextContent());
			} else {
				System.out.println("Unknown Problem");
			}
		}

	} catch (MalformedURLException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} catch (ParserConfigurationException e) {
		e.printStackTrace();
	} catch (SAXException e) {
		e.printStackTrace();
	} catch (NoSuchAlgorithmException e) {
		e.printStackTrace();
	} catch (InvalidKeySpecException e) {
		e.printStackTrace();
	} catch (OAuthException e) {
		e.printStackTrace();
	} catch (KeyStoreException e) {
		e.printStackTrace();
	} catch (CertificateException e) {
		e.printStackTrace();
	} catch (UnrecoverableKeyException e) {
		e.printStackTrace();
	} catch (KeyManagementException e) {
		e.printStackTrace();
	}
}

© 1994-2014. MasterCard. All rights reserved.