Java implementation Micro-letter payment (service side) _java

Source: Internet
Author: User
Tags sha1

Do not say much nonsense, directly look at the code.

RequestHandler RequestHandler = new RequestHandler (Super.getrequest (), Super.getresponse ());
 
Get Token//Two hours in effect, two hours later regain
 
Token = Requesthandler.gettoken ();
 
Update token to the application
 
requesthandler.gettokenreal ();
 
SYSTEM.OUT.PRINTLN ("micro-credit payment acquisition token=======================:" +token);
 
RequestHandler Initialization of
 
requesthandler.init ();
 
Requesthandler.init (Appid,appsecret, Appkey,partnerkey, key);

Code: Local System Build Order

Set Package order parameters sortedmap<string, string> packageparams = new treemap<string, string> (); Packageparams.put ("Bank_type", "WX"); Payment type Packageparams.put ("Body", "xxxx"); Product Description Packageparams.put ("Fee_type", "1"); Bank currency Packageparams.put ("Input_charset", "UTF-8"); Character Set Packageparams.put ("Notify_url", "Http://xxxx.com/xxxx/wxcallback");
 
  The notification address here uses an extranet address test to see if Port 80 is open. Packageparams.put ("Out_trade_no", no); Merchant Order number Packageparams.put ("partner", Partenerid); Set up merchant number Packageparams.put ("Spbill_create_ip", Super.getrequest (). Getremotehost ()); Order generated machine IP, refers to the user browser-side IP packageparams.put ("Total_fee", String.valueof (Rstotal)); 
 
  Total amount of merchandise, to be divided into units//Set up payment parameters sortedmap<string, string> signparams = new treemap<string, string> (); 
 
  Signparams.put ("AppID", AppID); 
 
  Signparams.put ("Noncestr", noncestr); 
 
  Signparams.put ("Traceid", Propertiesutils.getorderno ()); Signparams.put ("timestamp", timestamp); 
 
  Signparams.put ("package", packagevalue);
 
 
 
  Signparams.put ("Appkey", This.appkey); 
 
  Generate payment signatures, to use the original value of Urlencoder SHA1 algorithm!
 
    String sign = "";
 
    try {sign = sha1util.createsha1sign (signparams);
 
    catch (Exception e) {e.printstacktrace (); 
 
  //Increase the extra parameter signparams.put ("Sign_method", "SHA1") for the non-participating signature;   
 
  Signparams.put ("App_signature", sign); API Payment LCL End------------------------------------//Get Prepayid String Prepayid = Requesthandler.sendprepay (signpar
 
  AMS);
 System.out.println ("Prepayid:" + Prepayid);

code: Generate prepaid Quick order complete  

 Generate prepaid Quick order completion and return the parameters required for Android,ios to drop the micro-letter. 
 
  sortedmap<string, string> payparams = new treemap<string, string> ();
 
  Payparams.put ("AppID", AppID);
 
  Payparams.put ("Noncestr", noncestr);
 
  Payparams.put ("Package", "Sign=wxpay");
 
  Payparams.put ("Partnerid", Partenerid);
 
  Payparams.put ("Prepayid", Prepayid);
 
  Payparams.put ("Appkey", This.appkey);
 
  In addition to the 1000 is because of the parameter length limit.
 
  int time = (int) (System.currenttimemillis ()/1000);
 
  
 
  Payparams.put ("timestamp", string.valueof (time));
 
  
 
  System.out.println ("timestamp:" + time);
 
  Signature String paysign = "";
 
  try {paysign = sha1util.createsha1sign (payparams);
 
  catch (Exception e) {e.printstacktrace ();
 
  
 
 } payparams.put ("sign", paysign);
 
  Spell-JSON data is returned to the client basicdbobject Backobject = new Basicdbobject ();
 
  Backobject.put ("AppID", AppID);
 
  Backobject.put ("Noncestr", Payparams.get ("Noncestr"));
 
  Backobject.put ("Package", "Sign=wxpay"); Backobject.put ("PaRtnerid ", Payparams.get (" Partnerid "));
 
  Backobject.put ("Prepayid", Payparams.get ("Prepayid"));
 
  Backobject.put ("Appkey", This.appkey);
 
  Backobject.put ("timestamp", Payparams.get ("timestamp")); 
   
  Backobject.put ("Sign", Payparams.get ("sign"));
 
  String backstr = dataobject.tostring ();
 
  System.out.println ("Backstr:" + backstr); return backstr;

So far, the prepayment order has been generated and the client has been returned to wait for the micro-server notification, and the address of the notification is the notify_url that generated the prepayment order

 ResponseHandler Reshandler = new ResponseHandler (request, response);
 
  Reshandler.setkey (Partnerkey);
 
  Create Request object//requesthandler Queryreq = new RequestHandler (request, response);
 
  Queryreq.init (); if (reshandler.istenpaysign () = = True) {//Merchant order number String Out_trade_no = Reshandler.getparameter ("Out_trade_
 
      No ");
 
      System.out.println ("Out_trade_no:" + out_trade_no);
 
      Tenpay order number String transaction_id = Reshandler.getparameter ("transaction_id");
 
      System.out.println ("transaction_id:" + transaction_id);
 
      Amount to be divided into unit String Total_fee = Reshandler.getparameter ("Total_fee");
 
      If a discount coupon is used, the discount has a value, total_fee+discount= the original request Total_fee String discount = reshandler.getparameter ("discount");
 
  
 
      Payment result String Trade_state = Reshandler.getparameter ("Trade_state");
 
        To determine the signature and result if ("0". Equals (Trade_state)) {//------------------------------//Instant to account processing business start //------------------------------System.out.println ("----------------Business logic Execution-----------------");
 
        --Write a program based on your business logic (the above code is for reference only)--system.out.println ("----------------Business logic completed-----------------"); SYSTEM.OUT.PRINTLN ("Success");
 
        Please do not modify or delete System.out.println ("Instant to account payment success");
 
   
 
       Send the success information to the Tenpay system, Tenpay the system receives this result no longer follow the notice reshandler.sendtocft ("Success");
 
     To the micro-mail server return to success otherwise 30 minutes notice 8 times returns "Success";
 
      }else{System.out.println ("Notify Signature verification failure");
 
      RESHANDLER.SENDTOCFT ("fail");
 
    Response.setcharacterencoding ("Utf-8"); 
 
  }}else {System.out.println ("Fail-md5 failed"); }

The above is the Java implementation of the micro-letter payment server to provide the code, I hope that you study carefully to achieve micro-letter payment.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.