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; } }
@Override public PluginDispatcherReturnType<Void> call() throws Exception { throw new PaymentApiException(ErrorCode.PAYMENT_ADD_PAYMENT_METHOD, "foo", "foo"); } }, 100, TimeUnit.MILLISECONDS);
@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 testCreatePurchaseWithControlPluginRuntimeException() throws Exception { mockPaymentControlProviderPlugin.throwsException(new IllegalStateException()); final BigDecimal requestedAmount = BigDecimal.TEN; final String paymentExternalKey = "pay controle external key"; final String transactionExternalKey = "txn control external key"; try { paymentApi.createPurchaseWithPaymentControl( account, account.getPaymentMethodId(), null, requestedAmount, Currency.AED, null, paymentExternalKey, transactionExternalKey, ImmutableList.<PluginProperty>of(), CONTROL_PLUGIN_OPTIONS, callContext); fail(); } catch (PaymentApiException e) { assertTrue(e.getCause() instanceof IllegalStateException); } }
protected List<PaymentMethod> getPaymentMethodsForAccount(final UUID accountId, final TenantContext context) throws OSGIServiceNotAvailable { final PaymentApi paymentApi = getPaymentUserApi(); try { // Try to get all payment methods, with plugin information // TODO this will not return deleted payment methods return paymentApi.getAccountPaymentMethods(accountId, false, true, PLUGIN_PROPERTIES, context); } catch (final PaymentApiException e) { logService.log(LogService.LOG_INFO, "Error retrieving payment methods for accountId " + accountId + ": " + e.getMessage()); throw new OSGIServiceNotAvailable(e); } }
@Override public Payment apply(@Nullable final Void input) { try { return paymentApi.createRefundWithPaymentControl(account, payment.getId(), amount, currency, null, UUID.randomUUID().toString(), PLUGIN_PROPERTIES, PAYMENT_OPTIONS, callContext); } catch (final PaymentApiException e) { fail(e.toString()); return null; } } }, events);
@Test(groups = "slow") public void testCreatePurchaseWithControlPaymentPluginException() throws Exception { mockPaymentProviderPlugin.makeNextPaymentFailWithException(); final BigDecimal requestedAmount = BigDecimal.TEN; final String paymentExternalKey = "pay controle external key"; final String transactionExternalKey = "txn control external key"; try { paymentApi.createPurchaseWithPaymentControl( account, account.getPaymentMethodId(), null, requestedAmount, Currency.AED, null, paymentExternalKey, transactionExternalKey, ImmutableList.<PluginProperty>of(), CONTROL_PLUGIN_OPTIONS, callContext); fail(); } catch (PaymentApiException e) { assertTrue(e.getCause() instanceof PaymentPluginApiException); } }
protected PaymentMethod getPaymentMethod(final UUID paymentMethodId, final TenantContext context) throws OSGIServiceNotAvailable { final PaymentApi paymentApi = getPaymentUserApi(); try { return paymentApi.getPaymentMethodById(paymentMethodId, true, false, PLUGIN_PROPERTIES, context); } catch (final PaymentApiException e) { logService.log(LogService.LOG_INFO, "Error retrieving payment method for paymentMethodId " + paymentMethodId + ": " + e.getMessage()); throw new OSGIServiceNotAvailable(e); } }
@Override public Payment apply(@Nullable final Void input) { try { return paymentApi.createChargebackWithPaymentControl(account, payment.getId(), amount, currency, null, UUID.randomUUID().toString(), PAYMENT_OPTIONS, callContext); } catch (final PaymentApiException e) { fail(e.toString()); return null; } } }, events);
@Override public Response toResponse(final PaymentApiException exception) { if (exception.getCode() == ErrorCode.PAYMENT_ADD_PAYMENT_METHOD.getCode()) { return buildBadRequestResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.PAYMENT_CREATE_PAYMENT.getCode()) { return buildInternalErrorResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.PAYMENT_REFUND_AMOUNT_NEGATIVE_OR_NULL.getCode()) { return buildInternalErrorResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.PAYMENT_DEL_DEFAULT_PAYMENT_METHOD.getCode()) { return buildBadRequestResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.PAYMENT_DEL_PAYMENT_METHOD.getCode()) { return buildInternalErrorResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.PAYMENT_GET_PAYMENT_METHODS.getCode()) { return buildInternalErrorResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.PAYMENT_INTERNAL_ERROR.getCode()) { return buildInternalErrorResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.PAYMENT_NO_DEFAULT_PAYMENT_METHOD.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.PAYMENT_NO_SUCH_PAYMENT.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.PAYMENT_NO_SUCH_PAYMENT_METHOD.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.PAYMENT_NO_SUCH_SUCCESS_PAYMENT.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.PAYMENT_NULL_INVOICE.getCode()) { return buildBadRequestResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.PAYMENT_PLUGIN_TIMEOUT.getCode()) { return buildPluginTimeoutResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.PAYMENT_PLUGIN_GET_PAYMENT_INFO.getCode()) { return buildInternalErrorResponse(exception, uriInfo);
protected void validatePaymentMethodForAccount(final UUID accountId, final UUID paymentMethodId, final CallContext callContext) throws PaymentApiException { if (paymentMethodId != null) { final PaymentMethod paymentMethod = paymentApi.getPaymentMethodById(paymentMethodId, false, false, ImmutableList.<PluginProperty>of(), callContext); if (!paymentMethod.getAccountId().equals(accountId)) { throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_PAYMENT_METHOD, paymentMethodId); } } }
@Test(groups = "slow") public void testCreatePurchasePaymentPluginException() { mockPaymentProviderPlugin.makeNextPaymentFailWithException(); final BigDecimal requestedAmount = BigDecimal.TEN; final String paymentExternalKey = "pay external key"; final String transactionExternalKey = "txn external key"; try { paymentApi.createPurchase(account, account.getPaymentMethodId(), null, requestedAmount, Currency.AED, null, paymentExternalKey, transactionExternalKey, ImmutableList.<PluginProperty>of(), callContext); fail(); } catch (PaymentApiException e) { assertTrue(e.getCause() instanceof PaymentPluginApiException); } }
protected PaymentTransaction getPaymentTransaction(final UUID kbPaymentId, final UUID kbTransactionId, final TenantContext context) throws OSGIServiceNotAvailable { final Payment payment; try { payment = getPaymentUserApi().getPayment(kbPaymentId, false, false, PLUGIN_PROPERTIES, context); } catch (final PaymentApiException e) { logService.log(LogService.LOG_INFO, "Error retrieving payment for paymentId " + kbPaymentId + ": " + e.getMessage()); throw new OSGIServiceNotAvailable(e); } return getPaymentTransaction(kbTransactionId, payment); }
@Override public Payment apply(@Nullable final Void input) { try { return paymentApi.createChargebackReversalWithPaymentControl(account, payment.getId(), null, chargebackTransactionExternalKey, PAYMENT_OPTIONS, callContext); } catch (final PaymentApiException e) { fail(e.toString()); return null; } } }, events);
@Test(groups = "fast") public void testBuildFormDescriptorWithPaymentControlAbortedPayment() throws PaymentApiException { plugin.setAborted(true); // Set a random UUID to verify the plugin will successfully override it try { paymentGatewayApi.buildFormDescriptorWithPaymentControl(account, UUID.randomUUID(), ImmutableList.<PluginProperty>of(), ImmutableList.<PluginProperty>of(), paymentOptions, callContext); Assert.fail(); } catch (PaymentApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.PAYMENT_PLUGIN_API_ABORTED.getCode()); } }
parameterValue = initialPayment.getId().toString(); throw new PaymentApiException(ErrorCode.PAYMENT_INVALID_PARAMETER, parameterType, parameterValue); case 1: return pendingTransaction.iterator().next(); default: throw new PaymentApiException(ErrorCode.PAYMENT_INTERNAL_ERROR, String.format("Illegal payment state: Found multiple PENDING payment transactions for paymentId='%s'", initialPayment.getId()));
@Test(groups = "slow") public void testCreatePurchaseWithControlPluginException() throws Exception { mockPaymentControlProviderPlugin.throwsException(new PaymentControlApiException()); final BigDecimal requestedAmount = BigDecimal.TEN; final String paymentExternalKey = "pay controle external key"; final String transactionExternalKey = "txn control external key"; try { paymentApi.createPurchaseWithPaymentControl( account, account.getPaymentMethodId(), null, requestedAmount, Currency.AED, null, paymentExternalKey, transactionExternalKey, ImmutableList.<PluginProperty>of(), CONTROL_PLUGIN_OPTIONS, callContext); fail(); } catch (PaymentApiException e) { assertTrue(e.getCause() instanceof PaymentControlApiException); } }
fail("Failed to create payment", e); } else { log.info(e.getMessage());
@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);
@Test(groups = "slow") public void testUniqueExternalPaymentMethod() throws PaymentApiException { paymentApi.addPaymentMethod(account, "thisonewillwork", ExternalPaymentProviderPlugin.PLUGIN_NAME, true, null, ImmutableList.<PluginProperty>of(), callContext); try { paymentApi.addPaymentMethod(account, "thisonewillnotwork", ExternalPaymentProviderPlugin.PLUGIN_NAME, true, null, ImmutableList.<PluginProperty>of(), callContext); } catch (PaymentApiException e) { assertEquals(e.getCode(), ErrorCode.PAYMENT_EXTERNAL_PAYMENT_METHOD_ALREADY_EXISTS.getCode()); } }