@@ -48,12 +48,12 @@ export function verifyRazorpaySignature(body, signature) {
4848const PHONEPE_CONFIG = {
4949 sandbox : {
5050 baseUrl : 'https://api-preprod.phonepe.com/apis/pg-sandbox' ,
51- // Test credentials - use for sandbox testing
51+ // Test phone/OTP for sandbox testing
5252 testPhone : '9999999999' ,
5353 testOtp : '123456'
5454 } ,
5555 production : {
56- baseUrl : 'https://api.phonepe.com/apis/hermes '
56+ baseUrl : 'https://api.phonepe.com/apis/pg '
5757 }
5858} ;
5959
@@ -62,40 +62,45 @@ function getPhonePeBaseUrl() {
6262 return PHONEPE_CONFIG [ env ] ?. baseUrl || PHONEPE_CONFIG . sandbox . baseUrl ;
6363}
6464
65- function generatePhonePeChecksum ( payload , endpoint ) {
66- const saltKey = process . env . PHONEPE_SALT_KEY ;
65+ function generatePhonePeChecksum ( base64Payload , endpoint ) {
66+ const clientSecret = process . env . PHONEPE_CLIENT_SECRET ;
6767 const saltIndex = process . env . PHONEPE_SALT_INDEX || '1' ;
6868
69- const base64Payload = Buffer . from ( JSON . stringify ( payload ) ) . toString ( 'base64' ) ;
70- const stringToHash = base64Payload + endpoint + saltKey ;
69+ const stringToHash = base64Payload + endpoint + clientSecret ;
7170 const sha256Hash = crypto . createHash ( 'sha256' ) . update ( stringToHash ) . digest ( 'hex' ) ;
7271
73- return {
74- base64Payload,
75- checksum : sha256Hash + '###' + saltIndex
76- } ;
72+ return sha256Hash + '###' + saltIndex ;
7773}
7874
7975export async function createPhonePePayment ( order , callbackUrl ) {
8076 try {
81- const merchantId = process . env . PHONEPE_MERCHANT_ID ;
77+ const clientId = process . env . PHONEPE_CLIENT_ID ;
78+ const clientSecret = process . env . PHONEPE_CLIENT_SECRET ;
79+
80+ if ( ! clientId || ! clientSecret ) {
81+ throw new Error ( 'PhonePe credentials not configured' ) ;
82+ }
83+
8284 const merchantTransactionId = order . id . replace ( / - / g, '' ) . slice ( 0 , 35 ) ; // PhonePe limit: 35 chars
8385
8486 const payload = {
85- merchantId,
87+ merchantId : clientId ,
8688 merchantTransactionId,
87- merchantUserId : order . registration ?. userEmail || 'guest' ,
89+ merchantUserId : order . registration ?. userEmail ?. replace ( / [ ^ a - z A - Z 0 - 9 ] / g , '' ) || 'guest' ,
8890 amount : order . amountCents , // Amount in paise
8991 redirectUrl : callbackUrl ,
90- redirectMode : 'POST ' ,
91- callbackUrl : ` ${ process . env . FRONTEND_URL } /api/webhooks/phonepe` ,
92+ redirectMode : 'REDIRECT ' ,
93+ callbackUrl : callbackUrl ,
9294 paymentInstrument : {
9395 type : 'PAY_PAGE'
9496 }
9597 } ;
9698
99+ const base64Payload = Buffer . from ( JSON . stringify ( payload ) ) . toString ( 'base64' ) ;
97100 const endpoint = '/pg/v1/pay' ;
98- const { base64Payload, checksum } = generatePhonePeChecksum ( payload , endpoint ) ;
101+ const checksum = generatePhonePeChecksum ( base64Payload , endpoint ) ;
102+
103+ console . log ( 'PhonePe payment request:' , { clientId, merchantTransactionId, amount : order . amountCents } ) ;
99104
100105 const response = await fetch ( `${ getPhonePeBaseUrl ( ) } ${ endpoint } ` , {
101106 method : 'POST' ,
@@ -107,6 +112,7 @@ export async function createPhonePePayment(order, callbackUrl) {
107112 } ) ;
108113
109114 const data = await response . json ( ) ;
115+ console . log ( 'PhonePe payment initiation failed:' , data ) ;
110116
111117 if ( data . success && data . data ?. instrumentResponse ?. redirectInfo ?. url ) {
112118 return {
@@ -126,12 +132,12 @@ export async function createPhonePePayment(order, callbackUrl) {
126132
127133export async function checkPhonePePaymentStatus ( merchantTransactionId ) {
128134 try {
129- const merchantId = process . env . PHONEPE_MERCHANT_ID ;
130- const saltKey = process . env . PHONEPE_SALT_KEY ;
135+ const clientId = process . env . PHONEPE_CLIENT_ID ;
136+ const clientSecret = process . env . PHONEPE_CLIENT_SECRET ;
131137 const saltIndex = process . env . PHONEPE_SALT_INDEX || '1' ;
132138
133- const endpoint = `/pg/v1/status/${ merchantId } /${ merchantTransactionId } ` ;
134- const stringToHash = endpoint + saltKey ;
139+ const endpoint = `/pg/v1/status/${ clientId } /${ merchantTransactionId } ` ;
140+ const stringToHash = endpoint + clientSecret ;
135141 const sha256Hash = crypto . createHash ( 'sha256' ) . update ( stringToHash ) . digest ( 'hex' ) ;
136142 const checksum = sha256Hash + '###' + saltIndex ;
137143
@@ -140,11 +146,12 @@ export async function checkPhonePePaymentStatus(merchantTransactionId) {
140146 headers : {
141147 'Content-Type' : 'application/json' ,
142148 'X-VERIFY' : checksum ,
143- 'X-MERCHANT-ID' : merchantId
149+ 'X-MERCHANT-ID' : clientId
144150 }
145151 } ) ;
146152
147153 const data = await response . json ( ) ;
154+ console . log ( 'PhonePe status check response:' , data ) ;
148155
149156 return {
150157 success : data . success ,
@@ -163,10 +170,10 @@ export async function checkPhonePePaymentStatus(merchantTransactionId) {
163170
164171export function verifyPhonePeCallback ( xVerifyHeader , responseBody ) {
165172 try {
166- const saltKey = process . env . PHONEPE_SALT_KEY ;
173+ const clientSecret = process . env . PHONEPE_CLIENT_SECRET ;
167174 const saltIndex = process . env . PHONEPE_SALT_INDEX || '1' ;
168175
169- const stringToHash = responseBody + '/pg/v1/status' + saltKey ;
176+ const stringToHash = responseBody + '/pg/v1/status' + clientSecret ;
170177 const sha256Hash = crypto . createHash ( 'sha256' ) . update ( stringToHash ) . digest ( 'hex' ) ;
171178 const expectedChecksum = sha256Hash + '###' + saltIndex ;
172179
0 commit comments