0% found this document useful (0 votes)
102 views

Durgeshsb Payment Integration

The document explains how to integrate Razorpay payment gateway in a Java web application. It includes steps to generate API keys, make API calls to create orders and process payments using Razorpay Checkout. JavaScript and jQuery are used to make AJAX requests from the frontend to the backend API.

Uploaded by

subhabirajdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Durgeshsb Payment Integration

The document explains how to integrate Razorpay payment gateway in a Java web application. It includes steps to generate API keys, make API calls to create orders and process payments using Razorpay Checkout. JavaScript and jQuery are used to make AJAX requests from the frontend to the backend API.

Uploaded by

subhabirajdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Create A Razorpay Account

Step 2:

Generate and download API Keys.(Setting->API Keys->Generate API Keys)

After generate

Key id rzp_test_HdbQ0mLDRL9PZW

Key Secret 2iMIFUl2vSLSc9oakljTXwgZ

Go to userdash_board.html

<hr>
<h3 class="my-3">Donate Us</h3>
<input id="payment_field" type="text" class="form-control
my-2" placeholder="Enter amount here" />
<div class="container text-center mt-3">
<button onclick="paymentStart()" class="btn btn-success
btn-block">CHECKOUT</button>
</div>
</div>//here we are using Ajax through we are sending request

So here we are writing function and we write in script.js file

//first Request -to server to create order


//first Request -to server to create order

const paymentStart = ()=>{


console.log("payment started..")
let amount=$("#payment_field").val();
console.log(amount);
if(amount=="" || amount==null){
//alert("amount is required !!");
swal("Failed!","amount is required !!","error");
return;
}

//code...
//we will use ajax to send requet to server to create order
$.ajax({
url:"/user/create_order", //ab humara data jayega user ke
create_order pe
data:JSON.stringify({amount:amount,info:"order_request"}),
contentType:"application/json",
type:"POST",
dataType:"json",
success:function(response){
//invoked when success
console.log(response);
if(response.status=="created"){
//open payment form
let options ={
key:"rzp_test_HdbQ0mLDRL9PZW",
amount:response.amount,
currency:"INR",
name:"Subhash sb Payment",
description:'Donation',
image:"https://unsplash.com/s/photos/human",
order_id:response.id,
handler:function(response){
console.log(response.razorpay_payment_id);
console.log(response.razorpay_order_id);
console.log(response.razorpay_signature);
console.log("payment successful !!");
//alert("congrates !! Payment successful !!");
swal("Good job!","congrates !! Payment
successful !!","success");
},
prefill:{
name:"",
email:"",
contact:"",
},

notes:{
address:"Rozarpay Corporate Office",
},
theme:{
color:"#3399cc"
},
};

let rzp = new Razorpay(options);

rzp.on("payment failed",function(response){
console.log(response.error.code);
console.log(response.error.description);
console.log(response.error.source);
console.log(response.error.step);
console.log(response.error.reason);
console.log(response.error.metadata.order_id);
console.log(response.error.metadata.payment_id);
});

rzp.open();
}
},
else:function(error){
//invoked when error
console.log(error);
//alert("something went wrong !!");
swal("Failed!","something went wrong !!","error");

}
}
)

};
//add request to controller

//creating order for payment


@PostMapping("/create_order")
@ResponseBody
public String createOrder(@RequestBody Map<String,
Object> data) throws Exception {
//System.out.println("hey order function
executed");
System.out.println(data);
int
amt=Integer.parseInt(data.get("amount").toString());

String key = "rzp_test_HdbQ0mLDRL9PZW";


String secret = "2iMIFUl2vSLSc9oakljTXwgZ";

RazorpayClient client = new RazorpayClient(key,


secret);

// var client =new


RazorpayClient("rzp_test_HdbQ0mLDRL9PZW","2iMIFUl2vSLSc9oakl
jTXwgZ");

JSONObject obj = new JSONObject();


obj.put("amount", amt*100);
obj.put("currency", "INR");
obj.put("receipt","txn_235466");

//creating new order


Order order = client.orders.create(obj);
System.out.println(order);

//if you want you can save this to your database..


//order to string me convert karke return bhejenge
//here wapas jayega
return order.toString();

}
https://razorpay.com/docs/payments/server-integration/java/payment-gateway/build-integration/

//base.html normal

//before the script

<script
src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></
script>
<script
src="https://checkout.razorpay.com/v1/checkout.js"></script>
//in same base.html for ajax file before head tag

<script
src="https://code.jquery.com/jquery-3.7.0.min.js"
integrity="sha256-
2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g="
crossorigin="anonymous"></script>

You might also like