Skip to content

Commit

Permalink
Added payment getway
Browse files Browse the repository at this point in the history
  • Loading branch information
Amarjha01 committed Oct 26, 2024
1 parent b177945 commit 5cffdb3
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 6 deletions.
78 changes: 78 additions & 0 deletions backend/api/controllers/PaymentGetway.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const crypto = require('crypto')
const axios = require('axios')

const paymentGetway = async (req, res) => {
console.log('paymentInitiated')
try {


// Payload structure for PhonePe API
const data = {
"merchantId": "PGTESTPAYUAT86", // Example Merchant ID
'merchantTransactionId': `MT${Date.now()}`, // Dynamic Transaction ID
'merchantUserId': "MUID123",
'amount': 10000, // Amount in paise (100 rupees)
'redirectUrl': "http://localhost:3000",
'redirectMode': 'REDIRECT',
'paymentInstrument': {
'type': "PAY_PAGE",
},
};

// Base64 encode the payload
const payLoad = JSON.stringify(data);
const base64Payload = Buffer.from(payLoad).toString("base64");

// Compute X-VERIFY checksum
const saltKey = "96434309-7796-489d-8924-ab56988a6076"; // Example salt key
const saltIndex = "1"; // Example salt index
const stringToHash = base64Payload + '/pg/v1/pay' + saltKey;
const sha256Hash = crypto.createHash('sha256').update(stringToHash).digest('hex');
const checksum = sha256Hash + '###' + saltIndex;

// Setup the request options
const options = {
method: 'POST',
url: 'https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay',
headers: {
accept: 'application/json', // Expect a JSON response
'Content-Type': 'application/json',
'X-VERIFY': checksum, // X-VERIFY checksum header
},
data: {
request: base64Payload, // Base64-encoded payload
},
};

// Send the request using Axios
axios
.request(options)
.then(function (response) {
const redirectUrl = response.data.data.instrumentResponse.redirectInfo.url;
console.log('response.data: ', redirectUrl);

return res.status(200).json({
success:true,
url:redirectUrl
}); // Send back the API response
})
.catch(function (error) {
console.error('Error response:', error);
res.status(500).json({
success: false,
message: 'Payment gateway request failed',
error: error.response ? error.response.data : error.message,
});
});

} catch (error) {
console.error('Error:', error.message);
res.status(500).json({
success: false,
message: 'Server error',
error: error.message,
});
}
};

module.exports = paymentGetway;
4 changes: 2 additions & 2 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const helmet = require("helmet");
const authMiddleware = require("./api/middleware/authMiddleware");
require("dotenv").config();
const bodyParser = require('body-parser');

const payment = require("./api/controllers/PaymentGetway.js")

// Connect to the database
dbConnect();
Expand All @@ -39,7 +39,7 @@ app.use("/api/cart", authMiddleware, cartRoute); // Cart routes
app.use("/api/product", productRoute); // Product routes
app.use("/api/user", userRoute); // user routes
app.use("/api/auth", authStatusRoute); // Check Auth Status

app.use("/api/pay", payment)

// // Rate limiting middleware
// const limiter = rateLimit({
Expand Down
11 changes: 10 additions & 1 deletion src/common/constants/apiConstants.js
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
export const API_BASE_URL = 'http://localhost:8080/api';
export const API_BASE_URL = 'http://localhost:8080';

const commonBackendURL = {
paymentGetway: {
url: `${API_BASE_URL}/api/pay`,
method: 'post'
},
}

Check failure on line 8 in src/common/constants/apiConstants.js

View workflow job for this annotation

GitHub Actions / check

Missing semicolon

Check failure on line 8 in src/common/constants/apiConstants.js

View workflow job for this annotation

GitHub Actions / check

Missing semicolon

export default commonBackendURL;
38 changes: 35 additions & 3 deletions src/pages/Cart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { LeftDivider } from '../styles/Divider';
import { useDispatch } from 'react-redux';
import { Box, useToast } from '@chakra-ui/react';
import { deleteCartItem, updateCartItem } from '../store/slices/cartSlice';

import commonBackendURL from '../common/constants/apiConstants.js'

Check failure on line 13 in src/pages/Cart.jsx

View workflow job for this annotation

GitHub Actions / check

Missing semicolon

Check failure on line 13 in src/pages/Cart.jsx

View workflow job for this annotation

GitHub Actions / check

Missing semicolon
const Container = styled.div``;

const Wrapper = styled.div`
Expand Down Expand Up @@ -203,6 +203,38 @@ const Cart = () => {
const totalPrice = cartItems.reduce((total, item) => total + item.price * item.quantity, 0);
const toast = useToast();
const dispatch = useDispatch();
// payment getway initiate and send data to backend
const initiatePayment = async () => {
try {
// Await the fetch to complete
const response = await fetch(commonBackendURL.paymentGetway.url, {
method: commonBackendURL.paymentGetway.method,
credentials: "include",
headers: {
"content-type": "application/json",
}
});

// Parse the response as JSON
const responseData = await response.json();

// Log the parsed response data
console.log('responseData:', responseData);

// If the response contains a redirect URL, perform the redirection
if (responseData.success && responseData.url) {
const redirectUrl = responseData.url;
console.log('Redirecting to:', redirectUrl);

// Perform the redirection
window.location.href = redirectUrl;
// onSuccessPayment();
Cart();
}
} catch (error) {
console.error('Error:', error);
}
}

Check failure on line 237 in src/pages/Cart.jsx

View workflow job for this annotation

GitHub Actions / check

Missing semicolon

Check failure on line 237 in src/pages/Cart.jsx

View workflow job for this annotation

GitHub Actions / check

Missing semicolon

const handleQuantityChange = (productId, quantity) => {
dispatch(updateCartItem({ productId, quantity }))
Expand Down Expand Up @@ -275,7 +307,7 @@ const Cart = () => {
<Link to={'/wishlist'}>Your Wishlist ({wishListItems?.length})</Link>
</TopText>
</TopTexts>
<TopButton type='filled'>CHECKOUT NOW</TopButton>
<TopButton type='filled' onClick={()=>{initiatePayment()}}>CHECKOUT NOW</TopButton>

Check failure on line 310 in src/pages/Cart.jsx

View workflow job for this annotation

GitHub Actions / check

Missing semicolon

Check failure on line 310 in src/pages/Cart.jsx

View workflow job for this annotation

GitHub Actions / check

Missing semicolon
</Top>
<Bottom>
<Info>
Expand Down Expand Up @@ -319,7 +351,7 @@ const Cart = () => {
<SummaryItemText>Total</SummaryItemText>
<SummaryItemPrice>$ {totalPrice}</SummaryItemPrice>
</SummaryItem>
<Button>CHECKOUT NOW</Button>
<Button onClick={()=>{initiatePayment()}}>CHECKOUT NOW</Button>

Check failure on line 354 in src/pages/Cart.jsx

View workflow job for this annotation

GitHub Actions / check

Missing semicolon

Check failure on line 354 in src/pages/Cart.jsx

View workflow job for this annotation

GitHub Actions / check

Missing semicolon
</Summary>
</Bottom>
</Wrapper>
Expand Down

0 comments on commit 5cffdb3

Please sign in to comment.