@Override public PaymentJson apply(final Payment payment) { // Cache audit logs per account if (accountsAuditLogs.get().get(payment.getAccountId()) == null) { accountsAuditLogs.get().put(payment.getAccountId(), auditUserApi.getAccountAuditLogs(payment.getAccountId(), auditMode.getLevel(), tenantContext)); } final AccountAuditLogs accountAuditLogs = accountsAuditLogs.get().get(payment.getAccountId()); return new PaymentJson(payment, accountAuditLogs); } },
@Override public PaymentJson apply(final Payment payment) { // Cache audit logs per account if (accountsAuditLogs.get().get(payment.getAccountId()) == null) { accountsAuditLogs.get().put(payment.getAccountId(), auditUserApi.getAccountAuditLogs(payment.getAccountId(), auditMode.getLevel(), tenantContext)); } final AccountAuditLogs accountAuditLogs = accountsAuditLogs.get().get(payment.getAccountId()); return new PaymentJson(payment, accountAuditLogs); } },
@TimedResource @GET @Path("/{paymentId:" + UUID_PATTERN + "}/" + TAGS) @Produces(APPLICATION_JSON) @ApiOperation(value = "Retrieve payment payment tags", response = TagJson.class, responseContainer = "List", nickname = "getPaymentTags") @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid payment id supplied"), @ApiResponse(code = 404, message = "Invoice not found")}) public Response getTags(@PathParam(ID_PARAM_NAME) final UUID paymentId, @QueryParam(QUERY_TAGS_INCLUDED_DELETED) @DefaultValue("false") final Boolean includedDeleted, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws TagDefinitionApiException, PaymentApiException { final TenantContext tenantContext = context.createTenantContextNoAccountId(request); final Payment payment = paymentApi.getPayment(paymentId, false, false, ImmutableList.<PluginProperty>of(), tenantContext); return super.getTags(payment.getAccountId(), paymentId, auditMode, includedDeleted, tenantContext); }
@TimedResource @GET @Path("/{transactionId:" + UUID_PATTERN + "}/" + TAGS) @Produces(APPLICATION_JSON) @ApiOperation(value = "Retrieve payment transaction tags", response = TagJson.class, responseContainer = "List", nickname = "getTransactionTags") @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid transaction id supplied"), @ApiResponse(code = 404, message = "Invoice not found")}) public Response getTags(@PathParam(ID_PARAM_NAME) final UUID id, @QueryParam(QUERY_TAGS_INCLUDED_DELETED) @DefaultValue("false") final Boolean includedDeleted, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws TagDefinitionApiException, PaymentApiException { final TenantContext tenantContext = context.createTenantContextNoAccountId(request); final Payment payment = paymentApi.getPaymentByTransactionId(id, false, false, ImmutableList.<PluginProperty>of(), tenantContext); return super.getTags(payment.getAccountId(), id, auditMode, includedDeleted, tenantContext); }
@TimedResource @GET @Path("/{paymentId:" + UUID_PATTERN + "}/" + TAGS) @Produces(APPLICATION_JSON) @ApiOperation(value = "Retrieve payment tags", response = TagJson.class, responseContainer = "List", nickname = "getInvoicePaymentTags") @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid payment id supplied"), @ApiResponse(code = 404, message = "Payment not found")}) public Response getTags(@PathParam(ID_PARAM_NAME) final UUID paymentId, @QueryParam(QUERY_INCLUDED_DELETED) @DefaultValue("false") final Boolean includedDeleted, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws TagDefinitionApiException, PaymentApiException { final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString); final TenantContext tenantContext = context.createTenantContextNoAccountId(request); final Payment payment = paymentApi.getPayment(paymentId, false, false, pluginProperties, tenantContext); return super.getTags(payment.getAccountId(), paymentId, auditMode, includedDeleted, tenantContext); }
@TimedResource(name = "getPaymentByTransactionExternalKey") @GET @Produces(APPLICATION_JSON) @ApiOperation(value = "Retrieve a payment by transaction external key", response = PaymentJson.class) @ApiResponses(value = {@ApiResponse(code = 404, message = "Payment not found")}) public Response getPaymentByTransactionExternalKey(@ApiParam(required = true) @QueryParam(QUERY_TRANSACTION_EXTERNAL_KEY) final String paymentTransactionExternalKey, @QueryParam(QUERY_WITH_PLUGIN_INFO) @DefaultValue("false") final Boolean withPluginInfo, @QueryParam(QUERY_WITH_ATTEMPTS) @DefaultValue("false") final Boolean withAttempts, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws PaymentApiException { final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString); final TenantContext tenantContext = context.createTenantContextNoAccountId(request); final Payment payment = paymentApi.getPaymentByTransactionExternalKey(paymentTransactionExternalKey, withPluginInfo, withAttempts, pluginProperties, tenantContext); final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(payment.getAccountId(), auditMode.getLevel(), tenantContext); final PaymentJson result = new PaymentJson(payment, accountAuditLogs); return Response.status(Response.Status.OK).entity(result).build(); }
@TimedResource(name = "getPaymentByTransactionId") @GET @Path("/{transactionId:" + UUID_PATTERN + "}/") @Produces(APPLICATION_JSON) @ApiOperation(value = "Retrieve a payment by transaction id", response = PaymentJson.class) @ApiResponses(value = {@ApiResponse(code = 404, message = "Payment not found")}) public Response getPaymentByTransactionId(@PathParam("transactionId") final UUID transactionId, @QueryParam(QUERY_WITH_PLUGIN_INFO) @DefaultValue("false") final Boolean withPluginInfo, @QueryParam(QUERY_WITH_ATTEMPTS) @DefaultValue("false") final Boolean withAttempts, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws PaymentApiException { final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString); final TenantContext tenantContext = context.createTenantContextNoAccountId(request); final Payment payment = paymentApi.getPaymentByTransactionId(transactionId, withPluginInfo, withAttempts, pluginProperties, tenantContext); final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(payment.getAccountId(), auditMode.getLevel(), tenantContext); final PaymentJson result = new PaymentJson(payment, accountAuditLogs); return Response.status(Response.Status.OK).entity(result).build(); }
@TimedResource(name = "getPayment") @GET @Path("/{paymentId:" + UUID_PATTERN + "}/") @Produces(APPLICATION_JSON) @ApiOperation(value = "Retrieve a payment by id", response = PaymentJson.class) @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid paymentId supplied"), @ApiResponse(code = 404, message = "Payment not found")}) public Response getPayment(@PathParam("paymentId") final UUID paymentId, @QueryParam(QUERY_WITH_PLUGIN_INFO) @DefaultValue("false") final Boolean withPluginInfo, @QueryParam(QUERY_WITH_ATTEMPTS) @DefaultValue("false") final Boolean withAttempts, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws PaymentApiException { final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString); final TenantContext tenantContext = context.createTenantContextNoAccountId(request); final Payment payment = paymentApi.getPayment(paymentId, withPluginInfo, withAttempts, pluginProperties, tenantContext); final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(payment.getAccountId(), auditMode.getLevel(), tenantContext); final PaymentJson result = new PaymentJson(payment, accountAuditLogs); return Response.status(Response.Status.OK).entity(result).build(); }
@TimedResource(name = "getPayment") @GET @Produces(APPLICATION_JSON) @ApiOperation(value = "Retrieve a payment by external key", response = PaymentJson.class) @ApiResponses(value = {@ApiResponse(code = 404, message = "Payment not found")}) public Response getPaymentByExternalKey(@QueryParam(QUERY_WITH_PLUGIN_INFO) @DefaultValue("false") final Boolean withPluginInfo, @QueryParam(QUERY_WITH_ATTEMPTS) @DefaultValue("false") final Boolean withAttempts, @ApiParam(required=true) @QueryParam(QUERY_EXTERNAL_KEY) final String paymentExternalKey, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws PaymentApiException { verifyNonNullOrEmpty(paymentExternalKey, "Payment externalKey needs to be specified"); final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString); final TenantContext tenantContext = context.createTenantContextNoAccountId(request); final Payment payment = paymentApi.getPaymentByExternalKey(paymentExternalKey, withPluginInfo, withAttempts, pluginProperties, tenantContext); final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(payment.getAccountId(), auditMode.getLevel(), tenantContext); final PaymentJson result = new PaymentJson(payment, accountAuditLogs); return Response.status(Response.Status.OK).entity(result).build(); }
private void verifyPayment(final Payment payment, final String paymentExternalKey, final BigDecimal authAmount, final BigDecimal capturedAmount, final BigDecimal refundedAmount, final int transactionsSize) { Assert.assertEquals(payment.getAccountId(), account.getId()); // We cannot assume the number to be 1 here as the auto_increment implementation // depends on the database. On h2, it is implemented as a sequence, and the payment number // would be 33, 34, 35, etc. depending on the test // See also http://h2database.com/html/grammar.html#create_sequence Assert.assertTrue(payment.getPaymentNumber() > 0); Assert.assertEquals(payment.getExternalKey(), paymentExternalKey); Assert.assertEquals(payment.getAuthAmount().compareTo(authAmount), 0); Assert.assertEquals(payment.getCapturedAmount().compareTo(capturedAmount), 0); Assert.assertEquals(payment.getRefundedAmount().compareTo(refundedAmount), 0); Assert.assertEquals(payment.getCurrency(), CURRENCY); Assert.assertEquals(payment.getTransactions().size(), transactionsSize); }
public static Payment buildPayment(final UUID accountId, final UUID paymentMethodId, final UUID paymentId, final Currency currency, final String paymentExternalKey, @Nullable final OSGIKillbill killbillApi) throws PaymentApiException { final Payment payment = Mockito.mock(Payment.class); Mockito.when(payment.getId()).thenReturn(paymentId); Mockito.when(payment.getExternalKey()).thenReturn(paymentExternalKey); Mockito.when(payment.getAccountId()).thenReturn(accountId); Mockito.when(payment.getPaymentMethodId()).thenReturn(paymentMethodId); Mockito.when(payment.getPaymentNumber()).thenReturn(1); Mockito.when(payment.getCapturedAmount()).thenReturn(new BigDecimal("199999")); Mockito.when(payment.getRefundedAmount()).thenReturn(new BigDecimal("199998")); Mockito.when(payment.getCurrency()).thenReturn(currency); Mockito.when(payment.getTransactions()).thenReturn(new LinkedList<PaymentTransaction>()); Mockito.when(payment.getCreatedDate()).thenReturn(new DateTime(2016, 1, 22, 10, 56, 56, DateTimeZone.UTC)); if (killbillApi != null) { Mockito.when(killbillApi.getPaymentApi().getPayment(Mockito.eq(payment.getId()), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.<Iterable<PluginProperty>>any(), Mockito.<TenantContext>any())).thenReturn(payment); final List<Payment> payments = MoreObjects.firstNonNull(killbillApi.getPaymentApi().getAccountPayments(accountId, false, false, ImmutableList.<PluginProperty>of(), Mockito.mock(TenantContext.class)), new LinkedList<Payment>()); payments.add(payment); Mockito.when(killbillApi.getPaymentApi().getAccountPayments(Mockito.eq(accountId), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.<Iterable<PluginProperty>>any(), Mockito.<TenantContext>any())).thenReturn(payments); } return payment; }
private Response voidPaymentInternal(final PaymentTransactionJson json, @Nullable final UUID paymentId, final List<String> paymentControlPluginNames, final List<String> pluginPropertiesString, final String createdBy, final String reason, final String comment, final UriInfo uriInfo, final HttpServletRequest request) throws PaymentApiException, AccountApiException { final Iterable<PluginProperty> pluginPropertiesFromBody = extractPluginProperties(json != null ? json.getProperties() : null); final Iterable<PluginProperty> pluginPropertiesFromQuery = extractPluginProperties(pluginPropertiesString); final Iterable<PluginProperty> pluginProperties = Iterables.concat(pluginPropertiesFromQuery, pluginPropertiesFromBody); final CallContext callContext = context.createCallContextNoAccountId(createdBy, reason, comment, request); final Payment initialPayment = getPaymentByIdOrKey(paymentId, json.getPaymentExternalKey(), pluginProperties, callContext); final Account account = accountUserApi.getAccountById(initialPayment.getAccountId(), callContext); final String transactionExternalKey = json != null ? json.getTransactionExternalKey() : null; final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames); paymentApi.createVoidWithPaymentControl(account, initialPayment.getId(), json.getEffectiveDate(), transactionExternalKey, pluginProperties, paymentOptions, callContext); return Response.status(Status.NO_CONTENT).build(); }
private void checkPaymentNoAuditForRuntimeException(final UUID accountId, final Payment payment, final ExpectedPaymentCheck expected) { Assert.assertEquals(payment.getAccountId(), accountId); final PaymentTransaction transaction = getPurchaseTransaction(payment); Assert.assertTrue(transaction.getAmount().compareTo(expected.getAmount()) == 0); Assert.assertEquals(transaction.getTransactionStatus(), expected.getStatus()); Assert.assertEquals(payment.getCurrency(), expected.getCurrency()); }
@Override public Payment answer(final InvocationOnMock invocation) throws Throwable { final UUID accountId = ((Account) invocation.getArguments()[0]).getId(); final UUID paymentMethodId = (UUID) invocation.getArguments()[1]; final UUID paymentId = MoreObjects.firstNonNull((UUID) invocation.getArguments()[2], UUID.randomUUID()); final BigDecimal amount = (BigDecimal) invocation.getArguments()[3]; final Currency currency = (Currency) invocation.getArguments()[4]; final String paymentExternalKey = MoreObjects.firstNonNull((String) invocation.getArguments()[6], UUID.randomUUID().toString()); final String paymentTransactionExternalKey = MoreObjects.firstNonNull((String) invocation.getArguments()[7], paymentExternalKey); final Payment payment = buildPayment(accountId, paymentMethodId, paymentId, currency, paymentExternalKey, killbillApi); final PaymentTransaction paymentTransaction = buildPaymentTransaction(payment, paymentTransactionExternalKey, TransactionType.PURCHASE, TransactionStatus.UNKNOWN, amount, currency); final PaymentTransactionInfoPlugin transactionInfoPlugin = paymentPluginApi.purchasePayment(payment.getAccountId(), payment.getId(), paymentTransaction.getId(), payment.getPaymentMethodId(), paymentTransaction.getAmount(), paymentTransaction.getCurrency(), (Iterable<PluginProperty>) invocation.getArguments()[invocation.getArguments().length - 2], (CallContext) invocation.getArguments()[invocation.getArguments().length - 1]); updatePaymentTransaction(paymentTransaction, transactionInfoPlugin); return payment; } });
private List<AuditLog> getAuditLogForPaymentTransaction(final Payment payment, final PaymentTransaction paymentTransaction, final TenantContext context) { return auditUserApi.getAccountAuditLogs(payment.getAccountId(), AuditLevel.FULL, context).getAuditLogsForPaymentTransaction(paymentTransaction.getId()); }
private List<AuditLog> getAuditLogForPayment(final Payment payment, final TenantContext context) { return auditUserApi.getAccountAuditLogs(payment.getAccountId(), AuditLevel.FULL, context).getAuditLogsForPayment(payment.getId()); }
public PaymentJson(final Payment dp, @Nullable final AccountAuditLogs accountAuditLogs) { this(dp.getAccountId(), dp.getId(), dp.getPaymentNumber().toString(), dp.getExternalKey(), dp.getAuthAmount(), dp.getCapturedAmount(), dp.getPurchasedAmount(), dp.getRefundedAmount(), dp.getCreditedAmount(), dp.getCurrency() != null ? dp.getCurrency() : null, dp.getPaymentMethodId(), getTransactions(dp.getTransactions(), dp.getExternalKey(), accountAuditLogs), getAttempts(dp.getPaymentAttempts(), dp.getExternalKey(), accountAuditLogs), toAuditLogJson(accountAuditLogs == null ? null : accountAuditLogs.getAuditLogsForPayment(dp.getId()))); }
private Response chargebackReversalPaymentInternal(final PaymentTransactionJson json, @Nullable final UUID paymentId, final List<String> paymentControlPluginNames, final List<String> pluginPropertiesString, final String createdBy, final String reason, final String comment, final UriInfo uriInfo, final HttpServletRequest request) throws PaymentApiException, AccountApiException { verifyNonNullOrEmpty(json, "PaymentTransactionJson body should be specified"); verifyNonNullOrEmpty(json.getTransactionExternalKey(), "PaymentTransactionJson transactionExternalKey needs to be set"); final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString); final CallContext callContext = context.createCallContextNoAccountId(createdBy, reason, comment, request); final Payment initialPayment = getPaymentByIdOrKey(paymentId, json.getPaymentExternalKey(), pluginProperties, callContext); final Account account = accountUserApi.getAccountById(initialPayment.getAccountId(), callContext); final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames); final Payment payment = paymentApi.createChargebackReversalWithPaymentControl(account, initialPayment.getId(), json.getEffectiveDate(), json.getTransactionExternalKey(), paymentOptions, callContext); return createPaymentResponse(uriInfo, payment, TransactionType.CHARGEBACK, json.getTransactionExternalKey(), request); }
private void checkPayment(final UUID accountId, final Payment payment, final CallContext context, final ExpectedPaymentCheck expected) { Assert.assertEquals(payment.getAccountId(), accountId); Assert.assertEquals(payment.getCurrency(), expected.getCurrency()); if (expected.getInvoiceId() != null) { for (final InvoicePayment invoicePayment : invoicePaymentApi.getInvoicePayments(payment.getId(), context)) { Assert.assertEquals(invoicePayment.getInvoiceId(), expected.getInvoiceId()); } } final PaymentTransaction transaction = getPurchaseTransaction(payment); Assert.assertTrue(transaction.getAmount().compareTo(expected.getAmount()) == 0, "Actual amount " + transaction.getAmount() + ", expected amount " + expected.getAmount()); Assert.assertEquals(transaction.getTransactionStatus(), expected.getStatus()); Assert.assertEquals(transaction.getEffectiveDate().toLocalDate().compareTo(expected.getPaymentDate()), 0, "Actual date " + transaction.getEffectiveDate() + ", expected date " + expected.getPaymentDate()); auditChecker.checkPaymentCreated(payment, context); }
private Response captureAuthorizationInternal(final PaymentTransactionJson json, @Nullable final UUID paymentId, final List<String> paymentControlPluginNames, final List<String> pluginPropertiesString, final String createdBy, final String reason, final String comment, final UriInfo uriInfo, final HttpServletRequest request) throws PaymentApiException, AccountApiException { verifyNonNullOrEmpty(json, "PaymentTransactionJson body should be specified"); verifyNonNullOrEmpty(json.getAmount(), "PaymentTransactionJson amount needs to be set"); final Iterable<PluginProperty> pluginPropertiesFromBody = extractPluginProperties(json.getProperties()); final Iterable<PluginProperty> pluginPropertiesFromQuery = extractPluginProperties(pluginPropertiesString); final Iterable<PluginProperty> pluginProperties = Iterables.concat(pluginPropertiesFromQuery, pluginPropertiesFromBody); final CallContext callContext = context.createCallContextNoAccountId(createdBy, reason, comment, request); final Payment initialPayment = getPaymentByIdOrKey(paymentId, json.getPaymentExternalKey(), pluginProperties, callContext); final Account account = accountUserApi.getAccountById(initialPayment.getAccountId(), callContext); final Currency currency = json.getCurrency() == null ? account.getCurrency() : json.getCurrency(); final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames); final Payment payment = paymentApi.createCaptureWithPaymentControl(account, initialPayment.getId(), json.getAmount(), currency, json.getEffectiveDate(), json.getTransactionExternalKey(), pluginProperties, paymentOptions, callContext); return createPaymentResponse(uriInfo, payment, TransactionType.CAPTURE, json.getTransactionExternalKey(), request); }