PayPal APIs_ Up and Running_ A Developer's Guide - Michael Balderas [42]
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:
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.
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.
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. Table 5-2 outlines the parameters for fetchDeviceReferenceTokenWithAppID.
Table 5-2. fetchDeviceReferenceTokenWithAppID method
Parameter Description
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: