Search in sources :

Example 11 with PaymentOptions

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

the class PaymentGatewayResource method buildComboFormDescriptor.

@TimedResource
@POST
@Path("/" + HOSTED + "/" + FORM)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Combo API to generate form data to redirect the customer to the gateway", response = HostedPaymentPageFormDescriptorJson.class)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid data for Account or PaymentMethod") })
public Response buildComboFormDescriptor(final ComboHostedPaymentPageJson json, @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 PaymentApiException, AccountApiException {
    verifyNonNullOrEmpty(json, "ComboHostedPaymentPageJson body should be specified");
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames);
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    final Account account = getOrCreateAccount(json.getAccount(), callContext);
    final Iterable<PluginProperty> paymentMethodPluginProperties = extractPluginProperties(json.getPaymentMethodPluginProperties());
    final UUID paymentMethodId = getOrCreatePaymentMethod(account, json.getPaymentMethod(), paymentMethodPluginProperties, callContext);
    final HostedPaymentPageFieldsJson hostedPaymentPageFields = json.getHostedPaymentPageFieldsJson();
    final Iterable<PluginProperty> customFields = extractPluginProperties(hostedPaymentPageFields != null ? hostedPaymentPageFields.getCustomFields() : null);
    final HostedPaymentPageFormDescriptor descriptor = paymentGatewayApi.buildFormDescriptorWithPaymentControl(account, paymentMethodId, customFields, pluginProperties, paymentOptions, callContext);
    final HostedPaymentPageFormDescriptorJson result = new HostedPaymentPageFormDescriptorJson(descriptor);
    return Response.status(Response.Status.OK).entity(result).build();
}
Also used : PluginProperty(org.killbill.billing.payment.api.PluginProperty) Account(org.killbill.billing.account.api.Account) HostedPaymentPageFormDescriptorJson(org.killbill.billing.jaxrs.json.HostedPaymentPageFormDescriptorJson) HostedPaymentPageFormDescriptor(org.killbill.billing.payment.plugin.api.HostedPaymentPageFormDescriptor) PaymentOptions(org.killbill.billing.payment.api.PaymentOptions) UUID(java.util.UUID) HostedPaymentPageFieldsJson(org.killbill.billing.jaxrs.json.HostedPaymentPageFieldsJson) 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 12 with PaymentOptions

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

the class PaymentResource method chargebackReversalPaymentInternal.

private Response chargebackReversalPaymentInternal(final PaymentTransactionJson json, @Nullable final String paymentIdStr, final List<String> paymentControlPluginNames, final List<String> pluginPropertiesString, final String createdBy, final String reason, final String comment, final UriInfo uriInfo, final HttpServletRequest request) throws PaymentApiException, AccountApiException {
    verifyNonNullOrEmpty(json, "PaymentTransactionJson body should be specified");
    verifyNonNullOrEmpty(json.getTransactionExternalKey(), "PaymentTransactionJson transactionExternalKey needs to be set");
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    final Payment initialPayment = getPaymentByIdOrKey(paymentIdStr, json.getPaymentExternalKey(), pluginProperties, callContext);
    final Account account = accountUserApi.getAccountById(initialPayment.getAccountId(), callContext);
    final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames);
    final Payment payment = paymentApi.createChargebackReversalWithPaymentControl(account, initialPayment.getId(), json.getTransactionExternalKey(), paymentOptions, callContext);
    return createPaymentResponse(uriInfo, payment, TransactionType.CHARGEBACK, json.getTransactionExternalKey(), request);
}
Also used : PluginProperty(org.killbill.billing.payment.api.PluginProperty) Account(org.killbill.billing.account.api.Account) Payment(org.killbill.billing.payment.api.Payment) PaymentOptions(org.killbill.billing.payment.api.PaymentOptions) CallContext(org.killbill.billing.util.callcontext.CallContext)

Example 13 with PaymentOptions

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

the class AccountResource method processPayment.

private Response processPayment(final PaymentTransactionJson json, final Account account, final String paymentMethodIdStr, 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() : Currency.valueOf(json.getCurrency());
    final UUID paymentId = json.getPaymentId() == null ? null : UUID.fromString(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 = paymentMethodIdStr == null ? account.getPaymentMethodId() : UUID.fromString(paymentMethodIdStr);
    }
    validatePaymentMethodForAccount(account.getId(), paymentMethodId, callContext);
    final TransactionType transactionType = TransactionType.valueOf(json.getTransactionType());
    final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames);
    final Payment result;
    switch(transactionType) {
        case AUTHORIZE:
            result = paymentApi.createAuthorizationWithPaymentControl(account, paymentMethodId, paymentId, json.getAmount(), currency, json.getPaymentExternalKey(), json.getTransactionExternalKey(), pluginProperties, paymentOptions, callContext);
            break;
        case PURCHASE:
            result = paymentApi.createPurchaseWithPaymentControl(account, paymentMethodId, paymentId, json.getAmount(), currency, json.getPaymentExternalKey(), json.getTransactionExternalKey(), pluginProperties, paymentOptions, callContext);
            break;
        case CREDIT:
            result = paymentApi.createCreditWithPaymentControl(account, paymentMethodId, paymentId, json.getAmount(), currency, 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);
}
Also used : PaymentTransaction(org.killbill.billing.payment.api.PaymentTransaction) PluginProperty(org.killbill.billing.payment.api.PluginProperty) InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) Payment(org.killbill.billing.payment.api.Payment) TransactionType(org.killbill.billing.payment.api.TransactionType) Currency(org.killbill.billing.catalog.api.Currency) UUID(java.util.UUID) PaymentOptions(org.killbill.billing.payment.api.PaymentOptions)

Example 14 with PaymentOptions

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

the class TestInvoicePayment method testWithPendingPaymentThenFailure.

