Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Make payment using PayPal MPL(Mobile Payment Libraries) in Android

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.2k
    Comment on it

    There are certain applications in which we need to integrate the PayPal for enabling the user to pay certain amount to use further features of the app.

    Before starting, download the PayPal_MPL.jar file from PayPal developer's website and copy same in project's libs. Also, add the PayPal activity in manifest like:

     android:name="com.paypal.android.MEP.PayPalActivity" 

    Below is the code that's all required to make payment easily:

    I've used following code in the onCreate() method of the Activity. The "paypalBtn" used below initiates the Payment call.

     Button paypalBtn = (RelativeLayout)findViewById(R.id.paypalButton);
        PayPal ppObj = PayPal.initWithAppID(this.getBaseContext(), "your app id from apps.paypal.com", PayPal.ENV_LIVE);
        CheckoutButton launchPayPalButton = ppObj.getCheckoutButton(this, PayPal.BUTTON_194x37, CheckoutButton.TEXT_PAY);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    
        launchPayPalButton.setLayoutParams(params);
        launchPayPalButton.setOnClickListener(new OnClickListener() {           
            @Override
            public void onClick(View v) {
                PayPalPayment newPayment = new PayPalPayment();
                char val[] = { '9', '9' };
                BigDecimal obj_0 = new BigDecimal(val);
                newPayment.setSubtotal(obj_0);
                newPayment.setCurrencyType("USD");
                newPayment.setRecipient("sales@xyz.com");
                newPayment.setMerchantName("xyz.com");
                Intent paypalIntent = PayPal.getInstance().checkout(newPayment, confirmSubscription);
                startActivityForResult(paypalIntent, 1);                
            }
        });
    
        ((RelativeLayout) findViewById(R.id.paypalButton)).addView(launchPayPalButton);
    
    

    I'm making a payment of USD 99 in above code. Also, in initWithAppID() method at the beginning, you can set the payment environment to sandbox by using PayPal.ENV_SANDBOX in place of PayPal.ENV_LIVE and use APP-80W284485P519543T as the app id. The following code overrides the onCreate() method of Activity and looks for the result of payment request that we made above,

            
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
        case Activity.RESULT_OK:
            // The payment succeeded
            payKey = data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
            System.out.println("Result Ok: Paykey is:: "+payKey);
            // Tell the user their payment succeeded
            break;
        case Activity.RESULT_CANCELED:
            System.out.println("Result Cancelled!!!!");
            confirmSubscription.finish();
            // 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);
            System.out.println("Result Failure!!!!");
            System.out.println("Result Failure:: Error Id!!!!"+errorID);
            System.out.println("Result Failure:: Error Message!!!!"+errorMessage);
            confirmSubscription.finish();
            // Tell the user their payment was failed.
            break;
        }
    }
    

    Hope it makes you make your first payment :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: