question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Test nonces don't work?

See original GitHub issue

not sure what I’m doing wrong here, but I’m in a sandbox environment using nonce='fake-valid-nonce' here:

    resp = braintree.Subscription.create({
        'payment_method_nonce': nonce,
        'plan_id': config.BRAINTREE_SUBSCRIPTION_PLAN_ID
    })

and keep getting the error “Payment method nonce is invalid.”

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
amandapougetcommented, Feb 15, 2018

I found the solution to my problem by using the nonce_for_new_payment_method available from this spec helper, included with the Braintree gem: https://github.com/braintree/braintree_ruby/blob/master/spec/integration/braintree/client_api/spec_helper.rb

Recording this here in case anyone else is writing a Ruby controller test involving a Braintree::Subscription.

Here is my working example:

describe 'POST #create (success)' do
    let(:organization) { create(:organization) }
    let(:valid_card) do
      {
        number: "4111111111111111",
        expiration_month: "11",
        expiration_year: "2099"
      }
    end
    let(:nonce) do
      organization.update(
        braintree_customer_id: Braintree::Customer.create(company: organization.name).customer.id
      )
      nonce_for_new_payment_method(
       credit_card: valid_card,
       client_token_options: {customer_id: organization.braintree_customer_id}
     )
    end

    describe 'in Braintree' do
      it 'creates a BraintreeSubscription tied to our organization' do
        params = { payment_method_nonce: nonce, plan_id: 'monthly-recurring' }
        post :create, params: params
.....
           // in Controller...
           nonce_from_the_client = params[:payment_method_nonce]
           result = Braintree::Subscription.create(
             payment_method_nonce: nonce_from_the_client,
             plan_id: params[:plan_id]
           )
           result.success? // ...is true

Recommend updating this documentation page, which errantly suggests that the fake valid nonces can be using for recurring billing: https://developers.braintreepayments.com/guides/recurring-billing/testing-go-live/ruby

1reaction
EvanHahncommented, Apr 17, 2017

Our fake nonces are effectively one-time-use, but subscription calls need a payment method that they can hit periodically.

Subscription.create() calls will only accept a nonce if it is directly tied to a payment method in your Vault. You can generate a nonce like this by using PaymentMethodNonce.create() and passing it a token from your Vault. For example:

nonce_create = braintree.PaymentMethodNonce.create('A_PAYMENT_METHOD_TOKEN')
nonce = nonce_create.payment_method_nonce.nonce

resp = braintree.Subscription.create({
    'payment_method_nonce': nonce,
    'plan_id': config.BRAINTREE_SUBSCRIPTION_PLAN_ID
})

This shouldn’t affect you in production because you’ll never use fake nonces.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Braintree: test nonces are resulting in Payment Method Nonce ...
I am passing the sandbox credentials when I make my initial connection but it seems like it is not honoring the fake nonce....
Read more >
Testing | Ruby - Braintree Developer Documentation
The following nonces are only for testing Visa cards. Liability shift status is generally agnostic to the card type and other card brands...
Read more >
Testing and Go Live | Node.js
Test values from the sandbox testing page will not work. This means that every test transaction that you allow to settle in your...
Read more >
Sandbox test payment not working - Square Developer
A quick reference of test credit card numbers and payment nonces you can use to make test payments with the Square Sandbox. I...
Read more >
What is a Nonce? - Cryptographic Nonce from SearchSecurity
Using the sequential nonce method guarantees that values are not repeated, ... Proof of work systems use nonce values to vary input to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found