Gate2Play and iOS integration.

HI,

If you are making an application which targets middle east then you will come across a payement gateway  provider gate2play .The apis mentioned are al for web portal and FAQ states that it can be used on mobile platform  but

Ok so i spend my whole day and finally integrated it and now i just want to make your life easier becoz its no greatness to waste time about some thing less documented.. but before i start
THIS tutorial IS PROVIDED "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

now lets start.

Requirements:- I have used AFNetworking and SBJSON in my tutorial you can use any replacement.

Step1:- Create a single view application.
Step2:- Add a web view on the xib and set its delegate to file owner and make its outlet as objWebView





Step3:- In view did load you have to hit a POST request to get the transaction token.

For this
 [NSHTTPCookieStorage sharedHTTPCookieStorage].cookieAcceptPolicy =
    NSHTTPCookieAcceptPolicyAlways;
 NSDictionary *params = @{@"SECURITY.SENDER": @"<security key by Gate2play>",
                    @"USER.LOGIN":@"<provided by gate2play>",               @"TRANSACTION.MODE":@"CONNECTOR_TEST",                             @"TRANSACTION.CHANNEL":@"<provided by gate2play>",
                             @"USER.PWD":@"<provided by gate2play>",@"PAYMENT.TYPE":@"DB",@"PRESENTATION.AMOUNT":@"50.00",@"PRESENTATION.CURRENCY":@"EUR"};
   //if you need unique transaction id you can add it in prams //@"IDENTIFICATION.TRANSACTIONID":@"<unique transaction ID generate yourself>",


 AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:
                            [NSURL URLWithString:@"https://test.ctpe.net"]];
  
    [client postPath:@"/frontend/GenerateToken" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
       
        NSString *text = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSDictionary  *dic=[text JSONValue];
       
        // store the token received in a variable tokenWeb
        [tokenWeb setString:[[dic valueForKey:@"transaction"] valueForKey:@"token"]];
       
        //call method to load web view with javascript with received token
        [self callWebViewAfterToken:[[dic valueForKey:@"transaction"] valueForKey:@"token"]];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", [error localizedDescription]);
    }];
  

Step 4:- When the token for transaction is generated we need to load the web view with javascript and the given token. the callWebViewAfterToken: method is

-(void)callWebViewAfterToken:(NSString*)token{
    self.ObjWebview.delegate=self;
    [self.ObjWebview loadHTMLString:[NSString stringWithFormat:@"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\"http://www.w3.org/TR/html4/loose.dtd\"><html><head><script src=\"https://test.ctpe.net/frontend/widget/v3/widget.js?language=en&style=card\" ></script></head><body><form action=\"{ios:webCall}\" id=\"%@\">MASTER VISA AMEX CHINAUNIONPAY</form></body></html>",token] baseURL:Nil];
}

in form action we have ios:webCall as redirect url. you will be presented with following screen in web view fill the test card details and click on pay now
After you click on the pay now the redirect url will load which will send call to webview delegate method.

Step5:- In step 5 we need to check the request in webview delegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([[[request URL] absoluteString] rangeOfString:@"ios"].location!=NSNotFound) {
        // Call the given selector
        [self performSelector:@selector(webCall)];
        // Cancel the location change
        return NO;
    }
    return YES;
}

Step6:-Check the status of the transaction using the token. The get request for the token used in transaction will give you status of the transaction.

-(void)webCall{
    NSDictionary *params = @{@"jsessionid": tokenWeb,
                             };
    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:
                            [NSURL URLWithString:@"https://test.ctpe.net"]];
    [client getPath:@"/frontend/GetStatus" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *text = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"Response: %@", text);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", [error localizedDescription]);
    }];

}



Note i have used test url through out the tutorial you will get production and test url from gate2play its good to macro it.