0

I'm using Java Braintree API and I'm having issues when I try to add a new payment method to an existing customer using an existing address.

PaymentMethodRequest request = new PaymentMethodRequest()
                              .customerId(someId)
                              .paymentMethodNonce(paymentMethodNonce)
                              .billingAddressId("ny")
                              .options()
                              .failOnDuplicatePaymentMethod(true)
                              .done();
Result<? extends PaymentMethod> result = gateway.paymentMethod().create(request);

In result.message I have "Cannot provide both a billing address and a billing address ID." but in the request I only provide the billing address Id and not the full address. What's wrong? Thanks for your help.

guess85
  • 3
  • 3

2 Answers2

0

This is the way, how I make payment with Braintree API:

@Test
public void payout() {
    // credit card details used here are only valid for test purpose
    String userId = BraintreePayments.USER_ID;
    String cardNumber = "4111111111111111";
    String ccv = "100";
    String expirationDate = "05/2010";
    BigDecimal amount = new BigDecimal("2.02");

    // make a credit card payout
    Result<Transaction> transactionResult = createTransaction(userId, cardNumber, ccv, expirationDate, amount);
    BraintreePayments.showResult(transactionResult);
    Assert.assertTrue(transactionResult.isSuccess());
}


/**
 * Makes a payout via Braintree Payment API.
 *
 * @param userId User identifier number.
 * @param cardNumber Credit card number.
 * @param ccv Credit card's CCV code.
 * @param expirationDate Expiration date of the credit card.
 * @param amount Amount to deposit.
 * @return A tran
 */
private Result<Transaction> createTransaction(final String userId,
                                              final String cardNumber,
                                              final String ccv,
                                              final String expirationDate,
                                              final BigDecimal amount) {
    String customerId = BraintreePayments.customerIdGenerator(userId);
    String orderId = UUID.randomUUID().toString();
    System.out.println(String.format("Order ID: %s", orderId));

    createCustomerIfNotExist(userId, customerId);

    // make a credit card payout
    TransactionRequest transactionRequest = new TransactionRequest()
            .customerId(customerId)
            .orderId(orderId)
            .creditCard().number(cardNumber).cvv(ccv).expirationDate(expirationDate).cardholderName(userId).done()
            .amount(amount)
            .options().submitForSettlement(true)
            .done();

    Result<Transaction> transactionResult = GATEWAY.transaction().sale(transactionRequest);

    // save credit card if the transaction was success
    if (transactionResult.isSuccess()) {
        CreditCardRequest creditCardRequest = new CreditCardRequest().
                customerId(customerId).
                number(cardNumber).
                cvv(ccv).
                expirationDate(expirationDate).
                cardholderName(userId);

        GATEWAY.creditCard().create(creditCardRequest);
    }

    return transactionResult;
}




/**
 * Create customer if it does not exist.
 *
 * @param userId User Identification number.
 * @param customerId Braintree customer ID.
 */
private void createCustomerIfNotExist(String userId, String customerId) {
    try {
        BraintreePayments.findCustomer(customerId);
    } catch (com.braintreegateway.exceptions.NotFoundException e ) {
        System.out.println(String.format("User %s does not exist. Let's create it.", userId));

        CustomerRequest request = BraintreePayments.createCustomerRequest(userId);
        BraintreeGateway gateway = BraintreePayments.initApi();
        Result<Customer> result = gateway.customer().create(request);
        BraintreePayments.showResult(result);
    }
}

The code above works fine for me as a test code.

Hope that helps you.

zappee
  • 20,148
  • 14
  • 73
  • 129
  • Thanks for your reply. I haven't any problem in creating an user and add a payment method. I have issues if I want to specify an existing billing address for that payment method. If I remove .billingAddressId("ny") and instead I use `.billingAddress() ... .done()` I haven't any problems. But this create a new billing address for every transaction. – guess85 Aug 30 '18 at 10:15
  • Have you asked the Braintree souuport team? They are quite responsive. – zappee Aug 30 '18 at 11:35
  • I guees that at fist you need to create a new billing address before the financial transaction and then use this billing address id in the payment. – zappee Aug 30 '18 at 11:37
  • I've already created a billing address ("ny" is the id) before the creation of the new payment method. I'll try to contact their support. – guess85 Aug 30 '18 at 11:47
  • When you search for the billing address "ny" with the API then get any result? – zappee Aug 30 '18 at 12:02
0

I've omitted that I'm using Sandbox account and a 'fake-valid-visa-nonce'. I've contacted Braintree support, they reply that in the fake nonce there is already and address, that's why when I try to use an existing address and that fake nonce I got the error.

guess85
  • 3
  • 3