List of all Iphone and Blackberry Development codes in a one click Iphone,objective C,xcode,blackberry Development,iOS Development

Friday, May 5, 2017

ios PayFort Payment Gateway Integration

Reference https://docs.payfort.com/
NSMutableData *webDataglobal;
NSString *sdk_token;
PayFortController *payFort;
Add PayFortDelegate in .h file.

payFort = [[PayFortController  alloc]initWithEnviroment:KPayFortEnviromentSandBox];
payFort.delegate = self;
payFort.IsShowResponsePage = YES;
             
NSMutableString *post = [NSMutableString string];
[post appendFormat:@"TESTSHAINaccess_code=%@", @"accesscodeFromAdminPortal"];
[post appendFormat:@"device_id=%@",  [payFort getUDID]];
[post appendFormat:@"language=%@", @"en"];
[post appendFormat:@"merchant_identifier=%@", @"merchantIdentifierFromAdminPortal"];
post appendFormat:@"service_command=%@", @"SDK_TOKENTESTSHAIN"];
NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys:
                                           @"SDK_TOKEN", @"service_command",
                                           @"merchantIdentifierFromAdminPortal", @"merchant_identifier",
                                           @"accesscodeFromAdminPortal", @"access_code",                                           [self sha1Encode:post],@"signature",
                                           @"en", @"language",
                                           [payFort getUDID], @"device_id",
                                           nil];
             
NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error];
NSString *BaseDomain =@"https://sbpaymentservices.payfort.com/FortAPI/paymentApi";
NSString *urlString = [NSString stringWithFormat:@"%@",BaseDomain];
//NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%ld",[postdata length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
//[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postdata];
//NSLog(@"signal mname %@", signalNameFunction);
NSLog(@"url string %@",urlString);
NSLog(@"url post %@",tmp);
// NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request  delegate:self];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
if(connection){
  webDataglobal = [NSMutableData data];
 }
else{
  NSLog(@"The Connection is null");
}




-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    [webDataglobal setLength: 0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [webDataglobal appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"error %@",error);
      payFort.IsShowResponsePage = NO;
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    
    id collection = [NSJSONSerialization JSONObjectWithData:webDataglobal options:0 error:nil];
    NSLog(@"receive data %@",collection);
    sdk_token = collection[@"sdk_token"];
    [self launch];
    
}

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

-(void) launch{
    //[NSString stringWithFormat:@"%d", [self.cartResponse.amount_payable intValue]]
    NSMutableDictionary *request = [[NSMutableDictionary alloc]init];
    [request setValue:[NSString stringWithFormat:@"%d", [self.cartResponse.amount_payable intValue]*100] forKey:@"amount"];
    [request setValue:@"PURCHASE" forKey:@"command"];
    [request setValue:@"QAR" forKey:@"currency"];
    [request setValue:@"customerEmail" forKey:@"customer_email"];
    [request setValue:@"en" forKey:@"language"];
    [request setValue:[NSString stringWithFormat:@"%lld", [@(floor([[NSDate date] timeIntervalSince1970] * 1000)) longLongValue]] forKey:@"merchant_reference"];
    [request setValue:sdk_token forKey:@"sdk_token"];
    [payFort setPayFortRequest:request]; // Must Send [payFort callPayFort:self]
    
    payFort.IsShowResponsePage = YES;
    [payFort callPayFort:self];
}

- (void)sdkResult:(id)response{
    NSLog(@"Result--------%@",response);
    if([[response objectForKey:@"status"] intValue ]==14 ){
     //success
    }else{
    //fail
    }
}

2 comments:

  1. 1. How to register PAYFORT test account
    2. How to get the merchant_identifier
    3. How to get the access_code
    4. How to get the service_command

    I do not have any Idea about PayPort.Please share any Example program related to swift and also explain about PayFort.

    ReplyDelete