12

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:@"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];
}

Here's the response:

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";
    };
}
Alessandro
  • 4,000
  • 12
  • 63
  • 131
  • do any of the answers from http://stackoverflow.com/questions/15854931/paypal-implicit-payment-error-580001 help you out? – Timothy Jeffcoat Jul 20 '15 at 18:08
  • Had seen that page, didn't help – Alessandro Jul 20 '15 at 20:05
  • By adding the senderEmail, you are calling it as an Implicit Payment. This is only allowed if that sender is the API caller. The email address you have (sender@email.com) would have to be one added to the PayPal account attached to the API credentials of the API caller. If that isn't the case, you would get an error. Not sure if it would be the error your are getting. – PayPal_MSI_Robert Jul 28 '15 at 19:06
  • check these please http://stackoverflow.com/questions/18392145/adaptive-payments-pay-api-error-580001, http://stackoverflow.com/questions/20252360/580001-invalid-request-0-paypal-php, https://www.paypal-community.com/t5/About-Payments-Archive/Error-580001/td-p/485736, some have to do with encoding, set as JSON, but sent as URL-encoded, etc, others have to do with currency and cuurency format – Nikos M. Aug 19 '15 at 14:55

3 Answers3

1

check these please:

  1. Adaptive Payments Pay API Error 580001
  2. 580001 Invalid request: {0} PayPal (PHP)
  3. Error 580001

some have to do with encoding, set as JSON, but actualy sent as URL-encoded, etc, others have to do with currency and currency format used (e.g values sent should not include currency sign etc..)

Community
  • 1
  • 1
Nikos M.
  • 8,033
  • 4
  • 36
  • 43
1

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:@"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];
}

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: https://developer.paypal.com/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.

Alessandro
  • 4,000
  • 12
  • 63
  • 131
  • did my answer helped? what exactly was the problem? – Nikos M. Aug 19 '15 at 17:36
  • 1
    @NikosM. your answer has useful links but I actually figured it before you posted... The problem was the packaging of the input data which had be done in the form of [dictionary, @"key"] like [[NSDictionary alloc] initWithObjectsAndKeys:@"en_US", @"errorLanguage", nil], @"requestEnvelope", – Alessandro Aug 19 '15 at 17:40
0

Working code. Please check.

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];
yogesh wadhwa
  • 711
  • 8
  • 16