#include <WiFi.h>
#include <WiFiClientSecure.h>

const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* paytmHost = "securegw.paytm.in";
const int paytmPort = 443;
const String paytmMerchantId = "PDaoyi37070107198984";
const String paytmMerchantKey = "gidcfYk0IG7R3MhD";



// Root CA certificate for the Paytm server
const char* rootCACertificate = \
                                "-----BEGIN CERTIFICATE-----\n"
                                "MIIDhTCCAm2gAwIBAgIJAKIi6mlEtsmcMA0GCSqGSIb3DQEBCwUAMIGGMQswCQYD\n"
                                "VQQGEwJHQjEbMBkGA1UECAwSTW91bnRhaW4gVmlldzEZMBcGA1UEBwwQU2FuIEFy\n"
                                "Z3VtZXMxHjAcBgNVBAoMFU1pY3Jvc29mdCBDb3Jwb3JhdGlvbjELMAkGA1UECwwC\n"
                                "SUQxGTAXBgNVBAMMEHd3dy5tYXJrZXRpbmdkZXZlbG9wZXIuY29tMB4XDTE5MDEw\n"
                                "MTE3MTY0OVoXDTIxMDEwMTE3MTY0OVowgYwxCzAJBgNVBAYTAkdCMRswGQYDVQQI\n"
                                "DBJNb3VudGFpbiBWaWV3MRkwFwYDVQQHDBBTYW4gQXJndW1lczEeMBwGA1UECgMV\n"
                                "TWljcm9zb2Z0IENvcnBvcmF0aW9uMQswCQYDVQQLDAJJRDEZMBcGA1UEAwwQd3d3\n"
                                "Lm1hcmtldGluZ2RldmVsb3Blci5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw\n"
                                "ggEKAoIBAQDdwESc3JXF0X+QCNiDDK1oYECF7CJr9jM0xJXz7ZTzGK1tiQ3cY+pm\n"
                                "9H9EQ0fQRBl7/QxuEmB/tkzi9wxWRV9Mhsk9W8e2G3NZT8a01E8uMOf6rka5YHY2\n"
                                "SEnQhd0ZaFtL1INmDuzWZVrIPdW5w3D1C6PvX76p27sQ8yYTYN6q6ZVUcIMI3/dU\n"
                                "Uz3MGObUtVKKkD7VcaWk1ip7CJfViJ1xqtz6UW0ccJ4UaSsWDYzBxqES42YYd96Y\n"
                                "-----END CERTIFICATE-----\n";





void setup() {
  Serial.begin(115200);
  delay(10);

  // Connect to Wi-Fi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  WiFiClientSecure client;

  // Connect to Paytm server
  if (client.connect(paytmHost, paytmPort)) {
    Serial.println("Connected to Paytm server");

    // Construct the payment request
    String requestData = "merchant_id=" + paytmMerchantId + "&order_id=ORD0001&amount=10&channel_id=WAP&cust_id=CUST001&industry_type_id=Retail";

    // Generate checksum hash
    String checksum = generateChecksum(requestData);

    // Construct the complete request payload
    String payload = requestData + "&checksumhash=" + checksum;

    // Make an HTTP POST request
    client.println("POST /theia/api/v1/initiateTransaction?mid=" + paytmMerchantId + "&orderId=ORD0001 HTTP/1.1");
    client.println("Host: securegw.paytm.in");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Content-Length: " + String(payload.length()));
    client.println();
    client.println(payload);

    // Read and print the server response
    while (client.available()) {
      String line = client.readStringUntil('\r');
      Serial.print(line);
    }

    Serial.println();
    Serial.println("Request complete");
  } else {
    Serial.println("Connection to Paytm server failed");
  }

  // Wait for a few seconds before making the next request
  delay(5000);
}

String generateChecksum(String data) {
  // TODO: Implement your checksum generation logic here
  // You'll need to generate a checksum using your Paytm Merchant Key
  // Refer to Paytm documentation or their provided libraries for the checksum generation process
  return "YourGeneratedChecksum";
}