Has anyone implemented the PayPal API through a native iPhone app?

Re-Update:
As answered below this code may still be useful for the purchase of physical goods


Update:

Although this code works, App Store terms won’t allow you to use this code within an app.


Original Answer:

I figured this out after some heavy API research. Below is a method that creates an HTTP POST to send to Paypal and makes an NSURLRequest. You can fill in the appropriate string format variables. I used HTTP Client to check what I was doing.

- (void)sendPayPalRequestPOST{

perfomingSetMobileCheckout=YES;
recordResults = FALSE;

NSString *parameterString = [NSString stringWithFormat:@"USER=%@&PWD=%@&SIGNATURE=%@&VERSION=57.0&METHOD=SetMobileCheckout&AMT=%.2f&CURRENCYCODE=USD&DESC=%@&RETURNURL=%@", userName, password, signature, self.donationAmount, @"Some Charge", returnCallURL];

NSLog(parameterString);

NSURL *url = [NSURL URLWithString:paypalUrlNVP];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [parameterString length]];

[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [parameterString dataUsingEncoding:NSUTF8StringEncoding]];


NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if( theConnection ){
    webData = [[NSMutableData data] retain];
    [self displayConnectingView];

}else{
    NSLog(@"theConnection is NULL");
}
}

After this you need to parse the response, grab the session key and create a UIWebView to take them to the mobile paypal site. Paypal lets you specify a “return URL” which you can make anything you want. Just keep checking the UIWebview in the delegate method for this address and then you know the transaction is complete.

Then you send one more HTTP Post (similar to the one above) to Paypal to finalize the transaction. You can find the API information in the Paypal Mobile Checkout API docs.

Leave a Comment