Skip to content

Commit

Permalink
Merge pull request #9 from nazarovctrl/exp
Browse files Browse the repository at this point in the history
Merchant API fixed
  • Loading branch information
nazarovctrl authored May 9, 2024
2 parents db3e954 + ab0c6bb commit f08e88b
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.paymejava;

import com.example.paymejava.entity.OrderEntity;
import com.example.paymejava.enums.OrderStatus;
import com.example.paymejava.repository.OrderRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ private ResponseEntity<?> handler(UnableCompleteException e) {
return ResponseEntity.ok(new Result(new Error(-31008, e.getMessage(), "transaction")));
}

@ExceptionHandler({UnableCancelTransaction.class})
private ResponseEntity<?> handler(UnableCancelTransaction e) {
return ResponseEntity.ok(new Result(new Error(-31007, e.getMessage(), "transaction")));
}

@ExceptionHandler({TransactionNotFoundException.class})
private ResponseEntity<?> handler(TransactionNotFoundException e) {
return ResponseEntity.ok(new Result(new Error(-31003, e.getMessage(), "transaction")));
Expand All @@ -34,4 +39,9 @@ private ResponseEntity<?> handler(TransactionNotFoundException e) {
private ResponseEntity<?> handler(TransactionInWaiting e) {
return ResponseEntity.ok(new Result(new Error(-31099, e.getMessage(), "transaction")));
}

@ExceptionHandler({OrderAlreadyPayed.class})
private ResponseEntity<?> handler(OrderAlreadyPayed e) {
return ResponseEntity.ok(new Result(new Error(-31099, e.getMessage(), "order payed/canceled")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api")
@RequestMapping("/api/payme/merchant")
@RequiredArgsConstructor
public class MerchantController {
private final IMerchantService merchantService;
private final AuthUtil authUtil;
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private final Gson gson = new GsonBuilder().serializeNulls().create();

@PostMapping
public ResponseEntity<?> handle(HttpServletRequest request) {
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/example/paymejava/entity/OrderEntity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.paymejava.entity;

import com.example.paymejava.enums.OrderStatus;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -10,7 +11,6 @@
@Table(name = "custom_order")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class OrderEntity {
@Id
Expand All @@ -22,4 +22,14 @@ public class OrderEntity {

@Column
private Boolean delivered;

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private OrderStatus status = OrderStatus.UNPAID;

public OrderEntity(Long id, Long amount, Boolean delivered) {
this.id = id;
this.amount = amount;
this.delivered = delivered;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/example/paymejava/enums/OrderStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.paymejava.enums;

public enum OrderStatus {
UNPAID, CANCELED, PAID, REFUNDED
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.paymejava.exp;

public class OrderAlreadyPayed extends RuntimeException {
public OrderAlreadyPayed(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.paymejava.repository;

import com.example.paymejava.entity.OrderTransactionEntity;
import com.example.paymejava.enums.TransactionState;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
Expand All @@ -10,7 +9,7 @@
public interface TransactionRepository extends JpaRepository<OrderTransactionEntity, Long> {
Optional<OrderTransactionEntity> findByPaycomId(String id);

Optional<List<OrderTransactionEntity>> findByPaycomTimeBetweenAndState(Long from, Long to, TransactionState state);
Optional<List<OrderTransactionEntity>> findByPaycomTimeBetween(long from, long to);

Optional<OrderTransactionEntity> findByOrder_Id(long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.example.paymejava.dto.result.*;
import com.example.paymejava.entity.OrderEntity;
import com.example.paymejava.entity.OrderTransactionEntity;
import com.example.paymejava.enums.OrderCancelReason;
import com.example.paymejava.enums.OrderStatus;
import com.example.paymejava.enums.TransactionState;
import com.example.paymejava.exp.*;
import com.example.paymejava.repository.OrderRepository;
Expand Down Expand Up @@ -40,16 +42,27 @@ private OrderEntity getOrder(Long orderId) {
public CheckPerformTransactionResult checkPerformTransaction(CheckPerformTransaction checkPerformTransaction) {
OrderEntity order = getOrder(checkPerformTransaction.getAccount().getOrderId());

if (!order.getStatus().equals(OrderStatus.UNPAID)) {
throw new OrderAlreadyPayed("Invoice already paid/cancelled");
}
if (!checkPerformTransaction.getAmount().equals(order.getAmount())) {
throw new WrongAmountException("Wrong amount");
}

Optional<OrderTransactionEntity> optionalTransaction = transactionRepository
.findByOrder_Id(checkPerformTransaction.getAccount().getOrderId());
if (optionalTransaction.isPresent()) {
throw new TransactionInWaiting("transaction");
}

return CheckPerformTransactionResult.builder()
.allow(true)
.detail(new DetailResult(0, List.of(
Item.builder()
.code("10107002001000000")
.title("Услуги по перевозке грузов автомобильным транспортом")
.price(order.getAmount())
.count(1)
.packageCode("1209885")
.vatPercent(0)
.build())))
Expand All @@ -62,10 +75,16 @@ public CreateTransactionResult createTransaction(CreateTransaction createTransac

if (optional.isPresent()) {
OrderTransactionEntity transaction = optional.get();
if (transaction.getState().equals(TransactionState.STATE_IN_PROGRESS) && System.currentTimeMillis() - transaction.getPaycomTime() < time_expired) {
return merchantUtil.getCreateTransactionResult(transaction);
if (!transaction.getState().equals(TransactionState.STATE_IN_PROGRESS)) {
throw new UnableCompleteException("Unable to complete operation");
}
throw new UnableCompleteException("Unable to complete operation");
if (System.currentTimeMillis() - transaction.getPaycomTime() > time_expired) {
transaction.setReason(OrderCancelReason.TRANSACTION_TIMEOUT);
transaction.setState(TransactionState.STATE_CANCELED);
transactionRepository.save(transaction);
throw new UnableCompleteException("Unable to complete operation");
}
return merchantUtil.getCreateTransactionResult(transaction);
}

if (!checkPerformTransaction(new CheckPerformTransaction(createTransaction.getAmount(),
Expand Down Expand Up @@ -104,6 +123,11 @@ public PerformTransactionResult performTransaction(PerformTransaction performTra
if (System.currentTimeMillis() - transaction.getPaycomTime() < time_expired) {
transaction.setState(TransactionState.STATE_DONE);
transaction.setPerformTime(new Date().getTime());

OrderEntity order = transaction.getOrder();
order.setStatus(OrderStatus.PAID);
orderRepository.save(order);

transactionRepository.save(transaction);
return new PerformTransactionResult(transaction.getId().toString(), transaction.getPerformTime(), transaction.getState().getCode());
}
Expand All @@ -125,25 +149,36 @@ public CancelTransactionResult cancelTransaction(CancelTransaction cancelTransac
}

OrderTransactionEntity transaction = optional.get();
if (transaction.getState().equals(TransactionState.STATE_DONE)) {
if (transaction.getState().equals(TransactionState.STATE_CANCELED) ||
transaction.getState().equals(TransactionState.STATE_POST_CANCELED)) {
return new CancelTransactionResult(transaction.getId().toString(), transaction.getCancelTime(), transaction.getState().getCode());
}

OrderEntity order = transaction.getOrder();
if (transaction.getState().equals(TransactionState.STATE_IN_PROGRESS)) {
transaction.setState(TransactionState.STATE_CANCELED);
order.setStatus(OrderStatus.CANCELED);
orderRepository.save(order);
} else if (transaction.getState().equals(TransactionState.STATE_DONE)) {
if (transaction.getOrder().getDelivered()) {
throw new UnableCancelTransaction("Unable cancel transaction");
}
order.setStatus(OrderStatus.REFUNDED);
orderRepository.save(order);
transaction.setState(TransactionState.STATE_POST_CANCELED);
} else {
transaction.setState(TransactionState.STATE_CANCELED);
}

transaction.setCancelTime(new Date().getTime());
transaction.setReason(cancelTransaction.getReason());
transactionRepository.save(transaction);

return new CancelTransactionResult(transaction.getId().toString(), transaction.getCancelTime(), transaction.getState().getCode());
}

@Override
public Transactions getStatement(GetStatement getStatement) {
Optional<List<OrderTransactionEntity>> optional = transactionRepository
.findByPaycomTimeBetweenAndState(getStatement.getFrom(), getStatement.getTo(), TransactionState.STATE_DONE);
.findByPaycomTimeBetween(getStatement.getFrom(), getStatement.getTo());

if (optional.isEmpty()) {
return new Transactions(new ArrayList<>());
Expand Down
11 changes: 2 additions & 9 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@ server.error.include-message=always

paycom.checkout.url=https://checkout.paycom.uz/
paycom.user.name=Paycom
paycom.user.password=yENfUeo8bDpkCi%vfwHBu3K6qVOk76r4YjSI
paycom.merchant.id=6497d19943c6c1de2db3ca36
paycom.user.password=
paycom.merchant.id=

spring.h2.console.path=/db

logging.level.root=debug
logging.level.org.springframework=debug
logging.level.org.springframework.data=debug
logging.level.org.springframework.web=debug
logging.level.com.example=debug
logging.pattern.console="%d{yyyy-MM-dd HH:mm:ss} - %msg%n"
logging.pattern.file="%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
logging.file.path=/logs/paycom/merchant.log


0 comments on commit f08e88b

Please sign in to comment.