First, the payment flowchart
Second, process analysis
1. Page Ajax Access backstage (servlet or Resetful interface), send payment parameters to background
2. Back to front-end Ajax after the background is organized in clear and dark text by parameters
3. Front-end Ajax gets the plaintext and dark text of the background organization, as a parameter to access the payment gateway
4. After the successful payment, the light assembly visits the callback address set, the callback address in the verification, through the processing of orders to organize plaintext and ciphertext reply Everbright, payment completed
Third, the Code
1.view Layer
(1) Ajax access to backend resources
function Pay (Restfulurl, Formdata) {$.get (Requesturl+Formdata, function (data, status) {if(Data.status = =-1) {alert (data.msg); return; } Else if(Data.status = = 1) //window.open (payurl);//Open Form QQ Browser cannot open, the form of href can openWindow.location.href = data.msg.payurl+ "? Plain= "+data.msg.plain+" &signature= "+data.msg.signature; }, "JSON" );});
3.service Layer (restful)
(1) Payservice.java
1 Public classPayservice {2 3 /**4 * Send everbright Payment request5 */6 PublicString Pay (map<string, string> parammap)throwsException {7 8StringBuilder SB =NewStringBuilder ("");9String split = "~|~";TenSb.append ("merchantid=" +config.merchantid+split);//Merchant Code OneSb.append ("identitytype=2" +split);//user identity type fixed 2, user ID ASb.append ("mercifid=" +uuid.randomuuid (). toString () +split);//customer's unique identification in the system (Customer ID), Everbright to record the user's bank card information -Sb.append ("paytype=01" +split);//Payment Type 01 online payment (fixed value) -Sb.append ("merchantseqno=" +order_id+split);//Merchant Order Number theSb.append ("merchantdatetime=" +NewDate () +split);//Merchant Time -Sb.append ("transamount=" +payamount+split);//Transaction amount Unit: Yuan -Sb.append ("terminaltype=0" +split);//Terminal identification Type 0 imei,1 mac,2 UUID (for iOS system), 3 Other -Sb.append ("Terminalid=web" +split);//Terminal number +Sb.append ("mersecname=" +split);//level two merchant name, test must send Chinese, production can be empty -Sb.append ("productcatalog=57" +split);//57: Integrated Service (fixed value) +Sb.append ("merproduct=" + "+split");//Product Name ASb.append ("merchanturl=" callbackurl+split);//used to notify the merchant in the background, respond with servlet atSb.append ("merchanturl1=" +successurl+split);//used for background notification of merchant failure or by default, directs client back to Merchant page, responds with servlet - //sb.append ("Userip", "127.0.0.1"); //payment address, 127.0.0.1 not open in browser when testing -Sb.append ("msgext=" +parammap.get ("Biztypename"));//Additional Information - -String plain =sb.tostring (); -String signature =Cebmerchantsignverify.merchantsigndata_aba (plain); in -Sb.delete (0, Sb.length ()); toSb.append ("{"); +Sb.append ("\" payurl\ ": \" "+cebpayurl+" \ ","); -Sb.append ("\" plain\ ": \" "+plain+" \ ","); theSb.append ("\" signature\ ": \" "+signature+" \ ""); *Sb.append ("}"); $ Panax Notoginseng return NewJsonutil.tojson (Cebbean (1, sb.tostring (), "test")); - } the}
4. Callback Processing
(1) Callback.java
@Path ("/payback") public class Cebbackservice {@ContextHttpServletRequest req; @ContextHttpServletResponse resp;@ Post@path ("/cebback") public string Cebback (@FormParam ("Plain") string Plain, @FormParam ("Signature") string Signature {try {connection = jdbcutil.getconnection (); Connection.setautocommit (false);//response.setcharacterencoding ("GBK ");//Mobile get utf-8,pc end post gbk;//Check bank information Boolean flag = Cebmerchantsignverify.merchantverifypaygate_aba (signature, Plain); if (flag) {//split received plain, extract required field response Everbright string[] datas = Plain.split ("~\\|~"); map<string, string> map = new hashmap<string, string> (); String [] temp = new String[2];for (int i = 0; i < datas.length; i++) {System.out.println (datas[i]); temp = DATAS[I].SPL It ("="), if (temp.length = = 1) map.put (temp[0], ""); Elsemap.put (Temp[0], temp[1]);} Map.put ("signature", signature); The organization responds to Everbright's information stringbuffer SF = new StringBuffer (); Sf.append ("Merchantid=" +map.get ("Merchantid") + "~|~");//Merchant Code Char 12sf.append ("orderid=" +map.get ("orderId") + "~|~ ");//Order number char 30sf.append (" Transdatetime= "+map.get (" transdatetime ") +" ~|~ ");//Trading Time Char 14 order initiated time Sf.append (" Procstatus=1~|~ ");//Processing status Char 11: Processing Success Sf.append (" merchanturl2= "+config.successurl+"? orderid= "+map.get (" OrderId ")) ///Merchant Url2char 256 merchant successfully receives payment order, merchant Display page String plain = sf.tostring (); String signature = Cebmerchantsignverify.merchantsigndata_aba (plain); Resp.reset (); PrintWriter out = Resp.getwriter (); Resp.getwriter (). Print ("plain=" + Plain); Resp.getwriter (). Print ("\ n"); Resp.getwriter (). Print ("signature=" + Signature); Resp.getwriter (). Flush (); Resp.getwriter (). Close (); return "Everbright Callback succeeded!";} Else{return "Not Everbright callback this method";}} catch (Exception e) {e.printstacktrace (); try {if (connection! = null) connection.rollback (); log (E.getmessage ());} catch (SQLException E1) {E1.printstacktrace ();}} Finally{jdbcutil.close (connection);}} /** * After successful response to Everbright callback processing (form) */@GET @path ("/paysuccess") public String cebpaysuccess () {try {resp.sendredirect ("cebpaysuccesS.html?orderid= "+req.getparametermap (). Get (" Plain "). Get (" orderId ")); catch (IOException e) {e.printstacktrace ();} Return "Payment succeeded!";}
(2) servlet
Callback.java
1 Public voidService (HttpServletRequest request, httpservletresponse response)2 throwsservletexception, IOException {3Connection conn =NULL;4 Try {5conn =jdbcutil.getconn ();6Conn.setautocommit (false);7String plain = Request.getparameter ("plain");8String signature = request.getparameter ("signature");9 Ten //Check bank information One BooleanFlag =Cebmerchantsignverify.merchantverifypaygate_aba (signature, plain); A if(flag) { - //split the received plain, extract the required field response to Everbright -map<string, string> map = Cebutil.getparam (plain, "~\\|~"); theMap.put ("signature", signature); - - //Organization Response Information -StringBuffer SF =NewStringBuffer (); +Sf.append ("merchantid=" +map.get ("Merchantid") + "~|~");//Merchant Code Char -Sf.append ("orderid=" +map.get ("orderId") + "~|~");//Order number Char +Sf.append ("Transdatetime=" +map.get ("transdatetime") + "~|~");//Trading Time Char 14 time of order initiation ASf.append ("procstatus=1~|~");//processing Status Char 1 1: Processing succeeded atSf.append ("merchanturl2=" +cebsuccessurl);//Merchant URL2 Char 256 merchant successfully receives payment order, merchant display page with servlet response - //sf.append ("merchanturl2=" +cebsuccessurl/restful/callback/cebsuccess ");//Error: Java.lang.IllegalArgumentException:Illegal character in Query -String plain =sf.tostring (); -String signature =Cebmerchantsignverify.merchantsigndata_aba (plain); - - //response to Everbright payment callback in //Response.reset (); -PrintWriter out =Response.getwriter (); toOut.print ("plain=" +resplain); +Out.print ("\ n"); -Out.print ("signature=" +ressignature); the Out.flush (); * out.close (); $ //return;//write or do not write, Everbright payment can receive the correct responsePanax Notoginseng}Else{ -SYSTEM.OUT.PRINTLN ("Non-Everbright calls this method"); the return; + } A}Catch(Exception e) { the Try { + if(Conn! =NULL) - Conn.rollback (); $}Catch(SQLException E1) { $ e1.printstacktrace (); - } - return; the}finally{ - Jdbcutil.closeconn (conn);Wuyi } the}
Success.java
1 Public voidService (HttpServletRequest request, httpservletresponse response)2 throwsservletexception, IOException {3 Try {4Response.sendredirect (cebsuccessurl+ "? orderid=" +request.getparametermap (). Get ("Plain"). Get ("OrderId")));5}Catch(IOException e) {6 e.printstacktrace ();7 }8 }9
Iv. Results of operation
Hand in hand to teach you everbright pay