Skip to content

Commit

Permalink
Basic mail receipt implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
everoddandeven committed Dec 23, 2024
1 parent 14571a3 commit 0b766e0
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 51 deletions.
4 changes: 4 additions & 0 deletions moneroecwid.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ client-secret=Pe087Q6jr0CgwI96R2ZTuUMWfUAHQjMq
wallet-server-uri=http://node2.monerodevs.org:28089
wallet-password=supersecretpassword123
wallet-net-type=testnet
mail-host=
mail-port=
mail-username=
mail-password=
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
Expand Down
20 changes: 4 additions & 16 deletions src/main/java/monero/ecwid/server/GatewayController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import monero.ecwid.server.error.PaymentRequestAlreadyExistsException;
import monero.ecwid.server.repository.PaymentRequestEntity;
import monero.ecwid.server.service.PaymentRequestService;
import monero.ecwid.server.utils.XmrConverter;

import org.springframework.ui.Model;

Expand Down Expand Up @@ -68,36 +67,25 @@ private void processModel(PaymentRequestEntity request, Model model) {
model.addAttribute("txId", txId);
model.addAttribute("amountUsd", request.getAmountUsd() + " USD");
model.addAttribute("paymentStatus", request.getStatus());
model.addAttribute("createdAt", request.getCreatedAt().toString());
model.addAttribute("createdAt", request.getCreatedAt().toString());
model.addAttribute("customerMail", request.getCustomerMail());
}

