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

This code sample is used to create Refund of a prior Capture or Purchase in OpenAPI. For more information on Refund, why you would use it, and a complete service parameter breakdown, see the documentation here: [+Refund (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 refund 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.

When performing a Refund, you do need to provide an amount. You may not be refunding the entire amount of the original transaction, such as in a line-item refund where a customer has returned one item out of many.

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

	// Most of these values would normally be passed into the method.
	final String amount = "84864";
	final String currency = "USD";
	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\"?>" +
		"<RefundRequest>" +
			"<MerchantIdentity>" +
				"<CompanyId>" + companyId + "</CompanyId>" +
				"<CompanyPassword>" + companyPassword + "</CompanyPassword>" +
			"</MerchantIdentity>" +
			"<Amount>" +
				"<Currency>" + currency + "</Currency>" +
				"<Value>" +	amount + "</Value>" +
			"</Amount>" +
			"<Reference>" +
				"<MessageId>" +	messageId +	"</MessageId>" +
				"<SettlementId>" + settlementId + "</SettlementId>" +
				"<OrderId>" + orderId + "</OrderId>" +
			"</Reference>" +
		"</RefundRequest>";



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(refundEndPoint, request);

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

		if (refundDoc.getFirstChild().getNodeName().equals("Refund")) {
			// Test for approval.
			if (refundDoc.getElementsByTagName("TransactionResponse").item(0).getTextContent().equals("APPROVED")) {
				System.out.println("Refund was successful.");
			}
		} else {
			if (refundDoc.getFirstChild().getNodeName().equals("Errors")) {
				System.out.println("Error: " + refundDoc.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.