使用现有地址创建新付款方式时出现Braintree错误-Java

时间:2018-08-30 08:24:14

标签: java braintree payment-method

我正在使用Java Braintree API,尝试使用现有地址向现有客户添加新的付款方式时遇到问题。

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

在result.message中,我具有“无法同时提供帐单地址和帐单地址ID”。但在请求中,我仅提供帐单地址ID,而不提供完整地址。 怎么了? 感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

这就是我使用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);
    }
}

上面的代码作为测试代码对我来说很好用。

希望对您有帮助。

答案 1 :(得分:0)

我已经省略了我正在使用沙盒帐户和“ fake-valid-visa-nonce”的情况。我已经联系Braintree支持人员,他们答复说,在伪随机数中已经有地址,这就是为什么当我尝试使用现有地址并且那个伪随机数时出现错误。