在 iOS PayPal SDK 中使用信用卡选项时如何获取交易 ID
Posted
技术标签:
【中文标题】在 iOS PayPal SDK 中使用信用卡选项时如何获取交易 ID【英文标题】:How to get Transaction id when using Credit Card option in iOS PayPal SDK 【发布时间】:2014-01-02 01:41:51 【问题描述】:我在 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
];
Error Domain=NSURLErrorDomain Code=-1012 "操作无法完成。(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 "操作无法完成。(kCFErrorDomainCFNetwork 错误 -1012。)"
我在传递 clientid 和 secret 时遇到问题,有什么想法可以解决这个问题吗?
【问题讨论】:
参考链接:developer.paypal.com/webapps/developer/docs/integration/direct/… 您好,我也遇到了同样的问题,能否请您发布代码如何使用该 URL? 【参考方案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】:你不小心把这些值翻转了:
[theRequest addValue:@"Accept-Language" forHTTPHeaderField:@"en_US"];
应该是:
[theRequest addValue:@"en_US" forHTTPHeaderField:@"Accept-Language"];
【讨论】:
【参考方案3】:要设置凭据替换此代码
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"]
【讨论】:
试试这个,它对我有用并返回一个有效的响应以上是关于在 iOS PayPal SDK 中使用信用卡选项时如何获取交易 ID的主要内容,如果未能解决你的问题,请参考以下文章
即使在调用 [PayPalMobile clearAllUserData] 之后,Paypal ios SDK 也不会清除以前使用过的信用卡