Wednesday, June 3, 2015

Implementing PayPal in android

For implementing PayPal in your android app ..firstly you need PayPal lib ,import PayPal lib into your project .You should have a paypal account ,as we need PayPal API key to use PayPal.(You will get all instructions here -->https://developer.paypal.com/docs/classic/mobile/ht_mpl-itemPayment-Android/ )



Now to make code more easier and clean,I have created a class which contains all stuff related to PayPal you just have to use this class in your main class.
 
public class PaypalStuff
{
    Context    con;

    String    Money    = "";

    public PaypalStuff(Context con , String Money)
    {
        this.con = con;
        this.Money = Money;

        Intent intent = new Intent(con, PayPalService.class);
        intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
        con.startService(intent);

    }

    /**
     * - Set to PayPalConfiguration.ENVIRONMENT_PRODUCTION to move real money.
     *
     * - Set to PayPalConfiguration.ENVIRONMENT_SANDBOX to use your test
     * credentials from https://developer.paypal.com
     *
     * - Set to PayPalConfiguration.ENVIRONMENT_NO_NETWORK to kick the tires
     * without communicating to PayPal's servers.
     */
    private static final String            CONFIG_ENVIRONMENT        = PayPalConfiguration.ENVIRONMENT_SANDBOX;                                                //
    // private static final String CONFIG_ENVIRONMENT =
    // PayPalConfiguration.ENVIRONMENT_SANDBOX;
    // note that these credentials will differ between live & sandbox
   
    private static final String            CONFIG_CLIENT_ID        = "YOUR CLIENT ID";

    private static final int            REQUEST_CODE_PAYMENT    = 1;

    private static PayPalConfiguration    config                    = new PayPalConfiguration().environment(CONFIG_ENVIRONMENT).clientId(CONFIG_CLIENT_ID)

                                                                .merchantName("Name").merchantPrivacyPolicyUri(Uri.parse("https://www.example.com/privacy"))
                                                                        .merchantUserAgreementUri(Uri.parse("https://www.example.com/legal"));

    public void onBuyPressed()
    {
        /*
         * PAYMENT_INTENT_SALE will cause the payment to complete immediately.
         * Change PAYMENT_INTENT_SALE to - PAYMENT_INTENT_AUTHORIZE to only
         * authorize payment and capture funds later. - PAYMENT_INTENT_ORDER to
         * create a payment for authorization and capture later via calls from
         * your server.
         *
              */
        PayPalPayment thingToBuy = getThingToBuy(PayPalPayment.PAYMENT_INTENT_SALE);
        
        Intent intent = new Intent(con, PaymentActivity.class);

        intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);

        intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);

        ((Activity) con).startActivityForResult(intent, REQUEST_CODE_PAYMENT);

    }

    private PayPalPayment getThingToBuy(String paymentIntent)
    {
        return new PayPalPayment(new BigDecimal(Money), "USD", "Pay", paymentIntent);
    }



}



 NOW IN YOUR MAIN CLASS (where you want to add paypal) ADD this code:


on any event...say on button press ,write this code :

// CALLING PAYPAL

 PaypalStuff p = new PaypalStuff(context,Amount_to_add);
 p.onBuyPressed();


 ONACTIVITY RESULT FOR PAYPAL(in your main class..from where you call PaypalStuff )

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

  if( requestCode == REQUEST_CODE_PAYMENT )
  {
   if( resultCode == Activity.RESULT_OK )
   {
    PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
    if( confirm != null )
    {
     try
     {
      Log.i(TAG, confirm.toJSONObject().toString(4));
    
      JSONObject jsonObj = new JSONObject(confirm.toJSONObject().toString());

      String paymentId = jsonObj.getJSONObject("response").getString("id");   //paymentID   
            
               //check    jsonObj for more data..  

      Toast.makeText(con, paymentId, Toast.LENGTH_LONG).show();

     }
     catch(JSONException e)
     {
      Toast.makeText(con, "An Error has occur while adding money..please try again later..!", Toast.LENGTH_LONG).show();
    
     }
    }
   }
   else if( resultCode == Activity.RESULT_CANCELED )
   {
    Toast.makeText(con, "Request canceled..!", Toast.LENGTH_LONG).show();
  
   }
   else if( resultCode == PaymentActivity.RESULT_EXTRAS_INVALID )
   {
    Toast.makeText(con, "An invalid Payment was submitted. Please see the docs for more details.", Toast.LENGTH_LONG).show();
  
   }

   stopService(new Intent(context, PayPalService.class));
  }

 }

   // MAINFEST ENTIRES FOR PAYPAL:
    // <service
    // android:name="com.paypal.android.sdk.payments.PayPalService"
    // android:exported="false" />
    //
    // <activity android:name="com.paypal.android.sdk.payments.PaymentActivity"
    // />
    // <activity android:name="com.paypal.android.sdk.payments.LoginActivity" />
    // <activity
    // android:name="com.paypal.android.sdk.payments.PaymentMethodActivity" />
    // <activity
    // android:name="com.paypal.android.sdk.payments.PaymentConfirmActivity" />
    // <!-- <activity
    // android:name="com.paypal.android.sdk.payments.PaymentCompletedActivity"/>
    // -->
    // <activity
    // android:name="com.paypal.android.sdk.payments.PayPalFuturePaymentActivity"
    // />
    // <activity
    // android:name="com.paypal.android.sdk.payments.FuturePaymentConsentActivity"
    // />
    // <activity
    // android:name="com.paypal.android.sdk.payments.FuturePaymentInfoActivity"
    // />
    // <activity
    // android:name="com.paypal.android.sdk.payments.PayPalProfileSharingActivity"
    // />
    // <activity
    // android:name="com.paypal.android.sdk.payments.ProfileSharingConsentActivity"
    // />
    // <activity
    // android:name="io.card.payment.CardIOActivity"
    // android:configChanges="keyboardHidden|orientation" />
    // <activity android:name="io.card.payment.DataEntryActivity" />


 Just use above code where ever you want to integrate PayPal.
Enjoy..!

0 comments:

Post a Comment

Don't lose faith when you see others receive answers to their prayers

An elephant and a dog became pregnant at same time. Three months down the line the dog gave birth to six puppies. Six months later the dog...

 

G-Expo Template by Ipietoon Cute Blog Design