Advertisement
Guest User

Untitled

a guest
May 29th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 8.88 KB | None | 0 0
  1. import com.atlassian.jira.component.ComponentAccessor;
  2. import com.atlassian.jira.issue.Issue;
  3. import com.atlassian.jira.issue.CustomFieldManager;
  4. import com.atlassian.jira.issue.IssueManager;
  5. import com.atlassian.jira.issue.fields.CustomField;
  6. import com.atlassian.jira.issue.fields.FieldManager;
  7. import org.apache.commons.lang.WordUtils;
  8. import javax.xml.parsers.DocumentBuilderFactory;
  9. import javax.xml.parsers.DocumentBuilder;
  10. import org.xml.sax.InputSource;
  11. import org.w3c.dom.Document;
  12. import org.apache.commons.io.IOUtils;
  13. import org.apache.commons.lang.StringUtils;
  14. import com.atlassian.jira.bc.issue.IssueService;
  15. import com.atlassian.jira.issue.IssueInputParameters;
  16. import com.atlassian.crowd.embedded.api.User;
  17. import com.atlassian.jira.issue.util.IssueChangeHolder
  18. import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
  19. import com.atlassian.jira.issue.ModifiedValue
  20.  
  21.  
  22. IssueManager issueManager = ComponentAccessor.getIssueManager();
  23. Issue issue = issueManager.getIssueByKeyIgnoreCase("WORK-57876");
  24. CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
  25. CustomField transIdField = customFieldManager.getCustomFieldObjectByName("Transaction ID (NMI Only)");
  26. CustomField refNumField = customFieldManager.getCustomFieldObjectByName("Reference # (ACH Only)");
  27. CustomField nmiChargeTotal = customFieldManager.getCustomFieldObject("customfield_12904")
  28. CustomField info = customFieldManager.getCustomFieldObject("customfield_12903")
  29. CustomField npsTotal = customFieldManager.getCustomFieldObject("customfield_15000")
  30. CustomField npsInfo = customFieldManager.getCustomFieldObject("customfield_14402")
  31.  
  32. CustomField customerName = customFieldManager.getCustomFieldObjectByName("Customer Name");
  33. //CustomField customerEmail = customFieldManager.getCustomFieldObjectByName("Customer Email");
  34.  
  35. String nmiInfo = "";
  36. String achInfo = "";
  37. Double total = 0;
  38. Double nmiTotal = 0;
  39. Double achTotal = 0;
  40.  
  41. //loop through all transaction IDs here
  42.  
  43. String transId = (String)issue.getCustomFieldValue(transIdField);
  44. transId = transId?.trim()
  45. //log.error("Custy name: " + customerName)
  46. //log.error("Transaction ID: " + transId);
  47.  
  48. String refNum = (String)issue.getCustomFieldValue(refNumField);
  49. refNum = refNum?.trim()
  50. //log.error("Reference #: " + refNum);
  51.  
  52. if(refNum!=null ){  //&& 'ACH' in cfValues['Payment Method']*.value
  53.     List<String> transactions = Arrays.asList(refNum.split("\\s*,\\s*"));
  54.     log.error("transactions: " + transactions)
  55.    
  56.     for (String transaction : transactions){
  57.         def urlString = "https://www.edebitdirect.com/app/api/v1/check"
  58.         def queryString = "reference_number=" + transaction
  59.         //log.error("queryString: " + queryString)
  60.    
  61.         def json = ("https://www.edebitdirect.com/app/api/v1/check?reference_number=" + transaction).toURL().getText(requestProperties: [Authorization: "apikey 587GQRA2U:cabf4743de4c81de138a79751fb3d67239ade3a9"])
  62.         //log.error("json: " + json)
  63.        
  64.        
  65.         achInfo += "Reference Number: " + StringUtils.substringBetween(json, 'reference_number": "', '",') + "\n";
  66.         achInfo += "Customer name: " + StringUtils.substringBetween(json, 'customer_name": "', '",') + "\n";
  67.         achInfo += "Date/Time Processed: " + StringUtils.substringBetween(json, 'created_date": "', '",') + "\n";
  68.         achInfo += "Amount: " + StringUtils.substringBetween(json, 'amount": ', ',') + "\n";
  69.         achInfo += "Memo: " + StringUtils.substringBetween(json, 'memo": "', '",') + "\n";
  70.         achInfo += "\n";
  71.         //log.error("achInfo: " + achInfo);
  72.        
  73.         if(StringUtils.substringBetween(json, 'amount": ', ',')!=null){
  74.             achTotal += StringUtils.substringBetween(json, 'amount": ', ',').toDouble();
  75.         }
  76.         //log.error("achTotal: " + achTotal)
  77.     }
  78.    
  79.  
  80. }
  81.  
  82. if(refNum!=null ){  //&& 'ACH' in cfValues['Payment Method']*.value
  83.     CustomField xmlResponseField = customFieldManager.getCustomFieldObjectByName("ACH Transaction Information");
  84.    
  85.     IssueService issueService = ComponentAccessor.getIssueService();
  86.     //User user = ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser();
  87.     def user = ComponentAccessor.getJiraAuthenticationContext().getUser();
  88.     //log.error("user: " + user);
  89.        
  90.     IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
  91.     issueInputParameters.addCustomFieldValue("customfield_12907", achTotal.toString() );   //ACH Charge Total
  92.     issueInputParameters.addCustomFieldValue("customfield_12906", achInfo );   //ACH Transaction INformaiton
  93.     issueInputParameters.setSkipScreenCheck(true) ;
  94.        
  95.     IssueService.UpdateValidationResult updateValidationResult = issueService.validateUpdate( user, issue.getId(), issueInputParameters );
  96.     //log.error("updateValidationResult: " + updateValidationResult);
  97.      
  98.     if (!updateValidationResult.isValid()) {
  99.         log.error( updateValidationResult.getErrorCollection().getErrorMessages().toString() );
  100.     }
  101.    
  102.     IssueService.IssueResult updateResult = issueService.update( user, updateValidationResult );
  103.      
  104.     if (!updateResult.isValid()) {
  105.          log.error( updateValidationResult.getErrorCollection().getErrorMessages().toString() );
  106.     }
  107.    
  108. }
  109.  
  110. if(transId!=null ){  //  && 'NMI' in cfValues['Payment Method']*.value
  111.     List<String> transactions = Arrays.asList(transId.split("\\s*,\\s*"));
  112.     //log.error("transactions: " + transactions)
  113.  
  114.     for (String transaction : transactions){
  115.         def urlString = "https://secure.nmi.com/api/query.php"
  116.         def queryString = "username=JIRA&password=Salesteamjira1&transaction_id=" + transaction
  117.         //log.error("queryString: " + queryString)
  118.    
  119.         def url = new URL(urlString)
  120.         def connection = url.openConnection()
  121.         connection.setRequestMethod("GET")
  122.         connection.doOutput = true
  123.    
  124.         def writer = new OutputStreamWriter(connection.outputStream)
  125.         writer.write(queryString)
  126.         writer.flush()
  127.         writer.close()
  128.         connection.connect()
  129.    
  130.         def response = connection.content.text
  131.         //log.error(response)
  132.        
  133.     nmiInfo += "Processor ID: " + StringUtils.substringBetween(response, "<processor_id>", "</processor_id>") + "\n";
  134.         nmiInfo += "Transaction ID: " + StringUtils.substringBetween(response, "<transaction_id>", "</transaction_id>") + "\n";
  135.         nmiInfo += "Agent: " + StringUtils.substringBetween(response, "<merchant_defined_field id=\"1\">", "</merchant_defined_field>") + "\n";
  136.         nmiInfo += "Item: " + StringUtils.substringBetween(response, "<merchant_defined_field id=\"2\">", "</merchant_defined_field>") + "\n";
  137.         nmiInfo += "First Name: " + StringUtils.substringBetween(response, "<first_name>", "</first_name>") + "\n";
  138.         nmiInfo += "Last Name: " + StringUtils.substringBetween(response, "<last_name>", "</last_name>") + "\n";
  139.         nmiInfo += "Email: " + StringUtils.substringBetween(response, "<email>", "</email>") + "\n";
  140.         nmiInfo += "Phone: " + StringUtils.substringBetween(response, "<phone>", "</phone>") + "\n";
  141.         nmiInfo += "Customer ID: " + StringUtils.substringBetween(response, "<customerid>", "</customerid>") + "\n";
  142.         nmiInfo += "Amount: " + StringUtils.substringBetween(response, "<amount>", "</amount>") + "\n";
  143.         nmiInfo += "Action Type: " + StringUtils.substringBetween(response, "<action_type>", "</action_type>") + "\n";
  144.         nmiInfo += "Date: " + StringUtils.substringBetween(response, "<date>", "</date>") + "\n";
  145.         nmiInfo += "Response Text: " + StringUtils.substringBetween(response, "<response_text>", "</response_text>") + "\n";
  146.         nmiInfo += "Success: " + StringUtils.substringBetween(response, "<success>", "</success>") + "\n";
  147.         nmiInfo += "\n"
  148.    
  149.         String responsetext = StringUtils.substringBetween(response, "<response_text>", "</response_text>")
  150.         //log.error("nmiInfo: " + nmiInfo)
  151.         if(StringUtils.substringBetween(response, "<amount>", "</amount>") !=null && responsetext!="Issuer Declined"){
  152.             nmiTotal += StringUtils.substringBetween(response, "<amount>", "</amount>").toDouble();
  153.         }
  154.         //log.error("nmiTotal: " + nmiTotal)
  155.     }  
  156.    
  157.    
  158. }
  159.  
  160. //return nmiTotal
  161.  
  162. IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
  163. //issue.setCustomFieldvalue(nmiChargeTotal)
  164. nmiChargeTotal.updateValue(null, issue, new ModifiedValue(null, nmiTotal), changeHolder)
  165.  
  166. IssueChangeHolder changeHolder2 = new DefaultIssueChangeHolder();
  167. //issue.setCustomFieldvalue(info, nmiInfo)
  168. info.updateValue(null, issue, new ModifiedValue(null, nmiInfo), changeHolder2)
  169.  
  170. IssueChangeHolder changeHolder3 = new DefaultIssueChangeHolder();
  171. npsTotal.updateValue(null, issue, new ModifiedValue(null, null), changeHolder3)
  172.  
  173. IssueChangeHolder changeHolder4 = new DefaultIssueChangeHolder();
  174. npsInfo.updateValue(null, issue, new ModifiedValue(null, null), changeHolder4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement