Skip to content

Commit

Permalink
Implement getTransactions
Browse files Browse the repository at this point in the history
  • Loading branch information
gcatanese committed Dec 4, 2023
1 parent 44218f0 commit bf0df7a
Show file tree
Hide file tree
Showing 5 changed files with 335 additions and 4 deletions.
112 changes: 108 additions & 4 deletions react-app/src/dashboard/Payments.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,58 @@
import * as React from 'react';
import React, { useState, useEffect } from 'react';
import axios from "axios";



import Box from '@mui/material/Box';
import Chip from '@mui/material/Chip';

import Toolbar from '@mui/material/Toolbar';
import Divider from '@mui/material/Divider';
import { useParams } from "react-router-dom";
import { useNavigate } from 'react-router-dom';

import Paper from '@mui/material/Paper';
import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody';
import TableCell from '@mui/material/TableCell';
import TableContainer from '@mui/material/TableContainer';
import TableHead from '@mui/material/TableHead';
import TablePagination from '@mui/material/TablePagination';
import TableRow from '@mui/material/TableRow';

import DashboardHeader from "./DashboardHeader.js";
import DashboardDrawer from "./DashboardDrawer.js";

export default function Products() {

const navigate = useNavigate()

const [page, setPage] = useState(0);
const [rowsPerPage, setRowsPerPage] = useState(10);
const [rows, setRows] = useState([]);

const handleChangePage = (event, newPage) => {
setPage(newPage);
};

const handleChangeRowsPerPage = (event) => {
setRowsPerPage(+event.target.value);
setPage(0);
};

useEffect(() => {
const fetchData = async () => {
try {
const response = await axios.post('/api/dashboard/getTransactions');
setRows(response.data);
} catch (error) {
console.error('API request error:', error);
navigate('/');
}
};

fetchData();
}, []);

return (
<Box sx={{ display: 'flex' }}>

Expand All @@ -30,11 +71,74 @@ export default function Products() {
<Toolbar />

<Divider>
<Chip label="My payments" sx={{ fontSize: "20px" }}/>
<Chip label="My Transactions" variant="elevated" sx={{ fontSize: "20px", backgroundColor: "#0abf53", color: "white" }}/>
</Divider>
<br/><br/>
<div>

<Paper sx={{ width: '100%', overflow: 'hidden' }}>
<TableContainer sx={{ maxHeight: 500, minHeight: 500 }}>
<Table stickyHeader aria-label="sticky table">
<TableHead>
<TableRow>
{columns.map((column) => (
<TableCell
key={column.id}
align={column.align}
style={{ minWidth: column.minWidth }}
sx={{ fontSize: "18px", fontWeight: "bold" }}
>
{column.label}
</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{rows
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map((row) => {
return (
<TableRow hover role="checkbox" tabIndex={-1} key={row.code}>
{columns.map((column) => {
const value = row[column.id];
return (
<TableCell key={column.id} align={column.align}
sx={{ fontSize: "15px" }}>
{column.format && typeof value === 'number'
? column.format(value)
: value}
</TableCell>
);
})}
</TableRow>
);
})}
</TableBody>
</Table>
</TableContainer>
<TablePagination
rowsPerPageOptions={[10, 25]}
component="div"
count={rows.length}
rowsPerPage={rowsPerPage}
page={page}
onPageChange={handleChangePage}
onRowsPerPageChange={handleChangeRowsPerPage}
/>
</Paper>

</div>

</Box>
</Box>
);
}
}

const columns = [
{ id: 'id', label: 'ID', minWidth: 100 },
{ id: 'status', label: 'Status', minWidth: 120 },
{ id: 'type', label: 'Type', minWidth: 120 },
{ id: 'created', label: 'Created', minWidth: 220 },
{ id: 'amount', label: 'Amount', minWidth: 150 },
{ id: 'pspReference', label: 'PSP Reference', minWidth: 200 },
];
15 changes: 15 additions & 0 deletions src/main/java/com/adyen/controller/DashboardController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.adyen.controller;

import com.adyen.model.OnboardingLinkProperties;
import com.adyen.model.TransactionItem;
import com.adyen.model.User;
import com.adyen.model.balanceplatform.AccountHolder;
import com.adyen.model.legalentitymanagement.LegalEntity;
Expand All @@ -15,6 +16,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@RestController
Expand Down Expand Up @@ -92,6 +95,18 @@ ResponseEntity<String> getOnboardingLink(@RequestBody OnboardingLinkProperties o
);
}

@PostMapping("/getTransactions")
ResponseEntity<List<TransactionItem>> getTransactions() {

if (getUserIdOnSession() == null) {
log.warn("User is not logged in");
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}

return new ResponseEntity<>(
getConfigurationAPIService().getTransactions(getUserIdOnSession()), HttpStatus.ACCEPTED);
}

public ConfigurationAPIService getConfigurationAPIService() {
return configurationAPIService;
}
Expand Down
89 changes: 89 additions & 0 deletions src/main/java/com/adyen/model/TransactionItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.adyen.model;

public class TransactionItem {
private String id;
private String status;
private String type;
private String created;
private String amount;
private String pspReference;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getCreated() {
return created;
}

public void setCreated(String created) {
this.created = created;
}

public String getAmount() {
return amount;
}

public void setAmount(String amount) {
this.amount = amount;
}

public String getPspReference() {
return pspReference;
}

public void setPspReference(String pspReference) {
this.pspReference = pspReference;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public TransactionItem id(String id) {
this.id = id;
return this;
}

public TransactionItem status(String status) {
this.status = status;
return this;
}

public TransactionItem created(String created) {
this.created = created;
return this;
}

public TransactionItem amount(String amount) {
this.amount = amount;
return this;
}

public TransactionItem pspReference(String pspReference) {
this.pspReference = pspReference;
return this;
}

public TransactionItem type(String type) {
this.type = type;
return this;
}

}
57 changes: 57 additions & 0 deletions src/main/java/com/adyen/service/ConfigurationAPIService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@
import com.adyen.config.ApplicationProperty;
import com.adyen.enums.Environment;
import com.adyen.model.AccountHolderStatus;
import com.adyen.model.TransactionItem;
import com.adyen.model.balanceplatform.*;
import com.adyen.model.transfers.Transaction;
import com.adyen.model.transfers.TransactionSearchResponse;
import com.adyen.service.balanceplatform.AccountHoldersApi;
import com.adyen.service.balanceplatform.BalanceAccountsApi;
import com.adyen.service.transfers.TransactionsApi;
import com.adyen.util.TransactionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
Expand All @@ -32,6 +40,9 @@ public class ConfigurationAPIService {
@Autowired
private ApplicationProperty applicationProperty;

@Autowired
private TransactionHandler transactionHandler;

public Optional<AccountHolder> getAccountHolder(String accountHolderId) {

Optional<AccountHolder> accountHolder = Optional.empty();
Expand Down Expand Up @@ -119,6 +130,40 @@ public BalanceAccount createBalanceAccount(String accountHolderId) {
return balanceAccount;
}

/**
* Get all transactions for the user (accountHolder)
* @param accountHolderId
* @return
*/
public List<TransactionItem> getTransactions(String accountHolderId) {

List<TransactionItem> transactionItems = null;

try {

// in the last X days
OffsetDateTime createdSince = OffsetDateTime.now().minus(365, ChronoUnit.DAYS);
// until today
OffsetDateTime createdUntil = OffsetDateTime.now();
// max number of transactions to fetch
Integer limit = 100;

TransactionSearchResponse transactionSearchResponse = getTransactionsApi().getAllTransactions(
null, null, accountHolderId, null,
null, createdSince, createdUntil, limit, null);

log.info(transactionSearchResponse.getData().toString());

transactionItems = getTransactionHandler().getTransactionItems(transactionSearchResponse.getData());
} catch (Exception e) {
log.error(e.toString(), e);
throw new RuntimeException("Cannot create BalanceAccount: " + e.getMessage());
}

return transactionItems;

}

// AccountHoldersApi handler
private AccountHoldersApi getAccountHoldersApi() {
return new AccountHoldersApi(getApiClient());
Expand All @@ -129,6 +174,11 @@ private BalanceAccountsApi getBalanceAccountsApi() {
return new BalanceAccountsApi(getApiClient());
}

private TransactionsApi getTransactionsApi() {
return new TransactionsApi(getApiClient());
}


// create client to access the Configuration API
private Client getApiClient() {
if (apiClient == null) {
Expand All @@ -149,4 +199,11 @@ public void setApplicationProperty(ApplicationProperty applicationProperty) {
this.applicationProperty = applicationProperty;
}

public TransactionHandler getTransactionHandler() {
return transactionHandler;
}

public void setTransactionHandler(TransactionHandler transactionHandler) {
this.transactionHandler = transactionHandler;
}
}
Loading

0 comments on commit bf0df7a

Please sign in to comment.