Search in sources :

Example 1 with CustomerRequest

use of com.braintreegateway.CustomerRequest in project J2PAY by tranxactive.

the class BraintreeGateway method createCustomer.

private HTTPResponse createCustomer(JSONObject apiParameters, Customer customer, CustomerCard customerCard, Currency currency, float amount) {
    boolean hasException = false;
    CustomerRequest customerRequest = new CustomerRequest();
    customerRequest.firstName(customer.getFirstName()).lastName(customer.getLastName()).email(customer.getEmail()).phone(customer.getPhoneNumber());
    CreditCardRequest card = customerRequest.creditCard();
    card.cardholderName(customerCard.getName()).number(customerCard.getNumber()).cvv(customerCard.getCvv()).expirationDate(customerCard.getExpiryMonth() + "/" + customerCard.getExpiryYear());
    CreditCardAddressRequest address = card.billingAddress();
    address.countryCodeAlpha3(customer.getCountry().getCodeISO3()).postalCode(customer.getZip()).streetAddress(customer.getAddress()).region(customer.getState()).locality(customer.getCity());
    Result<com.braintreegateway.Customer> createCustomer = null;
    GeneralResponse successResponse = null;
    ErrorResponse errorResponse = new ErrorResponse();
    HTTPResponse httpResponse = new HTTPResponse();
    try {
        createCustomer = getGatewayObject(apiParameters).customer().create(customerRequest);
    } catch (NotFoundException e) {
        errorResponse.setMessage("No record found");
        hasException = true;
    } catch (AuthenticationException e) {
        errorResponse.setMessage("Invalid credentials");
        hasException = true;
    } catch (AuthorizationException e) {
        errorResponse.setMessage("Not authorized to perform this action");
        hasException = true;
    } catch (ConfigurationException e) {
        errorResponse.setMessage("Configuratoin error");
        hasException = true;
    } catch (DownForMaintenanceException e) {
        errorResponse.setMessage("Server is under maintenance");
        hasException = true;
    } catch (TimeoutException e) {
        errorResponse.setMessage("Request time out");
        hasException = true;
    } catch (ForgedQueryStringException | InvalidChallengeException | InvalidSignatureException | UnexpectedException e) {
        errorResponse.setMessage("Unexpected exception occured");
        hasException = true;
    } catch (ServerException e) {
        errorResponse.setMessage("Server error");
        hasException = true;
    } catch (TooManyRequestsException e) {
        errorResponse.setMessage("Too many requests");
        hasException = true;
    } catch (UpgradeRequiredException e) {
        errorResponse.setMessage("Need to upgrade");
        hasException = true;
    }
    if (hasException) {
        processFinalResponse(null, httpResponse, successResponse, errorResponse);
        return httpResponse;
    }
    if (createCustomer.isSuccess()) {
        successResponse = new GeneralResponse();
        successResponse.setMessage("Success");
        successResponse.setTransactionId(createCustomer.getTarget().getId());
    } else {
        errorResponse.setMessage(createCustomer.getMessage());
    }
    processFinalResponse(null, httpResponse, successResponse, errorResponse);
    return httpResponse;
}
Also used : InvalidSignatureException(com.braintreegateway.exceptions.InvalidSignatureException) UnexpectedException(com.braintreegateway.exceptions.UnexpectedException) ServerException(com.braintreegateway.exceptions.ServerException) Customer(com.tranxactive.j2pay.gateways.parameters.Customer) AuthenticationException(com.braintreegateway.exceptions.AuthenticationException) AuthorizationException(com.braintreegateway.exceptions.AuthorizationException) HTTPResponse(com.tranxactive.j2pay.net.HTTPResponse) CreditCardAddressRequest(com.braintreegateway.CreditCardAddressRequest) NotFoundException(com.braintreegateway.exceptions.NotFoundException) InvalidChallengeException(com.braintreegateway.exceptions.InvalidChallengeException) ErrorResponse(com.tranxactive.j2pay.gateways.responses.ErrorResponse) UpgradeRequiredException(com.braintreegateway.exceptions.UpgradeRequiredException) CustomerRequest(com.braintreegateway.CustomerRequest) TooManyRequestsException(com.braintreegateway.exceptions.TooManyRequestsException) ConfigurationException(com.braintreegateway.exceptions.ConfigurationException) ForgedQueryStringException(com.braintreegateway.exceptions.ForgedQueryStringException) CreditCardRequest(com.braintreegateway.CreditCardRequest) DownForMaintenanceException(com.braintreegateway.exceptions.DownForMaintenanceException) TimeoutException(com.braintreegateway.exceptions.TimeoutException)

Example 2 with CustomerRequest

use of com.braintreegateway.CustomerRequest in project camel by apache.

the class AddressGatewayIntegrationTest method doPostSetup.

@Override
protected void doPostSetup() throws Exception {
    this.gateway = getGateway();
    this.customer = gateway.customer().create(new CustomerRequest().firstName("user").lastName(UUID.randomUUID().toString())).getTarget();
    if (customer != null) {
        LOG.info("Customer created - id={}", this.customer.getId());
    }
}
Also used : CustomerRequest(com.braintreegateway.CustomerRequest)

Example 3 with CustomerRequest

use of com.braintreegateway.CustomerRequest in project camel by apache.

the class CustomerGatewayIntegrationTest method testCustomerManagementWorkflow.

/**
     * Customers management workflow:
     * - create a customer
     * - lookup by id
     * - update first name
     * - delete by id
     * - confirm deletion by searching again
     *
     * @throws Exception
     */
