东东prefer

erlang中http请求

1..app文件中

{application,backend,[
  {description,"ebank backend"},
  {vsn,1.0},
  {modules,[]},
  {applications,[kernel,stdlib,yaws]},
  {registered,[backend]},
  {env,[{env,development}]},
  {mod,backend_bootstrap},
  {controllers,[    {{"backend","mbu"},{backend, mbu},[{decrypt, false}, {verify, false}]}
               ]},
  {plugins,[]}]}.

2.erl文件中
handle_xmlResult(Xml_Res)->
    {dict,_,_,_,_,_,_,_,Context} = Xml_Res,
    io:format("==Xml_Res==========~p~n",[Xml_Res]),
    {{[],[],[],[],[],[],[],[],[],[Context1],[],[],[],[],[],[]}} = Context,
    Context2 = Context1 -- ["<?xml version"],
    Context3 = "<?xml version = " ++ Context2,
    Context3.

%%人到人被扫MBU004 C2B附加处理
mbu(Arg) ->
    Xml_Res = ewp_params:from_yaws_arg(Arg),
    try
        Context3 = handle_xmlResult(Xml_Res),
        Msg1 = list_to_binary([Context3]) ,
        log4erl:info(wailian_client, Msg1),
        Xml_ResUT0 = backend_msg:utf8_to_gbk(Context3),
        Xml_ResUT = backend_msg:gbk_to_utf8(Xml_ResUT0),
        Rx = erlang:binary_to_list(Xml_ResUT),
        Res = xml_eng:xml_to_term(Rx),
        Xml_ResBody = proplists:get_value('Body', proplists:get_value('Agw', Res)),
        ReqType = proplists:get_value('reqType', Xml_ResBody,undefined),
        case ReqType of
            "0230000903" ->
                mbu004(Xml_ResBody);
            "0250000903" ->
                mbu005(Xml_ResBody);
          %%  "0000000903" ->  mbu006(Xml_ResBody);
        _ ->
             no_result              end
    catch
        _A:_B ->
            ?ewp_err("=======REQUEST_SAOF_FAIL===========================~p~n~p~n",[_A,_B]),
            throw(?REQUEST_SAOF_FAIL)
    end.

mbu004(Xml_ResBody) ->
        QrNo = proplists:get_value('qrNo', Xml_ResBody),
        %%Xml_lists = proplists:get_value('currencyCode', Xml_ResBody),
        TxnAmt0 = proplists:get_value('txnAmt', Xml_ResBody),
        TxnAmt = list_to_integer(TxnAmt0)/100,
        VOUCHERNUM = proplists:get_value('voucherNum', Xml_ResBody),
        backend_db:update("update MY_PEOPLECODE_STATUSLOGS set money = :1, status = : 2, VOUCHERNUM = : 3 where qrNo = : 4 and tran_time = to_date(sysdate) ",[TxnAmt, "01", VOUCHERNUM, QrNo]),
        Res = "<?xml version='1.0' encoding='utf-8'?><Agw><Head direction='response'><Trade>MBU004</Trade><Channel>05095</Channel><KeyGenerator>001234</KeyGenerator><ErrorCode>00</ErrorCode><ErrorMsg>成功</ErrorMsg></Head><Body><version>1.0.0</version><reqType>0230000903</reqType><respCode></respCode><respMsg></respMsg></Body></Agw>",
        ewp_render_util:render_xml(Res).

mbu005(Xml_ResBody) ->
        OrigReqType  = proplists:get_value('origReqType', Xml_ResBody),
        OrigRespCode = proplists:get_value('origRespCode', Xml_ResBody),
        OrigRespMsg = proplists:get_value('origRespMsg', Xml_ResBody),
        MerName = proplists:get_value('merName', Xml_ResBody),
        VoucherNum = proplists:get_value('voucherNum', Xml_ResBody),
        QrNo = proplists:get_value('qrNo', Xml_ResBody),
        %%交易
        case OrigReqType of
            "0310000903" ->  %%消费交易
                      case OrigRespCode of
                         "00" ->
                            backend_db:update("update MY_PEOPLECODE_STATUSLOGS set merName = : 1, VoucherNum  = : 2 , status = : 3, OrigRespCode  = : 4, OrigRespMsg = : 5, OrigReqType = : 6 where qrNo = : 7 and tran_time = to_date(sysdate)", [MerName,VoucherNum,"05",OrigRespCode,OrigRespMsg,OrigReqType,QrNo]);
                         _ ->
                            backend_db:update("update MY_PEOPLECODE_STATUSLOGS set merName = : 1, VoucherNum  = : 2 , status = : 3, OrigRespCode  = : 4, OrigRespMsg = : 5, OrigReqType = : 6 where qrNo = : 7 and tran_time = to_date(sysdate)", [MerName,VoucherNum,"09",OrigRespCode,OrigRespMsg,OrigReqType,QrNo])
                      end;
            _ ->    %%消费冲正交易0320000903、消费撤销交易0330000903、退货交易0340000903等
                   backend_db:update("update MY_PEOPLECODE_STATUSLOGS set merName = : 1, VoucherNum  = : 2 , status = : 3, OrigRespCode  = : 4, OrigRespMsg = : 5, OrigReqType = : 6 where qrNo = : 7 and tran_time = to_date(sysdate)", [MerName,VoucherNum,"09",OrigRespCode,OrigRespMsg,OrigReqType,QrNo])
        end,
        %%取值如果是报错,存数据库应该是错误码
        Res = "<?xml version='1.0' encoding='utf-8'?><Agw><Head direction='response'><Trade>MBU005</Trade><Channel>05095</Channel><KeyGenerator>001234</KeyGenerator><ErrorCode>00</ErrorCode><ErrorMsg>成功</ErrorMsg></Head><Body><version>1.0.0</version><reqType>0250000903</reqType><respCode></respCode><respMsg></respMsg></Body></Agw>",
        ewp_render_util:render_xml(Res).

