当我在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;
};
};
}
)
如何使用 payment_id 获取 transactionID ?
使用PayPal REST API获取 transactionID 有两个步骤, 1.获取AccessToken 2.使用AccessToken和 payment_id 获取 transactionID 。
要获取AccessToken,他们给出了一些参考代码,我需要帮助将该代码转换为最后两行的Objective-C,我知道标题(-H),但不知道-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"
我使用了下面的代码,但它给出了一个错误:
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
{
}
}
];
错误域= NSURLErrorDomain代码= -1012"无法完成操作。 (NSURLErrorDomain错误-1012。)" UserInfo = 0xabd2bf0 {NSErrorFailingURLKey = https://api.sandbox.paypal.com/v1/oauth2/token,NSErrorFailingURLStringKey = https://api.sandbox.paypal.com/v1/oauth2/token,NSUnderlyingError = 0xabcf950"操作无法完成。 (kCFErrorDomainCFNetwork错误-1012。)"}
在传递clientid和secret时遇到问题,有什么想法可以解决这个问题吗?
答案 0 :(得分:2)
设置凭据替换此代码
NSData * credential = [strClientIdAndSecretKey dataUsingEncoding:NSUTF8StringEncoding];
NSString *credentialString = [credential base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];
[theRequest setValue:[NSString stringWithFormat:@"Basic %@",credentialString] forHTTPHeaderField:@"Authorization"];
与
[theRequest setValue:strClientIdAndSecretKey forHTTPHeaderField:@"client_id:secret"]
答案 1 :(得分:1)
你能看到以下代码吗?我希望它会对你有所帮助..
Objective-c源代码:
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];
<强>响应:: 强>
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";
}
答案 2 :(得分:0)
您偶然翻转了这些值:
[theRequest addValue:@"Accept-Language" forHTTPHeaderField:@"en_US"];
应该是:
[theRequest addValue:@"en_US" forHTTPHeaderField:@"Accept-Language"];