Like Us on Facebook

How to integrate Payment Gateway to Android Application

    Integrating Payment Gateway is  essential features for all Online Purchase Application . eg. Paypal , Payoneer etc
    In this tutorial  we will use Paypal library for secure and easy to implement in Our Android Application. so I am going to 
    tell you the whole processes
 
  1. First go to developers.paypal.com  and set up a sandbox Account if you don't have  Previously
  2. In  SandBox Account Account create an Apps from My Apps, you will get CLIENT ID and Secret key. this CLIENT ID you have to use in your  Android  Application.
  3. download Paypal Library PaypalAndroidSDk.Jar file 
  4. Create a new Project in Android studio  PaymentDemo with Blank Activity and Minimum API level 14
  5. Now open your manifest file and give the below permissions-
 
 
    <uses-permission android:name="android.permission.INTERNET"/>

     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />



    6. And Add  required Activity and Services-
 
    <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.PayPalFuturePaymentActivity" />
      <activity android:name="com.paypal.android.sdk.payments.FuturePaymentConsentActivity" />
      <activity android:name="com.paypal.android.sdk.payments.FuturePaymentInfoActivity" />
       <activity
       android:name="io.card.payment.CardIOActivity"
       android:configChanges="keyboardHidden|orientation" />
       <activity android:name="io.card.payment.DataEntryActivity" />



 7.Now open your Activity and configure your Apps
 
    public class MainActivity extends Activity implements OnClickListener{
    private Button pay;
    private static final String CONFIG = PayPalConfiguration.ENVIRONMENT_PRODUCTION;
    private static final String CLIENT_ID = "PUT_YOUR_CLIENT _ID _HERE";
    private static final int REQUEST_PAYMENT = 1;
    private static PayPalConfiguration config = new PayPalConfiguration()
                                     
                              .environment(CONFIG)
                              .clientId(CLIENT_ID)
                              .merchantName("MultiAndroid zone")
                              .merchantPrivacyPolicyUri(Uri.parse("http://www.example.com/privacy"))
                              .merchantUserAgreementUri(Uri.parse("http://www.example.com/legal"));
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
         pay = (Button)findViewById(R.id.button1);
         
         pay.setOnClickListener(this);
         
         Intent intent = new Intent(this,PayPalService.class);
         intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
         startService(intent);
        
         }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        
         switch (v.getId()){
        
        
        case R.id.button1:
            
            PayPalPayment item = new PayPalPayment(new BigDecimal(1), "USD", "MultiAndroid                  zone",PayPalPayment.PAYMENT_INTENT_SALE);
        
              Intent in = new Intent(MainActivity.this,PaymentActivity.class);
              
               in.putExtra(PaymentActivity.EXTRA_PAYMENT,item);
               
               startActivityForResult(in, REQUEST_PAYMENT);
               
               break;
        
        
            }
        
        }


    /* (non-Javadoc)
     * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
    
          if (requestCode == REQUEST_PAYMENT){
              
              
              if(resultCode == Activity.RESULT_OK){
              
              
         PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
         
          if(confirm== null){
              
               try {
                   
                   System.out.println("Responses" +confirm) ;
                   
                   Log.i("PayPal Example Payments" , confirm.toJSONObject().toString());
                   
                   JSONObject obj = new JSONObject(confirm.toJSONObject().toString());
                   
                
                   String paymentID=obj.getJSONObject("response").getString("id");
                   System.out.println("payment id:-=="+paymentID);
                   
                   Toast.makeText(getApplicationContext(), paymentID ,Toast.LENGTH_LONG).show();
                   
                   
                   }catch(JSONException e){
                   
                   Log.e("Payment demo", "failure occured:",e);
               }
          }
          
              }else if(requestCode == Activity.RESULT_CANCELED){
                  
                  
                  
                  Log.i("Paymentdemo","The user cancelled");
                  
                  
             }else if(requestCode == PaymentActivity.RESULT_EXTRAS_INVALID){
                 
                 Log.i("paymentdemo","Invalid payment Submitted");
                 
                 
             }
              
              
          }
              
          }
    
}



8. Now test  your Application in your Android device . remember  this application will not work in Emulator.
    it will look like  below screensot


      

        

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 0 )

    Leave a comment..

    Click to login.

    Subscribe Latest Information

    Most Popular Posts

    You May Like Also