My_Test_Repo/force-app/main/default/classes/RazorpayWebhookHandler.cls

57 lines
2.6 KiB
OpenEdge ABL

@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<String, Object> payload = (Map<String, Object>) JSON.deserializeUntyped(requestBody);
// Process the webhook payload
if(payload != null){
processWebhook(payload);
}
else{
System.debug('No Payload');
}
}
private static void processWebhook(Map<String, Object> payload) {
try {
String eventType = (String) payload.get('event');
Map<String, Object> paymentDetails = (Map<String, Object>) payload.get('payload');
Map<String, Object> payment = (Map<String, Object>) paymentDetails.get('payment_link');
Map<String, Object> entityMap = (Map<String, Object>) 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<Opportunity> 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;
}
}
}