将 Braintree PaymentMethod 与地址相关联

Associate Braintree PaymentMethod with an Address

require 'braintree'

# set credentials
Braintree::Configuration.merchant_id = 'XXX'
Braintree::Configuration.public_key = 'YYY'
Braintree::Configuration.private_key = 'ZZZ'

Braintree::Configuration.environment = :sandbox

# see the raw messages going to and from the Braintree server
Braintree::Configuration.logger = Logger.new(STDERR)
Braintree::Configuration.logger.level = Logger::DEBUG


customer = Braintree::Customer.create!(first_name: 'John', last_name: 'Doe')

begin
  address = Braintree::Address.create!(customer_id: customer.id, locality: 'London')
  pmethod = Braintree::PaymentMethod.create(customer_id: customer.id, billing_address_id: address.id, payment_method_nonce: 'fake-valid-visa-nonce')
  p pmethod
ensure
  # always delete the Customer in order not to leave much rubbish
  # behind the testing session
  Braintree::Customer.delete(customer.id)
end

我创造了一个 Customer,而不是为他创造了一个 Address。当我尝试创建 PaymentMethod,使用代表信用卡的随机数并提供帐单地址(通过先前保存的 Address 的 ID)时,PaymentMethod 不是保存并返回错误结果。 p pmethod 行打印

#<Braintree::ErrorResult params:{...} errors:<credit_card:[(91701) Cannot provide both a billing address and a billing address ID.]>>

错误消息没有意义,因为我只提供了账单地址 ID。我还检查过 SDK 没有,例如伪造一个空地址并将其与我代码中提供的 PaymentMethod 数据一起发送到服务器。

创建一个没有账单地址的 PaymentMethod 可行,但我真的想指定一个。

完全披露:我在 Braintree 工作。如果您有任何其他问题,请随时联系 support.

底线:此时,您无法使用 test nonce, but it will work with a nonce generated for the sandbox from a Drop-in or Custom Integration at the client side.

测试 billing_address_id

这是测试 API 本身的限制。当您提交其中一个测试随机数时,它会预先填充各种检查通常需要的数据。这些预先填充的字段之一是 billing_address=>zip_code 的字段,如果您使用您正在使用的假随机数创建普通付款方式,您可以看到它。 billing_address 可以用您想要的任何测试值覆盖,但您不能撤消它或将其清零。