在iOS中实施自适应支付时遇到了一些困难,不幸的是,贝宝(PayPal)的网站或响应中的文档很少.这是代码:
Having some difficulties in implementing Adaptive payments in iOS and unfortunately there is very little documentation on PayPal's website or response. This is the code:
- (void)makePaymentSandbox{ NSError *error; //NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; //NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil]; NSURL *url = [NSURL URLWithString:@"svcs.sandbox.paypal/AdaptivePayments/Pay"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; //setting [request setHTTPMethod:@"POST"]; //headers [request addValue:@"alex-facilitator_api1.fastwebnet.it" forHTTPHeaderField:@"X-PAYPAL-SECURITY-USERID"]; [request addValue:@"FW79EZXASW69NE8X" forHTTPHeaderField:@"X-PAYPAL-SECURITY-PASSWORD"]; [request addValue:@"ABZua9nnv9oieyN4MwVt15YdgetaJHcyzqOHjkLbuM-bGRoI7WRS" forHTTPHeaderField:@"X-PAYPAL-SECURITY-SIGNATURE"]; //NV [request addValue:@"JSON" forHTTPHeaderField:@"X-PAYPAL-REQUEST-DATA-FORMAT"]; [request addValue:@"JSON" forHTTPHeaderField:@"X-PAYPAL-RESPONSE-DATA-FORMAT"]; [request addValue:@"APP-80W288712P519543T" forHTTPHeaderField:@"X-PAYPAL-APPLICATION-ID"]; [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request addValue:@"en_US" forHTTPHeaderField:@"Accept-Language"]; //data /*NSString *userUpdate =[NSString stringWithFormat:@"clientDetails.applicationId=%@&actionType=%@",@"APP-80W284485P519543T", @"PAY",nil]; NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding]; [request setHTTPBody:data1]; [request setValue: [NSString stringWithFormat:@"%lu", (unsigned long)[data1 length]] forHTTPHeaderField:@"Content-Length"];*/ NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"PAY", @"actionType", @"USD", @"currencyCode", @"http:\\www.cleverlyapp", @"cancelUrl", @"http:\\www.cleverlyapp", @"returnUrl", @"ReturnAll", @"requestEnvelope.detailLevel", @"en_US", @"requestEnvelope.errorLanguage", @"seneder@email", @"senderEmail", @"0.1", @"receiverList.receiver(0).amount", @"a-buyer@fastwebnet.it", @"receiverList.receiver(0).email", @"0.1", @"receiverList.receiver(1).amount", @"a-facilitator@fastwebnet.it", @"receiverList.receiver(1).email", @"APP-80W284485P519543T", @"clientDetails.applicationId", nil]; NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error]; [request setHTTPBody:postData]; [NSURLConnection connectionWithRequest:request delegate:self]; }以下是回复:
String: { error = ( { category = Application; domain = PLATFORM; errorId = 580001; message = "Invalid request: {0}"; severity = Error; subdomain = Application; } ); responseEnvelope = { ack = Failure; build = 17325060; correlationId = e82ede718b929; timestamp = "2015-07-14T09:50:06.222-07:00"; }; }推荐答案
最后使它起作用.标头正确,输入数据有问题.这是正确的代码版本:
Finally got it to work. The headers are correct, the input data had some problems. This is the correct version of the code:
- (void)makePaymentSandboxWithPreapprovalToEmail:(NSString *)toEmail withCurrency:(NSString *)currency andAmount:(NSString *)moneyAmount completition:(void (^)(BOOL, NSString *))block{ NSError *error; NSURL *url = [NSURL URLWithString:@"svcs.sandbox.paypal/AdaptivePayments/Pay"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; //setting [request setHTTPMethod:@"POST"]; //headers [request addValue:@"alex.rietmann-facilitator_api1.fastwebnet.it" forHTTPHeaderField:@"X-PAYPAL-SECURITY-USERID"]; [request addValue:@"FW7ADTYZFP68XE0X" forHTTPHeaderField:@"X-PAYPAL-SECURITY-PASSWORD"]; [request addValue:@"ABSua9nnv9nnkoN4MwVt15YdgetaJHcyzqOHjkLbuM-bGRoI7JRS" forHTTPHeaderField:@"X-PAYPAL-SECURITY-SIGNATURE"]; //NV [request addValue:@"JSON" forHTTPHeaderField:@"X-PAYPAL-REQUEST-DATA-FORMAT"]; [request addValue:@"JSON" forHTTPHeaderField:@"X-PAYPAL-RESPONSE-DATA-FORMAT"]; [request addValue:@"APP-80W284485P519543T" forHTTPHeaderField:@"X-PAYPAL-APPLICATION-ID"]; [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request addValue:@"en_US" forHTTPHeaderField:@"Accept-Language"]; //other email NSDictionary *receiver0 = [[NSDictionary alloc] initWithObjectsAndKeys: toEmail, @"email", moneyAmount, @"amount", @"true", @"primary", nil]; //my account NSDictionary *receiver1 = [[NSDictionary alloc] initWithObjectsAndKeys: @"alex.rietmann-facilitator@fastwebnet.it", @"email", @"2", @"amount", nil]; NSDictionary *options0 = [[NSDictionary alloc] initWithObjectsAndKeys: [NSArray arrayWithObjects:receiver0, receiver1, nil], @"receiver", nil]; NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"PAY", @"actionType", @"EACHRECEIVER", @"feesPayer", @"true", @"reverseAllParallelPaymentsOnError", currency, @"currencyCode", [PaymentManager readPaymentCode], @"preapprovalKey", [PaymentManager readPaymentEmail], @"senderEmail", @"http:\\www.apple", @"cancelUrl", @"http:\\www.google", @"returnUrl", [[NSDictionary alloc] initWithObjectsAndKeys:@"en_US", @"errorLanguage", @"detailLevel", @"ReturnAll", nil], @"requestEnvelope", //[[NSDictionary alloc] initWithObjectsAndKeys: options0, @"0", nil], @"receiverList", options0, @"receiverList", [[NSDictionary alloc] initWithObjectsAndKeys:@"APP-80W284485P519543T", @"applicationId", [self getIPAddress], @"ipAddress", nil], @"clientDetails", //@"APP-80W284485P519543T", @"clientDetails.applicationId", //[self getIPAddress], @"clientDetails.ipAddress", nil]; NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error]; [request setHTTPBody:postData]; MyConnection * connection = [[MyConnection alloc]initWithRequest:request]; [connection setCompletitionBlock:^(id obj, NSError *err) { if (!err) { NSError *error = nil; NSDictionary* revDicn =[NSDictionary dictionary]; revDicn = [NSJSONSerialization JSONObjectWithData:obj options:NSJSONReadingMutableContainers error:&error]; NSLog(@"Response: %@", revDicn); if ([[revDicn objectForKey:@"paymentExecStatus"] isEqualToString:@"COMPLETED"]) { block(YES, [revDicn objectForKey:@"payKey"]); }else{ block(NO, @""); } } else { //There was an error block(NO, @""); } }]; [connection start]; }此行是可选的: [PaymentManager readPaymentCode],@"preapprovalKey", 它的使用取决于您是否要使用预先批准.这详细说明了预先批准的用法: developer.paypal/webapps/developer/docs/classic/adaptive-payments/ht_ap-basicPreapproval-curl-etc/.替换上面代码中的输入值,您将获得预批准密钥.
This line is optional: [PaymentManager readPaymentCode], @"preapprovalKey", The use of it depends on whether you wish to use pre-approval or not. This explains in detail the use of pre-approval: developer.paypal/webapps/developer/docs/classic/adaptive-payments/ht_ap-basicPreapproval-curl-etc/. Replace the input values in the code above and you will get the pre-approval key.
更多推荐
来自iOS的PayPal错误580001 HTTP请求
发布评论