日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java后台提供ios微信支付接口

發(fā)布時(shí)間:2024/1/8 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java后台提供ios微信支付接口 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

提供微信支付接口,需要先在微信開(kāi)放平臺(tái)(https://open.weixin.qq.com/)申請(qǐng)賬號(hào),進(jìn)行開(kāi)發(fā)者資格認(rèn)證通過(guò)后創(chuàng)建應(yīng)用。

創(chuàng)建成功后會(huì)有對(duì)應(yīng)應(yīng)用的AppID和AppSecret這個(gè)ios需要。我們需要的是商戶平臺(tái)的賬號(hào)、初始密碼、對(duì)應(yīng)應(yīng)用的AppID等,在開(kāi)發(fā)者資格認(rèn)證成功后微信會(huì)發(fā)到綁定的郵箱上。收到郵箱后需要登錄到微信商戶平臺(tái)(https://pay.weixin.qq.com/index.php/core/home/login?return_url=%2F)修改初始密碼并進(jìn)行設(shè)置api密鑰。

關(guān)于登錄微信商戶平臺(tái),我在進(jìn)行登錄時(shí)候火狐和谷歌經(jīng)常不行,證書(shū)安裝不好,后來(lái)試用qq瀏覽器才能登上,但是退款時(shí)候下載證書(shū)又不行,后來(lái)用了uc才可以的。配置好后我們就可以進(jìn)行編碼。首先看一下微信官方的文檔上app支付需要的參數(shù)。
ios要在app中調(diào)起微信支付,需要一下幾個(gè)參數(shù):

這些參數(shù)由后臺(tái)傳送給ios,后臺(tái)需要先傳入以下必填參數(shù)進(jìn)行第一次簽名:


注意:對(duì)于同一帳號(hào)的不同應(yīng)用,每次申請(qǐng)均需填寫(xiě)資料,均需簽署合同。
接下來(lái)就是代碼部分:微信的配置文件:

#weixin apy ##appid weixin.appid= ##應(yīng)用id 認(rèn)證成功后微信官方給你返回 weixin.mch_id= ##回調(diào)url weixin.notify_url= ##退款的回調(diào)url weixin.refund_notify_url=

提供給ios接口代碼:

import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; import java.math.BigDecimal; import java.math.RoundingMode; import java.security.Security; import java.text.DecimalFormat; import java.util.HashMap; import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; import java.util.UUID;import javax.annotation.Resource; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.StringUtils; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.util.EntityUtils; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import org.bouncycastle.jce.provider.BouncyCastleProvider;import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature;/*** 微信支付的接口* */ @Namespace("/out/weixinPay") @Results({ @Result(name = "500", location = "/WEB-INF/views/500.jsp") }) @SuppressWarnings("unused") public class WeixinPayAction extends OutInterfaceBasicAction<Consult> {private static final long serialVersionUID = 1L;private Map<String, Object> msg = new HashMap<String, Object>();/** @Resource private UnifiedOrderService unifiedOrderService;*/@Resourceprivate MemberService memberService;@Resourceprivate ALiPayService aLiPayService;private ALiPay aLiPay = new ALiPay();@Resourceprivate IMessageService iMessageService;@Resourceprivate RefundOrderService refundOrderService;private static DecimalFormat df = new DecimalFormat("0.00");// 構(gòu)造簽名的mapprivate SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();// 構(gòu)造向微信發(fā)送參數(shù)的實(shí)體類private Unifiedorder unifiedorder = new Unifiedorder();// 微信的參數(shù)private WeixinConfigUtils config = new WeixinConfigUtils();public static final String ALGORITHM = "AES/ECB/PKCS7Padding"; private RefundOrder refundOrder = new RefundOrder();@Resourceprivate AESDecodeUtil aesDecodeUtil;@Resourceprivate OrderService orderService;/*** 微信充值*/@Action("weChatPayRecharge")public String weChatPayRecharge() {try {df.setRoundingMode(RoundingMode.HALF_UP);// 獲取用戶IDString memberId = etRequest().getParameter("memberId");String integer = getRequest().getParameter("integer");if (StringUtils.isNotBlank(memberId)&& StringUtils.isNotBlank(integer)) {Integer score = Integer.valueOf(integer);// 產(chǎn)生訂單號(hào)String outTradeNo = UuIdUtils.getUUID();// 參數(shù)組String appid = config.appid;String mch_id = config.mch_id;String nonce_str = RandCharsUtils.getRandomString(16);String body = "";body = "購(gòu)買(mǎi)" + score + "積分,支付" + df.format(score / 100)+ "元";String detail = score / 100 + "元支付開(kāi)始";String attach = "暫時(shí)無(wú)用(這個(gè)用于區(qū)分不同版本)";String out_trade_no = outTradeNo;int total_fee = score;// 單位是分,現(xiàn)在按照ios傳遞過(guò)來(lái)的參數(shù)進(jìn)行String spbill_create_ip = "127.0.0.1";String time_start = RandCharsUtils.timeStart();String time_expire = RandCharsUtils.timeExpire();String notify_url = config.notify_url;String trade_type = "APP";// 參數(shù):開(kāi)始生成第一次簽名parameters.put("appid", appid);parameters.put("mch_id", mch_id);parameters.put("body", body);parameters.put("nonce_str", nonce_str);parameters.put("detail", detail);parameters.put("attach", attach);parameters.put("out_trade_no", out_trade_no);parameters.put("total_fee", total_fee);parameters.put("time_start", time_start);parameters.put("time_expire", time_expire);parameters.put("notify_url", notify_url);parameters.put("trade_type", trade_type);parameters.put("spbill_create_ip", spbill_create_ip);String sign = WXSignUtils.createSign("UTF-8", parameters);// 微信統(tǒng)一下單unifiedorder.setAppid(appid);unifiedorder.setMch_id(mch_id);unifiedorder.setNonce_str(nonce_str);unifiedorder.setSign(sign);unifiedorder.setBody(body);unifiedorder.setDetail(detail);unifiedorder.setAttach(attach);unifiedorder.setOut_trade_no(out_trade_no);unifiedorder.setTotal_fee(total_fee);unifiedorder.setSpbill_create_ip(spbill_create_ip);unifiedorder.setTime_start(time_start);unifiedorder.setTime_expire(time_expire);unifiedorder.setNotify_url(notify_url);unifiedorder.setTrade_type(trade_type);try {// 微信第二次簽名SortedMap<Object, Object> parameter2 = new TreeMap<Object, Object>();parameter2.put("appid", appid);parameter2.put("noncestr", nonce_str);parameter2.put("package", "Sign=WXPay");parameter2.put("partnerid", mch_id);String prepareId = HttpXmlUtils.getPrepareId(unifiedorder);parameter2.put("prepayid", prepareId);long timestamp = System.currentTimeMillis() / 1000;parameter2.put("timestamp", timestamp);String sign2 = WXSignUtils.createSign("UTF-8", parameter2);unifiedorder = null;// 訂單信息存入數(shù)據(jù)庫(kù)aLiPay.setApp_id(appid);aLiPay.setSubject(body);aLiPay.setScore(integer);aLiPay.setGmt_create(time_start);if (null != memberId && !"".equals(memberId)) {aLiPay.setMember(memberService.getMemberById(Integer.valueOf(memberId)));}aLiPay.setOut_trade_no(out_trade_no);aLiPay.setPayStatus(PayStatusEnum.WAITPAY.getComment());aLiPay.setPayType(PayTypeEnum.WEIXIN.getComment());aLiPay.setState(0);aLiPay.setTotal_amount(String.valueOf(total_fee / 100));aLiPay.setTrade_no(prepareId);aLiPay.setSign(sign);aLiPay.setTotalFee(total_fee);aLiPayService.addALiPay(aLiPay);// 傳遞給ios的參數(shù)msg.put("prepay_id", prepareId);msg.put("appid", appid);msg.put("mch_id", mch_id);msg.put("timestamp", timestamp);msg.put("nonce_str", nonce_str);msg.put("sign", sign2);msg.put("stateCode", MsgCode.SUCCESS);msg.put("message ", "請(qǐng)求成功并返回?cái)?shù)據(jù)!");} catch (Exception e) {msg.put("stateCode", MsgCode.ERROR);msg.put("message ", "請(qǐng)求失敗");}} else {msg.put("stateCode", MsgCode.GET_PARAM_ERROR);msg.put("message ", "獲取參數(shù)失敗!");}Struts2Utils.renderJson(JSONObject.toJSONString(msg,SerializerFeature.DisableCircularReferenceDetect));return NONE;} catch (Exception e) {log.error(e.getMessage());e.printStackTrace();return "500";}}*//*** 微信回調(diào)url* * @throws IOException*/@Action("notifyUrl")public String notifyUrl() throws IOException {try {Map<String, String> resultMap = JdomParseXmlUtils.getWeixinResult(Struts2Utils.getRequest(), Struts2Utils.getResponse());aLiPay = aLiPayService.searchALiPayByOutTradeNo(resultMap.get("out_trade_no"));String resutlTotal = String.valueOf(Integer.valueOf(resultMap.get("total_fee")) / 100);if (aLiPay.getState() == 0) {if (aLiPay.getOut_trade_no().equals(resultMap.get("out_trade_no"))&& aLiPay.getTotal_amount().equals(resutlTotal)) {aLiPay.setGmt_payment(resultMap.get("time_end"));aLiPay.setPayStatus(PayStatusEnum.PAYOVER.getComment());aLiPay.setState(1);aLiPayService.updateALiPay(aLiPay);iMessageService.sendUserscoreMsg(new IntegrationContent(aLiPay.getMember().getMemberId(), Integer.valueOf(aLiPay.getScore()), "微信充值"+ aLiPay.getScore() + "積分", 1));unifiedorder.setReturn_code("SUCCESS");}} else {unifiedorder.setReturn_code("FAIL");}String refundXml = HttpXmlUtils.refundXml(unifiedorder);Struts2Utils.getResponse().getWriter().write(refundXml);Struts2Utils.getResponse().getWriter().flush();return null;} catch (Exception e) {log.error(e.getMessage());e.printStackTrace();return "500";}} }

使用到的工具類代碼:
uuidUtis用于生成訂單號(hào):

import java.util.UUID;public class UuIdUtils {public static String getUUID() {return UUID.randomUUID().toString().replace("-", "");} }

WeixinConfigUtils加載weixin.properties文件。

import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.ResourceBundle;import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;/*** 微信的配置參數(shù)* @author * @date 2017/08/10*/ @SuppressWarnings("unused") public class WeixinConfigUtils {private static final Log log = LogFactory.getLog(WeixinConfigUtils.class);public static String appid;public static String mch_id;public static String notify_url;public static String order_notify_url;public static String doctor_notify_url;static {try{InputStream is = WeixinConfigUtils.class.getResourceAsStream("/weixin.properties");Properties properties = new Properties();properties.load(is);appid = properties.getProperty("weixin.appid");mch_id = properties.getProperty("weixin.mch_id");notify_url = properties.getProperty("weixin.notify_url");order_notify_url = properties.getProperty("weixin.order_notify_url");doctor_notify_url = properties.getProperty("weixin.doctor_notify_url");}catch(Exception ex){log.debug("加載配置文件:"+ex.getMessage());}} } import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Random;/*** nonce_str隨即字符串 * @author * @date 2017/08/10*/ public class RandCharsUtils {private static SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");public static String getRandomString(int length) { //length表示生成字符串的長(zhǎng)度String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"; Random random = new Random(); StringBuffer sb = new StringBuffer();int number = 0;for (int i = 0; i < length; i++) { number = random.nextInt(base.length()); sb.append(base.charAt(number)); } return sb.toString(); } /** 訂單開(kāi)始交易的時(shí)間*/public static String timeStart(){return df.format(new Date());}/** 訂單開(kāi)始交易的時(shí)間*/public static String timeExpire(){Calendar now=Calendar.getInstance();now.add(Calendar.MINUTE,30);return df.format(now.getTimeInMillis());}} import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.SortedMap;/*** 微信支付簽名* @author yangfuren* @date 2017/08/10*/ public class WXSignUtils {/*** 微信支付簽名算法sign* @param characterEncoding* @param parameters* @return*/@SuppressWarnings("rawtypes")public static String createSign(String characterEncoding,SortedMap<Object,Object> parameters){StringBuffer sb = new StringBuffer();Set es = parameters.entrySet();//所有參與傳參的參數(shù)按照accsii排序(升序)Iterator it = es.iterator();while(it.hasNext()) {Map.Entry entry = (Map.Entry)it.next();String k = (String)entry.getKey();Object v = entry.getValue();if(null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {sb.append(k + "=" + v + "&");}}sb.append("key=" + weixinConstant.KEY);String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();return sign;} } import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.StringReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.List; import java.util.Map;import javax.annotation.Resource; import javax.net.ssl.HttpsURLConnection;import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.xml.sax.InputSource;import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder; /*** post提交xml格式的參數(shù)* @author * @date 2017/08/10*/ public class HttpXmlUtils {@Resourceprivate RefundOrderService refundOrderService;public static XStream xStream = new XStream(new DomDriver("UTF-8",new XmlFriendlyNameCoder("-_", "_")));/*** 開(kāi)始post提交參數(shù)到接口* 并接受返回* @param url* @param xml* @param method* @param contentType* @return*/public static String xmlHttpProxy(String url,String xml,String method,String contentType){InputStream is = null;OutputStreamWriter os = null;try {URL _url = new URL(url);HttpURLConnection conn = (HttpURLConnection) _url.openConnection();conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestProperty("Content-type", "text/xml");conn.setRequestProperty("Pragma:", "no-cache"); conn.setRequestProperty("Cache-Control", "no-cache"); conn.setRequestMethod("POST");os = new OutputStreamWriter(conn.getOutputStream());os.write(new String(xml.getBytes(contentType)));os.flush();//返回值is = conn.getInputStream();return getContent(is, "utf-8");} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally{try {if(os!=null){os.close();}if(is!=null){is.close();}} catch (IOException e) {e.printStackTrace();}}return null;}/*** 解析返回的值* @param is* @param charset* @return*/public static String getContent(InputStream is, String charset) {String pageString = null;InputStreamReader isr = null;BufferedReader br = null;StringBuffer sb = null;try {isr = new InputStreamReader(is, charset);br = new BufferedReader(isr);sb = new StringBuffer();String line = null;while ((line = br.readLine()) != null) {sb.append(line + "\n");}pageString = sb.toString();} catch (Exception e) {e.printStackTrace();} finally {try {if (is != null){is.close();}if(isr!=null){isr.close();}if(br!=null){br.close();}} catch (IOException e) {e.printStackTrace();}sb = null;}return pageString;}/*** 解析申請(qǐng)退款之后微信返回的值并進(jìn)行存庫(kù)操作* @throws IOException * @throws JDOMException */public static Map<String, String> parseRefundXml(String refundXml) throws JDOMException, IOException{ParseXMLUtils.jdomParseXml(refundXml);StringReader read = new StringReader(refundXml);// 創(chuàng)建新的輸入源SAX 解析器將使用 InputSource 對(duì)象來(lái)確定如何讀取 XML 輸入InputSource source = new InputSource(read);// 創(chuàng)建一個(gè)新的SAXBuilderSAXBuilder sb = new SAXBuilder();// 通過(guò)輸入源構(gòu)造一個(gè)Documentorg.jdom.Document doc;doc = (org.jdom.Document) sb.build(source);org.jdom.Element root = doc.getRootElement();// 指向根節(jié)點(diǎn)List<org.jdom.Element> list = root.getChildren();Map<String, String> refundOrderMap = new HashMap<String, String>();if(list!=null&&list.size()>0){for (org.jdom.Element element : list) {refundOrderMap.put(element.getName(), element.getText());}return refundOrderMap;}return null;}/*** 解析申請(qǐng)退款之后微信退款回調(diào)返回的字符串中內(nèi)容* @throws IOException * @throws JDOMException */public static Map<String, String> parseRefundNotifyXml(String refundXml) throws JDOMException, IOException{ParseXMLUtils.jdomParseXml(refundXml);StringReader read = new StringReader(refundXml);// 創(chuàng)建新的輸入源SAX 解析器將使用 InputSource 對(duì)象來(lái)確定如何讀取 XML 輸入InputSource source = new InputSource(read);// 創(chuàng)建一個(gè)新的SAXBuilderSAXBuilder sb = new SAXBuilder();// 通過(guò)輸入源構(gòu)造一個(gè)Documentorg.jdom.Document doc;doc = (org.jdom.Document) sb.build(source);org.jdom.Element root = doc.getRootElement();// 指向根節(jié)點(diǎn)List<org.jdom.Element> list = root.getChildren();Map<String, String> resultMap = new HashMap<>();if(list!=null&&list.size()>0){for (org.jdom.Element element : list){resultMap.put(element.getName(), element.getText());}return resultMap;}return null;}/*** h5支付時(shí) 解析返回的值并返回prepareid* @throws IOException * @throws JDOMException */public static Map<String, String> getUrl(Unifiedorder unifiedorder) throws JDOMException, IOException{String xmlInfo = HttpXmlUtils.xmlH5Info(unifiedorder);String wxUrl = weixinConstant.URL;String method = "POST";String weixinPost = HttpXmlUtils.httpsRequest(wxUrl, method, xmlInfo).toString();ParseXMLUtils.jdomParseXml(weixinPost);StringReader read = new StringReader(weixinPost);// 創(chuàng)建新的輸入源SAX 解析器將使用 InputSource 對(duì)象來(lái)確定如何讀取 XML 輸入InputSource source = new InputSource(read);// 創(chuàng)建一個(gè)新的SAXBuilderSAXBuilder sb = new SAXBuilder();// 通過(guò)輸入源構(gòu)造一個(gè)Documentorg.jdom.Document doc;doc = (org.jdom.Document) sb.build(source);org.jdom.Element root = doc.getRootElement();// 指向根節(jié)點(diǎn)List<org.jdom.Element> list = root.getChildren();String prepayId =null;Map<String, String> msg = new HashMap<String, String>();if(list!=null&&list.size()>0){for (org.jdom.Element element : list) {msg.put(element.getName(), element.getText());}}return msg;}/*** 解析返回的值并返回prepareid* @throws IOException * @throws JDOMException */public static String getPrepareId(Unifiedorder unifiedorder) throws JDOMException, IOException{String xmlInfo = HttpXmlUtils.xmlInfo(unifiedorder);String wxUrl = weixinConstant.URL;String method = "POST";String weixinPost = HttpXmlUtils.httpsRequest(wxUrl, method, xmlInfo).toString();ParseXMLUtils.jdomParseXml(weixinPost);StringReader read = new StringReader(weixinPost);// 創(chuàng)建新的輸入源SAX 解析器將使用 InputSource 對(duì)象來(lái)確定如何讀取 XML 輸入InputSource source = new InputSource(read);// 創(chuàng)建一個(gè)新的SAXBuilderSAXBuilder sb = new SAXBuilder();// 通過(guò)輸入源構(gòu)造一個(gè)Documentorg.jdom.Document doc;doc = (org.jdom.Document) sb.build(source);org.jdom.Element root = doc.getRootElement();// 指向根節(jié)點(diǎn)List<org.jdom.Element> list = root.getChildren();String prepayId =null;if(list!=null&&list.size()>0){for (org.jdom.Element element : list) {if ( "prepay_id".equals(element.getName())) {prepayId= element.getText();break;}}}return prepayId;}/*** 向微信發(fā)送企業(yè)付款請(qǐng)求并解析返回結(jié)果* @throws IOException * @throws JDOMException */public static Map<String, String> getTransfersMap(Transfers transfers) throws JDOMException, IOException{String xmlInfo = HttpXmlUtils.xmlTransfer(transfers);String wxUrl = weixinConstant.WITHDRAW_URL;String method = "POST";String weixinPost = HttpXmlUtils.httpsRequest(wxUrl, method, xmlInfo).toString();ParseXMLUtils.jdomParseXml(weixinPost);StringReader read = new StringReader(weixinPost);// 創(chuàng)建新的輸入源SAX 解析器將使用 InputSource 對(duì)象來(lái)確定如何讀取 XML 輸入InputSource source = new InputSource(read);// 創(chuàng)建一個(gè)新的SAXBuilderSAXBuilder sb = new SAXBuilder();// 通過(guò)輸入源構(gòu)造一個(gè)Documentorg.jdom.Document doc;doc = (org.jdom.Document) sb.build(source);org.jdom.Element root = doc.getRootElement();// 指向根節(jié)點(diǎn)List<org.jdom.Element> list = root.getChildren();Map<String, String> transferMap=new HashMap<>();if(list!=null&&list.size()>0){for (org.jdom.Element element : list) {transferMap.put(element.getName(), element.getText());}}return transferMap;}/*** 構(gòu)造退款xml參數(shù)* @param xml* @return*/public static String refundXml(Unifiedorder unifiedorder){xStream.autodetectAnnotations(true);xStream.alias("xml", Unifiedorder.class);return xStream.toXML(unifiedorder);}/*** 構(gòu)造企業(yè)付款xml參數(shù)* @param xml* @return*/public static String transferXml(Transfers transfers){xStream.autodetectAnnotations(true);xStream.alias("xml", Transfers.class);return xStream.toXML(transfers);}/*** 構(gòu)造xml參數(shù)* @param xml* @return*/public static String xmlInfo(Unifiedorder unifiedorder){if(unifiedorder!=null){StringBuffer bf = new StringBuffer();bf.append("<xml>");bf.append("<appid><![CDATA[");bf.append(unifiedorder.getAppid());bf.append("]]></appid>");bf.append("<mch_id><![CDATA[");bf.append(unifiedorder.getMch_id());bf.append("]]></mch_id>");bf.append("<nonce_str><![CDATA[");bf.append(unifiedorder.getNonce_str());bf.append("]]></nonce_str>");bf.append("<sign><![CDATA[");bf.append(unifiedorder.getSign());bf.append("]]></sign>");bf.append("<body><![CDATA[");bf.append(unifiedorder.getBody());bf.append("]]></body>");bf.append("<detail><![CDATA[");bf.append(unifiedorder.getDetail());bf.append("]]></detail>");bf.append("<attach><![CDATA[");bf.append(unifiedorder.getAttach());bf.append("]]></attach>");bf.append("<out_trade_no><![CDATA[");bf.append(unifiedorder.getOut_trade_no());bf.append("]]></out_trade_no>");bf.append("<total_fee><![CDATA[");bf.append(unifiedorder.getTotal_fee());bf.append("]]></total_fee>");bf.append("<spbill_create_ip><![CDATA[");bf.append(unifiedorder.getSpbill_create_ip());bf.append("]]></spbill_create_ip>");bf.append("<time_start><![CDATA[");bf.append(unifiedorder.getTime_start());bf.append("]]></time_start>");bf.append("<time_expire><![CDATA[");bf.append(unifiedorder.getTime_expire());bf.append("]]></time_expire>");bf.append("<notify_url><![CDATA[");bf.append(unifiedorder.getNotify_url());bf.append("]]></notify_url>");bf.append("<trade_type><![CDATA[");bf.append(unifiedorder.getTrade_type());bf.append("]]></trade_type>");bf.append("</xml>");return bf.toString();}return "";}/*** 構(gòu)造xml參數(shù)* @param xml* @return*/public static String xmlH5Info(Unifiedorder unifiedorder){if(unifiedorder!=null){StringBuffer bf = new StringBuffer();bf.append("<xml>");bf.append("<appid><![CDATA[");bf.append(unifiedorder.getAppid());bf.append("]]></appid>");bf.append("<mch_id><![CDATA[");bf.append(unifiedorder.getMch_id());bf.append("]]></mch_id>");bf.append("<nonce_str><![CDATA[");bf.append(unifiedorder.getNonce_str());bf.append("]]></nonce_str>");bf.append("<sign><![CDATA[");bf.append(unifiedorder.getSign());bf.append("]]></sign>");bf.append("<body><![CDATA[");bf.append(unifiedorder.getBody());bf.append("]]></body>");bf.append("<attach><![CDATA[");bf.append(unifiedorder.getAttach());bf.append("]]></attach>");bf.append("<out_trade_no><![CDATA[");bf.append(unifiedorder.getOut_trade_no());bf.append("]]></out_trade_no>");bf.append("<total_fee><![CDATA[");bf.append(unifiedorder.getTotal_fee());bf.append("]]></total_fee>");bf.append("<spbill_create_ip><![CDATA[");bf.append(unifiedorder.getSpbill_create_ip());bf.append("]]></spbill_create_ip>");bf.append("<notify_url><![CDATA[");bf.append(unifiedorder.getNotify_url());bf.append("]]></notify_url>");bf.append("<trade_type><![CDATA[");bf.append(unifiedorder.getTrade_type());bf.append("]]></trade_type>");bf.append("<scene_info><![CDATA[");bf.append(unifiedorder.getScene_info());bf.append("]]></scene_info>");bf.append("</xml>");return bf.toString();}return "";}/*** 構(gòu)造退款xml參數(shù)* @param xml* @return*/public static String xmlTransfer(Transfers transfers){if(transfers!=null){StringBuffer bf = new StringBuffer();bf.append("<xml>");bf.append("<mch_appid><![CDATA[");bf.append(transfers.getMch_appid());bf.append("]]></mch_appid>");bf.append("<mchid><![CDATA[");bf.append(transfers.getMchid());bf.append("]]></mchid>");bf.append("<nonce_str><![CDATA[");bf.append(transfers.getNonce_str());bf.append("]]></nonce_str>");bf.append("<sign><![CDATA[");bf.append(transfers.getSign());bf.append("]]></sign>");bf.append("<partner_trade_no><![CDATA[");bf.append(transfers.getPartner_trade_no());bf.append("]]></partner_trade_no>");bf.append("<openid><![CDATA[");bf.append(transfers.getOpenid());bf.append("]]></openid>");bf.append("<check_name><![CDATA[");bf.append(transfers.getCheck_name());bf.append("]]></check_name>");bf.append("<amount><![CDATA[");bf.append(transfers.getAmount());bf.append("]]></amount>");bf.append("<desc><![CDATA[");bf.append(transfers.getDesc());bf.append("]]></desc>");bf.append("<spbill_create_ip><![CDATA[");bf.append(transfers.getSpbill_create_ip());bf.append("]]></spbill_create_ip>");bf.append("</xml>");return bf.toString();}return "";}/*** post請(qǐng)求并得到返回結(jié)果* @param requestUrl* @param requestMethod* @param output* @return*/public static String httpsRequest(String requestUrl, String requestMethod, String output) {try{URL url = new URL(requestUrl);HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();connection.setDoOutput(true);connection.setDoInput(true);connection.setUseCaches(false);connection.setRequestMethod(requestMethod);if (null != output) {OutputStream outputStream = connection.getOutputStream();outputStream.write(output.getBytes("UTF-8"));outputStream.close();}// 從輸入流讀取返回內(nèi)容InputStream inputStream = connection.getInputStream();InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");BufferedReader bufferedReader = new BufferedReader(inputStreamReader);String str = null;StringBuffer buffer = new StringBuffer();while ((str = bufferedReader.readLine()) != null) {buffer.append(str);}bufferedReader.close();inputStreamReader.close();inputStream.close();inputStream = null;connection.disconnect();return buffer.toString();}catch(Exception ex){ex.printStackTrace();}return "";} } /*** 微信支付相關(guān)的常量* @author yangfuren* @since 2017/08/16*/ public class weixinConstant {/*** 微信支付API秘鑰 第一次登錄時(shí)設(shè)置的*/public static final String KEY = "";/*** url*/public static final String URL = "https://api.mch.weixin.qq.com/pay/unifiedorder";/*** 退款url*/public static final String REFUND_URL = "https://api.mch.weixin.qq.com/secapi/pay/refund";/*** 證書(shū)地址 退款及h5支付使用*/public static final String PATH ="";/*** 付款url*/public static final String WITHDRAW_URL="https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; } import java.security.MessageDigest;public class MD5Util {private static String byteArrayToHexString(byte b[]) {StringBuffer resultSb = new StringBuffer();for (int i = 0; i < b.length; i++)resultSb.append(byteToHexString(b[i]));return resultSb.toString();}private static String byteToHexString(byte b) {int n = b;if (n < 0)n += 256;int d1 = n / 16;int d2 = n % 16;return hexDigits[d1] + hexDigits[d2];}public static String MD5Encode(String origin, String charsetname) {String resultString = null;try {resultString = new String(origin);MessageDigest md = MessageDigest.getInstance("MD5");if (charsetname == null || "".equals(charsetname))resultString = byteArrayToHexString(md.digest(resultString.getBytes()));elseresultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));} catch (Exception exception) {}return resultString;}private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5","6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };}

統(tǒng)一下單提交為微信的參數(shù)

import java.io.Serializable;/*** 統(tǒng)一下單提交為微信的參數(shù)* @author * @date 2017年08月11日*/ public class Unifiedorder implements Serializable{private static final long serialVersionUID = 1L;//微信支付表idprivate Integer weixinId;//微信分配的公眾賬號(hào)ID(企業(yè)號(hào)corpid即為此appId)private String appid;//商戶idprivate String mch_id;//終端設(shè)備號(hào)(門(mén)店號(hào)或收銀設(shè)備ID),注意:PC網(wǎng)頁(yè)或公眾號(hào)內(nèi)支付請(qǐng)傳"WEB"private String device_info;//隨機(jī)字符串:數(shù)字+大寫(xiě)字母的組合,32位private String nonce_str;//簽名private String sign;//商品或支付單簡(jiǎn)要描述private String body;//商品名稱明細(xì)列表private String detail;//附加參數(shù)(例如:用于區(qū)別本商戶不同的分店)private String attach;//商戶系統(tǒng)內(nèi)部的訂單號(hào)private String out_trade_no;//貨幣類型:符合ISO 4217標(biāo)準(zhǔn)的三位字母代碼,默認(rèn)人民幣:CNYprivate String fee_type;//總金額private int total_fee;//APP和網(wǎng)頁(yè)支付提交[用戶端ip],Native支付填調(diào)用微信支付API的機(jī)器IP。private String spbill_create_ip;//訂單生成時(shí)間,格式為yyyyMMddHHmmss,private String time_start;//訂單失效時(shí)間,格式為yyyyMMddHHmmss,最短失效時(shí)間間隔必須大于5分鐘[支付寶是30分鐘,同樣30分鐘]private String time_expire;//商品標(biāo)記,代金券或立減優(yōu)惠功能的參數(shù)private String goods_tag;//接收微信支付異步通知回調(diào)地址private String notify_url;//交易類型:JSAPI,NATIVE,APP h5為 MWEBprivate String trade_type;//trade_type=NATIVE,此參數(shù)必傳。此id為二維碼中包含的商品ID,商戶自行定義。private String product_id;//no_credit--指定不能使用信用卡支付private String limit_pay;//trade_type=JSAPI,此參數(shù)必傳,用戶在商戶appid下的唯一標(biāo)識(shí)private String openid;//商戶內(nèi)部自己的退款單號(hào)private String out_refund_no;//退款總金額單位為分private int refund_fee;//操作員的id默認(rèn)為mch_idprivate String op_user_id;//微信官方提供的訂單號(hào)private String prepayid;//記錄所對(duì)應(yīng)的memberprivate Member member;//返回給微信的狀態(tài)碼(用于支付回調(diào)時(shí))public String return_code;//微信h5支付時(shí)候的場(chǎng)景信息官方的信息模板 {"h5_info"://h5支付固定傳"h5_info" //{"type":"",//場(chǎng)景類型 "wap_url":"",//WAP網(wǎng)站URL地址"wap_name": ""//WAP 網(wǎng)站名}}public String scene_info;public String getScene_info() {return scene_info;}public void setScene_info(String scene_info) {this.scene_info = scene_info;}public String getReturn_code() {return return_code;}public void setReturn_code(String return_code) {this.return_code = return_code;}public String getAppid() {return appid;}public String getMch_id() {return mch_id;}public String getDevice_info() {return device_info;}public String getNonce_str() {return nonce_str;}public String getSign() {return sign;}public String getBody() {return body;}public String getDetail() {return detail;}public String getAttach() {return attach;}public String getOut_trade_no() {return out_trade_no;}public String getFee_type() {return fee_type;}public int getTotal_fee() {return total_fee;}public String getSpbill_create_ip() {return spbill_create_ip;}public String getTime_start() {return time_start;}public String getTime_expire() {return time_expire;}public String getGoods_tag() {return goods_tag;}public String getNotify_url() {return notify_url;}public String getTrade_type() {return trade_type;}public String getProduct_id() {return product_id;}public String getLimit_pay() {return limit_pay;}public String getOpenid() {return openid;}public void setAppid(String appid) {this.appid = appid;}public void setMch_id(String mch_id) {this.mch_id = mch_id;}public void setDevice_info(String device_info) {this.device_info = device_info;}public void setNonce_str(String nonce_str) {this.nonce_str = nonce_str;}public void setSign(String sign) {this.sign = sign;}public void setBody(String body) {this.body = body;}public void setDetail(String detail) {this.detail = detail;}public void setAttach(String attach) {this.attach = attach;}public void setOut_trade_no(String out_trade_no) {this.out_trade_no = out_trade_no;}public void setFee_type(String fee_type) {this.fee_type = fee_type;}public void setTotal_fee(int total_fee) {this.total_fee = total_fee;}public void setSpbill_create_ip(String spbill_create_ip) {this.spbill_create_ip = spbill_create_ip;}public void setTime_start(String time_start) {this.time_start = time_start;}public void setTime_expire(String time_expire) {this.time_expire = time_expire;}public void setGoods_tag(String goods_tag) {this.goods_tag = goods_tag;}public void setNotify_url(String notify_url) {this.notify_url = notify_url;}public void setTrade_type(String trade_type) {this.trade_type = trade_type;}public void setProduct_id(String product_id) {this.product_id = product_id;}public void setLimit_pay(String limit_pay) {this.limit_pay = limit_pay;}public void setOpenid(String openid) {this.openid = openid;}public String getOut_refund_no() {return out_refund_no;}public void setOut_refund_no(String out_refund_no) {this.out_refund_no = out_refund_no;}public int getRefund_fee() {return refund_fee;}public void setRefund_fee(int refund_fee) {this.refund_fee = refund_fee;}public Integer getWeixinId() {return weixinId;}public void setWeixinId(Integer weixinId) {this.weixinId = weixinId;}public Member getMember() {return member;}public void setMember(Member member) {this.member = member;}public String getPrepayid() {return prepayid;}public void setPrepayid(String prepayid) {this.prepayid = prepayid;}public String getOp_user_id() {return op_user_id;}public void setOp_user_id(String op_user_id) {this.op_user_id = op_user_id;}}

將參數(shù)傳遞給ios,ios會(huì)通過(guò)我們傳遞的參數(shù)調(diào)起微信支付。在生成簽名時(shí),可以將生成的sign和傳入的appid,隨機(jī)字符串,時(shí)間戳,微信官方的訂單號(hào),輸入到https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=20_1中按照順序輸入?yún)?shù)并生成sign看和你的sign是否一致。

總結(jié)

以上是生活随笔為你收集整理的java后台提供ios微信支付接口的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。