Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.cross.chain.payment.controller.invoice;

import com.cross.chain.payment.exception.InvoiceRequestNotFound;
import com.cross.chain.payment.model.InvoiceConfirmationRequest;
import com.cross.chain.payment.model.InvoiceRequest;
import com.cross.chain.payment.model.InvoiceResponse;
import com.cross.chain.payment.model.Transaction;
import com.cross.chain.payment.model.enums.InvoiceType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;


@Tag(name = "invoices", description = "Invoice Operations")
public interface InvoiceApiController {

String INVOICE = "/invoice";
String INVOICE_HASH = "/invoice/{invoiceHash}";
String INVOICE_HASH_TRANSACTION = "/invoice/{invoiceHash}/transaction";
String INVOICE_HASH_CONFIRMATION = "/invoice/{invoiceHash}/confirmation";
String INVOICE_HASH_CANCELLATION = "/invoice/{invoiceHash}/cancellation";
String INVOICE_FIND_BY_USER_ADDRESS = "/invoice/findByUserAddress";

@Operation(summary = "create a invoice", tags={ "invoices" })
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "successful operation", content = @Content(mediaType = APPLICATION_JSON_VALUE, schema = @Schema(implementation = InvoiceRequest.class))),
@ApiResponse(responseCode = "400", description = "Invalid data supplied"),
@ApiResponse(responseCode = "404", description = "User not found") })
ResponseEntity<InvoiceResponse> createInvoice(@Parameter(in = ParameterIn.DEFAULT, description = "Created invoice object", schema=@Schema()) @RequestBody InvoiceRequest body);


@Operation(summary = "get all invoices with filter", description = "", tags={ "invoices" })
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "successful operation", content = @Content(mediaType = APPLICATION_JSON_VALUE, schema = @Schema(implementation = InvoiceRequest.class))),
@ApiResponse(responseCode = "400", description = "Invalid data supplied")})
ResponseEntity<InvoiceRequest> invoiceByHash(@Parameter(in = ParameterIn.PATH, description = "hash that identify the invoice" ,schema=@Schema())@PathVariable(value = "invoiceHash") String invoiceHash) throws InvoiceRequestNotFound;

@Operation(summary = "get all transaction with filter", description = "", tags={ "invoices" })
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "successful operation", content = @Content(mediaType = APPLICATION_JSON_VALUE, schema = @Schema(implementation = InvoiceRequest.class))),
@ApiResponse(responseCode = "400", description = "Invalid data supplied")})
ResponseEntity<List<Transaction>> transactionByInvoiceHash(@Parameter(in = ParameterIn.PATH, description = "hash that identify the invoice" ,schema=@Schema())@PathVariable(value = "invoiceHash") String invoiceHash) throws InvoiceRequestNotFound;

@Operation(summary = "Update invoice given a hash", description = "", tags={ "invoices" })
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "successful operation", content = @Content(mediaType = APPLICATION_JSON_VALUE, schema = @Schema(implementation = InvoiceRequest.class))),
@ApiResponse(responseCode = "400", description = "Invalid data supplied"),
@ApiResponse(responseCode = "404", description = "Invoice not found") })
ResponseEntity<InvoiceRequest> updateInvoiceByHash(@Parameter(in = ParameterIn.PATH, description = "hash that identify the invoice" ,schema=@Schema()) @PathVariable(value = "invoiceHash") String invoiceHash, @RequestBody InvoiceRequest body) throws InvoiceRequestNotFound;

@Operation(summary = "Invoice confirmation", description = "", tags={ "invoices" })
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "successful operation", content = @Content(mediaType = APPLICATION_JSON_VALUE, schema = @Schema(implementation = InvoiceRequest.class))),
@ApiResponse(responseCode = "400", description = "Invalid data supplied"),
@ApiResponse(responseCode = "404", description = "User not found") })
ResponseEntity invoiceConfirmation(@Parameter(in = ParameterIn.PATH, description = "hash that identify the invoice" ,schema=@Schema())@PathVariable(value = "invoiceHash") String invoiceHash,
@Parameter(in = ParameterIn.DEFAULT, description = "Created invoice object", schema=@Schema()) @RequestBody InvoiceConfirmationRequest body) throws InvoiceRequestNotFound;

@Operation(summary = "Invoice cancellation", description = "", tags={ "invoices" })
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "successful operation", content = @Content(mediaType = APPLICATION_JSON_VALUE, schema = @Schema(implementation = InvoiceRequest.class))),
@ApiResponse(responseCode = "400", description = "Invalid data supplied"),
@ApiResponse(responseCode = "404", description = "User not found") })
ResponseEntity invoiceCancellation(@Parameter(in = ParameterIn.PATH, description = "hash that identify the invoice" ,schema=@Schema())@PathVariable(value = "invoiceHash") String invoiceHash) throws InvoiceRequestNotFound;

