Guest User

Untitled

a guest
Aug 14th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. public static void main(String args[]) throws IOException,NoSuchAlgorithmException {
  2.  
  3. String username = "XXXXX";
  4. String password = "XXXX";
  5. String nonce = "XXXXX";
  6.  
  7. SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
  8. String created = dateFormatter.format(new Date());
  9. // String created = "2018-07-12EAT14:31:1210800";
  10. String input = nonce + created + password;
  11. //String passwordDigest = "jro73ZmrGeYEG7OqPWdU9fgyEFY=";
  12. MessageDigest md = MessageDigest.getInstance("SHA-1");
  13. String passwordDigest = DatatypeConverter.printBase64Binary(md.digest(input.getBytes("UTF-8")));
  14. // System.out.println(header.toString());
  15. System.out.println(DatatypeConverter.printBase64Binary(input.getBytes("UTF-8")) + " === " + created + " " + username);
  16.  
  17. String xml = "<fulfillmentRequest>"
  18. + "<msisdn>260964293635</msisdn>"
  19. + "<operationType>3</operationType>"
  20. + "<iname>Stanchart</iname>"
  21. + "<clientTransactionId>19035542356435323</clientTransactionId>"
  22. + "<productCode>NACT_ZM_Data_4764</productCode>"
  23. + "<pin>2018</pin><benmsisdn>260966745046</benmsisdn>"
  24. + "</fulfillmentRequest>";
  25.  
  26. System.out.println("This is the request to MTN => " + xml);
  27. String endpoint = "http://172.25.48.43:8312/1/cis/services/fulfillment";
  28.  
  29. System.out.println("This is the endpoint we are posting the transaction to => " + endpoint);
  30.  
  31. boolean hasResponse = false;
  32. String statCode = null;
  33. String statusResponse = null;
  34. String receiptNo = null;
  35.  
  36. //OutputStream zp =null;
  37. BufferedReader br;
  38. StringBuilder sb;
  39.  
  40. String respTxt= "";
  41. int msg=0 ;
  42. String response;
  43. URL url;
  44. HttpURLConnection connection ;
  45.  
  46. try {
  47.  
  48. url = new URL(endpoint);
  49.  
  50. connection = (HttpURLConnection) url.openConnection();
  51. connection.setRequestMethod("POST");
  52. connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
  53. connection.setRequestProperty("Authorization", "WSSE realm="SDP", profile="UsernameToken"");
  54. connection.setRequestProperty("User-Agent", "Jakarta Commons-HttpClient/3.1");
  55. connection.setRequestProperty("X-WSSE", "UsernameToken Username="+username+",PasswordDigest="+passwordDigest+",Nonce=""+nonce+"",Created=""+created+""");
  56. connection.setRequestProperty("Host", "172.25.48.43:8312");
  57. connection.setUseCaches(false);
  58. connection.setDoInput(true);
  59. connection.setDoOutput(true);
  60. connection.setConnectTimeout(50000);
  61. connection.setReadTimeout(90000);
  62. DataOutputStream output = new DataOutputStream(connection.getOutputStream());
  63. output.writeBytes(xml);
  64. output.flush();
  65. responseCode = connection.getResponseCode();
  66. respTxt = connection.getResponseMessage();
  67. System.out.println("Status code after sending the the payload to mtn => " + responseCode + " Now waiting for response: ");
  68. Object vm = connection.toString();
  69.  
  70. msg = connection.getContentLength();
  71. br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
  72. sb = new StringBuilder();
  73.  
  74. System.out.println("| Response |" + br.toString() + " |beepTrxID: " );
  75. while ((response = br.readLine()) != null) {
  76. sb.append(response);
  77.  
  78. }
  79. System.out.println(sb.toString());
  80. if (br.toString().isEmpty() || br.toString() == null) {
  81. hasResponse = false;
  82. statusResponse = "Empty response from MTN server:" + br.toString();
  83. } else {
  84.  
  85. DocumentBuilder builder = dbf.newDocumentBuilder();
  86. InputSource source = new InputSource(new StringReader(sb.toString()));
  87. Document document = builder.parse(source);
  88. NodeList responseCode = document.getElementsByTagName("responseCode");
  89. NodeList responseDescription = document.getElementsByTagName("responseDescription");
  90. NodeList receipt;
  91. if (br.toString().contains("requestId")) {
  92. receipt = document.getElementsByTagName("requestId");
  93. receiptNo = receipt.item(0).getTextContent().trim();
  94. } else {
  95. receiptNo = "value";
  96. }
  97. System.out.println("|Extracting status code..." + " |beepTrxID: "+sb.toString());
  98. statCode = responseCode.item(0).getTextContent().trim();
  99. statusResponse = responseDescription.item(0).getTextContent().trim();
  100. System.out.println("|Status Code: " + statCode + "| Status Response: " + statusResponse + " |beepTrxID: ");
  101. }
  102.  
  103. br.close();
  104. connection.disconnect();
  105.  
  106.  
  107.  
  108. } catch (MalformedURLException ex) {
  109. System.out.println("An error occured:" + respTxt);
  110. System.out.println(msg);
  111. //System.out.println(zp.toString());
  112. // }catch(NotAuthorizedException ex){
  113.  
  114. // System.out.println("Operation Not Permitted"+ex);
  115. } catch (IOException ex ){
  116. System.out.println("An error occured:" + respTxt);
  117.  
  118. //} catch( NoSuchAlgorithmException ex) {
  119. // System.out.println("An error occured:" + ex);
  120. } catch (ParserConfigurationException ex) {
  121. System.out.println(ex);
  122. } catch (SAXException ex) {
  123. System.out.println(ex);
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. }
  134.  
  135. <?xml version="1.0" encoding="UTF-8" standalone="yes"?><fulfillmentService><responseCode>1106</responseCode><status>FAILURE</status><systemType>CIS</systemType><responseDescription>Sorry! We are unable to process your request. Call 111 for customer care or visit www.mtnzambia.com.</responseDescription><requestId>WEB_10.100.244.97_7889e49b-24f4-4c27-8d69-f4fd6d25c603</requestId></fulfillmentService>
Add Comment
Please, Sign In to add comment