This line solved this for me:
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
質問
I'm trying to use paypal Express checkout from a native iOS app (can't use paypal SDK because apple needs me to go through safari to make the payment). Problem is when i try to call the SetExpressCheckout API method. Here is the curl statement that they provide at the website:
curl -s --insecure https://api-3t.sandbox.paypal.com/nvp -d
"USER=callerID # User ID of the PayPal caller account
&PWD=callerPswd # Password of the caller account
&SIGNATURE=callerSig # Signature of the caller account
&METHOD=SetExpressCheckout
&VERSION=93
&PAYMENTREQUEST_0_PAYMENTACTION=SALE # type of payment
&PAYMENTREQUEST_0_AMT=19.95 # amount of transaction
&PAYMENTREQUEST_0_CURRENCYCODE=USD # currency of transaction
&RETURNURL=http://www.example.com/success.html # URL of your payment confirmation page
&CANCELURL=http://www.example.com/cancel.html" # URL redirect if customer cancels payment
I need to pass this to AFNetworking so i can do it inside the app, i've tried this but it's not working:
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: kPaypalClientId,@"USER",
kPaypalPassword, @"PWD",
kPaypalSignature, @"SIGNATURE",
@"SetExpressCheckout", @"METHOD",
@"93", @"VERSION",
@"SALE",@"PAYMENTREQUEST_0_PAYMENTACTION",
@"19.95",@"PAYMENTREQUEST_0_AMT",
@"USD", @"PAYMENTREQUEST_0_CURRENCYCODE",
@"shareapp://bien",@"RETURNURL",
@"shareapp://cancel", @"CANCELURL", nil];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[manager POST:@"https://api-3t.sandbox.paypal.com/nvp" parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=%@",tokenValue]]];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
This is the error i'm getting:
Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/plain" UserInfo=0xba7de40 {NSErrorFailingURLKey=https://api-3t.sandbox.paypal.com/nvp, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xd06e720> { URL: https://api-3t.sandbox.paypal.com/nvp } { status code: 200, headers {
Connection = "keep-alive";
"Content-Length" = 230;
"Content-Type" = "text/plain; charset=utf-8";
DC = "origin1-api-3t.sandbox.paypal.com";
Date = "Tue, 15 Apr 2014 13:36:59 GMT";
Server = Apache;
"Set-Cookie" = "DC=origin1-api-3t.sandbox.paypal.com; secure";
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/plain}
Thank you and sorry for my english!!
解決
This line solved this for me:
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
他のヒント
You need to set the header: Content-Type: application/x-www-form-urlencoded
when posting to the Paypal server.