ruby-on-railsrubybraintreebraintree-rails

NoMethodError - undefined method `split' for #<Hash:...:


I'm creating a webhook notification with:

signature, payload = Braintree::WebhookTesting.sample_notification(
  Braintree::WebhookNotification::Kind::SubMerchantAccountApproved,
  rand(10000)
)

And then parsing with:

 @message = Braintree::WebhookNotification.parse(signature, payload)

Signature and Payload responses:

{:bt_signature=&gt;&quot;gcsg95j47yvzpgrr|61350cd9c99cbac6a7905479a5fa061976114e51&quot;, :bt_payload=&gt;&quot;ICAgICAgICA8bm90aWZpY2F0aW9uPgogICAgICAgICAgPHRpbWVzdGFtcCB0
 eXBlPSJkYXRldGltZSI+MjAxNS0wOS0wNFQwMjo1Nzo0NFo8L3RpbWVzdGFt
 cD4KICAgICAgICAgIDxraW5kPnN1Yl9tZXJjaGFudF9hY2NvdW50X2FwcHJv
 dmVkPC9raW5kPgogICAgICAgICAgPHN1YmplY3Q+CiAgICAgICAgICAgICAg
 ICAgICAgPG1lcmNoYW50X2FjY291bnQ+CiAgICAgICAgICA8aWQ+MzEzNDwv
 aWQ+CiAgICAgICAgICA8bWFzdGVyX21lcmNoYW50X2FjY291bnQ+CiAgICAg
 ICAgICAgIDxpZD5tYXN0ZXJfbWFfZm9yXzMxMzQ8L2lkPgogICAgICAgICAg
 ICA8c3RhdHVzPmFjdGl2ZTwvc3RhdHVzPgogICAgICAgICAgPC9tYXN0ZXJf
 bWVyY2hhbnRfYWNjb3VudD4KICAgICAgICAgIDxzdGF0dXM+YWN0aXZlPC9z
 dGF0dXM+CiAgICAgICAgPC9tZXJjaGFudF9hY2NvdW50PgoKICAgICAgICAg
 IDwvc3ViamVjdD4KICAgICAgICA8L25vdGlmaWNhdGlvbj4K
 &quot;}

I keep getting the error:

NoMethodError - undefined method `split' for #<Hash:0x007fdb043e7b50>:

App backtrace
-------------

 -  () Users/johnmolina/Documents/Rails/Nyvur/app/controllers/webhooks_controller.rb:30:in `handle'
 -  () Users/johnmolina/Documents/Rails/Nyvur/bin/rails:8:in `<top (required)>'
 -  () Users/johnmolina/Documents/Rails/Nyvur/bin/spring:16:in `<top (required)>'

I couldn't find much in the source code, and was wondering if someone else has encountered this issue also?

Here's the Webhooks Controller.


Solution

  • The method Braintree::WebhookTesting.sample_notification returns a hash, while the signature, payload = syntax expects an array.

    You need to extract the signature and payload from the returned hash using their keys:

    sample_notification = Braintree::WebhookTesting.sample_notification(
      Braintree::WebhookNotification::Kind::SubMerchantAccountApproved,
      rand(10000)
    )
    
    signature = sample_notification[:bt_signature]
    payload = sample_notification[:bt_payload]