Paypal is a payment gateway used worldwide hence it is of importance to use it in your application for purchases,in this tutorial I will show you how to intergrate paypal with your android application.
Steps
1.Create a sandbox account at https://developer.paypal.com/developer/accounts/create
2.Create an App to get a client id by using this link https://developer.paypal.com/developer/applications/
We shall use the client id in our application when processing payments.
3.Back to android studio create a new project
4.Add the following dependancy in your android.gradle app level and sync your project
implementation 'com.paypal.sdk:paypal-android-sdk:2.16.2'
5.Navigate to your activity_main.xml and add the following code
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Edit text for entering the amount-->
<EditText
android:id="@+id/etAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="20dp"
android:hint="Enter Amount to be Paid"
android:inputType="numberDecimal" />
<!--button for making a payment-->
<Button
android:id="@+id/btnPay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/etAmount"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:text="Make Payment"/>
<!--text view for displaying payment status-->
<TextView
android:id="@+id/idTVStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idBtnPay"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:padding="5dp"
android:textAlignment="center"
android:textColor="@color/purple_200"
android:textSize="20sp" />
</RelativeLayout>
6.Go to your MainActivity.java and add the following code:
public class MainActivity extends AppCompatActivity {
public static final String clientKey = "Ab1bmaeMoxy9PNEAKohh6TOHUJ4BrFvRgKsxBT2Tu-0e6jfk45KMQukAU5mm0Vg67fk5WOSiMtjstSpi";
public static final int PAYPAL_REQUEST_CODE = 11;
// Paypal Configuration Object
private static PayPalConfiguration config = new PayPalConfiguration()
// Start with mock environment. When ready,
// switch to sandbox (ENVIRONMENT_SANDBOX)
// or live (ENVIRONMENT_PRODUCTION)
.environment(PayPalConfiguration.ENVIRONMENT_SANDBOX)
// on below line we are passing a client id.
.clientId(clientKey);
private EditText etAmount;
private TextView tvPayment;
private Button btnMakePayment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// on below line we are initializing our variables.
etAmount = findViewById(R.id.etAmount);
// creating a variable for button, edit text and status tv.
btnMakePayment = findViewById(R.id.btnPay);
tvPayment = findViewById(R.id.tvStatus);
// on below line adding click listener to our make payment button.
btnMakePayment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calling a method to get payment.
getPayment();
}
});
}
private void getPayment() {
// Getting the amount from editText
String amount = etAmount.getText().toString();
// Creating a paypal payment on below line.
PayPalPayment payment = new PayPalPayment(new BigDecimal(String.valueOf(amount)), "USD", "Course Fees",
PayPalPayment.PAYMENT_INTENT_SALE);
// Creating Paypal Payment activity intent
Intent intent = new Intent(this, PaymentActivity.class);
//putting the paypal configuration to the intent
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
// Puting paypal payment to the intent
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);
// Starting the intent activity for result
// the request code will be used on the method onActivityResult
startActivityForResult(intent, PAYPAL_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// If the result is from paypal
if (requestCode == PAYPAL_REQUEST_CODE) {
// If the result is OK i.e. user has not canceled the payment
if (resultCode == Activity.RESULT_OK) {
// Getting the payment confirmation
PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
// if confirmation is not null
if (confirm != null) {
try {
// Getting the payment details
String paymentDetails = confirm.toJSONObject().toString(4);
// on below line we are extracting json response and displaying it in a text view.
JSONObject payObj = new JSONObject(paymentDetails);
String payID = payObj.getJSONObject("response").getString("id");
String state = payObj.getJSONObject("response").getString("state");
tvPayment.setText("Payment " + state + "\n with payment id is " + payID);
} catch (JSONException e) {
// handling json exception on below line
Log.e("Error", "an extremely unlikely failure occurred: ", e);
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
// on below line we are checking the payment status.
Log.i("paymentExample", "The user canceled.");
} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
// on below line when the invalid paypal config is submitted.
Log.i("paymentExample", "An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
}
}
}
}
NOTE:use alt+enter to import the classes.
That is all run your app and it should run without errors.