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

Braintree error in creating new payment method with existing address - Java

我正在使用 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 中我有 "Cannot provide both a billing address and a billing address ID." 但在请求中我只提供了账单地址 ID 而不是完整地址。 怎么了? 感谢您的帮助。

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

上面的代码作为测试代码对我来说工作正常。

希望对你有帮助。

我忽略了我正在使用 Sandbox 帐户和 'fake-valid-visa-nonce'。我已经联系了 Braintree 支持,他们回复说在假随机数中已经有了地址,这就是为什么当我尝试使用现有地址和那个假随机数时我得到了错误。