https://developer.paypal.com/webapps/developer/docs/classic/mobile/ht_mpl-itemPayment-Android/


How to Accept Payments in an Android App Using MPL

This guide shows you how to add a Pay with PayPal button to an Android app using the PayPal Mobile Payment Libraries (MPL).

Process Overview

To integrate the MPL into an Android app, you need to:

  1. Download the Android Mobile Payments Library SDK from PayPal and include PayPal_MPL.jar and the other necessary MPL components in your Android app.
  2. Obtain an AppID (for testing purposes, use the PayPal Sandbox AppID).
  3. Specify the PayPal environment you're addressing (for example, ENV_SANDBOX or ENV_LIVE) and the business' PayPal Account as the receiver.
  4. Calculate the price of the item(s) or service to be purchased and input that value into your MPL call.
  5. Set the Payment Type (for example, PAYMENT_TYPE_SERVICE or PAYMENT_TYPE_PERSONAL).
  6. Make the MPL payment call.

    When you make the call, the customer is presented with an in-app PayPal log in screen and the payment processing is completed within your app (there is no browser or webview involved).

  7. When the payment flow is complete, MPL returns control to your app.

NOTE: For instructions on using the PayPal APIs, how to use the Sandbox for testing, and how to move your app into production, see Apps 101.

Do It!

The following code snippets show how to integrate MPL into an Android app. The snippets are part of an app that lets users pay for a pizza using their PayPal account. The sample code is distilled from the sample app that is provided in the MPL SDK.

  1. Download the Android Mobile Payment Libraries package from the PayPal SDK page.
  2. Add the necessary PayPal header files to your project.

    Declare both INTERNET and READ_PHONE_STATE permissions in the activity declared in your app's manifest file, like this:

    <activity android:name=".PizzaMain" android:label="@string/app_name">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    </activity>
    
  3. To add the library .jar file to an eclipse project:
    1. Right-click your project in eclipse and select Properties.
    2. Select Java Build Path.
    3. Select the Libraries tab.
    4. Select the Add Jars button.
    5. Choose PayPal_MPL.jar from your folder structure and click OK.
  4. Initialize the library with the initWithAppID method.

    The following code sets the required values needed to initialize the library, as well as setting some optional values. Note that the environment is set toENV_SANDBOX (the PayPal Sandbox) and the static Sandbox AppID is hard-coded into the call. You can also set the environment to ENV_LIVE orENV_NONE.

    public void initLibrary() {
    PayPal pp = PayPal.getInstance();
    
    if (pp == null) {  // Test to see if the library is already initialized
    
    // This main initialization call takes your Context, AppID, and target server
    pp = PayPal.initWithAppID(this, "APP-80W284485P519543T", PayPal.ENV_NONE);
    
    // Required settings:
    
    // Set the language for the library
    pp.setLanguage("en_US");
    
    // Some Optional settings:
    
    // Sets who pays any transaction fees. Possible values are:
    // FEEPAYER_SENDER, FEEPAYER_PRIMARYRECEIVER, FEEPAYER_EACHRECEIVER, and FEEPAYER_SECONDARYONLY
    pp.setFeesPayer(PayPal.FEEPAYER_EACHRECEIVER);
    
    // true = transaction requires shipping
    pp.setShippingEnabled(true);
    
    _paypalLibraryInit = true;
    }
    }
    
  5. Set up your Sandbox test accounts and get the credentials you need to make MPL calls.

    You need three Sandbox test accounts to make MPL test calls:

    • API Caller, whose API credentials your app uses to make the Pay call.
    • Sender, the entity making the payment.
    • Receiver, the entity receiving the payment.

    In addition to the API credentials, MPL calls also require an AppID. For the PayPal Sandbox, the AppID is static:

    APP-80W284485P519543T
  6. Implement the getPayButton method.

    This generates the button in the size you specify, along with onClick functionality.

    private void showPayPalButton() {
    
    // Generate the PayPal checkout button and save it for later use
    PayPal pp = PayPal.getInstance();
    launchPayPalButton = pp.getCheckoutButton(this, PayPal.BUTTON_278x43, CheckoutButton.TEXT_PAY);
    
    // The OnClick listener for the checkout button
    launchPayPalButton.setOnClickListener(this);
    
    // Add the listener to the layout
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams (LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    params.bottomMargin = 10;
    launchPayPalButton.setLayoutParams(params);
    launchPayPalButton.setId(PAYPAL_BUTTON_ID);
    ((RelativeLayout) findViewById(R.id.RelativeLayout01)).addView(launchPayPalButton);
    ((RelativeLayout) findViewById(R.id.RelativeLayout01)).setGravity(Gravity.CENTER_HORIZONTAL);
    }
    
  7. Create the payment.

    Clicking the PayPal button initiates the checkout call, which can include different payment parameters and optional criteria for shipping, tax, languages, and type of payments.

    public void PayPalButtonClick(View arg0) {
    // Create a basic PayPal payment
    PayPalPayment payment = new PayPalPayment();
    
    // Set the currency type
    payment.setCurrencyType("USD");
    
    // Set the recipient for the payment (can be a phone number)
    payment.setRecipient("ppalav_1285013097_biz@yahoo.com");
    
    // Set the payment amount, excluding tax and shipping costs
    payment.setSubtotal(new BigDecimal(_theSubtotal));
    
    // Set the payment type--his can be PAYMENT_TYPE_GOODS,
    // PAYMENT_TYPE_SERVICE, PAYMENT_TYPE_PERSONAL, or PAYMENT_TYPE_NONE
    payment.setPaymentType(PayPal.PAYMENT_TYPE_GOODS);
    
    // PayPalInvoiceData can contain tax and shipping amounts, and an
    // ArrayList of PayPalInvoiceItem that you can fill out.
    // These are not required for any transaction.
    PayPalInvoiceData invoice = new PayPalInvoiceData();
    
    // Set the tax amount
    invoice.setTax(new BigDecimal(_taxAmount));
    }
    
  8. Add a delegate to handle the response returned by the library.

    The handler will call one of three types (paymentSucceededpaymentCanceledpaymentFailed) based on the result. Here's an example callback:

    public void PayPalActivityResult(int requestCode, int resultCode, Intent intent) {
    switch (resultCode) {
    // The payment succeeded
    case Activity.RESULT_OK:
    String payKey = intent.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
    this.paymentSucceeded(payKey);
    break;
    
    // The payment was canceled
    case Activity.RESULT_CANCELED:
    this.paymentCanceled();
    break;
    
    // The payment failed, get the error from the EXTRA_ERROR_ID and EXTRA_ERROR_MESSAGE
    case PayPalActivity.RESULT_FAILURE:
    String errorID = intent.getStringExtra(PayPalActivity.EXTRA_ERROR_ID);
    String errorMessage = intent.getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE);
    this.paymentFailed(errorID, errorMessage);
    }
    }
    

Voilà! You've set up your first Mobile Payment Libraries calls.

Learn More

To continue learning about how PayPal works with Android apps: