Guest User

Untitled

a guest
Jun 19th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. @RestResource(urlMapping='/v1/getreceiptInfo/*')
  2. global with sharing class REST_GetDocInfo {
  3.  
  4. @HttpPost
  5. global static postJson doPOST(Details customer) {
  6.  
  7. RestRequest req = RestContext.request;
  8. RestResponse res = RestContext.response;
  9. postJson response = new postJson();
  10.  
  11. validateInput(customer, response);
  12.  
  13. if(response.status != 'Failure') {
  14. if(isAuthorizedUser(customer.Id)) {
  15. response.rawRecord.addAll(getResponseModelList(customer, response));
  16. if(response.status != 'Failure') {
  17. response.status = 'Success';
  18. response.errorCode.add('000');
  19. response.message.add('Operation successful');
  20. }
  21. } else {
  22. response.status = 'Failure';
  23. response.errorCode.add('003');
  24. response.message.add('You are not allowed to view the information');
  25. }
  26. }
  27.  
  28. return response;
  29. }
  30.  
  31. //this class has the response data fields
  32. global class postJson {
  33. public List<ResponseModel> rawRecord;
  34. public String status;
  35. public String [] message;
  36. public String [] errorCode;
  37. public Boolean leadcustomer;
  38.  
  39. public postJson(){
  40. rawRecord = new List<ResponseModel>();
  41. message = new List<String>();
  42. errorCode = new List<String>();
  43. }
  44. }
  45.  
  46. //this is custom response data model
  47. global class ResponseModel {
  48. public String receiptId;
  49. public String customerName;
  50. public String customerNumber;
  51. public String receiptType;
  52. public String valReceipt ;
  53. public String receiptName;
  54. public String leadcustomer;
  55. public Decimal isReceiptListAvailable;
  56. public String recOrderNumber;
  57.  
  58.  
  59. public ResponseModel(String receiptId, String customerName, String
  60. customerNumber, String receiptType,
  61. String valReceipt , String receiptName, String leadcustomer, Decimal
  62. isReceiptListAvailable, String recOrderNumber){
  63. this.receiptId = receiptId;
  64. this.customerName = customerName;
  65. this.customerNumber = customerNumber;
  66. this.receiptType = receiptType;
  67. this.valReceipt = valReceipt ;
  68. this.receiptName = receiptName;
  69. this.leadcustomer = leadcustomer;
  70. this.isReceiptListAvailable = isReceiptListAvailable;
  71. this.recOrderNumber = recOrderNumber;
  72. }
  73.  
  74. public ResponseModel(){}
  75. }
  76.  
  77. //call to server and return info as string
  78. private static String getInfo(String receipt , postJson response){
  79. String valReceipt ;
  80.  
  81. JSONToApex jsonRequest = new JSONToApex();
  82. jsonRequest.receiptUrl = receipt ;
  83.  
  84.  
  85. Http http = new Http();
  86. HttpRequest req = new HttpRequest();
  87. Custom_Settings__c settings = Custom_Settings__c.getValues('App');
  88. String reqBody= JSON.serialize(jsonRequest);
  89. req.setEndPoint(settings.RECEIPT_URL__c);
  90. req.setMethod('POST');
  91. req.setHeader('content-type','application/json');
  92. req.setHeader('x-api-key', settings.INFO_API_header_key__c);
  93. req.setBody(reqBody);
  94.  
  95. try {
  96. HTTPResponse res = http.send(req);
  97. valReceipt = res.getBody();
  98. } catch(Exception e) {
  99. valReceipt = 'blank';
  100. response.status = 'Failure';
  101. response.errorCode.add('007');
  102. response.message.add('Not able to get receipt. ' + e);
  103. }
  104. return valReceipt ;
  105. }
  106. }
Add Comment
Please, Sign In to add comment