package examples; //JDK imports import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; //WorldPay imports import com.worldpay.servlet.*; import com.worldpay.util.*; import com.worldpay.select.*; import com.worldpay.select.merchant.MerchantCurrencyConverter; import com.worldpay.select.merchant.MerchantInfo; import com.worldpay.select.SelectException; import com.worldpay.core.ArgumentException; /* * Call this servlet with URL parameter: instId= */ public class CurrTest extends WorldPayServlet implements SelectDefs { private static String orNull(String x) { return (x==null)?"":x; } private static boolean isEmpty(String x) { return (x==null || x.length()==0); } public void doRequest(WorldPayServletRequest req, WorldPayServletResponse resp) throws ServletException, IOException{ //setup response & get output stream resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); // object which writes output to browser MerchantInfo mInfo=null; // merchant info object. See usage below: int instId=0; // installation ID, brought in from request as string parameter and converted - see below: MerchantCurrencyConverter converter = null; // object which will convert one currency to another String mId = req.getParameter("instId"); // get the installation ID if(mId!=null){ // if we do indeed have an installation ID then procede String curr = req.getParameter("currList"); /* possible previous currency selection if the servlet is not being called by itself (i.e: from the 'conversion' button) then this will be null and will be dealt with below: */ try{ Integer intA=new Integer(mId); instId=intA.intValue(); // convert string ID to int ID mInfo = new MerchantInfo(instId); // get merchant information based on intallation ID } catch(SelectException se){ out.println("Currency Selection Test"); out.println("INVALID PARAMETER"); out.println(""); } if(mInfo!=null){ // if we do have merchant information converter = new MerchantCurrencyConverter(instId); // get a converter object based on installation ID // get a list of settlement currencies (currencies that the merchant will accept as settlement) CurrencyList settlementCurrencies=converter.getSettlementCurrencies(); // get a list of acquisition currencies (currencies that the customer can pay with) CurrencyList merchantCurrencies=mInfo.getCurrencyList(); // use the first available settlement currency as the base currency prior to any conversions Currency base = settlementCurrencies.first(); CurrencyAmount cAmount= null; /* a currency amount object which will hold the price in whatever currency is chosen */ Currency cash = null; // a currency object - holds the type of currency e.g: GBP (£) if(curr == null){ // if first run of servlet, choose first available currency for conversion cash = settlementCurrencies.first(); // get the first settlement currency curr = cash.getISOCode(); // store string version of currency e.g: "GBP" } else{ // else take the currency which was provided by the submission (from the html selection box) cash = SelectCurrency.getInstanceByISOCode(curr); } try{ cAmount= new CurrencyAmount(100,base); /* get the amount for 100 units of the base currency, CurrencyAmount objects can be used for conversion or output as required (see output below) */ cAmount = converter.convertToAcquisitionCurrency(cAmount,cash); // convert using the new currency } catch(ArgumentException arge){ // produce an HTML page describing the problem out.println("Currency Selection Test"); out.println("
"); out.println("

A Currency Conversion Demonstration

"); out.println("
"); out.println("

"); out.println("

A problem has occurred during currency conversion. Please check your server log for details.

"); // finish the output out.println(""); } // start the HTML output page out.println("Currency Selection Test"); out.println("
"); out.println("

A Currency Conversion Demonstration

"); out.println("
"); out.println("

"); out.println("

"); // this form allows the user to choose currencies and submit for price conversion out.println("
"); out.println(""); out.println(""); out.println("Please select a currency:
" + currList(merchantCurrencies,curr)); out.println(""); out.println("
"); /* this form allows the user to submit the purchase to Worldpay using the chosen currency. the submission is to the buythis servlet included in the examples directory. This example does not submit customer name/address/contact details but you could include them if you wish: see the 'BuyThis.java' servlet or 'buythese.html' example for the required form details. */ out.println("
"); out.println(""); out.println(""); // note that cAmount.getAmount() used below returns a numeric value (double) out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); // note that cAmount.toHTMLString() used below returns a string representation of the value out.println("Tee Shirt, price " + cAmount.toHTMLString() + ", no contact details, test mode 30."); out.println("

"); out.println(" Purchase This Item"); out.println("

"); out.println("
"); // finish the output out.println(""); } else{ // report lack of ID out.println("Currency Selection Test"); out.println("
"); out.println("

A Currency Conversion Demonstration

"); out.println("
"); out.println("

"); out.println("

The installation ID provided as 'instId' does not appear to be correct. Please check it and resubmit.

"); // finish the output out.println(""); } out.close(); } else{ // report no instId param out.println("Currency Selection Test"); out.println("
"); out.println("

A Currency Conversion Demonstration

"); out.println("
"); out.println("

"); out.println("

Please add your installation ID number to the URL as parameter 'instId'.

"); // finish the output out.println(""); out.close(); } } public String currList(CurrencyList merchantCurrencies, String curr){ // produce currency drop-down // parameters are a list of acquisition currencies and the last selected currency StringBuffer html = new StringBuffer(""); return html.toString(); } }