use of uk.gov.pay.products.service.transaction.TransactionContext in project pay-products by alphagov.
the class PaymentCreator method paymentCreation.
private NonTransactionalOperation<TransactionContext, PaymentEntity> paymentCreation(Long priceOverride) {
return context -> {
PaymentEntity paymentEntity = context.get(PaymentEntity.class);
ProductEntity productEntity = paymentEntity.getProductEntity();
String returnUrl = format("%s/%s", productsConfiguration.getProductsUiConfirmUrl(), paymentEntity.getExternalId());
Long paymentPrice = priceOverride != null ? priceOverride : productEntity.getPrice();
boolean isMoto = productEntity.getType() == ProductType.AGENT_INITIATED_MOTO;
Source source = productEntity.getType() == ProductType.AGENT_INITIATED_MOTO ? Source.CARD_AGENT_INITIATED_MOTO : Source.CARD_PAYMENT_LINK;
PaymentRequest paymentRequest = new PaymentRequest(paymentPrice, paymentEntity.getReferenceNumber(), productEntity.getName(), returnUrl, productEntity.getLanguage(), isMoto, productEntity.toProductMetadataMap(), source);
try {
PaymentResponse paymentResponse = publicApiRestClient.createPayment(productEntity.getPayApiToken(), paymentRequest);
paymentEntity.setGovukPaymentId(paymentResponse.getPaymentId());
paymentEntity.setNextUrl(getNextUrl(paymentResponse));
paymentEntity.setStatus(PaymentStatus.SUBMITTED);
paymentEntity.setAmount(paymentResponse.getAmount());
logger.info("Payment creation for product external id {} successful", paymentEntity.getProductEntity().getExternalId(), kv(PAYMENT_EXTERNAL_ID, paymentEntity.getGovukPaymentId()), kv("product_external_id", paymentEntity.getProductEntity().getExternalId()));
if (PanDetector.isSuspectedPan(paymentEntity.getReferenceNumber())) {
logger.warn("Suspected PAN entered by user in reference field", kv(PAYMENT_EXTERNAL_ID, paymentEntity.getGovukPaymentId()), kv(GATEWAY_ACCOUNT_ID, paymentEntity.getGatewayAccountId()), kv("number_of_digits", PanDetector.cleanedReference(paymentEntity.getReferenceNumber()).length()));
}
} catch (PublicApiResponseErrorException e) {
logger.error("Payment creation for product external id {} failed {}", paymentEntity.getProductEntity().getExternalId(), e);
paymentEntity.setStatus(PaymentStatus.ERROR);
}
return paymentEntity;
};
}
Aggregations