📜 ⬆️ ⬇️

Integrating PayPal Here into an iOS application

While working on one project, I was faced with the need to integrate PayPal Here into applications. PayPal never went to developers for a meeting, it was quite difficult to integrate, but with PayPal Here the situation is somewhat different (there is a whole example on gitHub). In short, if you are the owner of an online store, online auction, or even want to make a payment in your application, then you will be interested in reading about how you can integrate PayPal Here in iOS.

image

By the way, despite the large number of online stores that are paid through PayPal, the PayPal Here service is still not available in the Russian side.
')


What is PayPal Here?



PayPal Here is a mobile terminal through which you can make payments, and is a direct competitor to the similar program from Square. In addition to making payments by credit card, PayPal Here can be used to pay by check and to verify financial transactions over a specific period of time. You can also send invoices to the recipient's e-mail through the software from PayPal Here, and the buyer, upon receiving the invoice, will be able to pay it.

I will give an example of integration for iOS and Oblective-C. If someone is interested in Android, then its support is also available, but I have not worked with it.

Integration process.



Unfortunately, PayPal does not provide any separate libraries for conducting transactions directly from its application. But safety is paramount. We have to call a third-party application - we make up the URL and do:

UIApplication *application = [UIApplication sharedApplication]; if ([application canOpenURL:pphUrl]){ [application openURL:pphUrl]; } else { NSURL *url = [NSURL URLWithString:@"itms://itunes.apple.com/us/app/paypal-here/id505911015?mt=8"]; [application openURL:url]; } 

where pphUrl contains all the payment data.

Next, create a simple UIViewController to edit the data:

 @interface PPViewController : UIViewController <UITextFieldDelegate> @property (weak, nonatomic) IBOutlet UITextField *priceField; @property (weak, nonatomic) IBOutlet UITextField *taxField; @property (weak, nonatomic) IBOutlet UITextField *nameField; @property (weak, nonatomic) IBOutlet UITextField *quantityField; @property (weak, nonatomic) IBOutlet UITextField *descriptionField; @end 


we add all the required request fields:

 NSMutableDictionary *shirt = [NSMutableDictionary dictionary]; NSMutableDictionary *itemList = [NSMutableDictionary dictionary]; NSMutableDictionary *invoice = [NSMutableDictionary dictionary]; [shirt setObject:self.taxField.text forKey:@"taxRate"]; [shirt setObject:self.priceField.text forKey:@"unitPrice"]; [shirt setObject:self.quantityField.text forKey:@"quantity"]; [shirt setObject:self.nameField.text forKey:@"name"]; [shirt setObject:self.descriptionField.text forKey:@"description"]; [shirt setObject:@"Tax" forKey:@"taxName"]; NSMutableArray *items = [NSMutableArray arrayWithObject:shirt]; [itemList setObject:items forKey:@"item"]; [invoice setObject:@"DueOnReceipt" forKey:@"paymentTerms"]; [invoice setObject:@"0" forKey:@"discountPercent"]; [invoice setObject:@"USD" forKey:@"currencyCode"]; [invoice setObject:@"merchant@ebay.com" forKey:@"merchantEmail"]; [invoice setObject:@"foo@bar.com" forKey:@"payerEmail"]; [invoice setObject:itemList forKey:@"itemList"]; 


and go to the final stage (here we need JSONKit ):

 NSString *jsonInvoice = [invoice JSONString]; NSString *encodedInvoice = [jsonInvoice stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; NSString *encodedPaymentTypes = [@"cash,card,paypal" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; NSString *encodedReturnUrl = [@"myapp://handler?{result}?Type={Type}&InvoiceId={InvoiceId}&Tip={Tip}&Email={Email}&TxId={TxId}" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; NSString *pphUrlString = [NSString stringWithFormat:@"paypalhere://takePayment?accepted=%@&returnUrl=%@&invoice=%@&step=choosePayment", encodedPaymentTypes, encodedReturnUrl, encodedInvoice]; NSURL *pphUrl = [NSURL URLWithString:pphUrlString]; 

after that we open the application by pphUrl as described above.

Conclusion



There is one small BUT in this scheme - so far the PayPalHere application is not able to make a callback to your application after making a payment, but I think the developers will fix this soon.
Personally, I recommend to build the necessary url on the server, as there is much less fuss, and it looks more resistant to errors. Anyway, I did just that.

I hope that this article will help someone, all successful integration!
I will be glad to answer your questions and read about your experience.

Source: https://habr.com/ru/post/226809/


All Articles