Skip to content

Commit

Permalink
Merge pull request #475 from WISVCH/cleanup-payment-method
Browse files Browse the repository at this point in the history
Cleanup Payment Method Code
  • Loading branch information
praseodym authored May 4, 2024
2 parents 2d78079 + 6ccb96c commit 2267248
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 16 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ dependencies {

implementation 'javax.el:javax.el-api:2.2.5'
implementation "be.woutschoovaerts:mollie:3.6.1"
implementation"net.objecthunter:exp4j:0.4.8"


implementation 'org.thymeleaf:thymeleaf-spring5'
Expand Down
26 changes: 11 additions & 15 deletions src/main/java/ch/wisv/events/core/model/order/PaymentMethod.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ch.wisv.events.core.model.order;

import lombok.Getter;
import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;

import java.util.function.Function;

/**
* Payment method enum.
Expand All @@ -12,37 +12,37 @@ public enum PaymentMethod {
/**
* User paid his order with cash.
*/
CASH("cash", new ExpressionBuilder("x")),
CASH("cash", cost -> cost),

/**
* User paid his order by card.
*/
CARD("card", new ExpressionBuilder("x")),
CARD("card", cost -> cost),

/**
* User paid his order via Mollie iDeal.
*/
IDEAL("ideal", new ExpressionBuilder("x + 0.35")),
IDEAL("ideal", cost -> cost + 0.35),

/**
* User paid his order via Mollie SOFORT.
*/
SOFORT("sofort", new ExpressionBuilder("1.01089 * x + 0.3025")),
SOFORT("sofort", cost -> 1.01089 * cost + 0.3025),

/**
* User paid his order via another method.
*/
OTHER("other", new ExpressionBuilder("x"));
OTHER("other", cost -> cost);

/** gets the name. */
@Getter
private final String name;

/** gets the expressionbuilder. */
/** gets the transaction cost function. */
@Getter
private final ExpressionBuilder transactionCost;
private final Function<Double, Double> transactionCost;

PaymentMethod(String name, ExpressionBuilder transactionCost) {
PaymentMethod(String name, Function<Double, Double> transactionCost) {
this.name = name;
this.transactionCost = transactionCost;
}
Expand All @@ -55,11 +55,7 @@ public enum PaymentMethod {
* @return double
*/
public double calculateCostIncludingTransaction(double cost) {
Expression e = this.getTransactionCost().variables("x")
.build()
.setVariable("x", cost);

return Math.round(e.evaluate() * 100.d) / 100.d;
return Math.round(transactionCost.apply(cost) * 100.d) / 100.d;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ch.wisv.events.core.model.order;

import ch.wisv.events.core.model.customer.Customer;
import ch.wisv.events.core.model.product.Product;
import ch.wisv.events.core.util.VatRate;
import com.google.common.collect.ImmutableList;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;

@RunWith(MockitoJUnitRunner.class)
public class PaymentMethodTest {

@Test
public void testCalculateCostIncludingTransaction_Cash() {
double cost = 50.0;
PaymentMethod paymentMethod = PaymentMethod.CASH;

double expected = 50.0;
double actual = paymentMethod.calculateCostIncludingTransaction(cost);

assertEquals(expected, actual, 0.001);
}

@Test
public void testCalculateCostIncludingTransaction_Card() {
double cost = 50.0;
PaymentMethod paymentMethod = PaymentMethod.CARD;

double expected = 50.0;
double actual = paymentMethod.calculateCostIncludingTransaction(cost);

assertEquals(expected, actual, 0.001);
}
@Test
public void testCalculateCostIncludingTransaction_IDEAL() {
double cost = 50.0;
PaymentMethod paymentMethod = PaymentMethod.IDEAL;

double expected = 50.35;
double actual = paymentMethod.calculateCostIncludingTransaction(cost);

assertEquals(expected, actual, 0.001);
}

@Test
public void testCalculateCostIncludingTransaction_SOFORT() {
double cost = 50.0;
PaymentMethod paymentMethod = PaymentMethod.SOFORT;

double expected = 50.85; // Rounded 50.847
double actual = paymentMethod.calculateCostIncludingTransaction(cost);

assertEquals(expected, actual, 0.001);
}

@Test
public void testCalculateCostIncludingTransaction_OTHER() {
double cost = 50.0;
PaymentMethod paymentMethod = PaymentMethod.OTHER;

double expected = 50.0; // Since OTHER method has no transaction cost, the expected value should be same as the cost
double actual = paymentMethod.calculateCostIncludingTransaction(cost);

assertEquals(expected, actual, 0.001);
}
}

0 comments on commit 2267248

Please sign in to comment.