ios - PayUMoney 支付网关问题

标签 ios iphone payu

我必须在我的 iOS 应用程序中集成 PayUMoney 支付网关。他们没有适用于 iOS 的 SDK。所以我必须在 webview 中加载一些 web URL 来支付。我的参数是

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 = @"mani.ingenius@gmail.com";
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]];

我必须对我的测试 URL (https://test.payu.in/_payment) 使用 POST 方法并且需要传递参数。我在字典(“参数”)中有所有带有键和值的参数。所以我尝试了以下代码

 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;
}

但是当我运行它时,它说“缺少强制参数 tnxid”。但是我已经传递了你可以在参数字典中看到的 tnxid。如果我正确地传递了所有内容,那么结果将是用户可以在其中选择银行详细信息等的网页,我必须将其加载到我的 WebView 中。

请帮我找出我做错了什么或者我应该怎么做才能得到正确的结果。

最佳答案

我成功找到了答案。下面列出了我的工作代码

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 = @"mani.ingenius@gmail.com";
    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];

然后要用到的函数

-(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;
}

关于ios - PayUMoney 支付网关问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27898349/

相关文章:

iphone - 如何在 iOS 应用程序中使用 "open in..."功能?

android - PayUBiz-安卓 : Transaction failed due to incorrectly calculated hash parameter

ios - 如何暂停 DynamicAnimator

ios - 如何使用终端使用 xCode 命令行工具?

ios - 异步请求运行缓慢 - iOS

ios - 通过解析将时间戳添加到 MapView 注释?

iphone - 有没有适用于 iPhone 的手写识别库?

iphone - 设置模态 ImageView ?