@Operation(summary = "get all invoices with filter", description = "(INTERNAL USAGE ONLY)", tags={ "invoices" })
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "successful operation", content = @Content(mediaType = APPLICATION_JSON_VALUE, schema = @Schema(implementation = InvoiceRequest.class))),
@ApiResponse(responseCode = "400", description = "Invalid data supplied"),
@ApiResponse(responseCode = "404", description = "User not found") })
ResponseEntity<InvoiceRequest> getAllInvoice(@Parameter(in = ParameterIn.QUERY, description = "accountId that belongs the invoices" ,schema=@Schema())@RequestParam(value = "accountId", required = false) String accountId, @Parameter(in = ParameterIn.QUERY, description = "Invoice type that need to be considered for filter" ,schema=@Schema()) InvoiceType invoiceType);


@Operation(summary = "get a invoice by User Address", tags={ "invoices" })
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "successful operation", content = @Content(mediaType = "application/json", schema = @Schema(implementation = InvoiceRequest.class))),
@ApiResponse(responseCode = "400", description = "Invalid data supplied"),
@ApiResponse(responseCode = "404", description = "User not found") })
ResponseEntity<List<InvoiceRequest>> getAllInvoiceGivenUserAddressAndFilters(@Parameter(in = ParameterIn.QUERY, description = "User address that owns the invoices" ,required=true,schema=@Schema()) String accountId, @Parameter(in = ParameterIn.QUERY, description = "Invoice type that need to be considered for filter" ,schema=@Schema()) InvoiceType invoiceType);

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.cross.chain.payment.controller.invoice;

