package Demo.PayPalProject;
import java.math.BigDecimal;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.paypal.android.MEP.CheckoutButton;
import com.paypal.android.MEP.PayPal;
import com.paypal.android.MEP.PayPalActivity;
import com.paypal.android.MEP.PayPalPayment;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv=(LinearLayout)findViewById(R.id.lv);
PayPal ppObj = PayPal.initWithAppID(this.getBaseContext(), "APP-80W284485P519543T", PayPal.ENV_SANDBOX);
CheckoutButton launchPayPalButton = ppObj.getCheckoutButton(this, PayPal.BUTTON_278x43, CheckoutButton.TEXT_PAY); //button size 278x43 and text on it is 'PAY'
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.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
invokeSimplePayment();
}
});
lv.addView(launchPayPalButton);
}
private void invokeSimplePayment(){
try{
PayPalPayment newPayment = new PayPalPayment();
newPayment.setSubtotal(BigDecimal.valueOf(10));
newPayment.setCurrencyType("USD");
//.setCurrency("USD");
newPayment.setRecipient("abc@gmail.com");
newPayment.setMerchantName("My Company");
PayPal pp = PayPal.getInstance();
if(pp==null)
pp = PayPal.initWithAppID(this, "APP-80W284485P519543T", PayPal.ENV_SANDBOX);
Intent paypalIntent = pp.checkout(newPayment, this);
this.startActivityForResult(paypalIntent, 1);
}catch(Exception e){e.printStackTrace();}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(resultCode) {
case Activity.RESULT_OK:
//The payment succeeded
String payKey = data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
Log.e("success", payKey);
//Tell the user their payment succeeded
break;
case Activity.RESULT_CANCELED:
Log.e("success","canceled");
//The payment was canceled
//Tell the user their payment was canceled
break;
case PayPalActivity.RESULT_FAILURE:
//The payment failed -- we get the error from the EXTRA_ERROR_ID and EXTRA_ERROR_MESSAGE
String errorID = data.getStringExtra(PayPalActivity.EXTRA_ERROR_ID);
String errorMessage = data.getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE);
Log.e("errorID", errorID);
Log.e("errorMessage", errorMessage);
//Tell the user their payment was failed.
}
}
}Add a code snippet to your website: www.paste.org