Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
POST request to obtain customized MercadoPago's payment URL
@implementation MercadoPago
-(NSString *) generateMercadoPagoURL:{
NSMutableURLRequest *request = nil;
NSError *errorReturned = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
//we build the dictionary with the post's parameters
NSMutableDictionary *aDictionary = [[NSMutableDictionary alloc] init];
[aDictionary setObject:@"your_store_acc_id" forKey:@"acc_id"];
[aDictionary setObject:@"your_store_account_token" forKey:@"token"];
[aDictionary setObject:@"http://jonathanwiesel.com/success" forKey:@"url_succesfull"];
[aDictionary setObject:@"VEN" forKey:@"currency"];
[aDictionary setObject:@"Jonathan" forKey:@"cart_name"];
[aDictionary setObject:@"Wiesel" forKey:@"cart_surname"];
[aDictionary setObject:@"jonathanwiesel@gmail.com" forKey:@"cart_email"];
[aDictionary setObject:@"10000" forKey:@"price"];
[aDictionary setObject:@"10000234R" forKey:@"item_id"];
[aDictionary setObject:@"Ticket out of here" forKey:@"name"];
NSMutableString *post= [[NSMutableString alloc] init];
//we check for special characters in each parameter and build the post string
for (NSString* key in aDictionary) {
NSString *strValue = [NSString stringWithFormat:@"%@",[aDictionary objectForKey:key]];
strValue = [strValue stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"]; //found in URLs
strValue = [strValue stringByReplacingOccurrencesOfString:@"%" withString:@"%25"]; //found in tokens
//if you need more char escapes refere to http://www.december.com/html/spec/esccodes.html
NSString *current = [NSString stringWithFormat:@"&%@=%@",key,strValue];
[post appendString: current];
}
//we build the request
request = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:@"https://www.mercadopago.com/mlv/orderpreference"]];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
//we send the request
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
//and we recieve the post answer
NSString* url = [[NSString alloc] initWithData:urlData
encoding:NSUTF8StringEncoding];
if ([url hasPrefix:@"http"]) {
return url;
}else{
return @"Error contacting MercadoPago";
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment