Integrating PayPal Here in an iOS App

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

    image

    By the way, despite the large number of online stores that pay via PayPal, the PayPal Here service is still not available in the Russian store.



    What is PayPal Here?



    PayPal Here is a mobile terminal through which you can pay payments, and is a direct competitor to a similar program from Square. In addition to making payments by credit card, PayPal Here can be used to pay by check and to check financial transactions for a certain period of time. You can also send invoices to the recipient’s email via PayPal Here software, and the buyer, having received 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 his support is also available, but I did not work with it.

    Integration process.



    Unfortunately, PayPal does not provide any separate library for conducting transactions directly from its application. But safety is paramount. We have to call a third-party application - we compose 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 information.

    Next, create a simple UIViewController for editing data:

    @interfacePPViewController : UIViewController <UITextFieldDelegate>@property (weak, nonatomic) IBOutletUITextField *priceField;
    @property (weak, nonatomic) IBOutletUITextField *taxField;
    @property (weak, nonatomic) IBOutletUITextField *nameField;
    @property (weak, nonatomic) IBOutletUITextField *quantityField;
    @property (weak, nonatomic) IBOutletUITextField *descriptionField;
    @end


    add all the necessary 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 move on 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 - for now, the PayPalHere application is not able to call back your application after the payment has been made, but I think that the developers will fix it soon.
    Personally, I recommend building the necessary url on the server, since there is much less fuss, and it looks more error-resistant. In any case, I did just that.

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

    Also popular now: