Stripe Payment Integration with android
This post will explain you how to integrate your application with the stripe payment gateway.
Well, Last year I have made video on stripe payment integration and people seems to found it useful but for some reason my website has been taken down. That why I am going to rewrite the code again in this blog post.
If you prefer to watch video tutorial, Please check this out.
Also if you just want the source code , you will find it at the bottom of this blog post.
Now brace your self and lets get started.
This post has two part
- Android Project
- Node js (Server code) It is just 10 loc
We will start from android
- Android Project
a) In build.gradle
file
- Add
stripe
,okhttp
andgson
library
b)In AndroidManifest.xml
- Add internet permission
c)In activity_main.xml
- Now in
activity_main.xml
, design payment UI
d)In ActivityMain.java
Spoiler Alert: ActivityMain.java code is lengthy and it will get you bore, But it is actually simple and worth it. Copy and paste ActivityMain.java code in your project and read these point to understand better.
- Initialize view and stripe
- We need to set up BACKEND_URL in your project, we will do it in a minute
- Now when
payBtn
is clicked get the amount and start progress and call start checkout to getpaymentIntentClientSecret
key. - You will get
paymentIntentClientSecret
key in onPaymentSuccess method and then callconfirmPayment
function to start transaction - You will get the result in
onActivityResult
method and callPaymentResultCallback
and it will call onSuccess method if transaction is successful inside that class. - Your payment is complete
- In MainActivity.java
e)Before you run the we need to set up BACKEND_URL
so for that we need to deploy our server code and add network security configuration first.
- This setup is done to call network in your emulator and real device
- To set network security configuration, create new xml called network_security_config.xml
- If you are using emulator add 10.0.2.2
- For real device make sure you are connected to same network and then get IP of your pc and paste it here. In my case 192.168.1.6
- You can get your IP by run->
cmd
->ipconfig
->IPv4 address
f) Once you create network_security_config.xml, update your AndroidManifest.xml file like below.
Hu!! Now that android part is done. Lets start server side
2. Server Code
I know you are here for Android but trust me it is just few line of code and setup.
Get code from GitHub
or create new nodejs
project.
- If you are using
GitHub
code, then just run node index.js incmd
and update port and URL ofBACKEND_URL
and you are good to go. - But if you want to create new project
a) Create new project from cmd npm init
b) Install express npm install express — save
c) Install express npm install stripe — save
now create new file name index.js and add following code
Now update your BACKEND_URL
to http://YOUR_IP:PORT/, In above case PORT no is 4242.
Now run node index.js in your cmd
Okay, lot of work right. But It will pay off. Now run the app and check. Payment should work by now and It will reflect in stripe dashboard too.
Output
- Application
- Dashboard
Stripe Dashboard after payment
Let me know where ever you face issue in in comment section I will be happy to help you out. Also you can get the source code from GitHub link.
Alright, So again you made till end of this post, great achievement!!!
Checkout more blog from CodingWithTashi. Follow me on twitter or Instagram or YouTube. Buy me a cup of coffee PayPal ☕
That all,Thanks guys, I hope you like this shitty blog. please make sure to give clap 👏 and leave some engagement.