Skip to content

Instantly share code, notes, and snippets.

@jonathanwiesel
Created November 11, 2012 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathanwiesel/4053855 to your computer and use it in GitHub Desktop.
Save jonathanwiesel/4053855 to your computer and use it in GitHub Desktop.
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