Here i show you how to make PayPal Integration in android application.
implementation 'com.paypal.sdk:paypal-android-sdk:2.15.3'
Now , in your design layout activity_main.xml file add edittext for enter amount value and Pay button.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/linearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Amount"
/>
<EditText
android:id="@+id/edtAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btnPay"
android:text="Pay Now"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
Now make another design layout for confirmation of payment information, make activity_confirmation.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Payment Amount : " />
<TextView
android:id="@+id/paymentAmount"
android:layout_width="wrap_content"
android:textStyle="bold"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Status : " />
<TextView
android:id="@+id/paymentStatus"
android:layout_width="wrap_content"
android:textStyle="bold"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Payment Id : " />
<TextView
android:id="@+id/paymentId"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
</RelativeLayout>
Now, Java code in MainActivity.java file
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.paypal.android.sdk.payments.PayPalConfiguration;
import com.paypal.android.sdk.payments.PayPalPayment;
import com.paypal.android.sdk.payments.PayPalService;
import com.paypal.android.sdk.payments.PaymentActivity;
import com.paypal.android.sdk.payments.PaymentConfirmation;
import org.json.JSONException;
import java.math.BigDecimal;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
public static final String PAYPAL_CLIENT_ID = "PAYPAL CLIENT-ID HERE";
//The views
private Button btnPay;
private EditText edtAmount;
//Payment Amount
private String paymentAmount;
//Paypal intent request code to track onActivityResult method
public static final int PAYPAL_REQUEST_CODE = 123;
//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_NO_NETWORK)
.clientId(PAYPAL_CLIENT_ID);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPay = (Button) findViewById(R.id.btnPay);
edtAmount = (EditText) findViewById(R.id.edtAmount);
btnPay.setOnClickListener(MainActivity.this);
Intent intent = new Intent(this, PayPalService.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
startService(intent);
}
@Override
public void onDestroy() {
stopService(new Intent(this, PayPalService.class));
super.onDestroy();
}
private void getPayment() {
//Getting the amount from editText
paymentAmount = edtAmount.getText().toString();
//Creating a paypalpayment
PayPalPayment payment = new PayPalPayment(new BigDecimal(String.valueOf(paymentAmount)), "USD", "Test Payment",
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, Intent data) {
//If the result is from paypal
super.onActivityResult(requestCode, resultCode, data);
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);
Log.i("payment_Example", paymentDetails);
//Starting a new activity for the payment details and also putting the payment details with intent
startActivity(new Intent(this, PayconfirmationActivity.class)
.putExtra("Payment_Details", paymentDetails)
.putExtra("Payment_Amount", paymentAmount));
} catch (JSONException e) {
Log.e("payment_Example", "error occurred: ", e);
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.i("payment_Example", "The user canceled.");
} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
Log.i("payment_Example", "An invalid Payment or PayPalConfiguration was submitted.");
}
}
}
@Override
public void onClick(View v) {
getPayment();
}
}
And Make another PayconformationActivity.java for display confirmation of Payment. and define this .java file in Manifest file also.
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import androidx.appcompat.app.AppCompatActivity;
public class PayconfirmationActivity extends AppCompatActivity {
TextView txtid,txtstatus,txtamount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirmation);
txtid = (TextView) findViewById(R.id.payment_id);
txtstatus= (TextView) findViewById(R.id.payment_status);
txtamount = (TextView) findViewById(R.id.payment_amount);
Intent intent = getIntent();
try {
JSONObject jsonDetails = new JSONObject(intent.getStringExtra("Payment_Details"));
String payment_amount = intent.getStringExtra("Payment_Amount");
txtid.setText(jsonDetails.getString("id"));
txtstatus.setText(jsonDetails.getString("state"));
txtamount.setText(payment_amount+" USD");
} catch (JSONException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}Now , Here is the Code of this payment integration ,
No comments:
Post a Comment
Comments