Search in sources :

Example 16 with PaymentOptions

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

the class TestInvoicePayment method testWithPendingPaymentThenFailure.

@Test(groups = "slow", description = "Verify notifyPendingTransactionOfStateChanged behavior for PENDING->FAILURE")
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, 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)

Example 17 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 = 200, message = "Successful"),*/
@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.createCallContextNoAccountId(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.getHostedPaymentPageFields();
    final Iterable<PluginProperty> customFields = extractPluginProperties(hostedPaymentPageFields != null ? hostedPaymentPageFields.getFormFields() : 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 18 with PaymentOptions

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

the class PaymentGatewayResource method processNotification.

@TimedResource
@POST
@Path("/" + NOTIFICATION + "/{" + QUERY_PAYMENT_PLUGIN_NAME + ":" + ANYTHING_PATTERN + "}")
@Consumes(WILDCARD)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Process a gateway notification", notes = "The response is built by the appropriate plugin")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successful") })
public Response processNotification(@PathParam(PATH_PAYMENT_PLUGIN_NAME) final String pluginName, final String body, @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 {
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames);
    final CallContext callContext = context.createCallContextNoAccountId(createdBy, reason, comment, request);
    final String notificationPayload;
    if (Strings.emptyToNull(body) == null && uriInfo.getRequestUri() != null) {
        notificationPayload = uriInfo.getRequestUri().getRawQuery();
    } else {
        notificationPayload = body;
    }
    // Note: the body is opaque here, as it comes from the gateway. The associated payment plugin will know how to deserialize it though
    final GatewayNotification notification = paymentGatewayApi.processNotificationWithPaymentControl(notificationPayload, pluginName, pluginProperties, paymentOptions, callContext);
    final GatewayNotificationJson result = new GatewayNotificationJson(notification);
    // The plugin told us how to build the response
    return result.toResponse();
}
Also used : GatewayNotificationJson(org.killbill.billing.jaxrs.json.GatewayNotificationJson) PluginProperty(org.killbill.billing.payment.api.PluginProperty) GatewayNotification(org.killbill.billing.payment.plugin.api.GatewayNotification) PaymentOptions(org.killbill.billing.payment.api.PaymentOptions) 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 19 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 UUID paymentId, 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.createCallContextNoAccountId(createdBy, reason, comment, request);
    final Payment initialPayment = getPaymentByIdOrKey(paymentId, 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.getEffectiveDate(), 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 20 with PaymentOptions

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

the class PaymentResource method captureAuthorizationInternal.

private Response captureAuthorizationInternal(final PaymentTransactionJson json, @Nullable final UUID paymentId, 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.getAmount(), "PaymentTransactionJson amount needs to be set");
    final Iterable<PluginProperty> pluginPropertiesFromBody = extractPluginProperties(json.getProperties());
    final Iterable<PluginProperty> pluginPropertiesFromQuery = extractPluginProperties(pluginPropertiesString);
    final Iterable<PluginProperty> pluginProperties = Iterables.concat(pluginPropertiesFromQuery, pluginPropertiesFromBody);
    final CallContext callContext = context.createCallContextNoAccountId(createdBy, reason, comment, request);
    final Payment initialPayment = getPaymentByIdOrKey(paymentId, json.getPaymentExternalKey(), pluginProperties, callContext);
    final Account account = accountUserApi.getAccountById(initialPayment.getAccountId(), callContext);
    final Currency currency = json.getCurrency() == null ? account.getCurrency() : json.getCurrency();
    final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames);
    final Payment payment = paymentApi.createCaptureWithPaymentControl(account, initialPayment.getId(), json.getAmount(), currency, json.getEffectiveDate(), json.getTransactionExternalKey(), pluginProperties, paymentOptions, callContext);
    return createPaymentResponse(uriInfo, payment, TransactionType.CAPTURE, 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) Currency(org.killbill.billing.catalog.api.Currency) PaymentOptions(org.killbill.billing.payment.api.PaymentOptions) CallContext(org.killbill.billing.util.callcontext.CallContext)

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