Search in sources :

Example 26 with PaymentOptions

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

the class JaxRsResourceBase method completeTransactionInternal.

protected void completeTransactionInternal(final PaymentTransactionJson json, final Payment initialPayment, final List<String> paymentControlPluginNames, final Iterable<PluginProperty> pluginProperties, final TenantContext contextNoAccountId, final String createdBy, final String reason, final String comment, final UriInfo uriInfo, final HttpServletRequest request) throws PaymentApiException, AccountApiException {
    final Account account = accountUserApi.getAccountById(initialPayment.getAccountId(), contextNoAccountId);
    final BigDecimal amount = json == null ? null : json.getAmount();
    final Currency currency = json == null ? null : json.getCurrency();
    final CallContext callContext = context.createCallContextWithAccountId(account.getId(), createdBy, reason, comment, request);
    final PaymentTransaction pendingOrSuccessTransaction = lookupPendingOrSuccessTransaction(initialPayment, json != null ? json.getTransactionId() : null, json != null ? json.getTransactionExternalKey() : null, json != null ? json.getTransactionType() : null);
    // If transaction was already completed, return early (See #626)
    if (pendingOrSuccessTransaction.getTransactionStatus() == TransactionStatus.SUCCESS) {
        return;
    }
    final PaymentTransaction pendingTransaction = pendingOrSuccessTransaction;
    final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames);
    switch(pendingTransaction.getTransactionType()) {
        case AUTHORIZE:
            paymentApi.createAuthorizationWithPaymentControl(account, initialPayment.getPaymentMethodId(), initialPayment.getId(), amount, currency, null, initialPayment.getExternalKey(), pendingTransaction.getExternalKey(), pluginProperties, paymentOptions, callContext);
            break;
        case CAPTURE:
            paymentApi.createCaptureWithPaymentControl(account, initialPayment.getId(), amount, currency, null, pendingTransaction.getExternalKey(), pluginProperties, paymentOptions, callContext);
            break;
        case PURCHASE:
            paymentApi.createPurchaseWithPaymentControl(account, initialPayment.getPaymentMethodId(), initialPayment.getId(), amount, currency, null, initialPayment.getExternalKey(), pendingTransaction.getExternalKey(), pluginProperties, paymentOptions, callContext);
            break;
        case CREDIT:
            paymentApi.createCreditWithPaymentControl(account, initialPayment.getPaymentMethodId(), initialPayment.getId(), amount, currency, null, initialPayment.getExternalKey(), pendingTransaction.getExternalKey(), pluginProperties, paymentOptions, callContext);
            break;
        case REFUND:
            paymentApi.createRefundWithPaymentControl(account, initialPayment.getId(), amount, currency, null, pendingTransaction.getExternalKey(), pluginProperties, paymentOptions, callContext);
            break;
        default:
            throw new IllegalStateException("TransactionType " + pendingTransaction.getTransactionType() + " cannot be completed");
    }
}
Also used : PaymentTransaction(org.killbill.billing.payment.api.PaymentTransaction) Account(org.killbill.billing.account.api.Account) Currency(org.killbill.billing.catalog.api.Currency) PaymentOptions(org.killbill.billing.payment.api.PaymentOptions) CallContext(org.killbill.billing.util.callcontext.CallContext) BigDecimal(java.math.BigDecimal)

Example 27 with PaymentOptions

use of org.killbill.billing.payment.api.PaymentOptions 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", response = PaymentMethodJson.class)
@ApiResponses(value = { @ApiResponse(code = 201, message = "Payment method created"), @ApiResponse(code = 400, message = "Invalid account id supplied"), @ApiResponse(code = 404, message = "Account not found") })
public Response createPaymentMethod(@PathParam("accountId") final UUID accountId, final PaymentMethodJson json, @QueryParam(QUERY_PAYMENT_METHOD_IS_DEFAULT) @DefaultValue("false") final Boolean isDefault, @QueryParam(QUERY_PAY_ALL_UNPAID_INVOICES) @DefaultValue("false") final Boolean payAllUnpaidInvoices, @QueryParam(QUERY_PAYMENT_CONTROL_PLUGIN_NAME) final List<String> paymentControlPluginNames, @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.createCallContextWithAccountId(accountId, 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(), null, clock.getUTCToday(), callContext) : Collections.<Invoice>emptyList();
    if (payAllUnpaidInvoices && unpaidInvoices.size() > 0 && !hasDefaultPaymentMethod) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames);
    final UUID paymentMethodId = paymentApi.addPaymentMethodWithPaymentControl(account, data.getExternalKey(), data.getPluginName(), isDefault, data.getPluginDetail(), pluginProperties, paymentOptions, 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) PaymentOptions(org.killbill.billing.payment.api.PaymentOptions) 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)

Aggregations

PaymentOptions (org.killbill.billing.payment.api.PaymentOptions)27 Account (org.killbill.billing.account.api.Account)23 PluginProperty (org.killbill.billing.payment.api.PluginProperty)22 CallContext (org.killbill.billing.util.callcontext.CallContext)21 Payment (org.killbill.billing.payment.api.Payment)18 Currency (org.killbill.billing.catalog.api.Currency)12 UUID (java.util.UUID)11 ApiOperation (io.swagger.annotations.ApiOperation)9 ApiResponses (io.swagger.annotations.ApiResponses)9 Consumes (javax.ws.rs.Consumes)9 Path (javax.ws.rs.Path)9 Produces (javax.ws.rs.Produces)9 TimedResource (org.killbill.commons.metrics.TimedResource)9 POST (javax.ws.rs.POST)8 BigDecimal (java.math.BigDecimal)6 PaymentTransaction (org.killbill.billing.payment.api.PaymentTransaction)5 InvoicePayment (org.killbill.billing.invoice.api.InvoicePayment)4 Invoice (org.killbill.billing.invoice.api.Invoice)3 HostedPaymentPageFormDescriptorJson (org.killbill.billing.jaxrs.json.HostedPaymentPageFormDescriptorJson)3 TransactionType (org.killbill.billing.payment.api.TransactionType)3