Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RestResource(urlMapping='/v1/getreceiptInfo/*')
- global with sharing class REST_GetDocInfo {
- @HttpPost
- global static postJson doPOST(Details customer) {
- RestRequest req = RestContext.request;
- RestResponse res = RestContext.response;
- postJson response = new postJson();
- validateInput(customer, response);
- if(response.status != 'Failure') {
- if(isAuthorizedUser(customer.Id)) {
- response.rawRecord.addAll(getResponseModelList(customer, response));
- if(response.status != 'Failure') {
- response.status = 'Success';
- response.errorCode.add('000');
- response.message.add('Operation successful');
- }
- } else {
- response.status = 'Failure';
- response.errorCode.add('003');
- response.message.add('You are not allowed to view the information');
- }
- }
- return response;
- }
- //this class has the response data fields
- global class postJson {
- public List<ResponseModel> rawRecord;
- public String status;
- public String [] message;
- public String [] errorCode;
- public Boolean leadcustomer;
- public postJson(){
- rawRecord = new List<ResponseModel>();
- message = new List<String>();
- errorCode = new List<String>();
- }
- }
- //this is custom response data model
- global class ResponseModel {
- public String receiptId;
- public String customerName;
- public String customerNumber;
- public String receiptType;
- public String valReceipt ;
- public String receiptName;
- public String leadcustomer;
- public Decimal isReceiptListAvailable;
- public String recOrderNumber;
- public ResponseModel(String receiptId, String customerName, String
- customerNumber, String receiptType,
- String valReceipt , String receiptName, String leadcustomer, Decimal
- isReceiptListAvailable, String recOrderNumber){
- this.receiptId = receiptId;
- this.customerName = customerName;
- this.customerNumber = customerNumber;
- this.receiptType = receiptType;
- this.valReceipt = valReceipt ;
- this.receiptName = receiptName;
- this.leadcustomer = leadcustomer;
- this.isReceiptListAvailable = isReceiptListAvailable;
- this.recOrderNumber = recOrderNumber;
- }
- public ResponseModel(){}
- }
- //call to server and return info as string
- private static String getInfo(String receipt , postJson response){
- String valReceipt ;
- JSONToApex jsonRequest = new JSONToApex();
- jsonRequest.receiptUrl = receipt ;
- Http http = new Http();
- HttpRequest req = new HttpRequest();
- Custom_Settings__c settings = Custom_Settings__c.getValues('App');
- String reqBody= JSON.serialize(jsonRequest);
- req.setEndPoint(settings.RECEIPT_URL__c);
- req.setMethod('POST');
- req.setHeader('content-type','application/json');
- req.setHeader('x-api-key', settings.INFO_API_header_key__c);
- req.setBody(reqBody);
- try {
- HTTPResponse res = http.send(req);
- valReceipt = res.getBody();
- } catch(Exception e) {
- valReceipt = 'blank';
- response.status = 'Failure';
- response.errorCode.add('007');
- response.message.add('Not able to get receipt. ' + e);
- }
- return valReceipt ;
- }
- }
Add Comment
Please, Sign In to add comment