Untitled

mail@pastecode.io avatar
unknown
java
a year ago
3.7 kB
7
Indexable
Never
package com.smm.insta.controllers;

import com.smm.insta.payload.PaytmOrder;
import com.smm.insta.payload.PhonepeOrder;
import com.smm.insta.services.OrdersService;
import com.smm.insta.utils.AppConstants;
import com.smm.insta.utils.AppUtility;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

import static com.smm.insta.utils.AppConstants.INSTA_BASE;

@RestController
@RequestMapping(INSTA_BASE + AppConstants.ENDPOINTS.PHONEPE)
public class PhonePeController {

    @Autowired
    private OrdersService ordersService;

    String pgUrl = "/pg/v1/pay";
    String debugSaltKey = "05b8ed83-51ef-49ae-bb8b-aac4df140457";
    String debugMID = "PGTESTPAYUAT111";
    String debugApiUrl = "https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay";

    @PostMapping(AppConstants.ENDPOINTS.INITIATE_PHONEPE_TXN)
    public ResponseEntity<String> initiatePhonePeTxn(@RequestBody PhonepeOrder phonepeOrder) {
        JSONObject phonePeBody = new JSONObject();
        phonePeBody.put("merchantId", debugMID);
        phonePeBody.put("merchantTransactionId", System.currentTimeMillis() + "");
        phonePeBody.put("amount", phonepeOrder.getAmount().toString().replace(".", "") + "0");
        phonePeBody.put("merchantUserId", phonepeOrder.getEmail());
        phonePeBody.put("redirectUrl", "https://www.youtube.com/");
        phonePeBody.put("redirectMode", "POST");
        phonePeBody.put("callbackUrl", "https://igboosts.info/smm-insta/support/phonepe-callback");
        //phonePeBody.put("callbackUrl", "https://webhook.site/4b626c83-8d0e-47e8-8745-9928037adbec");
        JSONObject paymentInstrument = new JSONObject();
        paymentInstrument.put("type", "PAY_PAGE");
        phonePeBody.put("paymentInstrument", paymentInstrument);

        String request = AppUtility.encodeToBase64(phonePeBody.toString());

        String sha256 = AppUtility.hashToSHA256(request + pgUrl + debugSaltKey);
        String xVerify = sha256 + "###1";


        //return phonePeBody + "\n\n" + AppUtility.encodeToBase64(phonePeBody.toString()) + "\n\n" + xVerify + "\n\n" + debugCurl;
        return new ResponseEntity<>(callExternalApi(xVerify, request), HttpStatus.OK);
    }

    public String callExternalApi(String xVerify, String request) {
        RestTemplate restTemplate = new RestTemplate();

        String requestBody = "{\"request\": \"" + request + "\"}";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("accept", "application/json");
        headers.set("x-verify", xVerify);

        HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);

        ResponseEntity<String> responseEntity = restTemplate.exchange(
                debugApiUrl,
                HttpMethod.POST,
                requestEntity,
                String.class);

        return responseEntity.getBody();
        /*if (responseEntity.getStatusCode().is2xxSuccessful()) {

            return responseEntity.getBody();
        } else {
            return "API call failed with status code: " + responseEntity.getStatusCodeValue();
        }*/
    }

    @PostMapping(AppConstants.ENDPOINTS.PHONEPE_CALLBACK)
    public ResponseEntity<String> phonepeCallback(@RequestBody String requestBody) {
        ordersService.sendMessageOnTelegram("hello", AppConstants.TELEGRAM.SMM_ANALYTICS_BOT, AppConstants.TELEGRAM.SAMPLE_TESTING_GRP_CHAT_ID);
        return new ResponseEntity<>("ok", HttpStatus.OK);
    }
}