use of org.killbill.billing.catalog.api.Currency in project killbill by killbill.
the class AccountResource method processPayment.
private Response processPayment(final PaymentTransactionJson json, final Account account, final UUID inputPaymentMethodId, final List<String> paymentControlPluginNames, final List<String> pluginPropertiesString, final UriInfo uriInfo, final CallContext callContext, final HttpServletRequest request) throws PaymentApiException {
verifyNonNullOrEmpty(json, "PaymentTransactionJson body should be specified");
verifyNonNullOrEmpty(json.getTransactionType(), "PaymentTransactionJson transactionType needs to be set", json.getAmount(), "PaymentTransactionJson amount needs to be set");
final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
final Currency currency = json.getCurrency() == null ? account.getCurrency() : json.getCurrency();
final UUID paymentId = json.getPaymentId();
//
// If paymentId was specified, it means we are attempting a payment completion. The preferred way is to use the PaymentResource
// (PUT /1.0/kb/payments/{paymentId}/completeTransaction), but for backward compatibility we still allow the call to proceed
// as long as the request/existing state is healthy (i.e there is a matching PENDING transaction)
//
final UUID paymentMethodId;
if (paymentId != null) {
final Payment initialPayment = paymentApi.getPayment(paymentId, false, false, pluginProperties, callContext);
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 uriBuilder.buildResponse(uriInfo, PaymentResource.class, "getPayment", pendingOrSuccessTransaction.getPaymentId(), request);
}
paymentMethodId = initialPayment.getPaymentMethodId();
} else {
paymentMethodId = inputPaymentMethodId == null ? account.getPaymentMethodId() : inputPaymentMethodId;
}
validatePaymentMethodForAccount(account.getId(), paymentMethodId, callContext);
final TransactionType transactionType = json.getTransactionType();
final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames);
final Payment result;
switch(transactionType) {
case AUTHORIZE:
result = paymentApi.createAuthorizationWithPaymentControl(account, paymentMethodId, paymentId, json.getAmount(), currency, json.getEffectiveDate(), json.getPaymentExternalKey(), json.getTransactionExternalKey(), pluginProperties, paymentOptions, callContext);
break;
case PURCHASE:
result = paymentApi.createPurchaseWithPaymentControl(account, paymentMethodId, paymentId, json.getAmount(), currency, json.getEffectiveDate(), json.getPaymentExternalKey(), json.getTransactionExternalKey(), pluginProperties, paymentOptions, callContext);
break;
case CREDIT:
result = paymentApi.createCreditWithPaymentControl(account, paymentMethodId, paymentId, json.getAmount(), currency, json.getEffectiveDate(), json.getPaymentExternalKey(), json.getTransactionExternalKey(), pluginProperties, paymentOptions, callContext);
break;
default:
return Response.status(Status.PRECONDITION_FAILED).entity("TransactionType " + transactionType + " is not allowed for an account").build();
}
return createPaymentResponse(uriInfo, result, transactionType, json.getTransactionExternalKey(), request);
}
use of org.killbill.billing.catalog.api.Currency in project killbill by killbill.
the class PaymentAutomatonDAOHelper method processPaymentInfoPlugin.
public PaymentAndTransactionModelDao processPaymentInfoPlugin(final TransactionStatus transactionStatus, @Nullable final PaymentTransactionInfoPlugin paymentInfoPlugin, final String currentPaymentStateName, @Nullable final String lastSuccessPaymentState, final BigDecimal defaultSuccessfulProcessedAmount, final Currency defaultProcessedCurrency, final UUID accountId, final UUID attemptId, final UUID paymentId, final UUID transactionId, final TransactionType transactionType, final boolean isApiPayment, final boolean forceOverrideLastSuccessPaymentState) {
final BigDecimal processedAmount;
if (TransactionStatus.SUCCESS.equals(transactionStatus) || TransactionStatus.PENDING.equals(transactionStatus)) {
if (paymentInfoPlugin == null || paymentInfoPlugin.getAmount() == null) {
processedAmount = defaultSuccessfulProcessedAmount;
} else {
processedAmount = paymentInfoPlugin.getAmount();
}
} else {
processedAmount = BigDecimal.ZERO;
}
final Currency processedCurrency;
if (paymentInfoPlugin == null || paymentInfoPlugin.getCurrency() == null) {
processedCurrency = defaultProcessedCurrency;
} else {
processedCurrency = paymentInfoPlugin.getCurrency();
}
final String gatewayErrorCode = paymentInfoPlugin == null ? null : paymentInfoPlugin.getGatewayErrorCode();
final String gatewayErrorMsg = paymentInfoPlugin == null ? null : paymentInfoPlugin.getGatewayError();
final PaymentAndTransactionModelDao paymentAndTransactionModelDao;
if (lastSuccessPaymentState != null || forceOverrideLastSuccessPaymentState) {
paymentAndTransactionModelDao = paymentDao.updatePaymentAndTransactionOnCompletion(accountId, attemptId, paymentId, transactionType, currentPaymentStateName, lastSuccessPaymentState, transactionId, transactionStatus, processedAmount, processedCurrency, gatewayErrorCode, gatewayErrorMsg, isApiPayment, internalCallContext);
} else {
paymentAndTransactionModelDao = paymentDao.updatePaymentAndTransactionOnCompletion(accountId, attemptId, paymentId, transactionType, currentPaymentStateName, transactionId, transactionStatus, processedAmount, processedCurrency, gatewayErrorCode, gatewayErrorMsg, isApiPayment, internalCallContext);
}
return paymentAndTransactionModelDao;
}
use of org.killbill.billing.catalog.api.Currency in project killbill by killbill.
the class DefaultPayment method getAmountForTransactions.
private static BigDecimal getAmountForTransactions(final Currency paymentCurrency, final Collection<PaymentTransaction> transactions, final TransactionType transactiontype, final Collection<PaymentTransaction> chargebackTransactions, final BigDecimal chargebackProcessedAmount, final Currency chargebackProcessedCurrency, final BigDecimal chargebackAmount, final Currency chargebackCurrency) {
BigDecimal unformattedAmountForTransactions = null;
final Collection<PaymentTransaction> candidateTransactions = Collections2.<PaymentTransaction>filter(transactions, new Predicate<PaymentTransaction>() {
@Override
public boolean apply(final PaymentTransaction transaction) {
return transaction.getTransactionType() == transactiontype && TransactionStatus.SUCCESS.equals(transaction.getTransactionStatus());
}
});
final boolean takeChargebacksIntoAccount = ImmutableList.<TransactionType>of(TransactionType.CAPTURE, TransactionType.PURCHASE).contains(transactiontype);
Currency currencyForTransactions = getCurrencyForTransactions(candidateTransactions, true);
if (currencyForTransactions == null || currencyForTransactions != paymentCurrency) {
currencyForTransactions = getCurrencyForTransactions(candidateTransactions, false);
if (currencyForTransactions == null) {
// Multiple currencies - cannot compute the total
unformattedAmountForTransactions = BigDecimal.ZERO;
} else if (currencyForTransactions != paymentCurrency) {
// Different currency than the main payment currency
unformattedAmountForTransactions = BigDecimal.ZERO;
} else {
final BigDecimal amountForTransactions = getAmountForTransactions(candidateTransactions, false);
unformattedAmountForTransactions = getAmountForTransactions(amountForTransactions, takeChargebacksIntoAccount, currencyForTransactions, chargebackTransactions, chargebackProcessedAmount, chargebackProcessedCurrency, chargebackAmount, chargebackCurrency);
}
} else {
final BigDecimal amountForTransactions = getAmountForTransactions(candidateTransactions, true);
unformattedAmountForTransactions = getAmountForTransactions(amountForTransactions, takeChargebacksIntoAccount, currencyForTransactions, chargebackTransactions, chargebackProcessedAmount, chargebackProcessedCurrency, chargebackAmount, chargebackCurrency);
}
return unformattedAmountForTransactions == null || currencyForTransactions == null ? unformattedAmountForTransactions : KillBillMoney.of(unformattedAmountForTransactions, currencyForTransactions);
}
use of org.killbill.billing.catalog.api.Currency 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.catalog.api.Currency in project killbill by killbill.
the class KillbillClient method getAccount.
public Account getAccount(final String name, final String externalKey, final String email, final UUID parentAccountId) {
final UUID accountId = UUID.randomUUID();
final int length = 4;
final Currency currency = DEFAULT_CURRENCY;
final String timeZone = "UTC";
final String address1 = "12 rue des ecoles";
final String address2 = "Poitier";
final String postalCode = "44 567";
final String company = "Renault";
final String city = "Quelque part";
final String state = "Poitou";
final String country = "France";
final String locale = "fr";
final String phone = "81 53 26 56";
final String notes = "notes";
final boolean isPaymentDelegatedToParent = parentAccountId != null;
// Note: the accountId payload is ignored on account creation
return new Account(accountId, name, length, externalKey, email, null, currency, parentAccountId, isPaymentDelegatedToParent, null, null, timeZone, address1, address2, postalCode, company, city, state, country, locale, phone, notes, false, null, null, EMPTY_AUDIT_LOGS);
}
Aggregations