PayUMoney payment gateway issue

Mani murugan picture Mani murugan · Jan 12, 2015 · Viewed 10.4k times · Source

I have to integrate PayUMoney payment gateway in my iOS app. They don't have SDK for iOS. So I have to load some web URL in webview for the payment. My parameters are

int i = arc4random() % 9999999999;
NSString *strHash = [self createSHA512:[NSString stringWithFormat:@"%d%@",i,[NSDate date]]];// Generatehash512(rnd.ToString() + DateTime.Now);
NSString *txnid1 = [strHash substringToIndex:20];
NSLog(@"tnx1 id %@",txnid1);
NSString *key = @"JBZaLc";
NSString *amount = @"1000";
NSString *productInfo = @"Nice product";
NSString *firstname = @"Mani";
NSString *email = @"[email protected]";
NSString *phone = @"1234566";
NSString *surl = @"www.google.com";
NSString *furl = @"www.google.com";
NSString *serviceprovider = @"payu_paisa";
NSString *action = @"https://test.payu.in/_payment";
NSString *hashValue = [NSString stringWithFormat:@"%@|%@|%@|%@|%@|%@|udf1|udf2|udf3|udf4|udf5||||||salt",key,txnid1,amount,productInfo,firstname,email];
NSString *hash = [self createSHA512:hashValue];
NSDictionary *parameters = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:txnid1,key,amount,productInfo,firstname,email,phone,surl,furl,hash,serviceprovider,action, nil] forKeys:[NSArray arrayWithObjects:@"txnid",@"key",@"amount",@"productinfo",@"firstname",@"email",@"phone",@"surl",@"furl",@"hash",@"service_provider",@"action", nil]];

I have to use POST method with my test URL (https://test.payu.in/_payment) and need to pass parameters. I have all parameters with key and value in dictionary("parameters"). So I tried following code

 NSData *dataValue = [self getPropertiesAsData:parameters];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://test.payu.in/_payment"]];
    // Create a mutable copy of the immutable request and add more headers
    NSMutableURLRequest *mutableRequest = [request mutableCopy];
    [mutableRequest setHTTPMethod: @"POST"];
    [mutableRequest setHTTPBody: dataValue];
    request = [mutableRequest copy];
    [_webviewSample loadRequest:request];


-(NSData *)getPropertiesAsData :(NSDictionary *)dict{
    NSMutableData *body = [NSMutableData postData];
    [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            [body addValue:[obj stringByReplacingOccurrencesOfString:@" " withString:@"%20"] forKey:key];
            }];
    return body;
}

-(NSString *)createSHA512:(NSString *)string
{
    const char *cstr = [string cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:string.length];
    uint8_t digest[CC_SHA512_DIGEST_LENGTH];
    CC_SHA512(data.bytes, data.length, digest);
    NSMutableString* output = [NSMutableString  stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    return output;
}

But when I run this, it says "Mandatory parameter tnxid is missing". But I have passed the tnxid which you can see in parameters dictionary. If I pass everything correctly then result will be the webpage where user can select bank details, etc that I have to load it in my web view.

Please help me to find what I did wrong or what I should do to get correct result.

Answer

Mani murugan picture Mani murugan · Jan 13, 2015

I found answer successfully. My working code is listed below

int i = arc4random() % 9999999999;
    NSString *strHash = [self createSHA512:[NSString stringWithFormat:@"%d%@",i,[NSDate date]]];// Generatehash512(rnd.ToString() + DateTime.Now);
    NSString *txnid1 = [strHash substringToIndex:20];
    NSLog(@"tnx1 id %@",txnid1);
    NSString *key = @"JBZaLc";
    NSString *amount = @"1000";
    NSString *productInfo = @"Nice product";
    NSString *firstname = @"Mani";
    NSString *email = @"[email protected]";
    NSString *phone = @"1234566";
    NSString *surl = @"www.google.com";
    NSString *furl = @"www.google.com";
    NSString *serviceprovider = @"payu_paisa";


    NSString *hashValue = [NSString stringWithFormat:@"%@|%@|%@|%@|%@|%@|||||||||||GQs7yium",key,txnid1,amount,productInfo,firstname,email];
    NSString *hash = [self createSHA512:hashValue];
    NSDictionary *parameters = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:txnid1,key,amount,productInfo,firstname,email,phone,surl,furl,hash,serviceprovider
                                                                    , nil] forKeys:[NSArray arrayWithObjects:@"txnid",@"key",@"amount",@"productinfo",@"firstname",@"email",@"phone",@"surl",@"furl",@"hash",@"service_provider", nil]];
   __block NSString *post = @"";
    [parameters enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        if ([post isEqualToString:@""]) {
            post = [NSString stringWithFormat:@"%@=%@",key,obj];
        }else{
            post = [NSString stringWithFormat:@"%@&%@=%@",post,key,obj];
        }

    }];

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://test.payu.in/_payment"]]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
    [request setHTTPBody:postData];

    [_webviewSample loadRequest:request];

then functions to be used

-(NSString *)createSHA512:(NSString *)string
{
    const char *cstr = [string cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:string.length];
    uint8_t digest[CC_SHA512_DIGEST_LENGTH];
    CC_SHA512(data.bytes, (CC_LONG)data.length, digest);
    NSMutableString* output = [NSMutableString  stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    return output;
}