Helcim Logo Support & Tutorials

© Copyright 2006-2018 Helcim Inc.

All Rights Reserved.

Legacy Platforms » Helcim Virtual Terminal » Helcim Gateway API » Code Samples »

iOS (Objective C)



    // TEXT FIELD FOR RESPONSE
    @property (weak, nonatomic) IBOutlet UITextView *txtResponseField;

    // SETTINGS
    @property (retain, nonatomic) NSURLConnection *connection;
    @property (retain, nonatomic) NSMutableData *receivedData;

    ///////////////////
    // CONNECT HTTP BUTTON
    - (IBAction)btnConnect:(id)sender {
        
        // CLEAR CONNECTION IF THERE IS ONE (mainly if you click the button twice)
        [self.connection cancel];
        
        //INITALIZE DATA
        NSMutableData *data = [[NSMutableData alloc] init];
        self.receivedData = data;

        
        //INITALIZE URL
        NSURL *url = [NSURL URLWithString:@"https://gateway.qa.helcim.com/"];
        
        //INITALIZE REQUEST
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];
        
        // SET METHOD
        [request setHTTPMethod:@"POST"];
        
        //INITALIZE POST STRING
        NSString *postData = @"merchantId=9999999999&token=ABC1123123ABC123123&type=purchase&amount=10.00&cardNumber=5454545454545454&expiryDate=1020&cvvIndicator=1&cvv=100";
        
        //SET REQUEST TYPE !!HAS TO BE SET!!
        [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
        // SET POST DATA OF REQUEST
        [request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
        
        // INITALIZE A CONNECTION WITH REQUEST
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        self.connection = connection;

        
        // START CONNECTION
        [connection start];
        
    }

    /*
     this method might be calling more than one times according to incoming data size
     */
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
        [self.receivedData appendData:data];
    }

    /*
     if there is an error occured, this method will be called by connection
     */
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
        
        NSLog(@"%@" , error);

    }

    /*
     if data is successfully received, this method will be called by connection
     */
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
        
        // CONVERT RECIEVED DATA INTO STRING
        NSString *htmlSTR = [[NSString alloc] initWithData:self.receivedData
                                                  encoding:NSUTF8StringEncoding];
        
        // SET TEXTFIELD
        self.txtResponseField.text = htmlSTR;
        
    }

    ///////////////////////
    // BUTTON PARSE DATA
    - (IBAction)btnParse:(id)sender {
        
        // PARSED ARRAY
        NSArray *responseArray = [self.txtResponseField.text componentsSeparatedByString:@"\r\n"];
        
        // CREATE A DICTIONARY TO SET KEY AND VALUE
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
        
        // LOOP THROUGH RESPONSE ARRAY
        for (int i; i < [responseArray count]-1; i++) {

            // EXPLODE VALUE
            NSArray *tempArray = [responseArray[i] componentsSeparatedByString:@"="];

            // SET KEY AND VALUE
            [dictionary setObject:tempArray[1] forKey:tempArray[0]];

        }

        // CLEAR TEXT
        self.txtResponseField.text = @"";
        
        // LOOP THROUGH DICTIONARY
        for (NSString *key in dictionary) {
            
            // SHOW PARSED DATA
            self.txtResponseField.text =[self.txtResponseField.text stringByAppendingString:[NSString stringWithFormat:@"%@ = \"%@\"\n\n",key,[dictionary objectForKey:key]]];
            
        }
    }