Working with Response after a Customer Checkout
You will require to wrAll PayU India APIs use cURL format for posting requests to endpoints and endpoints provide a response in encoded URL format. You need to convert the encoded URL format to an array format.ite some amount of code to collect the details from your customer and submit those details with PayU using a cURL with PayU Hosted Checkout integration.
The response will contain response parameters and a few response parameters may contain the values in a JSON format. Therefore, you need to learn how to read and write data in the array format and JSON format. The array and JSON formats are similar, where the data is stored in key-value pairs. For instance, the following response is an example of a successful response to a Collect Payment API request.
Array
(
[mihpayid] => 4***********2
[mode] => NB
[status] => success
[unmappedstatus] => captured
[key] => JPM7Fg
[txnid] => ewP8oRopzdHEtC
[amount] => 10.00
[discount] => 0.00
[net_amount_debit] => 10
[addedon] => 2021-09-06 13:27:08
[productinfo] => iPhone
[firstname] => Ashish
[lastname] =>
[address1] =>
[address2] =>
[city] =>
[state] =>
[country] =>
[zipcode] =>
[email] => [email protected]
[phone] => 9876543210
[udf1] =>
[udf2] =>
[udf3] =>
[udf4] =>
[udf5] =>
[udf6] =>
[udf7] =>
[udf8] =>
[udf9] =>
[udf10] =>
[hash] => 1be7e6e97ab1ea9034b9a107e7cf9718308aa9637b4dbbd1a3343c91b0da02b34a40d00ac7267ebe81c20ea1129b931371c555d565bc6e11f470c3d2cf69b5a3
[field1] =>
[field2] =>
[field3] =>
[field4] =>
[field5] =>
[field6] =>
[field7] =>
[field8] =>
[field9] => Transaction Completed Successfully
[payment_source] => payu
[PG_TYPE] => NB-PG
[bank_ref_num] => 87d3b2a1-5a60-4169-8692-649f61923b3d
[bankcode] => TESTPGNB
[error] => E000
[error_Message] => No Error
)
Adding a make Payment button to your website (HTML)
After your customer completes adding the products on your website to the cart, the customer will checkout. Here, in this example, start by creating an HTML where a button.
<button id="button" type='button'>Get Joke</button>
Later, you will reference the button in the JS. TheΒ type="button"
Β tells the browser that this isnβt a typical form submission button.
You need to parse this response as per the programming language you are using to implement. For example, the following sample code is in JS, Java, Python, and PHP:
import requests
def button_click(event):
# Checking if the button was clicked
if not event.target.matches("#button"):
return
# Fetching the URL and logging the response data
response = requests.get("https://test.payu.in/_payment")
data = response.json()
print(data)
# Registering the event listener on button click
document.addEventListener("click", button_click)
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['button'])) {
// Perform your fetch request here using the PHP cURL library
$url = "https://test.payu.in/_payment";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Decode the JSON response
$data = json_decode($response, true);
var_dump($data); // Output the decoded JSON data
}
?>
document.addEventListener("click", function (event) {
// Checking if the button was clicked
if (!event.target.matches("#button")) return;
fetch("<https://https://test.payu.in/_payment>")
.then((response) => response.json())
.then((data) => console.log(data));
});
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
public class Example {
public static void main(String[] args) {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://test.payu.in/_payment"))
.build();
// Registering the event listener on button click
// Checking if the button was clicked
// Fetching the URL and logging the response data
CompletableFuture<HttpResponse<String>> future = client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println);
}
}
In the above code snippet:
- Javascript:
Here, the code snippet is for listening for all clicks. The Fetch API (provided by the browser) makes requests andΒ fetches resources. Here, theΒ fetch()
Β and two instances ofΒ .then()
.
fetch("<https://https://test.payu.in/_payment>")
.then((response) => response.json())
.then((data) => console.log(data));
- Java: The Java HTTP client is used to make an asynchronous HTTP request to the specified URL. Then, log the response data to the console.
- Python: A function
button_click
checks if the button was clicked and fetches the specified URL using therequests
library. Then, decoding the JSON response using the.json()
method and printing it usingprint()
. The document.addEventListener line is specific to the browser environment and wonβt work in Python. Youβll need to adjust that part of the code to fit your specific use case. - PHP: The code checks if the button was clicked by checking if the HTTP request method is POST and if the βbuttonβ parameter was sent in the request. Later, posting a cURL request to the specified URL and decoding the JSON response using
json_decode()
. Finally, the output of the decoded data usingvar_dump()
.
Updated 12 months ago