Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Transaction id when using Credit Card option in iOS PayPal SDK

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?

like image 833
Sudheer Kumar Palchuri Avatar asked Feb 02 '26 01:02

Sudheer Kumar Palchuri


1 Answers

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"]          
like image 92
SoftwareDeveloper Avatar answered Feb 03 '26 18:02

SoftwareDeveloper