Micro-Credit Payment H5 call payment details (Java version) _java

Source: Internet
Author: User
Tags oauth openid

The recent project needs micro-letter payment, and then looked at the micro-letter public number to pay, although not difficult, but the details still need to pay attention to, with a large half a day to write a demo, and a complete test of the payment process, the following share the micro-trust payment experience.

I. Allocation of public number micro-letter payment

We need to configure the micro-credit public number payment address and test white list.

For example: Pay JS page address for http://www.xxx.com/shop/pay/

Then configure www.xxx.com/shop/pay/here

Second, the development process

Using the micro-credit payment API (address Http://pay.weixin.qq.com/wiki/doc/api/index.PHP?chapter=7_4), we need to develop a red-flagged. As follows:

Third, to the micro-trust server to place orders

Call the unified next single interface so that you can obtain the prepay_id (http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=9_1) for the micro-letter payment.

There are several fields before calling this interface H5 payment must be filled in OpenID

3.1 Get OpenID

Available via Web page authorization form (http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html)

Send the following link in the micro-letter
Https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx520c15f417810387&redirect_uri= to jump the order of the url& Response_type=code&scope=snsapi_base&state=123#wechat_redirect

3.2 Order Acquisition prepay_id

The code is, in effect, sending an XML file via post to get the prepay_id sent over from the micro-trust server.

Import Java.io.ByteArrayInputStream; 
Import javaioioexception; 
Import Javaioinputstream; 
Import javaiounsupportedencodingexception; 
Import Javautildate; 
Import Javautilhashmap; 
Import Javautiliterator; 
Import Javautilmap; 
Import Javautilmapentry; 
 
Import Javautilrandom; 
Import Javaxservlethttphttpservletrequest; 
 
Import Javaxservlethttphttpservletresponse; 
Import Orgapachecommonscodecdigestdigestutils; 
Import Orgspringframeworkstereotypecontroller; 
Import orgspringframeworkwebbindannotationrequestmapping; 
Import Orgxmlpullvxmlpullparser; 
Import orgxmlpullvxmlpullparserexception; 
 
Import Orgxmlpullvxmlpullparserfactory; 
Import Comfasterxmljacksondatabindjsonnode; 
Import Comgsonoauthoauth; 
Import Comgsonoauthpay; 
Import Comgsonutilhttpkit; 
Import Comsyutildatetimeutil; 
 
Import Comsyutiljsonutil; @Controller @RequestMapping ("/pay") public class Wxpaycontroller {@RequestMapping (value = ' wxprepaydo ') public void Jspay (HttpServletRequest request, HttpSErvletresponse response, String callback) throws Exception {//get OpenID String OpenID = Sessionutilgetatt (reque 
    St, "OpenId"); 
    if (OpenID = null) {OpenID = Getuseropenid (request); 
    } String AppID = "WX16691FCB0523C1A4"; 
     
    String Paternerkey = "iningfeng1234567fdfwfdfd1ss234567"; 
    String out_trade_no = Gettradeno (); 
    map<string, string> paramap = new hashmap<string, string> (); 
    Paramapput ("AppID", AppID); 
    Paramapput ("Attach", "test"); 
    Paramapput ("Body", "Test purchase Payment"); 
    Paramapput ("mch_id", "10283271"); 
    Paramapput ("Nonce_str", Create_nonce_str ()); 
    Paramapput ("OpenID", OpenID); 
    Paramapput ("Out_trade_no", out_trade_no); 
    Paramapput ("Spbill_create_ip", Getaddrip (Request)); 
    Paramapput ("Total_fee", "1"); 
    Paramapput ("Trade_type", "Jsapi"); 
    Paramapput ("Notify_url", "http://wwwxxxco/bank/page/wxnotify"); 
    String sign = getsign (Paramap, Paternerkey); 
 
Paramapput ("sign", sign);    Unify the next single https://apimchweixinqqcom/pay/unifiedorder String url = "Https://apimchweixinqqcom/pay/unifiedorder"; 
 
    String XML = Arraytoxml (Paramap); 
 
    String xmlstr = httpkitpost (URL, XML); 
 
    Prepaid Commodity id String prepay_id = ""; 
      if (Xmlstrindexof ("SUCCESS")!=-1) {map<string, string> Map = Doxmlparse (XMLSTR); 
    prepay_id = (String) mapget ("prepay_id"); 
    } map<string, string> paymap = new hashmap<string, string> (); 
    Paymapput ("AppId", appId); 
    Paymapput ("TimeStamp", Create_timestamp ()); 
    Paymapput ("Noncestr", Create_nonce_str ()); 
    Paymapput ("Signtype", "MD5"); 
    Paymapput ("Package", "prepay_id=" + prepay_id); 
     
    String paysign = getsign (Paymap, Paternerkey); 
    Paymapput ("PG", prepay_id); 
     
     
    Paymapput ("Paysign", paysign); Webutilresponse (response, Webutilpackjsonp (callback, Jsonutilwarpjsonnoderesponse ( 
  Paymap)) (toString)); } 
 
  /** 
   *MAP to XML * * @param arr * @return/public String arraytoxml (map<string, string> arr) {STR 
 
    ing XML = "<xml>"; 
    Iterator<entry<string, string>> iter = Arrentryset () iterator (); 
      while (Iterhasnext ()) {entry<string, string> Entry = Iternext (); 
      String key = Entrygetkey (); 
      String val = Entrygetvalue (); 
    XML + + "<" + key + ">" + val + "</" + key + ">"; 
    XML = = "</xml>"; 
  return XML; }//Get OpenID private string Getuseropenid (HttpServletRequest request) throws Exception {String code = Reque 
    Stgetparameter ("code"); 
      if (code = NULL) {String OpenID = Requestgetparameter ("OpenID"); 
    return to OpenId; 
    } Oauth o = new Oauth (); 
    String token = ogettoken (code); 
    Jsonnode node = jsonutilstringtojsonnode (token); 
    String openId = Nodeget ("OpenID") Astext (); 
  return to OpenId; 
 Private String Create_nonce_str () {     String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 
      String res = ""; 
        for (int i = 0; i < i++) {Random rd = new Random (); 
      Res + Charscharat (Rdnextint (Charslength ()-1)); 
  return res; 
  Private String Getaddrip (HttpServletRequest request) {return requestgetremoteaddr (); 
  Private String Create_timestamp () {return longtostring (Systemcurrenttimemillis ()/1000); private String Gettradeno () {string timestamp = Datetimeutilformatdate (new Date (), Datetimeutildatetime_patt 
    ERN); 
  Return "Hzno" + timestamp; Private String getsign (map<string, string> params, String paternerkey) throws Unsupportedencodingex 
    ception {String string1 = Paycreatesign (params, false); 
    String stringsigntemp = string1 + "&key=" + paternerkey; 
    String Signvalue = Digestutilsmd5hex (stringsigntemp) touppercase (); 
  return signvalue; 
 
}  Private map<string, string> doxmlparse (String xml) throws Xmlpullparserexception, IOException {Inpu 
 
    Tstream InputStream = new Bytearrayinputstream (Xmlgetbytes ()); 
 
    map<string, string> map = null; 
 
    Xmlpullparser Pullparser = xmlpullparserfactorynewinstance () newpullparser (); Pullparsersetinput (InputStream, "UTF-8"); 
 
    Sets the XML data int eventtype = Pullparsergeteventtype () to parse for XML; 
        while (EventType!= xmlpullparserend_document) {switch (eventtype) {case xmlpullparserstart_document: 
        Map = new hashmap<string, string> (); 
 
      Break 
        Case Xmlpullparserstart_tag:string key = Pullparsergetname (); 
 
        if (keyequals ("xml")) break; 
        String value = Pullparsernexttext (); 
 
        Mapput (key, value); 
 
      Break 
 
      Case Xmlpullparserend_tag:break; 
 
    } EventType = Pullparsernext (); 
  } return map; 
} 
 
} 

Iv. Payment of H5

H5 payment is actually very simple, only need to call the micro-letter embedded Browser JS method on the line (http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=7_7)

<%@ page language= "java" contenttype= "text/html; 
Charset=utf-8 "pageencoding=" UTF-8 "%> <%@ taglib prefix=" Spring "uri=" Http://wwwspringframeworkorg/tags "%> 
  <% String Path = Requestgetcontextpath (); 
String basepath = requestgetscheme () + "://" + requestgetservername () + ":" + requestgetserverport () + path + "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML transitional//en" "HTTP://WWWWORG/TR/HTML4/LOOSEDTD" >  

The effect is as follows

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.