在iOS中实施自适应支付方面存在一些困难,遗憾的是,PayPal网站或响应的文档很少。这是代码:
- (void)makePaymentSandbox{
NSError *error;
//NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
//NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"https://svcs.sandbox.paypal.com/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.com", @"cancelUrl",
@"http:\\www.cleverlyapp.com", @"returnUrl",
@"ReturnAll", @"requestEnvelope.detailLevel",
@"en_US", @"requestEnvelope.errorLanguage",
@"seneder@email.com", @"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";
};
}
答案 0 :(得分:1)
请检查这些:
有些与编码有关,设置为JSON
,但实际上是URL
- 编码等,其他与使用的货币和货币格式有关(例如发送的值不应包含货币)标志等..)
答案 1 :(得分:1)
终于开始工作了。标题是正确的,输入数据有一些问题。这是代码的正确版本:
- (void)makePaymentSandboxWithPreapprovalToEmail:(NSString *)toEmail withCurrency:(NSString *)currency andAmount:(NSString *)moneyAmount completition:(void (^)(BOOL, NSString *))block{
NSError *error;
NSURL *url = [NSURL URLWithString:@"https://svcs.sandbox.paypal.com/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.com", @"cancelUrl",
@"http:\\www.google.com", @"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", 它的使用取决于您是否希望使用预先批准。这详细解释了预先批准的使用:https://developer.paypal.com/webapps/developer/docs/classic/adaptive-payments/ht_ap-basicPreapproval-curl-etc/。替换上面代码中的输入值,您将获得预批准密钥。
答案 2 :(得分:0)
工作代码。请检查。
int amount = 195;
// int x = 146;
NSDictionary *parameters;
parameters = @{
@"actionType" : @"PAY",
@"currencyCode" : @"USD",
@"receiverList" : @{@"receiver" : @[@{@"amount": [NSNumber numberWithInt:amount],@"email":@"testerigniva12@gmail.com"}]},
@"returnUrl" : @"https://example.com/return",
@"cancelUrl" : @"https://example.com/cancel",
@"requestEnvelope" : @{@"errorLanguage":@"en_US", @"detailLevel":@"ReturnAll"}
};
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters
options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
error:&error];
NSURL *url = [NSURL URLWithString:@"https://svcs.sandbox.paypal.com/AdaptivePayments/Pay"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
[request setHTTPMethod:@"POST"];
//headers
[request addValue:@"testios1_api1.grr.la" forHTTPHeaderField:@"X-PAYPAL-SECURITY-USERID"];
[request addValue:@"TP3DRR4WLYAJ5HWR" forHTTPHeaderField:@"X-PAYPAL-SECURITY-PASSWORD"];
[request addValue:@"AKiJVI-zRf1GGbfcTE2iPRb31l2ZAdq7HY4WrG6uxNAhc79Vtg7myGk3" 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"];
[request setHTTPBody:jsonData];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
// do something with the data
NSDictionary* revDicn =[NSDictionary dictionary];
revDicn = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSLog(@"Response: %@", revDicn);
}];
[dataTask resume];