Search in sources :

Example 16 with PaymentMethod

use of org.killbill.billing.payment.api.PaymentMethod in project killbill by killbill.

the class AccountResource method createPaymentMethod.

@TimedResource
@POST
@Path("/{accountId:" + UUID_PATTERN + "}/" + PAYMENT_METHODS)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Add a payment method")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied"), @ApiResponse(code = 404, message = "Account not found") })
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, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @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 {
    verifyNonNullOrEmpty(json, "PaymentMethodJson body should be specified");
    verifyNonNullOrEmpty(json.getPluginName(), "PaymentMethodJson pluginName should be specified");
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    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(account, data.getExternalKey(), data.getPluginName(), isDefault, data.getPluginDetail(), pluginProperties, callContext);
    if (payAllUnpaidInvoices && unpaidInvoices.size() > 0) {
        for (final Invoice invoice : unpaidInvoices) {
            createPurchaseForInvoice(account, invoice.getId(), invoice.getBalance(), paymentMethodId, false, null, null, pluginProperties, callContext);
        }
    }
    return uriBuilder.buildResponse(uriInfo, PaymentMethodResource.class, "getPaymentMethod", paymentMethodId, request);
}
Also used : PluginProperty(org.killbill.billing.payment.api.PluginProperty) Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) PaymentMethod(org.killbill.billing.payment.api.PaymentMethod) UUID(java.util.UUID) CallContext(org.killbill.billing.util.callcontext.CallContext) Path(javax.ws.rs.Path) TimedResource(org.killbill.commons.metrics.TimedResource) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 17 with PaymentMethod

use of org.killbill.billing.payment.api.PaymentMethod in project killbill by killbill.

the class TestPaymentMethodProcessorRefreshWithDB method testRefreshWithNewPaymentMethod.

@Test(groups = "slow")
public void testRefreshWithNewPaymentMethod() throws Exception {
    final Account account = testHelper.createTestAccount("foo@bar.com", true);
    Assert.assertEquals(getPluginApi().getPaymentMethods(account.getId(), true, PLUGIN_PROPERTIES, callContext).size(), 1);
    final UUID existingPMId = account.getPaymentMethodId();
    // Add new payment in plugin directly
    final UUID newPmId = UUID.randomUUID();
    getPluginApi().addPaymentMethod(account.getId(), newPmId, new DefaultNoOpPaymentMethodPlugin(UUID.randomUUID().toString(), false, ImmutableList.<PluginProperty>of()), false, PLUGIN_PROPERTIES, callContext);
    // Verify that the refresh does indeed show 2 PMs
    final List<PaymentMethod> methods = paymentMethodProcessor.refreshPaymentMethods(MockPaymentProviderPlugin.PLUGIN_NAME, account, PLUGIN_PROPERTIES, callContext, internalCallContext);
    Assert.assertEquals(methods.size(), 2);
    checkPaymentMethodExistsWithStatus(methods, existingPMId, true);
    checkPaymentMethodExistsWithStatus(methods, newPmId, true);
}
Also used : Account(org.killbill.billing.account.api.Account) PluginProperty(org.killbill.billing.payment.api.PluginProperty) PaymentMethod(org.killbill.billing.payment.api.PaymentMethod) UUID(java.util.UUID) DefaultNoOpPaymentMethodPlugin(org.killbill.billing.payment.provider.DefaultNoOpPaymentMethodPlugin) Test(org.testng.annotations.Test)

Aggregations

PaymentMethod (org.killbill.billing.payment.api.PaymentMethod)17 Account (org.killbill.billing.account.api.Account)11 UUID (java.util.UUID)10 PluginProperty (org.killbill.billing.payment.api.PluginProperty)10 ApiOperation (io.swagger.annotations.ApiOperation)7 ApiResponses (io.swagger.annotations.ApiResponses)7 Produces (javax.ws.rs.Produces)7 TimedResource (org.killbill.commons.metrics.TimedResource)7 Path (javax.ws.rs.Path)6 GET (javax.ws.rs.GET)5 PaymentMethodJson (org.killbill.billing.jaxrs.json.PaymentMethodJson)5 AccountAuditLogs (org.killbill.billing.util.audit.AccountAuditLogs)5 TenantContext (org.killbill.billing.util.callcontext.TenantContext)5 AccountApiException (org.killbill.billing.account.api.AccountApiException)4 DefaultPaymentMethod (org.killbill.billing.payment.api.DefaultPaymentMethod)4 PaymentMethodModelDao (org.killbill.billing.payment.dao.PaymentMethodModelDao)4 Test (org.testng.annotations.Test)4 ArrayList (java.util.ArrayList)3 PaymentApiException (org.killbill.billing.payment.api.PaymentApiException)3 DefaultNoOpPaymentMethodPlugin (org.killbill.billing.payment.provider.DefaultNoOpPaymentMethodPlugin)3