PayPal payment gateway is one of the biggest payment gateway and is used in many businesses for the payment purpose. PayPal adaptive is a method by which you can make payment to more than one person on a single go. This is generally used in multi vendor sites so that is payment is transferred directly to the vendor of the product. Its implementation in mobile is also simple as the SDK is provided by PayPal. Start with adding the library. Now we have to create PayPalAdvancedPayment object which will contain all the values like list of amount and sellers PayPal email respectively, currency etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
private PayPalAdvancedPayment extractPayPalinformation(List<PayPalSeller> sellerList) { PayPalAdvancedPayment payPalAdvancedPayment = new PayPalAdvancedPayment(); for (PayPalSeller seller : sellerList) { PayPalReceiverDetails payPalReceiverDetails = new PayPalReceiverDetails(); BigDecimal st = new BigDecimal(seller.getAmount()); st = st.setScale(2, RoundingMode.HALF_UP); payPalReceiverDetails.setSubtotal(st); payPalReceiverDetails.setRecipient(seller.getPaypalEmail()); if (seller.isPrimary()) { payPalReceiverDetails.setIsPrimary(true); } payPalReceiverDetails.setPaymentType(PayPal.PAYMENT_TYPE_SERVICE); payPalAdvancedPayment.getReceivers().add(payPalReceiverDetails); } payPalAdvancedPayment.setCurrencyType("INR"); payPalAdvancedPayment.setMemo("123"); payPalAdvancedPayment.setIpnUrl(PaypalAdaptiveConfiguration.IPN_URL); return payPalAdvancedPayment; } public class PaypalAdaptiveConfiguration { public static final int ENVIRONMENT = PayPal.ENV_SANDBOX; public static String APP_ID = "APP-80W284485P519543T"; public static String IPN_URL = "http://example.com/paypalpaymenttest/index.php"; } |
Now we have to check if PayPal library is initiated or not. If yes then proceed with the payment like below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
public void makePaypalPaymentButton() { if (ContextCompat.checkSelfPermission(this.getContext(), Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) { initLibrary(); } else { ActivityCompat.requestPermissions(this.getActivity(), new String[]{Manifest.permission.READ_PHONE_STATE,}, RC_READ_HONE_STATE); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == RC_READ_HONE_STATE) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { initLibrary(); } } } public void initLibrary() { PayPal pp = PayPal.getInstance(); if (pp == null) { // This is the main initialization call that takes in your Context, // the Application ID, and the server you would like to connect to. pp = PayPal.initWithAppID(getContext(), PaypalAdaptiveConfiguration.APP_ID, PaypalAdaptiveConfiguration.ENVIRONMENT); pp.setLanguage("en_US"); // Sets the language for the library. pp.setFeesPayer(PayPal.FEEPAYER_EACHRECEIVER); pp.setShippingEnabled(false); pp.setDynamicAmountCalculationEnabled(false); mPaypalLibraryInit = true; } if (mPaypalLibraryInit) { showPayPalButton(); } else { mProgressDialog = new ProgressDialog(this.getContext()); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); mProgressDialog.setMessage("Loading PayPal Payment Library"); mProgressDialog.setCancelable(false); mProgressDialog.show(); Thread newThread = new Thread(checkforPayPalInitRunnable); newThread.start(); } } private void showPayPalButton() { PayPal pp = PayPal.getInstance(); CheckoutButton mPaypalCheckoutButtonView = pp.getCheckoutButton(getContext(), PayPal.BUTTON_278x43, CheckoutButton.TEXT_PAY); mPaypalCheckoutButtonView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mProgressDialog = ProgressDialog.show(getContext(), null, getResources().getString(R.string.loading), true); mProgressDialog.setCanceledOnTouchOutside(false); try { PayPalAdvancedPayment payPalAdvancedPayment = (PayPalAdvancedPayment) getArguments().getSerializable("PayPalAdvancedPayment"); Intent checkoutIntent = PayPal.getInstance().checkout(payPalAdvancedPayment, getActivity() /* , new ResultDelegate() */); startActivityForResult(checkoutIntent, RC_PAYPAL_ADAPTIVE); } catch (Exception e) { e.printStackTrace(); } } }); mPaypalCheckoutButtonView.setVisibility(View.GONE); mainLayout.addView(mPaypalCheckoutButtonView); mPaypalCheckoutButtonView.performClick(); if (mProgressDialog != null) { mProgressDialog.dismiss(); } progress.dismiss(); } private void checkForPayPalLibraryInit() { while (!mPaypalLibraryInit) { try { Thread.sleep(500); } catch (InterruptedException e) { DialogInterface.OnClickListener positiveButtonOnClickListener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }; } } getActivity().runOnUiThread(showPayPalButtonRunnable); } @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { try { if (requestCode == RC_PAYPAL_ADAPTIVE) { Log.d(TAG, "onActivityResult:requestCode " + requestCode); Log.d(TAG, "onActivityResult:resultCode " + resultCode); switch (resultCode) { case Activity.RESULT_OK: String payKey = intent.getStringExtra(PayPalActivity.EXTRA_PAY_KEY); String data = intent.getStringExtra(PayPalActivity.EXTRA_PAYMENT_STATUS); String payInfo = intent.getStringExtra(PayPalActivity.EXTRA_PAYMENT_INFO); redirectToOrderActivity(true); break; case Activity.RESULT_CANCELED: this.getActivity().finish(); Toast.makeText(getContext(), getString(R.string.payment_cancelled), Toast.LENGTH_SHORT).show(); break; case PayPalActivity.RESULT_FAILURE: String errorMessage = intent.getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE); String id = intent.getStringExtra(PayPalActivity.EXTRA_ERROR_ID); Log.d("paypal error", errorMessage + " " + id); Log.d("intent", intent.getExtras().toString()); Toast.makeText(getContext(), errorMessage, Toast.LENGTH_SHORT).show(); this.getActivity().finish(); } } else { super.onActivityResult(requestCode, resultCode, intent); } } catch (Exception e) { e.printStackTrace(); } } |
And you are ready to go with Adaptive PayPal in your application.