Midtrans ❤️ Java! This is the Official Midtrans java-client sample store Spring Boot web application. For more information about the product and documentation please refer to http://docs.midtrans.com and also you can try visit demo app.
You can get the sample store source code from Midtrans Java Client repository on Midtrans Github official, by cloning or downloading the repo. This sample store web application is using Spring Boot Framework. This web app use Maven build tools to compile, build, and running the application. Before building and running sample store application, you need to setup several config first such as merchant server-key and client-key which can be obtained from Midtrans Admin Portal.
- CoreApiController.java
Config for
MidtransCoreApi
object, the 3rd param (false
) is environment type. Available value are:true
for Production andfalse
for Sandbox mode. ~/midtrans-java/example/src/main/java/com/midtrans/sample/controller/CoreApiController.java
MidtransCoreApi coreApi = new ConfigFactory(new Config("YOU_SERVER_KEY","YOUR_CLIENT_KEY", false)).getCoreApi();
- SnapController.java
Config for
MidtransSnapApi
object, the 3rd param (false
) is environment type. Available value are:true
for Production andfalse
for Sandbox mode. ~/midtrans-java/example/src/main/java/com/midtrans/sample/controller/SnapController.java
MidtransSnapApi snapApi = new ConfigFactory(new Config("YOU_SERVER_KEY","YOUR_CLIENT_KEY", false)).getSnapApi();
- HomeController.java
The clientKey will be used for
midtrans-new-3ds.min.js
~/midtrans-java/example/src/main/java/com/midtrans/sample/controller/HomeController.java
String clientKey = "YOUR-CLIENT-KEY";
To run the app, you can simply run this command inside sample store project directory ~/midtrans-java/example
:
$ mvn spring-boot:run
you need Maven installed on your machine to run app with mvn
Maven command.
After building and running the apps via maven, you can visit http://localhost:7777/ on your browser and try out the sample store application.
On the sample store, you can try to do transactions. Then you can check your transaction status on Midtrans Admin Portal.
Midtrans java-client library usage example can be seen on controller classes. MidtransCoreApi
/ MidtransSnapApi
object is constructed with ConfigFactory object, and few configs as input.
After that, API method like chargeTransaction
from MidtransCoreApi
/ MidtransSnapApi
object is used to create the transaction.
Before charging a transaction, request params is constructed, that will be used as BodyRequest MapObject charge parameter.
For example, sample store application use DataMockup
object for request parameter.
Important note: if you use
MidtransCoreAPI
for credit card transaction, you need to convert customer card credentials into token first before you can charge the transaction. If you useMidtransSnapAPI
you just need to create snap token to create transaction. This part will be explained more detail on javascript part below.
Credit card transaction need special handling compared to other payment type. For security reason, merchant backend should not transmit any customer card credentials, instead the card credentials need to be exchanged with midtrans card token.
Midtrans card token can be obtained using javascript library that needs to be included on payment page. You can see the overview about this javascript library on Midtrans credit card documentation. For example on sample store application, midtrans javascript library is used on merchant payment page see credit-card.html
midtrans-new-3ds.min.js library is used to trigger get_token
transaction request to Midtrans api. This request will exchange customer card credentials into midtrans card token that can be use to charge a credit card transaction.
Snap frontend integration goal is to show Snap payment page within the site. You can see the overview about snap.js library on check-out.html file. Payment page is initiated by calling snap.pay
with SNAP_TOKEN
acquired from MidtransSnapApi
method createToken
which will return tokenSnap. if you need more detail about snap.js you can refer to Snap.jS documentation for detail.
example:
snap.pay(your_snap_token, {
// Optional
onSuccess: function(result){
/* You may add your own js here, this is just example */
},
// Optional
onPending: function(result){
/* You may add your own js here, this is just example */
},
// Optional
onError: function(result){
/* You may add your own js here, this is just example */
}
- Midtrans Docs
- Midtrans Dashboard
- SNAP documentation
- Core API documentation
- Can't find answer you looking for? email to [email protected]