private String processPaymentRequest(EcwidPaymentData paymentData, Model model) {
String orderId = paymentData.cart.order.id;
String txId = paymentData.cart.order.referenceTransactionId;
Float usdTotal = paymentData.cart.order.usdTotal;
String returnUrl = paymentData.returnUrl;
String token = paymentData.token;
Integer storeId = paymentData.storeId;

logger.info("New payment request: order id: " + orderId + ", tx id: " + txId + ", usd total: " + usdTotal);

PaymentRequestEntity request;
PaymentRequestEntity request = null;

try {
request = paymentRequestService.newPaymentRequest(txId, storeId, token, usdTotal, XmrConverter.convertUsdToPiconero(usdTotal), returnUrl);
request = paymentRequestService.newPaymentRequest(paymentData);
}
catch (Exception e) {
if (e instanceof PaymentRequestAlreadyExistsException) {
Optional<PaymentRequestEntity> req = paymentRequestService.repository.findById(txId);

if (!req.isEmpty()) {

request = req.get();

logger.info("already existing request status: " + request.getStatus() + ", created at: " + request.getCreatedAt().toString());

processModel(request, model);

return "payment.html";
}
}
Expand Down
18 changes: 2 additions & 16 deletions src/main/java/monero/ecwid/server/config/DataSourceConfig.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
package monero.ecwid.server.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;
import java.io.IOException;

@Configuration
public class DataSourceConfig {
private static final Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);


public DataSourceConfig() {
}

private static ServerConfig getServerConfig() {
try {
return ServerConfigFileReader.read();
}
catch (IOException e) {
logger.error("Config file not found");
return new ServerConfig();
}
}

public static String getDataSourceUrl() {
ServerConfig config = getServerConfig();
ServerConfig config = ServerConfig.getServerConfig();

String host = config.dbHost;
String port = config.dbPort.toString();
Expand All @@ -37,7 +23,7 @@ public static String getDataSourceUrl() {

@Bean
public DataSource dataSource() {
ServerConfig config = getServerConfig();
ServerConfig config = ServerConfig.getServerConfig();

String username = config.dbUsername;
String password = config.dbPassword;
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/monero/ecwid/server/config/MailConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package monero.ecwid.server.config;

import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

@Configuration
public class MailConfig {

public MailConfig() {

}

@Bean
public JavaMailSender javaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
ServerConfig serverConfig = ServerConfig.getServerConfig();

mailSender.setHost(serverConfig.mailHost);
mailSender.setPort(serverConfig.mailPort);
mailSender.setUsername(serverConfig.mailUsername);
mailSender.setPassword(serverConfig.mailPassword);

Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", true);
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.starttls.required", true);

return mailSender;
}

}
24 changes: 24 additions & 0 deletions src/main/java/monero/ecwid/server/config/ServerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import monero.daemon.model.MoneroNetworkType;

public class ServerConfig {

private static ServerConfig config = null;

public String dbHost = "localhost";
public Integer dbPort = 3306;
Expand All @@ -15,6 +17,11 @@ public class ServerConfig {
public String walletNetType = "testnet";
public String walletServerUri = "";
public String clientSecret = "";

public String mailHost = "";
public Integer mailPort = 587;
public String mailUsername = "";
public String mailPassword = "";

public MoneroNetworkType getNetType() {
if (walletNetType.toLowerCase().equals("mainnet")) {
Expand All @@ -29,4 +36,21 @@ else if (walletNetType.toLowerCase().equals("stagenet")) {

return MoneroNetworkType.TESTNET;
}

public boolean validMailConfig() {
return !mailHost.isEmpty() && !mailUsername.isEmpty() && !mailPassword.isEmpty() && mailPort.compareTo(Integer.valueOf(0)) > 0 ;
}

public static ServerConfig getServerConfig() {
if (config == null) {
try {
config = ServerConfigFileReader.read();
}
catch (Exception e) {
config = new ServerConfig();
}
}

return config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ public static ServerConfig read(String configFilePath) throws IOException {
String walletPassword = rawConfig.getOrDefault("wallet-password", "");
String walletNetType = rawConfig.getOrDefault("wallet-net-type", "");
String clientSecret = rawConfig.getOrDefault("client-secret", "");
String mailHost = rawConfig.getOrDefault("mail-host", "");
String mailPort = rawConfig.getOrDefault("mail-port", "");
String mailUsername = rawConfig.getOrDefault("mail-username", "");
String mailPassword = rawConfig.getOrDefault("mail-password", "");

Integer port = Integer.valueOf(dbPort);
Long confirmations = Long.valueOf(reqConfirmations);


config.dbHost = dbHost;
config.dbPort = port;
config.dbUsername = dbUsername;
Expand All @@ -57,6 +62,11 @@ public static ServerConfig read(String configFilePath) throws IOException {
config.walletNetType = walletNetType;
config.clientSecret = clientSecret;

config.mailHost = mailHost;
config.mailPort = Integer.valueOf(mailPort);
config.mailUsername = mailUsername;
config.mailPassword = mailPassword;

config.walletServerUri = walletServerUri;

return config;
Expand Down Expand Up @@ -102,6 +112,10 @@ private static void ensureConfigFileExists(String configFilePath) throws IOExcep
writer.write("wallet-password=supersecretpassword123\n");
writer.write("wallet-net-type=testnet\n");
writer.write("client-secret=\n");
writer.write("mail-host=\n");
writer.write("mail-port=\n");
writer.write("mail-username=\n");
writer.write("mail-password=\n");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public class PaymentRequestEntity {
@Column(name = "ecwid_api_updated", nullable = false)
private boolean ecwidApiUpdated = false;

@Column(name = "customer_mail", nullable = false)
private String customerMail = "";

@Transient
private Long blockchainHeight = 0l;
@Transient
Expand Down Expand Up @@ -172,6 +175,14 @@ public BigInteger getAmountToPay() {
return amountXmr.subtract(amountDeposited);
}

public String getCustomerMail() {
return customerMail;
}

public void setCustomerMail(String mail) {
customerMail = mail;
}

public boolean isExpired() {
if (isPartiallyPaid()) {
return false;
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/monero/ecwid/server/service/MailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package monero.ecwid.server.service;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import monero.ecwid.server.config.ServerConfig;
import monero.ecwid.server.repository.PaymentRequestEntity;

@Service
public class MailService {

public static class ReceiptMessage extends SimpleMailMessage {
private PaymentRequestEntity request;

public ReceiptMessage(PaymentRequestEntity request) {
this.request = request;
init();
}

private void init() {
setSubject();
setText();
setTo(request.getCustomerMail());
setFrom(ServerConfig.getServerConfig().mailUsername);
}

private void setSubject() {
setSubject("Monero Payment Receipt " + getTxId());
}

private void setText() {

}

public String getTxId() {
return request.getTxId();
}
}

private final JavaMailSender sender;

public MailService(JavaMailSender sender) {
this.sender = sender;
}

public boolean isEnabled() {
return ServerConfig.getServerConfig().validMailConfig();
}

public void sendReceipt(PaymentRequestEntity request) {
if (!isEnabled()) {
throw new Error("Cannot send receipt, mail is not configured");
}

ReceiptMessage msg = new ReceiptMessage(request);

sender.send(msg);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,31 @@
import java.math.BigInteger;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import monero.ecwid.model.EcwidPaymentData;
import monero.ecwid.server.error.PaymentRequestAlreadyExistsException;
import monero.ecwid.server.repository.MoneroTransactionEntity;
import monero.ecwid.server.repository.MoneroTransactionRepository;
import monero.ecwid.server.repository.PaymentRequestEntity;
import monero.ecwid.server.repository.PaymentRequestRepository;
import monero.ecwid.server.utils.WalletListener;
import monero.ecwid.server.utils.WalletUtils;
import monero.ecwid.server.utils.XmrConverter;
import monero.wallet.MoneroWalletFull;
import monero.wallet.model.MoneroSubaddress;
import monero.wallet.model.MoneroWalletListener;

@Service
public class PaymentRequestService {
private static final Logger logger = LoggerFactory.getLogger(PaymentRequestService.class);

@Autowired
public final PaymentRequestRepository repository;

@Autowired
public final MoneroTransactionRepository transactionRepository;

Expand All @@ -40,7 +47,20 @@ public List<PaymentRequestEntity> getAll() {
return this.repository.findAll();
}

public PaymentRequestEntity newPaymentRequest(String txId, Integer storeId, String storeToken, Float amountUsd, BigInteger amountXmr, String returnUrl) throws PaymentRequestAlreadyExistsException {
public PaymentRequestEntity newPaymentRequest(EcwidPaymentData paymentData) throws Exception, PaymentRequestAlreadyExistsException {
String orderId = paymentData.cart.order.id;

String txId = paymentData.cart.order.referenceTransactionId;
Float amountUsd = paymentData.cart.order.usdTotal;
BigInteger amountXmr = XmrConverter.convertUsdToPiconero(amountUsd);
String returnUrl = paymentData.returnUrl;
String storeToken = paymentData.token;
Integer storeId = paymentData.storeId;
String customerMail = paymentData.cart.order.email;

logger.info("New payment request: order id: " + orderId + ", tx id: " + txId + ", usd total: " + amountUsd);


if (this.repository.existsById(txId)) {
throw new PaymentRequestAlreadyExistsException(txId);
}
Expand All @@ -58,6 +78,7 @@ public PaymentRequestEntity newPaymentRequest(String txId, Integer storeId, Stri
request.setReturnUrl(returnUrl);
request.setStoreId(storeId);
request.setStoreToken(storeToken);
request.setCustomerMail(customerMail);

return this.repository.save(request);
}
Expand Down
Loading

0 comments on commit 0b766e0

Please sign in to comment.