◎위챗 : speedseoul
https://developer.paypal.com/webapps/developer/docs/classic/mobile/ht_mpl-itemPayment-Android/
This guide shows you how to add a Pay with PayPal button to an Android app using the PayPal Mobile Payment Libraries (MPL).
To integrate the MPL into an Android app, you need to:
PayPal_MPL.jar
and the other necessary MPL components in your Android app.ENV_SANDBOX
or ENV_LIVE
) and the business' PayPal Account as the receiver.PAYMENT_TYPE_SERVICE
or PAYMENT_TYPE_PERSONAL
).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).
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.
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.
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>
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; } }
You need three Sandbox test accounts to make MPL test calls:
Pay
call.In addition to the API credentials, MPL calls also require an AppID. For the PayPal Sandbox, the AppID is static:
APP-80W284485P519543T
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); }
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)); }
The handler will call one of three types (paymentSucceeded
, paymentCanceled
, paymentFailed
) 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.
To continue learning about how PayPal works with Android apps: