Skip to content
Snippets Groups Projects
Commit fa4301a2 authored by Raditss's avatar Raditss
Browse files

move api key to db and add sending email

parent 29f90d96
Branches
Tags
No related merge requests found
......@@ -43,6 +43,17 @@
<artifactId>java-dotenv</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
......
package org.toco;
import org.toco.publisher.service_publisher;
import org.toco.service.mail;
public class Main {
public static void main(String[] args) {
System.out.println("server started");
service_publisher.publish();
System.out.println("all services online");
}
}
\ No newline at end of file
package org.toco.model;
import org.toco.core.connector;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class api_model {
public Boolean checkApiKey (String apikey){
String sql = "select * from api where api_key = (?)";
try (Connection connection = connector.connect() ;
PreparedStatement command = connection.prepareStatement(sql)) {
command.setString(1, apikey);
command.execute();
if(command.getResultSet().next()){
return true;
}
else{
return false;
}
} catch (SQLException exception) {
throw new RuntimeException("Error when checking apikey", exception);
}
}
}
package org.toco.service;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class mail {
public static void sendMail(String email, String body) {
// Sender's email address and password
final String username = "tocowbd@gmail.com";
final String password = "drwf pahx wnwr ahnw";
// Recipient's email address
String to = email;
// SMTP server settings
String host = "smtp.gmail.com";
String port = "587";
// Set the properties
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
// Get the Session object
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a default MimeMessage object
Message message = new MimeMessage(session);
// Set From: header field of the header
message.setFrom(new InternetAddress(username));
// Set To: header field of the header
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
// Set Subject: header field
message.setSubject("Thankyou for your purchase with toco!");
// Now set the actual message
message.setContent(
"<h1>Thank You for Your Purchase!</h1>\n" +
" <p>We appreciate your recent purchase of our merchandise. Your support means the world to us!</p>\n" +
" <p>" + body + "</p>\n" +
" <p>If you have any questions or concerns regarding your order, feel free to reply to this email and we'll be in contact ASAP.</p>\n" +
" <p>Thank you again for choosing toco to be your partner in studying language!</p>\n" +
" <p>Best regards,</p>\n" +
" <p>Toco Team</p>\n" +
" <a href=\"http://localhost:8008\" class=\"button\">Visit Our Website</a>"
, "text/html"
);
// Send the message
Transport.send(message);
System.out.println("Email sent successfully!");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
......@@ -28,7 +28,8 @@ public interface toco_service {
public String createTransaction(
@WebParam(name = "user_id") Integer user_id,
@WebParam(name = "amount") Integer amount,
@WebParam(name = "type") String type //add gems / buy item
@WebParam(name = "type") String type, //add gems / buy item
@WebParam(name = "email") String email
);
@WebMethod
......
......@@ -2,6 +2,7 @@ package org.toco.service;
import org.toco.model.*;
import org.toco.entity.*;
import org.toco.service.mail;
import javax.jws.WebService;
......@@ -61,7 +62,7 @@ public class toco_service_impl implements toco_service {
}
@Override
public String createTransaction(Integer user_id, Integer amount, String type) {
public String createTransaction(Integer user_id, Integer amount, String type, String email) {
if (validateApiKey()){
userGems_model userGemsModel = new userGems_model();
Integer userGems = userGemsModel.getUserGems(user_id);
......@@ -70,6 +71,7 @@ public class toco_service_impl implements toco_service {
transaction_model transactionModel = new transaction_model();
transactionModel.insert(new transaction_entity(user_id, amount, type, "accepted"));
addLoggging("User with id " + user_id + " created a transaction with amount " + amount + " and status ACCEPTED");
mail.sendMail(email,"Your transaction with " + amount + " gems has been accepted and will be processed immediately.");
return "success";
} else {
transaction_model transactionModel = new transaction_model();
......@@ -105,13 +107,13 @@ public class toco_service_impl implements toco_service {
}
public Boolean validateApiKey() {
String[] API_KEYS = { "toco_rest", "Postman", "toco_php"};
api_model apiModel = new api_model();
MessageContext mc = wsctx.getMessageContext();
HttpExchange exchange = (HttpExchange) mc.get("com.sun.xml.ws.http.exchange");
String apiKey = exchange.getRequestHeaders().getFirst("X-API-KEY");
if (apiKey == null) {
return false;
} else if (apiKey.equals(API_KEYS[0]) || apiKey.equals(API_KEYS[1]) || apiKey.equals(API_KEYS[2])) {
} else if (apiModel.checkApiKey(apiKey)) {
return true;
} else {
return false;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment