6

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 = @"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]];

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.

Brian
  • 14,610
  • 7
  • 35
  • 43
Mani murugan
  • 1,792
  • 2
  • 17
  • 32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69431/discussion-between-iphonemaclover-and-mani-murugan). – 9to5ios Jan 23 '15 at 08:53
  • I am using your code but no luck, always getting error "Sorry, Some Problem Occurred". I am using: Post Url: https://test.payu.in/_payment Test Key: JBZaLc Test Salt: GQs7yium Do i need to signup and get my own credentials from PayUMoney for Testing. I would be really thankful if you can guide me to the right path. – Suraj Mirajkar Jul 23 '15 at 06:04

4 Answers4

10

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 = @"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];

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;
}
Mani murugan
  • 1,792
  • 2
  • 17
  • 32
  • 1
    hello, you are using payUmonkey SALT and merchant key. Did you use payUmoney? as i am working with PayUindia and its not working by key provided by them.Any help welcome – 9to5ios Feb 02 '15 at 09:59
  • hello @iphonemaclover , I am using a same code and it will work fine in some cases. means if i used this key = "gtKFFx" and salt = "eCwWELxi' , it will not work. Please tell me what is the reason behind this. – SGDev Nov 19 '15 at 10:17
  • @iphonemaclover , payUindia and payUmoney are different? – SGDev Nov 19 '15 at 11:18
  • @SumitGarg sorry for being late reply, No they have both have same provider, but according to need merchant purchase payuindia or payumoney. – 9to5ios Dec 10 '15 at 05:32
  • Where can I find sandbox credit card details for testing successfull payment? – Bhumit Mehta Jul 05 '16 at 06:36
  • 1
    One can find test credentials here. http://www.edugcet.in/franchisee_download/7c8660cce1870f606064ff76c258d76d.pdf – Bhumit Mehta Jul 05 '16 at 07:14
5

Finally i resolved the issue regarding PayU India (not payU,payU and payUindia have a slight difference as mention) integration (above code is for payU Money help alot) Download github Repo here

You only need to remove an extra parameter which is service_provider whose value is payu_paisa.

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 = @"YOURKEY";
    NSString *salt = @"YOURSALTKEY";

    NSString *amount = @"100";
    NSString *productInfo = @"Niceproduct";
    NSString *firstname = @"Deepak";
    NSString *email = @"iphonemaclover@gmail.com";
    NSString *phone = @"9212138007";
    NSString *surl = @"www.google.com";
    NSString *furl = @"www.google.com";


    NSString *hashValue = [NSString stringWithFormat:@"%@|%@|%@|%@|%@|%@|||||||||||%@",key,txnid1,amount,productInfo,firstname,email,salt];
    NSString *hash = [self createSHA512:hashValue];
    NSDictionary *parameters = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:txnid1,key,amount,productInfo,firstname,email,phone,surl,furl,hash
                                                                    , nil] forKeys:[NSArray arrayWithObjects:@"txnid",@"key",@"amount",@"productinfo",@"firstname",@"email",@"phone",@"surl",@"furl",@"hash", 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];

EDIT: how to handle URL payment is sucess or not

-(void)webViewDidFinishLoad:(UIWebView *)webView{
        if (web_view_PayU.isLoading)
        return;
    NSURL *requestURL = [[web_view_PayU request] URL];

    NSLog(@"requestURL=%@",requestURL);

    NSString *getStringFromUrl=[NSString stringWithFormat:@"%@",requestURL];

    if ([getStringFromUrl isEqualToString:@"https://test.payu.in/yoursucessurladdedhere "]||[getStringFromUrl isEqualToString:@"https://secure.payu.in/yoursucessurladdedhere "])
    {
       //SUCCESS ALERT
       //jump to place order API

    }
   else if ([getStringFromUrl isEqualToString:@"https://test.payu.in/yourfailureurladdedhere "]||[getStringFromUrl isEqualToString:@"https://secure.payu.in/yourfailureurladdedhere"])
    {
        // FAIL ALERT
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Sorry!" message:@"Your Order Not Successfull!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        alert.tag=2222;

        [alert show];
    }

}

