Skip to content

Commit

Permalink
Add proxy to call whatsapp server
Browse files Browse the repository at this point in the history
  • Loading branch information
hvs-flick committed Dec 24, 2023
1 parent c4b47f2 commit 1b1116a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ProxyConfiguration {
Expand All @@ -20,4 +21,9 @@ public class ProxyConfiguration {
PhonePePaymentClient phonePePaymentClient() {
return new PhonePePaymentClient(merchantId, saltKey, saltIndex, Env.PROD, true);
}

@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.flickmatch.platform.proxy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class WhatsAppProxy {
// Create a RestTemplate
@Autowired
private RestTemplate restTemplate;

public void sendNotification() {
String nodeServerUrl = "http://ec2-18-223-205-234.us-east-2.compute.amazonaws.com:3000/";

// Create headers
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

// Create the request payload
String requestData = "{\"name\":\"John\",\"age\":30}";
HttpEntity<String> requestEntity = new HttpEntity<>(requestData, headers);

// Make a POST request to the Node.js server
ResponseEntity<String> responseEntity = restTemplate.getForEntity(nodeServerUrl, String.class);
// Print the response
if (responseEntity.getStatusCode().is2xxSuccessful()) {
String responseBody = responseEntity.getBody();
System.out.println("Response from Node.js server: " + responseBody);
} else {
System.err.println("Failed to post data to Node.js server");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.flickmatch.platform.dynamodb.model.PaymentRequest;
import com.flickmatch.platform.graphql.builder.EventBuilder;
import com.flickmatch.platform.graphql.builder.PaymentRequestBuilder;
import com.flickmatch.platform.proxy.WhatsAppProxy;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JacksonJsonParser;
Expand All @@ -32,6 +33,8 @@ public class PhonePeCallBackController {
PaymentRequestBuilder paymentRequestBuilder;
@Autowired
EventBuilder eventBuilder;
@Autowired
WhatsAppProxy whatsAppProxy;

@PostMapping("/payment")
void processCallBack(@RequestBody CallBackResponse callBackResponse,
Expand All @@ -48,6 +51,7 @@ void processCallBack(@RequestBody CallBackResponse callBackResponse,
if ("PAYMENT_SUCCESS".equals(phonePeResponse.get("code"))) {
eventBuilder.joinEvent(paymentRequest);
paymentRequestBuilder.updatePaymentRequestStatus(paymentRequest, true);
whatsAppProxy.sendNotification();
} else {
log.info(merchantTransactionId);
log.error(decodedResponse);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.flickmatch.platform.proxy;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;

@ExtendWith(MockitoExtension.class)
class WhatsAppProxyTest {

@Mock
RestTemplate restTemplate;

@InjectMocks WhatsAppProxy whatsAppProxy;

@Test
public void test_WhenCallIsSuccessful() {
Mockito.when(restTemplate.getForEntity(anyString(), any())).thenReturn(ResponseEntity.accepted().build());
whatsAppProxy.sendNotification();
Mockito.verify(restTemplate).getForEntity(anyString(), any());
}

@Test
public void test_WhenCallFails() {
Mockito.when(restTemplate.getForEntity(anyString(), any())).thenReturn(ResponseEntity.internalServerError().build());
whatsAppProxy.sendNotification();
Mockito.verify(restTemplate).getForEntity(anyString(), any());
}
}

0 comments on commit 1b1116a

Please sign in to comment.