示例#1
0
 public DefaultCallContext(
     final UUID tenantId,
     final String userName,
     final CallOrigin callOrigin,
     final UserType userType,
     final UUID userToken,
     final Clock clock) {
   super(tenantId, userName, callOrigin, userType, userToken);
   this.createdDate = clock.getUTCNow();
   this.updateDate = createdDate;
 }
  @Override
  public PaymentInfoPlugin processPayment(
      final UUID kbAccountId,
      final UUID kbPaymentId,
      final UUID kbPaymentMethodId,
      final BigDecimal amount,
      final Currency currency,
      final CallContext context)
      throws PaymentPluginApiException {
    if (makeNextInvoiceFailWithException.getAndSet(false)) {
      throw new PaymentPluginApiException("", "test error");
    }

    final PaymentPluginStatus status =
        (makeAllInvoicesFailWithError.get() || makeNextInvoiceFailWithError.getAndSet(false))
            ? PaymentPluginStatus.ERROR
            : PaymentPluginStatus.PROCESSED;
    final PaymentInfoPlugin result =
        new DefaultNoOpPaymentInfoPlugin(
            amount, currency, clock.getUTCNow(), clock.getUTCNow(), status, null);
    payments.put(kbPaymentId.toString(), result);
    return result;
  }
  @Override
  public RefundInfoPlugin processRefund(
      final UUID kbAccountId,
      final UUID kbPaymentId,
      final BigDecimal refundAmount,
      final Currency currency,
      final CallContext context)
      throws PaymentPluginApiException {
    final PaymentInfoPlugin paymentInfoPlugin = getPaymentInfo(kbAccountId, kbPaymentId, context);
    if (paymentInfoPlugin == null) {
      throw new PaymentPluginApiException(
          "",
          String.format(
              "No payment found for payment id %s (plugin %s)",
              kbPaymentId.toString(), PLUGIN_NAME));
    }

    BigDecimal maxAmountRefundable = paymentInfoPlugin.getAmount();
    for (final RefundInfoPlugin refund : refunds.get(kbPaymentId.toString())) {
      maxAmountRefundable = maxAmountRefundable.add(refund.getAmount().negate());
    }
    if (maxAmountRefundable.compareTo(refundAmount) < 0) {
      throw new PaymentPluginApiException(
          "",
          String.format(
              "Refund amount of %s for payment id %s is bigger than the payment amount %s (plugin %s)",
              refundAmount, kbPaymentId.toString(), paymentInfoPlugin.getAmount(), PLUGIN_NAME));
    }

    final DefaultNoOpRefundInfoPlugin refundInfoPlugin =
        new DefaultNoOpRefundInfoPlugin(
            refundAmount,
            currency,
            clock.getUTCNow(),
            clock.getUTCNow(),
            RefundPluginStatus.PROCESSED,
            null);
    refunds.put(kbPaymentId.toString(), refundInfoPlugin);

    return refundInfoPlugin;
  }