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; }
@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()); }
@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); } } }
final PaymentMethod input = new DefaultPaymentMethod(paymentMethodId, account.getId(), pluginName); final PaymentMethodModelDao pmModel = new PaymentMethodModelDao(input.getId(), input.getCreatedDate(), input.getUpdatedDate(), input.getAccountId(), input.getPluginName(), input.isActive()); finalPaymentMethods.add(pmModel);
properties); return new PaymentMethodJson(in.getId().toString(), account.getId().toString(), isDefault, in.getPluginName(), pluginDetailJson, toAuditLogJson(accountAuditLogs == null ? null : accountAuditLogs.getAuditLogsForPaymentMethod(in.getId())));
paymentMethodString, "Electronic", paymentMethod == null ? null : paymentMethod.getPluginName(), payment.getPaymentStatus().toString(), payment.getAmount(),
@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(); } });