Saturday, 7 November 2015

Paypal Integration In Android

Hello Guys,

Here I show you how to integrate Paypal Payment Gateway in your Android application.

Before you integrate Paypal in your app, you must have an sandbox account in paypal for testing.
Refer this link: https://developer.paypal.com/developer/accounts/

After create an account successfully start integration in android app.

Here I share a sample code for it.

Sample Code:

File: PayPalIntegrationActivity.java

package com.sneha.paypalintegration;

import java.math.BigDecimal;
import java.util.Currency;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;
import com.paypal.android.MEP.CheckoutButton;
import com.paypal.android.MEP.PayPal;
import com.paypal.android.MEP.PayPalActivity;
import com.paypal.android.MEP.PayPalPayment;
import com.sneha.util.Constants;


public class PayPalIntegrationActivity extends Activity implements OnClickListener
{
private PayPal mPayPal;
private CheckoutButton launchPayPalButton;
String payKey, errorID, errorMessage; 

@Override
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_pay_pal_integration);

mPayPal=PayPal.initWithAppID(this,Constants.PAYPAL_APP_ID,PayPal.ENV_SANDB                 OX);

launchPayPalButton = mPayPal.getCheckoutButton(this,PayPal.BUTTON_278x43,                            CheckoutButton.TEXT_PAY);

RelativeLayout.LayoutParams params = new RelativeLayout. LayoutParams                                        (LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.CENTER_IN_PARENT);

params.bottomMargin = 10;

launchPayPalButton.setLayoutParams(params);
        launchPayPalButton.setOnClickListener(this);

((RelativeLayout)findViewById(R.id.main_layout)).addView(launchPayPalButton);
}

@Override
public void onClick(View v) 
{
PayPalPayment newPayment = new PayPalPayment();
BigDecimal bigDecimal=new BigDecimal(10);
newPayment.setSubtotal(bigDecimal);
newPayment.setCurrencyType(Currency.getInstance(Locale.US));
newPayment.setRecipient("pay2me@example.com");
newPayment.setMerchantName("My Merchant");
Intent paypalIntent = PayPal.getInstance().checkout(newPayment, this);
this.startActivityForResult(paypalIntent, 1);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
super.onActivityResult(requestCode, resultCode, data);

switch(resultCode)
{
case Activity.RESULT_OK:
payKey = data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
Toast.makeText(this,"Paymnet Successful",Toast.LENGTH_LONG).show();
break;

case Activity.RESULT_CANCELED:
Toast.makeText(this,"Paymnet Cancel",Toast.LENGTH_LONG).show();
break;

case PayPalActivity.RESULT_FAILURE:
Toast.makeText(this,"Paymnet Failed",Toast.LENGTH_LONG).show();
errorID = data.getStringExtra(PayPalActivity.EXTRA_ERROR_ID);
errorMessage = data.getStringExtra (PayPalActivity.                                                                               EXTRA_ERROR_MESSAGE);
break;
}
}
}

Output:




















































Download Full Source Code From Here: PaypalIntegration

Happy Coding...!!!

1 comment: