Reputation: 2954
I got the below response when i use credit card in PayPal:
{
client = {
environment = sandbox;
"paypal_sdk_version" = "1.4.3";
platform = iOS;
"product_name" = "PayPal iOS SDK";
};
payment = {
amount = "1.00";
"currency_code" = USD;
"short_description" = Order;
};
"proof_of_payment" = {
"rest_api" = {
"payment_id" = "PAY-0HB84369BG507770XKKV7ZYI";
state = approved;
};
};
}
)
How to get the transactionID by using payment_id?
There are two steps to get the transactionID using PayPal REST API, 1. Get AccessToken 2. Use AccessToken and payment_id to get transactionID.
To get the AccessToken, they given some reference code which is below, i need help to convert that code into Objective-C for last two lines, i know for headers (-H), but don't know for -u,-d
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" \
-d "grant_type=client_credentials"
I used the below code , but it is giving an error:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.sandbox.paypal.com/v1/oauth2/token"]];
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:url];
[theRequest addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[theRequest addValue:@"Accept-Language" forHTTPHeaderField:@"en_US"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSString *strClientIdAndSecretKey=[NSString stringWithFormat:@"%@:%@",kPayPalClientId,kPayPalSecret];
[theRequest setValue:strClientIdAndSecretKey forHTTPHeaderField:@"client_id:secret"];
NSString *parameterString = [NSString stringWithFormat:@"grant_type=client_credentials"];
NSString *msgLength = [NSString stringWithFormat:@"%d", [parameterString length]];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
//do post request for parameter passing
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[parameterString dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"Headers %@",[theRequest allHTTPHeaderFields]);
[NSURLConnection sendAsynchronousRequest:theRequest
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *dataOrder, NSError *error)
{
if(error == nil)
{
NSString *jsonString = [[[NSString alloc] initWithData:dataOrder encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"Result is %@",jsonString);
}
else
{
}
}
];
Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0xabd2bf0 {NSErrorFailingURLKey=https://api.sandbox.paypal.com/v1/oauth2/token, NSErrorFailingURLStringKey=https://api.sandbox.paypal.com/v1/oauth2/token, NSUnderlyingError=0xabcf950 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1012.)"}
I feel problem while passing clientid and secret, any ideas to fix this issue?
Upvotes: 3
Views: 990
Reputation: 2283
Can you plz see the following code i hope it will be helpful to you..
Objective-c source code:
NSString *clientID = @"YOUR_CLIENT_ID";
NSString *secret = @"YOUR_SECRET";
NSString *authString = [NSString stringWithFormat:@"%@:%@", clientID, secret];
NSData * authData = [authString dataUsingEncoding:NSUTF8StringEncoding];
NSString *credentials = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:0]];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
[configuration setHTTPAdditionalHeaders:@{ @"Accept": @"application/json", @"Accept-Language": @"en_US", @"Content-Type": @"application/x-www-form-urlencoded", @"Authorization": credentials }];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.sandbox.paypal.com/v1/oauth2/token"]];
request.HTTPMethod = @"POST";
NSString *dataString = @"grant_type=client_credentials";
NSData *theData = [dataString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromData:theData completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
NSLog(@"data = %@", [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]);
}
}];
[task resume];
Response::
data = {
"access_token" = "YOUR_NEW_ACCESS_TOKEN";
"app_id" = "APP-YOUR_APP_ID";
"expires_in" = 34400;
scope = "https://uri.paypal.com/services/subscriptions https://api.paypal.com/v1/payments/.* https://api.paypal.com/v1/vault/credit-card https://uri.paypal.com/services/applications/webhooks openid https://uri.paypal.com/services/invoicing https://api.paypal.com/v1/vault/credit-card/.*";
"token_type" = "YOUR_Token_Type";
}
Upvotes: 1
Reputation: 4138
You have these values flipped by accident:
[theRequest addValue:@"Accept-Language" forHTTPHeaderField:@"en_US"];
It should be:
[theRequest addValue:@"en_US" forHTTPHeaderField:@"Accept-Language"];
Upvotes: 0
Reputation: 243
To Set credential replace this code
NSData * credential = [strClientIdAndSecretKey dataUsingEncoding:NSUTF8StringEncoding];
NSString *credentialString = [credential base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];
[theRequest setValue:[NSString stringWithFormat:@"Basic %@",credentialString] forHTTPHeaderField:@"Authorization"];
with
[theRequest setValue:strClientIdAndSecretKey forHTTPHeaderField:@"client_id:secret"]
Upvotes: 2