final TenantContext tenantContext = context.createTenantContextNoAccountId(request); final Payment payment = paymentApi.getPayment(paymentId, false, false, ImmutableList.<PluginProperty>of(), tenantContext); final List<InvoicePayment> invoicePayments = invoicePaymentApi.getInvoicePayments(paymentId, tenantContext); invoicePaymentApi.createPurchaseForInvoicePayment(account, invoiceId, payment.getPaymentMethodId(), payment.getId(), amount, currency, null, payment.getExternalKey(), pendingOrSuccessTransaction.getExternalKey(), pluginProperties, paymentOptions, callContext);
@Override public Payment apply(@Nullable final Void input) { try { final InvoicePayment invoicePayment = invoicePaymentApi.createRefundForInvoicePayment(true, iias, account, payment.getId(), amount, currency, null, UUID.randomUUID().toString(), ImmutableList.<PluginProperty>of(), PAYMENT_OPTIONS, callContext); return paymentApi.getPayment(invoicePayment.getPaymentId(), false, false, ImmutableList.<PluginProperty>of(), callContext); } catch (final PaymentApiException e) { fail(e.toString()); return null; } } }, events);
final String transactionExternalKey = json.getTransactionExternalKey() != null ? json.getTransactionExternalKey() : UUIDs.randomUUID().toString(); invoicePaymentApi.createChargebackForInvoicePayment(account, payment.getId(), json.getAmount(),
@Override public Payment apply(@Nullable final Void input) { try { final InvoicePayment invoicePayment = invoicePaymentApi.createPurchaseForInvoicePayment(account, invoice.getId(), account.getPaymentMethodId(), null, amount, currency, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), ImmutableList.<PluginProperty>of(), PAYMENT_OPTIONS, callContext); return paymentApi.getPayment(invoicePayment.getPaymentId(), false, false, ImmutableList.<PluginProperty>of(), callContext); } catch (final PaymentApiException e) { fail(e.toString()); return null; } } }, events);
invoice.addInvoiceItem(invoiceItem); invoicePaymentApi.createPurchaseForInvoicePayment(account, invoice.getId(), account.getPaymentMethodId(), uuidBigDecimalHashMap.put(invoiceItem.getId(), null); invoicePaymentApi.createRefundForInvoicePayment(true, uuidBigDecimalHashMap, account, payment.getId(), null, Currency.USD, null, transactionExternalKey2, refundProperties, INVOICE_PAYMENT, callContext); final Payment payment2 = paymentApi.getPayment(payment.getId(), false, false, ImmutableList.<PluginProperty>of(), callContext);
protected Map<UUID, List<InvoicePayment>> getAccountInvoicePayments(final Iterable<Payment> payments, final TenantContext context) throws OSGIServiceNotAvailable { final InvoicePaymentApi invoicePaymentApi = getInvoicePaymentUserApi(); final Map<UUID, List<InvoicePayment>> allInvoicePaymentsByPaymentId = new HashMap<UUID, List<InvoicePayment>>(); for (final Payment payment : payments) { // Retrieve all invoice payment types (including refunds and chargebacks) for that payment allInvoicePaymentsByPaymentId.put(payment.getId(), invoicePaymentApi.getInvoicePayments(payment.getId(), context)); } return allInvoicePaymentsByPaymentId; }
invoicePaymentApi.createCreditForInvoicePayment(isAdjusted, adjustments, account, invoicePaymentApi.createRefundForInvoicePayment(isAdjusted, adjustments, account,
@Override public List<InvoicePayment> call() throws Exception { return invoicePaymentApi.getInvoicePaymentsByAccount(accountId, tenantContext); } };
final CallContext callContext = context.createCallContextWithAccountId(account.getId(), createdBy, reason, comment, request); invoicePaymentApi.createChargebackReversalForInvoicePayment(account, payment.getId(), json.getEffectiveDate(),
@Override public Payment apply(@Nullable final Void input) { try { final InvoicePayment invoicePayment = invoicePaymentApi.createPurchaseForInvoicePayment(account, invoice.getId(), account.getPaymentMethodId(), null, invoice.getBalance(), invoice.getCurrency(), null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), ImmutableList.<PluginProperty>of(), EXTERNAL_PAYMENT_OPTIONS, callContext); return paymentApi.getPayment(invoicePayment.getPaymentId(), false, false, ImmutableList.<PluginProperty>of(), callContext); } catch (final PaymentApiException e) { fail(e.toString()); return null; } } }, events);
invoicePaymentApi.createPurchaseForInvoicePayment(account, invoice.getId(), account.getPaymentMethodId(), invoicePaymentApi.createRefundForInvoicePayment(false, uuidBigDecimalHashMap, account, payment.getId(), null, Currency.USD, null, transactionExternalKey2, refundProperties, INVOICE_PAYMENT, callContext); final Payment payment2 = paymentApi.getPayment(payment.getId(), false, false, refundProperties, callContext);
private InvoicePayment getInvoicePaymentEntry(final UUID paymentId, final InvoicePaymentType type, final CallContext context) { final List<InvoicePayment> invoicePayments = invoicePaymentApi.getInvoicePayments(paymentId, context); final Collection<InvoicePayment> refundInvoicePayments = Collections2.filter(invoicePayments, new Predicate<InvoicePayment>() { @Override public boolean apply(@Nullable final InvoicePayment invoicePayment) { return invoicePayment.getType() == type && invoicePayment.getPaymentId().equals(paymentId); } }); Assert.assertEquals(refundInvoicePayments.size(), 1); return refundInvoicePayments.iterator().next(); }
@TimedResource @GET @Path("/{accountId:" + UUID_PATTERN + "}/" + INVOICE_PAYMENTS) @Produces(APPLICATION_JSON) @ApiOperation(value = "Retrieve account invoice payments", response = InvoicePaymentJson.class, responseContainer = "List") @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid account id supplied"), @ApiResponse(code = 404, message = "Account not found")}) public Response getInvoicePayments(@PathParam("accountId") final UUID accountId, @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, AccountApiException { final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString); final TenantContext tenantContext = context.createTenantContextWithAccountId(accountId, request); final Account account = accountUserApi.getAccountById(accountId, tenantContext); final List<Payment> payments = paymentApi.getAccountPayments(account.getId(), withPluginInfo, withAttempts, pluginProperties, tenantContext); final List<InvoicePayment> invoicePayments = invoicePaymentApi.getInvoicePaymentsByAccount(accountId, tenantContext); final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(accountId, auditMode.getLevel(), tenantContext); final List<InvoicePaymentJson> result = new ArrayList<InvoicePaymentJson>(payments.size()); for (final Payment payment : payments) { final UUID invoiceId = getInvoiceId(invoicePayments, payment); result.add(new InvoicePaymentJson(payment, invoiceId, accountAuditLogs)); } return Response.status(Status.OK).entity(result).build(); }
protected InvoicePayment createPurchaseForInvoice(final Account account, final UUID invoiceId, final BigDecimal amountToPay, final UUID paymentMethodId, final Boolean externalPayment, final String paymentExternalKey, final String transactionExternalKey, final Iterable<PluginProperty> pluginProperties, final CallContext callContext) throws PaymentApiException { try { return invoicePaymentApi.createPurchaseForInvoicePayment(account, invoiceId, paymentMethodId, null, amountToPay, account.getCurrency(), null, paymentExternalKey, transactionExternalKey, pluginProperties, createInvoicePaymentControlPluginApiPaymentOptions(externalPayment), callContext); } catch (final PaymentApiException e) { if (e.getCode() == ErrorCode.PAYMENT_PLUGIN_EXCEPTION.getCode() /* && e.getMessage().contains("Invalid amount") */) { /* Plugin received bad input */ throw e; } else if (e.getCode() == ErrorCode.PAYMENT_PLUGIN_API_ABORTED.getCode()) { /* Plugin aborted the call (e.g invoice already paid) */ return null; } throw e; } }
@TimedResource @GET @Path("/{paymentId:" + UUID_PATTERN + "}/") @Produces(APPLICATION_JSON) @ApiOperation(value = "Retrieve a payment by id", response = InvoicePaymentJson.class) @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid payment id supplied"), @ApiResponse(code = 404, message = "Payment not found")}) public Response getInvoicePayment(@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 List<InvoicePayment> invoicePayments = invoicePaymentApi.getInvoicePayments(paymentId, tenantContext); final InvoicePayment invoicePayment = Iterables.tryFind(invoicePayments, new Predicate<InvoicePayment>() { @Override public boolean apply(final InvoicePayment input) { return input.getType() == InvoicePaymentType.ATTEMPT; } }).orNull(); final UUID invoiceId = invoicePayment != null ? invoicePayment.getInvoiceId() : null; final InvoicePaymentJson result = new InvoicePaymentJson(payment, invoiceId, accountAuditLogs); return Response.status(Response.Status.OK).entity(result).build(); }
@Test(groups = "slow") public void testFailedRefundWithInvoiceAdjustment() throws Exception { try { invoicePaymentApi.createRefundForInvoicePayment(true, null, account, payment.getId(), payment.getPurchasedAmount(), payment.getCurrency(), null, UUID.randomUUID().toString(), ImmutableList.<PluginProperty>of(), PAYMENT_OPTIONS, callContext); fail("Refund with invoice adjustment should now throw an Exception"); } catch (final PaymentApiException e) { Assert.assertEquals(e.getCause(), null); // Unfortunately we lose the original error code : INVOICE_ITEMS_ADJUSTMENT_MISSING Assert.assertEquals(e.getCode(), ErrorCode.PAYMENT_PLUGIN_EXCEPTION.getCode()); } }
@Test(groups = "slow") public void testCreatePurchaseWithExternalKeyOverLimit() throws InvoiceApiException, EventBusException { final BigDecimal requestedAmount = BigDecimal.TEN; final LocalDate now = clock.getUTCToday(); final Invoice invoice = testHelper.createTestInvoice(account, now, Currency.USD); final String paymentExternalKey = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,."; final String transactionExternalKey = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,."; try { invoicePaymentApi.createPurchaseForInvoicePayment(account, invoice.getId(), account.getPaymentMethodId(), null, requestedAmount, Currency.USD, null, paymentExternalKey, transactionExternalKey, ImmutableList.<PluginProperty>of(), INVOICE_PAYMENT, callContext); Assert.fail(); } catch (final PaymentApiException e) { assertEquals(e.getCode(), ErrorCode.EXTERNAL_KEY_LIMIT_EXCEEDED.getCode()); } }
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); }
invoicePaymentApi.createPurchaseForInvoicePayment(account, invoice.getId(), account.getPaymentMethodId(),
final InvoicePayment invoicePayment = invoicePaymentApi.getInvoicePayments(invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext).get(1).getPayments().get(0).getPaymentId(), callContext).get(0); Payment payment = paymentApi.getPayment(invoicePayment.getPaymentId(), false, false, ImmutableList.<PluginProperty>of(), callContext); payment = createChargeBackAndCheckForCompletion(account, payment, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT, NextEvent.BLOCK);