@Test
public void testCustomerManagementWorkflow() throws Exception {
    String customerLastName = UUID.randomUUID().toString();
    String customerId = null;
    // Create customer
    Result<Customer> createResult = requestBody("direct://CREATE_IN_BODY", new CustomerRequest().firstName("user").lastName(customerLastName).company("Apache").email("user@braintree.camel").website("http://user.braintree.camel"), Result.class);
    assertNotNull(createResult);
    assertTrue(createResult.isSuccess());
    assertNotNull(createResult.getTarget());
    assertNotNull(createResult.getTarget().getId());
    customerId = createResult.getTarget().getId();
    // Find customer by ID
    Customer customer1 = requestBody("direct://FIND_IN_BODY", customerId, Customer.class);
    assertNotNull(customer1);
    assertEquals("user", customer1.getFirstName());
    assertEquals(customerLastName, customer1.getLastName());
    assertEquals("Apache", customer1.getCompany());
    assertEquals("user@braintree.camel", customer1.getEmail());
    assertEquals("http://user.braintree.camel", customer1.getWebsite());
    // Update customer
    HashMap<String, Object> headers = new HashMap<>();
    headers.put("CamelBraintree.id", customerId);
    Result<Customer> updateResult = requestBodyAndHeaders("direct://UPDATE_IN_BODY", new CustomerRequest().firstName("user-mod"), headers, Result.class);
    assertNotNull(updateResult);
    assertTrue(updateResult.isSuccess());
    assertNotNull(updateResult.getTarget());
    assertEquals("user-mod", updateResult.getTarget().getFirstName());
    // Delete customer
    Result<Customer> customerResult = requestBody("direct://DELETE_IN_BODY", customerId, Result.class);
    assertNotNull(customerResult);
    assertTrue(customerResult.isSuccess());
    assertNull(customerResult.getTarget());
    // Check if customer has been deleted customer
    ResourceCollection<Customer> customers = requestBody("direct://SEARCH_IN_BODY", new CustomerSearchRequest().id().is(customerId), ResourceCollection.class);
    assertNotNull(customers);
    assertEquals(0, customers.getMaximumSize());
}
Also used : CustomerRequest(com.braintreegateway.CustomerRequest) Customer(com.braintreegateway.Customer) HashMap(java.util.HashMap) CustomerSearchRequest(com.braintreegateway.CustomerSearchRequest) Test(org.junit.Test)

Example 4 with CustomerRequest

use of com.braintreegateway.CustomerRequest in project camel by apache.

the class PaymentMethodGatewayIntegrationTest method doPostSetup.

@Override
protected void doPostSetup() throws Exception {
    this.gateway = getGateway();
    this.customer = gateway.customer().create(new CustomerRequest().firstName("user").lastName(UUID.randomUUID().toString())).getTarget();
    if (customer != null) {
        LOG.info("Customer created - id={}", this.customer.getId());
    }
}
Also used : CustomerRequest(com.braintreegateway.CustomerRequest)

Example 5 with CustomerRequest

use of com.braintreegateway.CustomerRequest in project camel by apache.

the class CustomerGatewayIntegrationTest method testWrongCustomerCreateRequest.

@Test
public void testWrongCustomerCreateRequest() throws Exception {
    // Create customer
    Result<Customer> createResult = requestBody("direct://CREATE_IN_BODY", new CustomerRequest().firstName("user").lastName(UUID.randomUUID().toString()).company("Apache").email("wrongEmail").website("http://user.braintree.camel"), Result.class);
    assertNotNull(createResult);
    assertFalse(createResult.isSuccess());
    final ValidationErrors errors = createResult.getErrors();
    assertNotNull(errors);
    assertNotNull(errors.getAllDeepValidationErrors());
    ValidationError invalidMailError = null;
    for (ValidationError error : errors.getAllDeepValidationErrors()) {
        if (error.getCode() == ValidationErrorCode.CUSTOMER_EMAIL_FORMAT_IS_INVALID) {
            invalidMailError = error;
            break;
        }
    }
    assertNotNull(invalidMailError);
}
Also used : CustomerRequest(com.braintreegateway.CustomerRequest) Customer(com.braintreegateway.Customer) ValidationErrors(com.braintreegateway.ValidationErrors) ValidationError(com.braintreegateway.ValidationError) Test(org.junit.Test)

Aggregations

CustomerRequest (com.braintreegateway.CustomerRequest)6 Customer (com.braintreegateway.Customer)3 Test (org.junit.Test)3 CreditCardAddressRequest (com.braintreegateway.CreditCardAddressRequest)1 CreditCardRequest (com.braintreegateway.CreditCardRequest)1 CustomerSearchRequest (com.braintreegateway.CustomerSearchRequest)1 ValidationError (com.braintreegateway.ValidationError)1 ValidationErrors (com.braintreegateway.ValidationErrors)1 AuthenticationException (com.braintreegateway.exceptions.AuthenticationException)1 AuthorizationException (com.braintreegateway.exceptions.AuthorizationException)1 ConfigurationException (com.braintreegateway.exceptions.ConfigurationException)1 DownForMaintenanceException (com.braintreegateway.exceptions.DownForMaintenanceException)1 ForgedQueryStringException (com.braintreegateway.exceptions.ForgedQueryStringException)1 InvalidChallengeException (com.braintreegateway.exceptions.InvalidChallengeException)1 InvalidSignatureException (com.braintreegateway.exceptions.InvalidSignatureException)1 NotFoundException (com.braintreegateway.exceptions.NotFoundException)1 ServerException (com.braintreegateway.exceptions.ServerException)1 TimeoutException (com.braintreegateway.exceptions.TimeoutException)1 TooManyRequestsException (com.braintreegateway.exceptions.TooManyRequestsException)1 UnexpectedException (com.braintreegateway.exceptions.UnexpectedException)1