@Test(groups = "slow")
public void testWithPendingPaymentThenFailure() throws Exception {
    clock.setDay(new LocalDate(2012, 4, 1));
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    paymentPlugin.makeNextPaymentPending();
    final DefaultEntitlement baseEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    // INVOICE_PAYMENT_ERROR is sent for PENDING payments
    addDaysAndCheckForCompletion(30, NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT_ERROR);
    invoiceChecker.checkChargedThroughDate(baseEntitlement.getId(), new LocalDate(2012, 6, 1), callContext);
    final List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, callContext);
    assertEquals(invoices.size(), 2);
    final Invoice invoice1 = invoices.get(0).getInvoiceItems().get(0).getInvoiceItemType() == InvoiceItemType.RECURRING ? invoices.get(0) : invoices.get(1);
    assertTrue(invoice1.getBalance().compareTo(new BigDecimal("249.95")) == 0);
    assertTrue(invoice1.getPaidAmount().compareTo(BigDecimal.ZERO) == 0);
    assertTrue(invoice1.getChargedAmount().compareTo(new BigDecimal("249.95")) == 0);
    assertEquals(invoice1.getPayments().size(), 1);
    assertEquals(invoice1.getPayments().get(0).getAmount().compareTo(new BigDecimal("249.95")), 0);
    assertEquals(invoice1.getPayments().get(0).getCurrency(), Currency.USD);
    assertFalse(invoice1.getPayments().get(0).isSuccess());
    assertNotNull(invoice1.getPayments().get(0).getPaymentId());
    final BigDecimal accountBalance1 = invoiceUserApi.getAccountBalance(account.getId(), callContext);
    assertTrue(accountBalance1.compareTo(new BigDecimal("249.95")) == 0);
    final List<Payment> payments = paymentApi.getAccountPayments(account.getId(), false, true, ImmutableList.<PluginProperty>of(), callContext);
    assertEquals(payments.size(), 1);
    assertEquals(payments.get(0).getPurchasedAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(payments.get(0).getTransactions().size(), 1);
    assertEquals(payments.get(0).getTransactions().get(0).getAmount().compareTo(new BigDecimal("249.95")), 0);
    assertEquals(payments.get(0).getTransactions().get(0).getCurrency(), Currency.USD);
    assertEquals(payments.get(0).getTransactions().get(0).getProcessedAmount().compareTo(new BigDecimal("249.95")), 0);
    assertEquals(payments.get(0).getTransactions().get(0).getProcessedCurrency(), Currency.USD);
    assertEquals(payments.get(0).getTransactions().get(0).getTransactionStatus(), TransactionStatus.PENDING);
    assertEquals(payments.get(0).getPaymentAttempts().size(), 1);
    assertEquals(payments.get(0).getPaymentAttempts().get(0).getPluginName(), InvoicePaymentControlPluginApi.PLUGIN_NAME);
    assertEquals(payments.get(0).getPaymentAttempts().get(0).getStateName(), "SUCCESS");
    // Transition the payment to failure
    final List<String> paymentControlPluginNames = ImmutableList.<String>of(InvoicePaymentControlPluginApi.PLUGIN_NAME);
    final PaymentOptions paymentOptions = Mockito.mock(PaymentOptions.class);
    Mockito.when(paymentOptions.getPaymentControlPluginNames()).thenReturn(paymentControlPluginNames);
    busHandler.pushExpectedEvents(NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
    paymentApi.notifyPendingTransactionOfStateChangedWithPaymentControl(account, payments.get(0).getTransactions().get(0).getId(), false, paymentOptions, callContext);
    assertListenerStatus();
    final Invoice invoice2 = invoiceUserApi.getInvoice(invoice1.getId(), callContext);
    assertEquals(invoice2, invoice1);
    final BigDecimal accountBalance2 = invoiceUserApi.getAccountBalance(account.getId(), callContext);
    assertTrue(accountBalance2.compareTo(accountBalance1) == 0);
    final List<Payment> payments2 = paymentApi.getAccountPayments(account.getId(), false, true, ImmutableList.<PluginProperty>of(), callContext);
    assertEquals(payments2.size(), 1);
    assertEquals(payments2.get(0).getPurchasedAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(payments2.get(0).getTransactions().size(), 1);
    assertEquals(payments2.get(0).getTransactions().get(0).getAmount().compareTo(new BigDecimal("249.95")), 0);
    assertEquals(payments2.get(0).getTransactions().get(0).getCurrency(), Currency.USD);
    assertEquals(payments2.get(0).getTransactions().get(0).getProcessedAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(payments2.get(0).getTransactions().get(0).getProcessedCurrency(), Currency.USD);
    assertEquals(payments2.get(0).getTransactions().get(0).getTransactionStatus(), TransactionStatus.PAYMENT_FAILURE);
    assertEquals(payments2.get(0).getPaymentAttempts().size(), 1);
    assertEquals(payments2.get(0).getPaymentAttempts().get(0).getPluginName(), InvoicePaymentControlPluginApi.PLUGIN_NAME);
    // Note that because notifyPendingTransactionOfStateChangedWithPaymentControl is considered an API call, no retry will be attempted
    assertEquals(payments2.get(0).getPaymentAttempts().get(0).getStateName(), "ABORTED");
}
Also used : Account(org.killbill.billing.account.api.Account) Payment(org.killbill.billing.payment.api.Payment) Invoice(org.killbill.billing.invoice.api.Invoice) AccountData(org.killbill.billing.account.api.AccountData) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) PaymentOptions(org.killbill.billing.payment.api.PaymentOptions) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

PaymentOptions (org.killbill.billing.payment.api.PaymentOptions)14 PluginProperty (org.killbill.billing.payment.api.PluginProperty)12 Account (org.killbill.billing.account.api.Account)11 Payment (org.killbill.billing.payment.api.Payment)10 CallContext (org.killbill.billing.util.callcontext.CallContext)10 Currency (org.killbill.billing.catalog.api.Currency)6 ApiOperation (io.swagger.annotations.ApiOperation)4 ApiResponses (io.swagger.annotations.ApiResponses)4 UUID (java.util.UUID)4 Consumes (javax.ws.rs.Consumes)4 POST (javax.ws.rs.POST)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 TimedResource (org.killbill.commons.metrics.TimedResource)4 BigDecimal (java.math.BigDecimal)3 LocalDate (org.joda.time.LocalDate)2 AccountData (org.killbill.billing.account.api.AccountData)2 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)2 Invoice (org.killbill.billing.invoice.api.Invoice)2 HostedPaymentPageFormDescriptorJson (org.killbill.billing.jaxrs.json.HostedPaymentPageFormDescriptorJson)2