For Android Integration of Pay U check link--

9to5ios
  • 5,319
  • 2
  • 37
  • 65
  • Can anyone tell how I will get to know that my payment is succeeded or failed ? The above code is working fine for me and showing payment screen, but what about payment result ? Please help me as I am desperate to the answer. I am stuck at this point. – NSPratik Feb 12 '15 at 13:30
  • 1
    Hello pratik, please check the github repo,it includes the code to handle the payment sucess of fail,quick hit,just check the sucess and fail url load at webview delegate method and according to it add your condition...hope it will help – 9to5ios Feb 16 '15 at 05:14
  • Thanks you so much for your reply iphonemaclover. I have implemented as per your example. But my main point is how I will know that payment succeeded or not in **-(void)webViewDidFinishLoad:(UIWebView *)webView** method ? I have made request with checksum and everything using webview, but this is the point I have stuck. – NSPratik Feb 16 '15 at 06:25
  • 1
    check my edit answer now,simily compair the load url with the url u got it something url/urlfail or url/urlsucess just check if the url string contain it or not... – 9to5ios Feb 16 '15 at 06:38
  • hi @iphonemaclover i have use you github example and try to run using my key but return error 404 page not fount plese help me bcz i need integrate payu payment in ios. – Dharmesh Dhorajiya Jul 26 '15 at 06:40
  • must confirm, you are using payu or payUindia,my example is for payUindia. And must check your NSString *key = @"YOURKEY"; NSString *salt = @"YOURSALTKEY"; is correct. – 9to5ios Jul 29 '15 at 04:51
  • @DharmeshDhorajiya for only integration follow Mani murugan accepted answer. – 9to5ios Jul 29 '15 at 04:52
  • yes i have try both but i get 404 page error some problem occur like that @iphonemaclover – Dharmesh Dhorajiya Jul 29 '15 at 05:00
  • and i tried then ask question, see this http://stackoverflow.com/questions/31681781/payumoney-getway-error-occur-in-ios @iphonemaclover – Dharmesh Dhorajiya Jul 29 '15 at 05:02
4

I mailed to the PayUMoney Technical team and got my answer why i am getting error "Sorry, Some Problem Occurred."

Got a quick reply for the Technical Team:

Recently, we have done some modifications in test environment due to which test key-JBZaLc and salt-GQs7yium will not work anymore.

In order to test the gateway using a test key and salt, kindly follow these steps:
1 - Go on https://test.payumoney.com
2 - Sign up as a merchant - use any of your valid email ids - kindly do not use a random email id.
3 - Complete the "Business Details"  - you may use PAN no. ABCDE1234F and DOB - 01/04/1990
4 - Complete "Bank Account Details" (You may use IFSC- ALLA0212632)
5 - Go to below mentioned location to get the Test Merchant Id :     
 Seller Dashboard -> Settings -> My account -> Profile Settings

Once you provide your test merchant id, we will approve it so that you can find your test key and salt at :
Seller Dashboard -> Settings -> My account -> Merchant Key - Salt

Download Github Repository

9to5ios
  • 5,319
  • 2
  • 37
  • 65
Suraj Mirajkar
  • 1,369
  • 10
  • 23
  • hi @Suraj Mirajkar i have use above code but i return error 404 page not found and use this link https://test.payu.in/_payment please help. – Dharmesh Dhorajiya Jul 26 '15 at 06:44
0

For swift Version

1) import into bridging header

** import CommonCrypto/CommonDigest.h **

2) after

func sha512Hex( string: String) -> String {
    var digest = [UInt8](repeating: 0, count: Int(CC_SHA512_DIGEST_LENGTH))
    if let data = string.data(using: String.Encoding.utf8) {
        let value =  data as NSData
        CC_SHA512(value.bytes, CC_LONG(data.count), &digest)

    }
    var digestHex = ""
    for index in 0..<Int(CC_SHA512_DIGEST_LENGTH) {
        digestHex += String(format: "%02x", digest[index])
    }

    return digestHex
}

3) install pod PlugNPlay

4) after follow step provided PlugNPlay

Hardik Bar
  • 1,660
  • 18
  • 27