支付宝移动支付中,服务器需要根据支付宝发来的异步 POST 请求来处理具体的业务逻辑。
我开发的应用中,Android 客户端已经成功收到支付宝的同步消息,服务器也受到了支付宝的异步 POST,但无法执行该 POST 路径对应的方法,也就无法处理请求中的参数,无法处理任何业务逻辑。
根据我测试的写法,当收到 POST 时候,我会把这个 POST 中的参数在数据库里存成一个账单,然而服务器的响应如下:
Started POST "/api/users/34341/bills" for 110.75.152.2 at 2015-06-30 11:57:52 +0800
Processing by Api::BillsController#create as HTML
Parameters: {"discount"=>"0.00", "payment_type"=>"1", "subject"=>"xxx", "trade_no"=>"2015063000001001", "buyer_email"=>"[email protected]", "gmt_create"=>"2015-06-30 11:57:51", "notify_type"=>"trade_status_sync", "quantity"=>"1", "out_trade_no"=>"063011574", "seller_id"=>"208851211", "notify_time"=>"2015-06-30 11:57:52", "body"=>"测试", "trade_status"=>"TRADE_FINISHED", "is_total_fee_adjust"=>"N", "total_fee"=>"0.01", "gmt_payment"=>"2015-06-30 11:57:52", "seller_email"=>"[email protected]", "gmt_close"=>"2015-06-30 11:57:51", "price"=>"0.01", "buyer_id"=>"208800251", "notify_id"=>"001eb177a9475a8d", "use_coupon"=>"N", "sign_type"=>"RSA", "sign"=>"ahpwrghaiuhgaiouerghiauer", "user_id"=>"34341"}
Completed 200 OK in 1ms (Views: 0.2ms | ActiveRecord: 0.0ms)
以上,收到 POST 之后直接相应 200,并没有任何数据操作,经检查数据库里也确实没有账单。
但如果我自己按照参数格式强行 POST 这个接口,就可以正常完成业务逻辑。
Started POST "/api/users/27/bills" for 127.0.0.1 at 2015-06-30 10:19:22 +0800
Processing by Api::BillsController#create as */*
Parameters: {"discount"=>"0.00", "payment_type"=>"1", "subject"=>"xxx", "trade_no"=>"2015063000001001", "buyer_email"=>"[email protected]", "gmt_create"=>"2015-06-30 11:57:51", "notify_type"=>"trade_status_sync", "quantity"=>"1", "out_trade_no"=>"063011574", "seller_id"=>"208851211", "notify_time"=>"2015-06-30 11:57:52", "body"=>"测试", "trade_status"=>"TRADE_FINISHED", "is_total_fee_adjust"=>"N", "total_fee"=>"0.01", "gmt_payment"=>"2015-06-30 11:57:52", "seller_email"=>"[email protected]", "gmt_close"=>"2015-06-30 11:57:51", "price"=>"0.01", "buyer_id"=>"208800251", "notify_id"=>"001eb177a9475a8d", "use_coupon"=>"N", "sign_type"=>"RSA", "sign"=>"ahpwrghaiuhgaiouerghiauer", "user_id"=>"34341"}
Unpermitted parameters: notify_type, seller_email, gmt_close
(0.1ms) begin transaction
SQL (1.0ms) INSERT INTO "bills" ......
(0.8ms) commit transaction
Completed 200 OK in 31ms (Views: 0.3ms | ActiveRecord: 3.1ms)
在自己测试中,发现了一个服务器响应中一个值得注意的地方:
# 处理支付宝异步POST
Processing by Api::BillsController#create as HTML
# 对应的Nginx Log
"POST /api/users/34341/bills HTTP/1.1" 200 115 "-" "Mozilla/4.0" "-"
# 处理自己的POST测试
Processing by Api::BillsController#create as */*
# 对应的Nginx Log
"POST /api/users/7/bills HTTP/1.1" 200 93 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36" "-"
根据回帖中的方法进行调试,Bill.create! bill_params
即可正常运行。