public T dispatchWithAccountLockAndTimeout(final Callable<T> task, final long timeout, final TimeUnit unit) throws PaymentApiException, TimeoutException { try { final Future<T> future = executor.submit(task); return future.get(timeout, unit); } catch (ExecutionException e) { if (e.getCause() instanceof PaymentApiException) { throw (PaymentApiException) e.getCause(); } else { throw new PaymentApiException(ErrorCode.PAYMENT_INTERNAL_ERROR, e.getMessage()); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new PaymentApiException(ErrorCode.PAYMENT_INTERNAL_ERROR, e.getMessage()); } }
@Override public Void call() throws Exception { throw new PaymentApiException(ErrorCode.PAYMENT_ADD_PAYMENT_METHOD, "foo", "foo"); } }, 100, TimeUnit.MILLISECONDS);
@Override public Payment getPayment(final UUID paymentId, final InternalTenantContext context) throws PaymentApiException { final Payment payment = paymentProcessor.getPayment(paymentId, false, context); if (payment == null) { throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_PAYMENT, paymentId); } return payment; }
protected PaymentPluginApi getPaymentPluginApi(final String pluginName) throws PaymentApiException { final PaymentPluginApi pluginApi = pluginRegistry.getServiceForName(pluginName); if (pluginApi == null) { throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_PAYMENT_PLUGIN, pluginName); } return pluginApi; }
protected void setAccountAutoPayOff(final UUID accountId, final InternalCallContext context) throws PaymentApiException { try { tagInternalApi.addTag(accountId, ObjectType.ACCOUNT, ControlTagType.AUTO_PAY_OFF.getId(), context); } catch (TagApiException e) { log.error("Failed to add AUTO_PAY_OFF on account " + accountId, e); throw new PaymentApiException(ErrorCode.PAYMENT_INTERNAL_ERROR, "Failed to add AUTO_PAY_OFF on account " + accountId); } }
@Override public Pagination<PaymentInfoPlugin> build() throws PaymentApiException { try { return pluginApi.searchPayments(searchKey, offset, limit, buildTenantContext(internalTenantContext)); } catch (final PaymentPluginApiException e) { throw new PaymentApiException(e, ErrorCode.PAYMENT_PLUGIN_SEARCH_PAYMENTS, pluginName, searchKey); } } },
@Override public Refund createRefund(final Account account, final UUID paymentId, final BigDecimal refundAmount, final CallContext context) throws PaymentApiException { if (refundAmount == null || refundAmount.compareTo(BigDecimal.ZERO) <= 0) { throw new PaymentApiException(ErrorCode.PAYMENT_REFUND_AMOUNT_NEGATIVE_OR_NULL); } return refundProcessor.createRefund(account, paymentId, refundAmount, false, ImmutableMap.<UUID, BigDecimal>of(), internalCallContextFactory.createInternalCallContext(account.getId(), context)); }
@Override public Pagination<PaymentMethodPlugin> build() throws PaymentApiException { try { return pluginApi.searchPaymentMethods(searchKey, offset, limit, buildTenantContext(internalTenantContext)); } catch (final PaymentPluginApiException e) { throw new PaymentApiException(e, ErrorCode.PAYMENT_PLUGIN_SEARCH_PAYMENT_METHODS, pluginName, searchKey); } } },
@Override public Refund createRefundWithAdjustment(final Account account, final UUID paymentId, final BigDecimal refundAmount, final CallContext context) throws PaymentApiException { if (refundAmount == null || refundAmount.compareTo(BigDecimal.ZERO) <= 0) { throw new PaymentApiException(ErrorCode.PAYMENT_REFUND_AMOUNT_NEGATIVE_OR_NULL); } return refundProcessor.createRefund(account, paymentId, refundAmount, true, ImmutableMap.<UUID, BigDecimal>of(), internalCallContextFactory.createInternalCallContext(account.getId(), context)); }
private BigDecimal getAndValidatePaymentAmount(final Invoice invoice, @Nullable final BigDecimal inputAmount, final boolean isInstantPayment) throws PaymentApiException { if (invoice.getBalance().compareTo(BigDecimal.ZERO) <= 0) { throw new PaymentApiException(ErrorCode.PAYMENT_NULL_INVOICE, invoice.getId()); } if (isInstantPayment && inputAmount != null && invoice.getBalance().compareTo(inputAmount) < 0) { throw new PaymentApiException(ErrorCode.PAYMENT_AMOUNT_DENIED, invoice.getId(), inputAmount.floatValue(), invoice.getBalance().floatValue()); } final BigDecimal result = inputAmount != null ? inputAmount : invoice.getBalance(); return result.setScale(2, RoundingMode.HALF_UP); }
@Override public Payment getPayment(final UUID paymentId, final boolean withPluginInfo, final TenantContext context) throws PaymentApiException { final Payment payment = paymentProcessor.getPayment(paymentId, withPluginInfo, internalCallContextFactory.createInternalTenantContext(context)); if (payment == null) { throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_PAYMENT, paymentId); } return payment; }
protected PaymentPluginApi getPaymentProviderPlugin(final UUID paymentMethodId, final InternalTenantContext context) throws PaymentApiException { final PaymentMethodModelDao methodDao = paymentDao.getPaymentMethodIncludedDeleted(paymentMethodId, context); if (methodDao == null) { log.error("PaymentMethod does not exist", paymentMethodId); throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_PAYMENT_METHOD, paymentMethodId); } return getPaymentPluginApi(methodDao.getPluginName()); }
public T processAccountWithLock(final GlobalLocker locker, final String accountExternalKey, final WithAccountLockCallback<T> callback) throws PaymentApiException { GlobalLock lock = null; try { lock = locker.lockWithNumberOfTries(LockerType.ACCOUNT_FOR_INVOICE_PAYMENTS.toString(), accountExternalKey, NB_LOCK_TRY); return callback.doOperation(); } catch (LockFailedException e) { final String format = String.format("Failed to lock account %s", accountExternalKey); log.error(String.format(format), e); throw new PaymentApiException(ErrorCode.PAYMENT_INTERNAL_ERROR, format); } finally { if (lock != null) { lock.release(); } } } }
protected PaymentPluginApi getPaymentProviderPlugin(final Account account, final InternalTenantContext context) throws PaymentApiException { final UUID paymentMethodId = account.getPaymentMethodId(); if (paymentMethodId == null) { throw new PaymentApiException(ErrorCode.PAYMENT_NO_DEFAULT_PAYMENT_METHOD, account.getId()); } return getPaymentProviderPlugin(paymentMethodId, context); }
@Override public Void doOperation() throws PaymentApiException { final PaymentModelDao payment = paymentDao.getPayment(paymentId, context); if (payment == null) { throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_PAYMENT, paymentId); } if (payment.getPaymentStatus() != PaymentStatus.PENDING) { throw new PaymentApiException(ErrorCode.PAYMENT_NOT_PENDING, paymentId); } final List<PaymentAttemptModelDao> attempts = paymentDao.getAttemptsForPayment(paymentId, context); final PaymentAttemptModelDao lastAttempt = attempts.get(attempts.size() - 1); final PaymentStatus newPaymentStatus = isSuccess ? PaymentStatus.SUCCESS : PaymentStatus.PAYMENT_FAILURE_ABORTED; paymentDao.updatePaymentAndAttemptOnCompletion(paymentId, newPaymentStatus, payment.getProcessedAmount(), payment.getProcessedCurrency(), lastAttempt.getId(), null, null, context); return null; } });
public PaymentMethod getPaymentMethodById(final UUID paymentMethodId, final boolean includedDeleted, final boolean withPluginInfo, final InternalTenantContext context) throws PaymentApiException { final PaymentMethodModelDao paymentMethodModel = includedDeleted ? paymentDao.getPaymentMethodIncludedDeleted(paymentMethodId, context) : paymentDao.getPaymentMethod(paymentMethodId, context); if (paymentMethodModel == null) { throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_PAYMENT_METHOD, paymentMethodId); } return buildDefaultPaymentMethod(paymentMethodModel, withPluginInfo, context); }
private PaymentPluginApi getPluginApi(final UUID paymentMethodId, final InternalTenantContext context) throws PaymentApiException { final PaymentMethodModelDao paymentMethod = paymentDao.getPaymentMethod(paymentMethodId, context); if (paymentMethod == null) { throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_PAYMENT_METHOD, paymentMethodId); } return getPaymentPluginApi(paymentMethod.getPluginName()); }
@Override public Void doOperation() throws PaymentApiException { try { final RefundModelDao refund = paymentDao.getRefund(refundId, context); if (refund == null) { throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_REFUND, refundId); } if (refund.getRefundStatus() != RefundStatus.PENDING) { throw new PaymentApiException(ErrorCode.PAYMENT_NOT_PENDING, refundId); } // TODO STEPH : Model is broken if we had an invoice item adjustements as we lost track of them invoiceApi.createRefund(refund.getPaymentId(), refund.getAmount(), refund.isAdjusted(), Collections.<UUID, BigDecimal>emptyMap(), refund.getId(), context); paymentDao.updateRefundStatus(refund.getId(), RefundStatus.COMPLETED, refund.getAmount(), refund.getCurrency(), context); } catch (InvoiceApiException e) { } return null; } });
@Override public Void doOperation() throws PaymentApiException { try { for (final RefundModelDao cur : refundsToBeFixed) { // TODO - we currently don't save the items to be adjusted. If we crash, they won't be adjusted... invoiceApi.createRefund(cur.getPaymentId(), cur.getAmount(), cur.isAdjusted(), ImmutableMap.<UUID, BigDecimal>of(), cur.getId(), context); paymentDao.updateRefundStatus(cur.getId(), RefundStatus.COMPLETED, cur.getProcessedAmount(), cur.getProcessedCurrency(), context); } } catch (InvoiceApiException e) { throw new PaymentApiException(e); } return null; } });
private PaymentMethod buildDefaultPaymentMethod(final PaymentMethodModelDao paymentMethodModelDao, final boolean withPluginInfo, final InternalTenantContext context) throws PaymentApiException { final PaymentMethodPlugin paymentMethodPlugin; if (withPluginInfo) { try { final PaymentPluginApi pluginApi = getPaymentPluginApi(paymentMethodModelDao.getPluginName()); paymentMethodPlugin = pluginApi.getPaymentMethodDetail(paymentMethodModelDao.getAccountId(), paymentMethodModelDao.getId(), buildTenantContext(context)); } catch (PaymentPluginApiException e) { log.warn("Error retrieving payment method " + paymentMethodModelDao.getId() + " from plugin " + paymentMethodModelDao.getPluginName(), e); throw new PaymentApiException(ErrorCode.PAYMENT_GET_PAYMENT_METHODS, paymentMethodModelDao.getAccountId(), paymentMethodModelDao.getId()); } } else { paymentMethodPlugin = null; } return new DefaultPaymentMethod(paymentMethodModelDao, paymentMethodPlugin); }