use of net.petafuel.styx.core.xs2a.entities.PeriodicPayment in project styx by petafuel.
the class PaymentInitiationResource method initiatePeriodicPayment.
/**
* Initiate a reoccurring payment
*
* @param paymentTypeBean contains which payment product is used
* @param periodicPaymentBody contains the request body as parsed json
* @return 201 if successful
* @throws BankRequestFailedException in case the communication between styx and aspsp was not successful
*/
@POST
@Path("/periodic-payments/{paymentProduct}")
@RequiresMandatoryHeader
@AcceptsPreStepAuth
public Response initiatePeriodicPayment(@BeanParam PaymentTypeBean paymentTypeBean, @Valid PeriodicPaymentInitiation periodicPaymentBody) throws BankRequestFailedException {
Optional<SinglePayment> singlePayment = periodicPaymentBody.getPayments().stream().findFirst();
if (!singlePayment.isPresent()) {
throw new StyxException(new ResponseEntity("No valid payment object was found", ResponseConstant.BAD_REQUEST, ResponseCategory.ERROR, ResponseOrigin.CLIENT));
}
SinglePayment payment = singlePayment.get();
PeriodicPayment periodicPayment = new PeriodicPayment();
periodicPayment.setCreditorAccount(payment.getCreditorAccount());
periodicPayment.setCreditorName(payment.getCreditorName());
periodicPayment.setDebtorAccount(payment.getDebtorAccount());
periodicPayment.setEndToEndIdentification(payment.getEndToEndIdentification());
periodicPayment.setInstructedAmount(payment.getInstructedAmount());
periodicPayment.setRemittanceInformationUnstructured(payment.getRemittanceInformationUnstructured());
periodicPayment.setDayOfExecution(String.valueOf(periodicPaymentBody.getDayOfExecution()));
periodicPayment.setExecutionRule(periodicPaymentBody.getExecutionRule());
periodicPayment.setStartDate(Date.from(periodicPaymentBody.getStartDate().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
periodicPayment.setFrequency(periodicPaymentBody.getFrequency().name());
XS2AFactoryInput xs2AFactoryInput = new XS2AFactoryInput();
xs2AFactoryInput.setPayment(periodicPayment);
xs2AFactoryInput.setPsu(getPsu());
xs2AFactoryInput.setPaymentService(PaymentService.PERIODIC_PAYMENTS);
xs2AFactoryInput.setPaymentProduct(paymentTypeBean.getPaymentProduct());
IOProcessor ioProcessor = new IOProcessor(getXS2AStandard());
ioProcessor.modifyInput(xs2AFactoryInput);
PISRequest periodicPaymentInitiation = new PISRequestFactory().create(getXS2AStandard().getRequestClassProvider().paymentInitiation(), xs2AFactoryInput);
periodicPaymentInitiation.getHeaders().putAll(getAdditionalHeaders());
ioProcessor.modifyRequest(periodicPaymentInitiation, xs2AFactoryInput);
periodicPaymentInitiation.setTppRedirectPreferred(getRedirectPreferred());
periodicPaymentInitiation.setXrequestId(ThreadContext.get(RequestUUIDAdapter.REQUEST_UUID));
InitiatedPayment initiatedPayment = getXS2AStandard().getPis().initiatePayment(periodicPaymentInitiation);
PaymentResponse paymentResponse = new PaymentResponse(initiatedPayment);
SCAApproach approach = SCAHandler.decision(initiatedPayment);
if (approach instanceof OAuth2) {
paymentResponse.getLinks().getScaOAuth().setUrl(((OAuth2) approach).getAuthoriseLink());
}
LOG.info("Initiate periodic payment bic={} aspsp_name={} aspsp_id={}", getXS2AStandard().getAspsp().getBic(), getXS2AStandard().getAspsp().getName(), getXS2AStandard().getAspsp().getId());
AspspUrlMapper aspspUrlMapper = new AspspUrlMapper(PaymentService.PERIODIC_PAYMENTS, paymentTypeBean.getPaymentProduct(), paymentResponse.getPaymentId(), null);
paymentResponse.setLinks(aspspUrlMapper.map(paymentResponse.getLinks()));
PersistentPayment.create(periodicPaymentInitiation.getXrequestId(), paymentResponse.getPaymentId(), (String) getContainerRequestContext().getProperty(AbstractTokenFilter.class.getName()), getXS2AStandard().getAspsp().getBic(), paymentResponse.getTransactionStatus(), PaymentService.PERIODIC_PAYMENTS, paymentTypeBean.getPaymentProduct());
return Response.status(ResponseConstant.CREATED).entity(paymentResponse).build();
}
Aggregations