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");
}
}
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);
}
Aggregations