Created
November 29, 2011 17:46
-
-
Save saranyan/1405686 to your computer and use it in GitHub Desktop.
PayPal MPL Integration into an Art Store
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Full source code is available at https://github.com/saranyan/PayPal-Code-Examples | |
//--------------------------------------------------------------------- | |
//Step 1 - Initialize. | |
//Do this before anything else. | |
public void initLibrary() { | |
PayPal pp = PayPal.getInstance(); | |
if (pp == null) { | |
// This is the main initialization call that takes in your Context, | |
// the Application ID, and the server you would like to connect to. | |
pp = PayPal.initWithAppID(this, "APP-80W284485P519543T", | |
PayPal.ENV_SANDBOX); | |
// -- These are required settings. | |
pp.setLanguage("en_US"); // Sets the language for the library. | |
// -- | |
// -- These are a few of the optional settings. | |
// Sets the fees payer. If there are fees for the transaction, this | |
// person will pay for them. Possible values are FEEPAYER_SENDER, | |
// FEEPAYER_PRIMARYRECEIVER, FEEPAYER_EACHRECEIVER, and | |
// FEEPAYER_SECONDARYONLY. | |
pp.setFeesPayer(PayPal.FEEPAYER_EACHRECEIVER); | |
// Set to true if the transaction will require shipping. | |
pp.setShippingEnabled(true); | |
// Dynamic Amount Calculation allows you to set tax and shipping | |
// amounts based on the user's shipping address. Shipping must be | |
// enabled for Dynamic Amount Calculation. This also requires you to | |
// create a class that implements PaymentAdjuster and Serializable. | |
pp.setDynamicAmountCalculationEnabled(false); | |
// -- | |
_paypalLibraryInit = true; | |
//if dialog is running, close it | |
if (_progressDialog.isShowing()) { | |
_progressDialog.dismiss(); | |
_progressDialogRunning = false; | |
} | |
} | |
} | |
//--------------------------------------------------------------------- | |
//Step 2 - | |
//Show a PayPal button. | |
//Note that the removePayPalButton is called. This is to ensure that there is no exception when | |
//the parent view is called more than once | |
//This is important to do or else your onClick will not get registered after the first time. | |
public void showPayPalButton() { | |
removePayPalButton(); | |
// Back in the UI thread -- show the "Pay with PayPal" button | |
// Generate the PayPal Checkout button and save it for later use | |
PayPal pp = PayPal.getInstance(); | |
//get the checkoutbutton | |
launchPayPalButton = pp.getCheckoutButton(Android_PP_Test1Activity.this, PayPal.BUTTON_194x37, CheckoutButton.TEXT_PAY); | |
//add it to layout | |
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); | |
//position this at the bottom | |
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); | |
//some margins for visual goodness | |
params.bottomMargin = 5; | |
params.leftMargin= 50; | |
launchPayPalButton.setLayoutParams(params); | |
launchPayPalButton.setId(PAYPAL_BUTTON_ID); | |
launchPayPalButton.setOnClickListener(Android_PP_Test1Activity.this); | |
((RelativeLayout)findViewById(R.id.RelativeLayout01)).addView(launchPayPalButton); | |
} | |
/* this method removes the PayPal button from the view | |
*/ | |
private void removePayPalButton() { | |
// Avoid an exception for setting a parent more than once | |
if (launchPayPalButton != null) { | |
((RelativeLayout) findViewById(R.id.RelativeLayout01)) | |
.removeView(launchPayPalButton); | |
} | |
} | |
//--------------------------------------------------------------------- | |
//Step 3- | |
//Handling the return from MPL | |
// PayPal Activity Results. This handles all the responses from the PayPal | |
// Payments Library | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent intent) { | |
AlertDialog alertDialog = new AlertDialog.Builder(this).create(); | |
if (requestCode == REQUEST_PAYPAL_CHECKOUT) { | |
mPurchase[_value] = true; | |
alertDialog.setTitle("Success"); | |
alertDialog.setMessage("This item will be shipped to you in one week!"); | |
alertDialog.show(); | |
} else { | |
super.onActivityResult(requestCode, resultCode, intent); | |
} | |
} | |
//--------------------------------------------------------------------- | |
//Bonus Code - Display an Image Gallery | |
//Gallery code | |
public class ImageAdapter extends BaseAdapter { | |
int mGalleryItemBackground; | |
private Context mContext; | |
private Integer[] mImageIds = { | |
R.drawable.picasso1, | |
R.drawable.picasso2, | |
R.drawable.picasso3 | |
}; | |
public ImageAdapter(Context c) { | |
mContext = c; | |
TypedArray attr = mContext.obtainStyledAttributes(R.styleable.Android_PP_Test1); | |
mGalleryItemBackground = attr.getResourceId( | |
R.styleable.Android_PP_Test1_android_galleryItemBackground, 0); | |
attr.recycle(); | |
} | |
public int getCount() { | |
return mImageIds.length; | |
} | |
public Object getItem(int position) { | |
return position; | |
} | |
public long getItemId(int position) { | |
return position; | |
} | |
public View getView(int position, View convertView, ViewGroup parent) { | |
ImageView imageView = new ImageView(mContext); | |
imageView.setImageResource(mImageIds[position]); | |
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100)); | |
imageView.setScaleType(ImageView.ScaleType.FIT_XY); | |
imageView.setBackgroundResource(mGalleryItemBackground); | |
return imageView; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment