Jump to content

How to Use PayPal's Mobile Express Checkout Library for iOS

+ 1
  sarahkim's Photo
Posted Mar 02 2011 03:18 PM

The following excerpt from PayPal APIs: Up and Running walks you through the steps required to setup Paypal's Mobile Express Checkout for your mobile app or mobile website.
PayPal provides a Mobile Express Checkout (MEC) library for iOS, available for download from https://www.x.com/co...obile/mobile_ec. This MEC library supports two different programming flows: it can be called either directly from your mobile application or via a PayPal button on your mobile website.

MEC Mobile Application Integration

MEC can be integrated into your mobile application, allowing you to start and end the payment process with screens inside your application. The MEC checkout pages themselves are contained inside a web view. The steps for doing so are as follows:

1. Acquire a device token from the MEC library before rendering the payment screen with the PayPal Button. Include a pointer to the method you delegate for handling the device token.

2. Acquire a PayPal payment button from the Library and render it on your mobile application screen. Include a pointer to the method you delegate for handling the button-click event.

3. When your customer clicks the PayPal button, it will initialize your delegated method to do the following:

  • Call a routine on your mobile web server for passing the payment information.

  • Your mobile web server will then send a SetExpressCheckout request with the payment information to PayPal.

  • Pass the checkout token returned from SetExpressCheckout back to your mobile application.

  • Open a web view redirecting the customer’s browser to PayPal with the Mobile command, using the device token and the checkout token as URL parameters. For example:

    https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout-mobile &drt=valueFromMobileExpressCheckoutLibrary&token=valueFromSetExpressCheckoutResponse


  • Watch the web view for a redirect call from PayPal to either your return or cancel URLs.

  • If PayPal redirects the web view to your return URL, call surrogate routines on your mobile web server that send GetExpressCheckoutDetails and DoExpressCheckoutPayment requests to PayPal to complete the payment.


MEC Mobile Website Integration

MEC can be integrated directly with your mobile website, allowing you to start and end the payment process with pages on your site. The steps for doing so are as follows:

  • Acquire a device token from the MEC library before rendering the web view of your MEC implementation. Include a pointer to the method you delegate for handling the device token.

  • Launch a web view of the web page or routine on your mobile server that initiates your checkout process. Include the device token as a URL parameter, as well as the item details in the shopping cart.

  • Watch the web view for a redirect from your mobile web server that contains a call to a known URL, signaling the checkout process is complete.


MEC Library Methods

MEC contains three methods: fetchDeviceReferenceTokenWithAppID, getPayButtonWithTarget, and getInstance. Let’s look at each in depth.

fetchDeviceReferenceTokenWithAppID method

The fetchDeviceReferenceTokenWithAppID method returns a device token for use with the transaction. Use the del parameter to specify your delegate function that receives device tokens. Device tokens have a 45-minute expiration time limit, and are passed as the &drt parameter on the URL when you redirect the mobile browser to PayPal.

The MEC library uses the PayPal production servers by default to get device tokens. During your development process, use the env parameter to fetch device tokens from the sandbox. Be sure to fetch the device token just before you get the PayPal button. When you fetch the device token, the library determines whether the buyer stays logged in on the device. If the buyer is already logged into PayPal, the library will display the buyer’s name above the button when it renders. The table below outlines the parameters for fetchDeviceReferenceTokenWithAppID.

ParameterDescription
inAppId:The PayPal Application ID from X.com (required). For the sandbox environment, use APP-80W284485P519543T.
env:Indicates which PayPal servers the library uses (optional). Allowable values are:
  • ENV_LIVE
  • ENV_SANDBOX
  • ENV_NONE
del:Your delegate function that receives device tokens (required).


getPayButtonWithTarget method

If your payment implementation is in a mobile application, you can get a button from the MEC library. The getPayButtonWithTarget method returns a UIButton for use on your mobile application screen, and it provides a target: parameter that allows you to specify which UIViewController receives the callbacks. The table below outlines the parameters for getPayButtonWithTarget.

ParameterDescription
target:The UIViewController that is the delegate for callbacks (required).
action:Your method that responds to the PayPal button click (required).
buttonType:The size and appearance of the PayPal button (required). Allowable values are:
  • BUTTON_152x33
  • BUTTON_194x37
  • BUTTON_278x43
  • BUTTON_294x43


getInstance method

You can use the getInstance method to specify and access the library’s runtime properties. This can be used for debugging purposes as well as to verify that your payment is working properly. The table below outlines the parameters for getInstance.

ParameterDescription
langThe locale code for the label of the PayPal button. By default, the library uses the locale of the device.
errorMessageIf the library fails to acquire a valid device token, the error message provides more details about the failure.
paymentsEnabledIf your attempt to fetch a device token succeeded, the value of this property is TRUE.


A full example of using the MEC library for an online pizza delivery service application can be found in the library download at https://www.x.com/co...obile/mobile_ec.

PayPal APIs: Up and Running

Learn more about this topic from PayPal APIs: Up and Running.

Does your web application provide users with a convenient way to complete transactions? This book introduces you to PayPal’s APIs with instruction and resources for integrating this popular payment solution in different application environments, including mobile. By the end of this book, you'll have a clear understanding of what PayPal is and how you can get the most out of its powerful features for your particular payment situation.

See what you'll learn


Tags:
2 Subscribe


3 Replies

 : Mar 03 2011 12:50 PM
Is the PayPal Mobile Express Checkout (MEC) library for iOS apps now banned due to Apple's enforcement of its payment platform?
0
  MaryO's Photo
Posted Mar 08 2011 01:50 PM

PayPal's Mobile Express Checkout facilitates mobile browser based purchases. If it is used from a native iOS app, it can only be used for physical goods, services or personal payments, but it is not banned.

Mary Treseler
Editorial Strategist
O'Reilly Media Inc.
0
  Phien Tram's Photo
Posted Nov 19 2012 02:15 AM

Hello,

Please help me.
I've just integrated MECL Library to my sample project.
I used Xcode 4.5.2 on Mac OSX 10.8.2
MECL library version 1.0.3.

In my viewDidLoad method I just call fetch to get device reference token, like this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[[PayPal getPayPalInst] fetchDeviceReferenceTokenWithAppID:@"APP-80W284485P519543T" forEnvironment:ENV_SANDBOX withDelegate:self];
}

Issue: In the callback method, I just log the token, but the token is always empty. While the sample code of that lib package can genenate token.

- (void)receivedDeviceReferenceToken:(NSString *)token
{
NSLog(@"Token:%@", token);
}

- (void)couldNotFetchDeviceReferenceToken
{
//optionally check the errorMessage property to see what the problem was
NSLog(@"DEVICE REFERENCE TOKEN ERROR: %@", [PayPal getPayPalInst].errorMessage);
}

Please see project attachment.

Attached File(s)