将Braintree PaymentMethod与地址相关联

时间:2016-06-22 16:05:58

标签: ruby braintree

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有效,但我真的想指定一个。

1 个答案:

答案 0 :(得分:2)

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

底线:目前,您无法使用test nonce测试billing_address_id,但它可以与a nonce generated for the sandbox from a Drop-in or Custom Integration at the client side一起使用。

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