Skip to content

Commit

Permalink
feature: Move PAYOS from IDP to Enterprise
Browse files Browse the repository at this point in the history
  • Loading branch information
Tran Gia Bao authored and GiaBaorr committed Feb 16, 2025
1 parent 9a72163 commit 0414749
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 101 deletions.
1 change: 1 addition & 0 deletions sep490-enterprise/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.kafka:spring-kafka'
implementation 'vn.payos:payos-java:1.0.3'

// Testing
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package green_buildings.idp.configs;
package enterprise.configs;

import jakarta.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package enterprise.dtos;

public record CreditPurchaseDTO(Long code, Integer amount, Integer price) {
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package enterprise.services.impl;

import commons.springfw.impl.utils.SecurityUtils;
import enterprise.dtos.CreditPurchaseDTO;
import enterprise.dtos.PaymentCriteriaDTO;
import enterprise.entities.PaymentEntity;
import enterprise.repositories.PaymentRepository;
import enterprise.services.PaymentService;
import green_buildings.commons.api.dto.SearchCriteriaDTO;
import green_buildings.commons.api.exceptions.TechnicalException;
import jakarta.annotation.PostConstruct;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;
import vn.payos.PayOS;
import vn.payos.type.CheckoutResponseData;
import vn.payos.type.ItemData;
import vn.payos.type.PaymentData;

import java.util.UUID;

Expand All @@ -21,10 +30,66 @@
@RequiredArgsConstructor
public class PaymentServiceImpl implements PaymentService {
private final PaymentRepository payRepo;
private final PayOS payOS;

@Value("${payment.payos.returnPath}")
private String returnPath;

@Value("${payment.payos.cancelPath}")
private String cancelPath;

@Value("${spring.application.homepage}")
private String homepage;

private String returnUrl;
private String cancelUrl;

@PostConstruct
public void setUrls() {
returnUrl = UriComponentsBuilder.fromUriString(homepage)
.path(returnPath)
.build()
.toUriString();
cancelUrl = UriComponentsBuilder.fromUriString(homepage)
.path(cancelPath)
.build()
.toUriString();
}

@Override
public Page<PaymentEntity> search(SearchCriteriaDTO<PaymentCriteriaDTO> searchCriteria, Pageable pageable) {
UUID enterpriseId = SecurityUtils.getCurrentUserEnterpriseId().orElseThrow();
return payRepo.findByEnterpriseId(enterpriseId, pageable);
}

public CheckoutResponseData getCheckoutData(CreditPurchaseDTO creditPurchaseItem) {
try {
log.info("Creating payment link for item: {}", creditPurchaseItem);
PaymentData paymentData = createPaymentData(creditPurchaseItem);
log.debug("Generated PaymentData: {}", paymentData);
return payOS.createPaymentLink(paymentData);
} catch (Exception ex) {
log.error("Error creating payment link for item: {}", creditPurchaseItem);
throw new TechnicalException("Error creating payment link for item", ex);
}
}

private PaymentData createPaymentData(CreditPurchaseDTO creditPurchaseItem) {
ItemData itemData = createItemData(creditPurchaseItem);
return PaymentData.builder()
.orderCode(creditPurchaseItem.code())
.amount(creditPurchaseItem.price())
.item(itemData)
.returnUrl(returnUrl)
.cancelUrl(cancelUrl)
.build();
}

private ItemData createItemData(CreditPurchaseDTO creditPurchaseItem) {
return ItemData.builder()
.name("Credit Purchase")
.quantity(creditPurchaseItem.amount())
.price(creditPurchaseItem.price())
.build();
}
}
10 changes: 9 additions & 1 deletion sep490-enterprise/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,12 @@ logging:
SQL: ${HIBERNATE_SQL_LOG_LEVEL:TRACE}
type:
descriptor:
sql: ${HIBERNATE_SQL_PARAM_LOG_LEVEL:TRACE}
sql: ${HIBERNATE_SQL_PARAM_LOG_LEVEL:TRACE}

payment:
payos:
client_id: ${PAYOS_CLIENT_ID:not_a_real_key_to_prevent_null}
api_key: ${PAYOS_API_KEY:not_a_real_key_to_prevent_null}
checksum_key: ${PAYOS_CHECKSUM_KEY:not_a_real_key_to_prevent_null}
returnPath: /success-payment
cancelPath: /fail-payment
1 change: 0 additions & 1 deletion sep490-idp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ dependencies {
implementation 'org.mapstruct:mapstruct:1.6.2'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-freemarker'
implementation 'vn.payos:payos-java:1.0.3'
}

tasks.register('npmBuild', NpmTask) {
Expand Down

This file was deleted.

This file was deleted.

8 changes: 0 additions & 8 deletions sep490-idp/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,3 @@ logging:
sql: ${HIBERNATE_SQL_PARAM_LOG_LEVEL:TRACE}
server:
port: 8180

payment:
payos:
client_id: ${PAYOS_CLIENT_ID:not_a_real_key_to_prevent_null}
api_key: ${PAYOS_API_KEY:not_a_real_key_to_prevent_null}
checksum_key: ${PAYOS_CHECKSUM_KEY:not_a_real_key_to_prevent_null}
returnPath: /success-payment
cancelPath: /fail-payment

0 comments on commit 0414749

Please sign in to comment.