%% Created: 2017-3-3
%% Description: http请求对外接口测试
test_interface(RequestBody,Interface) ->
    Host = "127.0.0.1",
    Port = "4003",
    try
          Url = "http://" ++ Host ++ ":" ++ Port ++ "/backend/" ++ Interface,
          Request = {Url, "", "", RequestBody},
          ewp_http_client:request({post, Request, [{timeout, 75000}],[{body_format, binary}]}),
          ok
    catch
      _:_->
       ?ewp_err("request error:~p~n",["Error to delete file."])
    end.

%% Created: 2017-3-3
%% Description: 异步http请求我方接口,测试方法 backend_controller:test_mbu().
test_mbu()->
    %%QrNoTest= "123456",   %%lists:flatten(io_lib:format("~6.10.0B",[random:uniform(999999)])),
    %%RequestBody1 = "<?xml version='1.0' encoding='utf-8'?><Agw><Head direction='response'><Trade>MBU004</Trade><Channel>05095</Channel><KeyGenerator>001234</KeyGenerator><ErrorCode>0000</ErrorCode><ErrorMsg>交易成功</ErrorMsg></Head><Body><version>1.0.0</version><reqType>0230000903</reqType><qrNo>" ++ QrNoTest ++ "</qrNo><currencyCode></currencyCode><txnAmt>299</txnAmt><payerInfo></payerInfo><transAddnInfo></transAddnInfo><reqReserved></reqReserved><voucherNum></voucherNum><upReserved></upReserved><encryptCertId></encryptCertId></Body></Agw>",
    RequestBody2 = "<?xml version='1.0' encoding='UTF-8'?><Agw><Head direction='request'><Trade>MBU005</Trade><ErrorMsg></ErrorMsg></Head><Body><version>1.0.0</version><signature>rrOGNTufHIwmo15Anh94xI7EZ UMMpp/mr6w4Q72ugAYurVp/xgKXzc9SZquYFMYNvZfhGtQ IhknD OW/PV5kbEHPhIgkWipH2u5nMQpQQV5YcpYtCQqvulRFb/EAsd6ObH2oQQXfVduQbihZYm6VzAHJYrvD1cnh3r9/14RBWU6jBCOTCv0NBOAluQxXFvLxJtIVqLzFWqUkngKoY4RJG2b7lqI2fkQCFDmRnOi 7THZoQkEHp4LuI/JHtCFcj/E9 5GU8KZYj0RWE8fmBBb9ner/6lQTUTJ5yL0KVobVdjyNWb1k1nnLEna542W6lZoOHCSXBbCJK6z7R3yigUQ</signature><certId>68759585097</certId><reqType>0250000903</reqType><qrNo>6222806305983432309</qrNo><currencyCode>156</currencyCode><txnAmt>30000</txnAmt><origRespCode>61</origRespCode><origRespMsg>输入的卡号无效,请确认后输入</origRespMsg><merId>777290058135880</merId><merCatCode>5811</merCatCode><merName>商户名称</merName><termId>49000002</termId><voucherNum>20170314381647956089</voucherNum><settleKey></settleKey><settleDate></settleDate><reqReserved>01</reqReserved></Body></Agw>",
    _A = test_interface(RequestBody2,"mbu").
    %%RequestBody2 = "<?xml version='1.0' encoding='utf-8'?><Agw><Head direction='response'><Trade>MBU005</Trade><Channel>05095</Channel><KeyGenerator>001234</KeyGenerator><ErrorCode>0000</ErrorCode><ErrorMsg>交易成功</ErrorMsg></Head><Body><version>1.0.0</version><reqType>0250000903</reqType><origReqType></origReqType><qrNo>" ++ QrNoTest ++ "</qrNo><currencyCode></currencyCode><txnAmt></txnAmt><origRespCode></origRespCode><origRespMsg></origRespMsg><merId></merId><merCatCode></merCatCode>okname<merName></merName><termId></termId><voucherNum>123456</voucherNum><settleKey></settleKey><settleDate></settleDate><reqReserved></reqReserved></Body></Agw>",
    %%B = test_interface(RequestBody2,"mbu"),
    %%io:format("B=============~p~n",[B]).

posted on 2017-03-29 08:31  东东prefer  阅读(1830)  评论(0编辑  收藏  举报

导航