@RestResource(urlMapping='/razorpay/webhook/*') global without sharing class RazorpayWebhookHandler { @HttpPost global static void handleWebhook() { String requestBody = RestContext.request.requestBody.toString(); System.debug('Received payload: ' + requestBody); Map payload = (Map) JSON.deserializeUntyped(requestBody); // Process the webhook payload if(payload != null){ processWebhook(payload); } else{ System.debug('No Payload'); } } private static void processWebhook(Map payload) { try { String eventType = (String) payload.get('event'); Map paymentDetails = (Map) payload.get('payload'); Map payment = (Map) paymentDetails.get('payment_link'); Map entityMap = (Map) payment.get('entity'); String paymentLinkId = (String) entityMap.get('id'); String paymentStatus = (String) entityMap.get('status'); Decimal paidAmount = (Decimal) entityMap.get('amount_paid'); // Update the Salesforce record with the new payment status updatePaymentStatus(paymentLinkId, paymentStatus,paidAmount); RestContext.response.statusCode = 200; RestContext.response.addHeader('Content-Type', 'application/json'); RestContext.response.responseBody = Blob.valueOf('{"status":"success"}'); } catch (Exception e) { System.debug('Error in webhook processing: ' + e.getMessage() + '\n' + e.getStackTraceString()); RestContext.response.statusCode = 500; RestContext.response.addHeader('Content-Type', 'application/json'); RestContext.response.responseBody = Blob.valueOf('{"status":"error","message":"' + e.getMessage() + '"}'); } } private static void updatePaymentStatus(String paymentLinkId, String paymentStatus,Decimal paidAmount) { System.debug('PaymentLinkId: '+ paymentLinkId + 'paymentStatus :'+paymentStatus); List oppList = [SELECT Id,Payment_Link_ID__c,Payment_Staus__c,Recieved_Amount__c FROM Opportunity WHERE Payment_Link_ID__c = :paymentLinkId LIMIT 1]; if (!oppList.isEmpty()) { Opportunity opp = oppList[0]; opp.Payment_Staus__c = paymentStatus; opp.Recieved_Amount__c = paidAmount/100; update opp; } } }