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kN2Y1YTA5ZTJiYTk5NjUxMGZjNmExMTA3OTFjMjY0NTMyODQ3YjRiYjc3MDlkMmY4YzllY2ZlNzE2MTE1ZjNkMzRlZGRhMmFhOGU4NTA0NThiZTdiOWUzYTM0MDczZTFjNzgxZDFkYzY1ZTRjYzRiYTY3ZGE4NzE4ZjJjNDBmMGU5ZDZlNWU2Y2RiNzAyMzUyNDIyZGVkYmU4NWM4MDdlZTVkNDhkZjg0NGMwNGUwMzA1ZWM5ODJkNmIwMWRmMzg3MjZjZGRkMTVhMWI4YjI1YTdhMWVmNTI2MDUzYmYzYzFlYTBmYmM2MTI3MDMwNTcyNTU1MzQyYjA0ZmZjN2NjOTg4Y2Q2YjQ1M2JhMDQ1NmE3ODUzNDMzMmNmNDFiNWI0M2M5ODhmNmNkMDI2MjFlZDIwYzRhYjliMjU2YmM3ZDU1Yzg2OTBkZWZjNTVhYjA1NzdiYzQxZmY0MzAxNmE3OTRlNWVkMjRjNDc4ZDgzN2JhNDZhMDAwY2ExOGMwMDQ4MWEwMmUyZTcyZjIwYTE0N2MyMTUwMWI3Mjg0NWE0ZDY3YTFiYTEyYzI2MTljYzhkMGM5YzJhOWU1NDljYmY0ZDJlYTM5M2IxYTg5ZWQ4NjMxZWM4NmIwNDI0YzJkYzBjNDU=
EOF
@result = {"jdpay"=>{"version"=>"V2.0", "merchant"=>"22294531", "result"=>{"code"=>"000000", "desc"=>"成功"}, "orderId"=>"1029148637575787617463", "merchantName"=>"京东支付测试商户号", "amount"=>"1", "expireTime"=>"600", "tradeNum"=>"1486346954111", "qrCode"=>"https://h5pay.jd.com/code?c=616zlks7djfb1z", "sign"=>"C5Mn72q+w1ttkqsUSuhFwJjK8rpikxHkPHaJAXNBVvJGOLMYrSRkHTchACkAISiUzJ60ppWC4DnN6nfnbT5xyrK7kKmHuUivfGRGVnfucvZnV7eDS0Jv+7Np64P/ZyHUkesTDxb0+oDNilTaX82pV5Y2O0qmfs5Ft0LhpJ4Le/w="}}
end
def test_uniorder
stub_request(:post, JdPay::Service::UNIORDER_URL).to_return(body: @response_body)
params = {
tradeNum: '123456780',
tradeName: '测试商品',
amount: '1',
orderType: '0',
notifyUrl: 'http://making.dev/notify'
}
assert_equal @result, JdPay::Service.uniorder(params)
end
def test_query
stub_request(:post, JdPay::Service::QUERY_URL).to_return(body: @response_body)
params = {
tradeNum: '12345678',
tradeType: '1',
oTradeNum: "12345677",
}
assert_equal @result, JdPay::Service.query(params)
end
def test_refund
stub_request(:post, JdPay::Service::REFUND_URL).to_return(body: @response_body)
params = {
tradeNum: '12345678',
oTradeNum: "12345677",
amount: '1',
notifyUrl: 'http://making.dev/notify',
}
assert_equal @result, JdPay::Service.refund(params)
end
def test_pc_pay
html_str = "
"
params = {
tradeNum: '12345678',
tradeName: '测试商品',
amount: '1',
orderType: '0',
notifyUrl: 'http://making.dev/notify',
callbackUrl: 'http://frontend.com/return',
userId: "0000001",
tradeTime: '20170718101010'
}
assert_equal html_str, JdPay::Service.pc_pay(params)
end
def test_h5_pay
html_str = ""
params = {
tradeNum: '12345678',
tradeName: '测试商品',
amount: '1',
orderType: '0',
notifyUrl: 'http://making.dev/notify',
callbackUrl: 'http://frontend.com/return',
userId: "0000001",
tradeTime: '20170718101010'
}
assert_equal html_str, JdPay::Service.h5_pay(params)
end
def test_revoke
stub_request(:post, JdPay::Service::REVOKE_URL).to_return(body: @response_body)
params = {
tradeNum: '12345678',
oTradeNum: "12345677",
amount: '1',
}
assert_equal @result, JdPay::Service.revoke(params)
end
def test_notify_verify
assert_equal @result, JdPay::Service.notify_verify(@response_body)
end
def test_user_relation
xml_str = <<-EOF
V2.0
22294531
000000
成功
OWQxYWMyN2I1M2M1NmJhNThjNjc0YmY1ZWJlY2QyODU0YTc5NmQ3ZWQxMWU1NzE3MWQ0OTUwOGI5NzllYmE4ZjM1YzRiZjlmYWE1M2ZiYjVjYTg5YjA4NTdhMjg3NTBhNzQxM2ZjOWFlN2U3YTNlYzM5ZTI5OTBkZDczNzQ5MjhjM2U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