import com.cross.chain.payment.exception.InvoiceRequestNotFound;
import com.cross.chain.payment.model.InvoiceConfirmationRequest;
import com.cross.chain.payment.model.InvoiceRequest;
import com.cross.chain.payment.model.InvoiceResponse;
import com.cross.chain.payment.model.Transaction;
import com.cross.chain.payment.model.enums.InvoiceType;
import com.cross.chain.payment.service.invoice.InvoiceProcessorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@Slf4j
@RestController
@RequestMapping(value = "/api/v1")
public class InvoiceApiControllerImpl implements InvoiceApiController {

@Autowired
private InvoiceProcessorService invoiceProcessorService;
@Override
@PostMapping(value = INVOICE, produces = { APPLICATION_JSON_VALUE }, consumes = { APPLICATION_JSON_VALUE })
public ResponseEntity<InvoiceResponse> createInvoice(@Valid @RequestBody InvoiceRequest body) {
return ResponseEntity.ok(invoiceProcessorService.processInvoiceRequest(body));
}

@Override
@GetMapping(value = INVOICE_HASH, produces = { APPLICATION_JSON_VALUE })
public ResponseEntity<InvoiceRequest> invoiceByHash(@PathVariable("invoiceHash") String invoiceHash) throws InvoiceRequestNotFound {
return ResponseEntity.ok(invoiceProcessorService.retrieveInvoiceRequest(invoiceHash));
}

@Override
@GetMapping(value = INVOICE_HASH_TRANSACTION, produces = { APPLICATION_JSON_VALUE })
public ResponseEntity<List<Transaction>> transactionByInvoiceHash(String invoiceHash) throws InvoiceRequestNotFound {
return ResponseEntity.ok(invoiceProcessorService.retrieveInvoiceTransactions(invoiceHash));
}

@Override
@PatchMapping(value = INVOICE_HASH, produces = { APPLICATION_JSON_VALUE })
public ResponseEntity<InvoiceRequest> updateInvoiceByHash(@PathVariable("invoiceHash") String invoiceHash, @Valid @RequestBody InvoiceRequest body) throws InvoiceRequestNotFound {
return ResponseEntity.ok(invoiceProcessorService.updateInvoiceRequest(invoiceHash, body));
}

@Override
@PostMapping(value = INVOICE_HASH_CONFIRMATION, consumes = { APPLICATION_JSON_VALUE })
public ResponseEntity invoiceConfirmation(@PathVariable("invoiceHash") String invoiceHash, @Valid @RequestBody InvoiceConfirmationRequest body) throws InvoiceRequestNotFound {
invoiceProcessorService.invoiceConfirmation(invoiceHash, body);
return ResponseEntity.noContent().build();
}

@Override
@PostMapping(value = INVOICE_HASH_CANCELLATION)
public ResponseEntity invoiceCancellation(@PathVariable("invoiceHash") String invoiceHash) throws InvoiceRequestNotFound {
invoiceProcessorService.invoiceCancellation(invoiceHash);
return ResponseEntity.noContent().build();
}

@Override
@GetMapping(value = INVOICE, produces = { APPLICATION_JSON_VALUE })
public ResponseEntity<InvoiceRequest> getAllInvoice(@Valid @RequestParam(value = "accountId", required = false) String accountId, @Valid @RequestParam(value = "invoiceType", required = false) InvoiceType invoiceType) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
@Override
@GetMapping(value = INVOICE_FIND_BY_USER_ADDRESS, produces = { APPLICATION_JSON_VALUE })
public ResponseEntity<List<InvoiceRequest>> getAllInvoiceGivenUserAddressAndFilters(@NotNull @Valid @RequestParam(value = "address") String address, @Valid @RequestParam(value = "invoiceType", required = false) InvoiceType invoiceType) {
return ResponseEntity.ok(invoiceProcessorService.retrieveByUserAddress(address));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.cross.chain.payment.exception;

public class InvoiceLinkCreationException extends RuntimeException {
public InvoiceLinkCreationException(Exception exception){
super(exception);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.cross.chain.payment.exception;

public class InvoiceProcessorException extends RuntimeException{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.cross.chain.payment.exception;

public class InvoiceRequestNotFound extends Exception {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.cross.chain.payment.mapper;

import com.cross.chain.payment.model.InvoiceConfirmationDTO;
import com.cross.chain.payment.model.InvoiceConfirmationRequest;
import com.cross.chain.payment.model.InvoiceRequest;
import com.cross.chain.payment.model.InvoiceRequestDTO;
import org.bson.BsonBinarySubType;
import org.bson.types.Binary;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(componentModel = "spring")
public interface InvoiceRequestMapper {

InvoiceRequestDTO map(InvoiceRequest invoiceRequest);
@Mapping(source = "createdAt", target = "createdAt", dateFormat = "dd/MM/yyyy HH:mm:ss")
InvoiceRequest map(InvoiceRequestDTO invoiceRequest);

InvoiceConfirmationDTO map(InvoiceConfirmationRequest invoiceConfirmationDTO);

default Binary map(byte[] value){
if(value == null){
return null;
}
return new Binary(BsonBinarySubType.BINARY, value);
}
default byte[] map(Binary value){
if(value == null){
return null;
}
return value.getData();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cross.chain.payment.mapper;

import com.cross.chain.payment.model.InvoiceConfirmationDTO;
import com.cross.chain.payment.model.PaymentConfirmationDTO;
import com.cross.chain.payment.model.Transaction;
import com.cross.chain.payment.model.TransactionDTO;
Expand Down Expand Up @@ -27,6 +28,21 @@ public interface TransactionMapper {
})
TransactionDTO map(PaymentConfirmationDTO paymentConfirmationDTO);

@Mappings({
@Mapping(source = "invoiceConfirmationDTO.transactionDetails.transactionHash", target = "transactionHash"),
@Mapping(source = "invoiceConfirmationDTO.transactionDetails.blockHash", target = "blockHash"),
@Mapping(source = "invoiceConfirmationDTO.transactionDetails.blockNumber", target = "blockNumber"),
@Mapping(source = "invoiceConfirmationDTO.transactionDetails.gasUsed", target = "gasUsed"),
@Mapping(source = "invoiceConfirmationDTO.transactionDetails.toAddress", target = "toAddress"),
@Mapping(source = "invoiceConfirmationDTO.transactionDetails.fromAddress", target = "fromAddress"),
@Mapping(source = "invoiceConfirmationDTO.transactionDetails.confirmations", target = "confirmations"),
@Mapping(source = "invoiceConfirmationDTO.customerInfoDTO.name", target = "customerInfo.name"),
@Mapping(source = "invoiceConfirmationDTO.customerInfoDTO.email", target = "customerInfo.email"),
@Mapping(source = "invoiceConfirmationDTO.customerInfoDTO.phoneNumber", target = "customerInfo.phoneNumber"),
@Mapping(source = "invoiceConfirmationDTO.customerInfoDTO.shippingAddress", target = "customerInfo.shippingAddress")
})
TransactionDTO map(InvoiceConfirmationDTO invoiceConfirmationDTO);

@Mappings({
@Mapping(source = "transaction.customerInfo.name", target = "customerInfo.name"),
@Mapping(source = "transaction.customerInfo.email", target = "customerInfo.email"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.cross.chain.payment.model;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.List;

/**
* PaymentConfirmation
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class InvoiceConfirmationDTO {
private TransactionDetailsDTO transactionDetails;
private BigDecimal amount;
private List<ProductPaymentRequest> products;
private CustomerInfoDTO customerInfoDTO;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.cross.chain.payment.model;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

/**
* PaymentConfirmation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clearly an copy and past, fix here :)

*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
@Validated
public class InvoiceConfirmationRequest {

@Valid
@JsonProperty("transactionDetails")
private TransactionDetails transactionDetails;

@Valid
@JsonProperty("amountPaid")
private BigDecimal amount;

@Valid
@JsonProperty("products")
private List<ProductPaymentRequest> products;

@Valid
@JsonProperty("customerInfo")
private CustomerInfoDTO customerInfoDTO;

}
Loading