@Override public UUID doOperation() throws PaymentApiException { PaymentMethod pm = null; PaymentPluginApi pluginApi = null; try { pluginApi = getPaymentPluginApi(paymentPluginServiceName); pm = new DefaultPaymentMethod(account.getId(), paymentPluginServiceName, paymentMethodProps); pluginApi.addPaymentMethod(account.getId(), pm.getId(), paymentMethodProps, setDefault, context.toCallContext(tenantId)); final PaymentMethodModelDao pmModel = new PaymentMethodModelDao(pm.getId(), pm.getCreatedDate(), pm.getUpdatedDate(), pm.getAccountId(), pm.getPluginName(), pm.isActive()); paymentDao.insertPaymentMethod(pmModel, context); if (setDefault) { accountInternalApi.updatePaymentMethod(account.getId(), pm.getId(), context); } } catch (PaymentPluginApiException e) { log.warn("Error adding payment method " + pm.getId() + " for plugin " + paymentPluginServiceName, e); // STEPH all errors should also take a pluginName throw new PaymentApiException(ErrorCode.PAYMENT_ADD_PAYMENT_METHOD, account.getId(), e.getErrorMessage()); } catch (AccountApiException e) { throw new PaymentApiException(e); } return pm.getId(); } });
private void checkPaymentMethodExistsWithStatus(final List<PaymentMethod> methods, UUID expectedPaymentMethodId, boolean expectedActive) { PaymentMethod foundPM = null; for (PaymentMethod cur : methods) { if (cur.getId().equals(expectedPaymentMethodId)) { foundPM = cur; break; } } Assert.assertNotNull(foundPM); Assert.assertEquals(foundPM.isActive().booleanValue(), expectedActive); }
@Test(groups = "fast") public void testGetExternalPaymentProviderPlugin() throws Exception { final UUID accountId = UUID.randomUUID(); final Account account = Mockito.mock(Account.class); Mockito.when(account.getId()).thenReturn(accountId); Mockito.when(account.getExternalKey()).thenReturn(accountId.toString()); Assert.assertEquals(paymentMethodProcessor.getPaymentMethods(account, false, internalCallContext).size(), 0); // The first call should create the payment method final ExternalPaymentProviderPlugin providerPlugin = paymentMethodProcessor.getExternalPaymentProviderPlugin(account, internalCallContext); final List<PaymentMethod> paymentMethods = paymentMethodProcessor.getPaymentMethods(account, false, internalCallContext); Assert.assertEquals(paymentMethods.size(), 1); Assert.assertEquals(paymentMethods.get(0).getPluginName(), ExternalPaymentProviderPlugin.PLUGIN_NAME); Assert.assertEquals(paymentMethods.get(0).getAccountId(), account.getId()); // The succeeding calls should not create any other payment method final UUID externalPaymentMethodId = paymentMethods.get(0).getId(); for (int i = 0; i < 50; i++) { final ExternalPaymentProviderPlugin foundProviderPlugin = paymentMethodProcessor.getExternalPaymentProviderPlugin(account, internalCallContext); Assert.assertNotNull (foundProviderPlugin); final List<PaymentMethod> foundPaymentMethods = paymentMethodProcessor.getPaymentMethods(account, false, internalCallContext); Assert.assertEquals(foundPaymentMethods.size(), 1); Assert.assertEquals(foundPaymentMethods.get(0).getPluginName(), ExternalPaymentProviderPlugin.PLUGIN_NAME); Assert.assertEquals(foundPaymentMethods.get(0).getAccountId(), account.getId()); Assert.assertEquals(foundPaymentMethods.get(0).getId(), externalPaymentMethodId); } } }
public static PaymentMethodJson toPaymentMethodJson(final Account account, final PaymentMethod in, @Nullable final AccountAuditLogs accountAuditLogs) { final boolean isDefault = account.getPaymentMethodId() != null && account.getPaymentMethodId().equals(in.getId()); final PaymentMethodPlugin pluginDetail = in.getPluginDetail(); PaymentMethodPluginDetailJson pluginDetailJson = null; if (pluginDetail != null) { properties); return new PaymentMethodJson(in.getId().toString(), account.getId().toString(), isDefault, in.getPluginName(), pluginDetailJson, toAuditLogJson(accountAuditLogs == null ? null : accountAuditLogs.getAuditLogsForPaymentMethod(in.getId())));
@POST @Path("/{accountId:" + UUID_PATTERN + "}/" + PAYMENT_METHODS) @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) public Response createPaymentMethod(final PaymentMethodJson json, @PathParam("accountId") final String accountId, @QueryParam(QUERY_PAYMENT_METHOD_IS_DEFAULT) @DefaultValue("false") final Boolean isDefault, @QueryParam(QUERY_PAY_ALL_UNPAID_INVOICES) @DefaultValue("false") final Boolean payAllUnpaidInvoices, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final UriInfo uriInfo, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException, PaymentApiException { final CallContext callContext = context.createContext(createdBy, reason, comment, request); final PaymentMethod data = json.toPaymentMethod(accountId); final Account account = accountUserApi.getAccountById(data.getAccountId(), callContext); final boolean hasDefaultPaymentMethod = account.getPaymentMethodId() != null || isDefault; final Collection<Invoice> unpaidInvoices = payAllUnpaidInvoices ? invoiceApi.getUnpaidInvoicesByAccountId(account.getId(), clock.getUTCToday(), callContext) : Collections.<Invoice>emptyList(); if (payAllUnpaidInvoices && unpaidInvoices.size() > 0 && !hasDefaultPaymentMethod) { return Response.status(Status.BAD_REQUEST).build(); } final UUID paymentMethodId = paymentApi.addPaymentMethod(data.getPluginName(), account, isDefault, data.getPluginDetail(), callContext); if (payAllUnpaidInvoices && unpaidInvoices.size() > 0) { for (final Invoice invoice : unpaidInvoices) { paymentApi.createPayment(account, invoice.getId(), invoice.getBalance(), callContext); } } return uriBuilder.buildResponse(PaymentMethodResource.class, "getPaymentMethod", paymentMethodId, uriInfo.getBaseUri().toString()); }
if (paymentMethod.getId().equals(account.getPaymentMethodId()) && paymentMethod.getPluginDetail() != null) { paymentMethodType = PaymentMethodUtils.getPaymentMethodType(paymentMethod.getPluginDetail()); creditCardType = PaymentMethodUtils.getCardType(paymentMethod.getPluginDetail()); billingAddressCountry = PaymentMethodUtils.getCardCountry(paymentMethod.getPluginDetail()); break;
final String paymentMethodString; if (paymentMethod != null) { final PaymentMethodPlugin pluginDetail = paymentMethod.getPluginDetail(); cardCountry = PaymentMethodUtils.getCardCountry(pluginDetail); cardType = PaymentMethodUtils.getCardType(pluginDetail); paymentMethodString, "Electronic", paymentMethod == null ? null : paymentMethod.getPluginName(), payment.getPaymentStatus().toString(), payment.getAmount(),
@Override public PaymentMethodJson apply(final PaymentMethod paymentMethod) { // Cache audit logs per account if (accountsAuditLogs.get().get(paymentMethod.getAccountId()) == null) { accountsAuditLogs.get().put(paymentMethod.getAccountId(), auditUserApi.getAccountAuditLogs(paymentMethod.getAccountId(), auditMode.getLevel(), tenantContext)); } // Lookup the associated account(s) if (accounts.get(paymentMethod.getAccountId()) == null) { final Account account; try { account = accountUserApi.getAccountById(paymentMethod.getAccountId(), tenantContext); accounts.put(paymentMethod.getAccountId(), account); } catch (final AccountApiException e) { log.warn("Unable to retrieve account", e); return null; } } return PaymentMethodJson.toPaymentMethodJson(accounts.get(paymentMethod.getAccountId()), paymentMethod, accountsAuditLogs.get().get(paymentMethod.getAccountId())); } },
public PaymentMethod getExternalPaymentMethod(final Account account, final InternalTenantContext context) throws PaymentApiException { final List<PaymentMethod> paymentMethods = getPaymentMethods(account, false, context); for (final PaymentMethod paymentMethod : paymentMethods) { if (ExternalPaymentProviderPlugin.PLUGIN_NAME.equals(paymentMethod.getPluginName())) { return paymentMethod; } } return null; }
@BeforeMethod(groups = "slow") public void beforeMethod() throws Exception { super.beforeMethod(); Mockito.when(catalogService.getFullCatalog()).thenReturn(new MockCatalog()); final PaymentMethod paymentMethod = Mockito.mock(PaymentMethod.class); final UUID paymentMethodId = UUID.randomUUID(); Mockito.when(paymentMethod.getId()).thenReturn(paymentMethodId); final Account account = new MockAccountBuilder(UUID.randomUUID()) .externalKey(ACCOUNT_KEY) .currency(ACCOUNT_CURRENCY) .paymentMethodId(paymentMethodId) .build(); Mockito.when(accountInternalApi.getAccountById(Mockito.eq(account.getId()), Mockito.<InternalCallContext>any())).thenReturn(account); try { // Create events for the bus and expected results createSubscriptionTransitionEvent(account); createAccountCreationEvent(account); createInvoiceAndPaymentCreationEvents(account); } catch (Throwable t) { fail("Initializing accounts failed.", t); } }
@Override public PaymentMethodJson apply(final PaymentMethod paymentMethod) { // Cache audit logs per account if (accountsAuditLogs.get().get(paymentMethod.getAccountId()) == null) { accountsAuditLogs.get().put(paymentMethod.getAccountId(), auditUserApi.getAccountAuditLogs(paymentMethod.getAccountId(), auditMode.getLevel(), tenantContext)); } // Lookup the associated account(s) if (accounts.get(paymentMethod.getAccountId()) == null) { final Account account; try { account = accountUserApi.getAccountById(paymentMethod.getAccountId(), tenantContext); accounts.put(paymentMethod.getAccountId(), account); } catch (final AccountApiException e) { log.warn("Unable to retrieve account", e); return null; } } return PaymentMethodJson.toPaymentMethodJson(accounts.get(paymentMethod.getAccountId()), paymentMethod, accountsAuditLogs.get().get(paymentMethod.getAccountId())); } },
final PaymentMethodModelDao pmModel = new PaymentMethodModelDao(input.getId(), input.getCreatedDate(), input.getUpdatedDate(), input.getAccountId(), input.getPluginName(), input.isActive()); finalPaymentMethods.add(pmModel);
paymentMethodId = paymentMethodProcessor.getExternalPaymentMethod(account, context).getId(); } else { plugin = getPaymentProviderPlugin(account, context);
@GET @Path("/{paymentMethodId:" + UUID_PATTERN + "}") @Produces(APPLICATION_JSON) public Response getPaymentMethod(@PathParam("paymentMethodId") final String paymentMethodId, @QueryParam(QUERY_PAYMENT_METHOD_PLUGIN_INFO) @DefaultValue("false") final Boolean withPluginInfo, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException, PaymentApiException { final TenantContext tenantContext = context.createContext(request); final PaymentMethod paymentMethod = paymentApi.getPaymentMethodById(UUID.fromString(paymentMethodId), false, withPluginInfo, tenantContext); final Account account = accountUserApi.getAccountById(paymentMethod.getAccountId(), tenantContext); final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(paymentMethod.getAccountId(), auditMode.getLevel(), tenantContext); final PaymentMethodJson json = PaymentMethodJson.toPaymentMethodJson(account, paymentMethod, accountAuditLogs); return Response.status(Status.OK).entity(json).build(); }
@DELETE @Produces(APPLICATION_JSON) @Path("/{paymentMethodId:" + UUID_PATTERN + "}") public Response deletePaymentMethod(@PathParam("paymentMethodId") final String paymentMethodId, @QueryParam(QUERY_DELETE_DEFAULT_PM_WITH_AUTO_PAY_OFF) @DefaultValue("false") final Boolean deleteDefaultPaymentMethodWithAutoPayOff, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final HttpServletRequest request) throws PaymentApiException, AccountApiException { final CallContext callContext = context.createContext(createdBy, reason, comment, request); final PaymentMethod paymentMethod = paymentApi.getPaymentMethodById(UUID.fromString(paymentMethodId), false, false, callContext); final Account account = accountUserApi.getAccountById(paymentMethod.getAccountId(), callContext); paymentApi.deletedPaymentMethod(account, UUID.fromString(paymentMethodId), deleteDefaultPaymentMethodWithAutoPayOff, callContext); return Response.status(Status.OK).build(); }