我需要遍历Square事务的哈希,并将每个记录连接到MySQL查询的结果。有没有办法将Square交易哈希中的记录放入更像表的数组中?
几周前,我发布了一个类似的问题(How to parse Square response nested key values in Ruby 2.2.10),其中哈希只有一个记录,但是现在我需要遍历所有事务。请注意,哈希还可以包含退款交易,我想将其分开解析。而且某些交易将缺少reference_id字段。
{:transactions=>[
{:id=>"sacfx8eqLYsBQom7G9kV7gS1K4lbnMG83w3U0Gpzu1cCJROfZvRJqLhO",
:location_id=>"CBASEJJZMYbNZW8F1bbRjRuPQ7Q",
:created_at=>"2019-04-10T16:38:57Z",
:tenders=>[{
:id=>"eeb92f2a-379b-55c6-439b-f0f5c44925c8",
:location_id=>"CBASEJJZMYbNZW8F1bbRjRuPQ7Q",
:transaction_id=>"sacfx8eqLYsBQom7G9kV7gS1K4lbnMG83w3U0Gpzu1cCJROfZvRJqLhO",
:created_at=>"2019-04-10T16:38:57Z",
:note=>"Online Transaction",
:amount_money=>{:amount=>65000, :currency=>"USD"},
:processing_fee_money=>{:amount=>1915, :currency=>"USD"},
:type=>"CARD",
:card_details=>{:status=>"CAPTURED",
:card=>{:card_brand=>"VISA",
:last_4=>"1111",
:fingerprint=>"50b16672bf4472e9058524e3c0a060faab196bc3e43f9c693a870c7e8b6da626"
},
:entry_method=>"KEYED"
}
}],
:reference_id=>"796",
:product=>"EXTERNAL_API"
},
{:id=>"mvMhdOrfBdXQvE0S6EPFoeHf7Fm8duSJgsk4bZyt6APQTa2PGkix6LhO",
:location_id=>"CBASEJJZMYbNZW8F1bbRjRuPQ7Q",
:created_at=>"2019-04-10T01:22:16Z",
:tenders=>[{
:id=>"693c42d4-5e16-52a3-5594-5e8dd4ff9f95",
:location_id=>"CBASEJJZMYbNZW8F1bbRjRuPQ7Q",
:transaction_id=>"mvMhdOrfBdXQvE0S6EPFoeHf7Fm8duSJgsk4bZyt6APQTa2PGkix6LhO",
:created_at=>"2019-04-10T01:22:16Z",
:note=>"Online Transaction",
:amount_money=>{:amount=>65000, :currency=>"USD"},
:processing_fee_money=>{:amount=>1915, :currency=>"USD"},
:type=>"CARD",
:card_details=>{:status=>"CAPTURED",
:card=>{:card_brand=>"VISA",
:last_4=>"1111",
:fingerprint=>"50b16672bf4472e9058524e3c0a060faab196bc3e43f9c693a870c7e8b6da626"
},
:entry_method=>"KEYED"
}
}],
:product=>"EXTERNAL_API"
},
...
],
:refunds=>[... refunds here ...]
}
我将回车符放回去,以便于查看键/值。
另外,Square在其文档(https://docs.connect.squareup.com/api/connect/v2/?q=reference_id#endpoint-transactions-listtransactions)中的示例显示了带引号的键,但是对Ruby的实际响应与我上面显示的一样,其中所有键只是冒号键名称-如:id >