8455澳门新

图片 15
学室内设计的学生党适合买什么样的笔记本呢?

游戏台机看这边!神舟GTX750Ti战神G50

微信 JS API 支付教程

响铃:流量/入口对于移动电商/O2O仍然只是海市蜃楼

• 2014年06月29日19:26 • 亿邦动力

图片 1

6月的最后一个周末本以为宁财神把头条抢走确定无疑,27日晚间58同城却公开宣布获得来自腾讯的7.36亿美元投资,腾讯将以每股20美元的价格向58认购买36,805,000股A类和B类普通股,交易完成后,腾讯控股获得58同城完全摊薄后19.9%的股份

联想到不久前腾讯投资大众点评、滴滴打车、京东商城、乐居等一系列O2O上布局的动作。以及近期腾讯帝国内部展开的针对坐拥8亿用户的微信变现动作:微信小店、微信支付、微信开放平台、微信新广告系统等等。明眼人都清楚:移动端早已不是战国各路英雄争霸的时代,而提前进入到拼爹挖用户的三国时期,一面是BAT忙着收购入股给投资者讲一个个好听有想象力的故事,一面是中小诸侯国纷纷抱大腿分派系列战队争取留个好印象谈个好价钱。统一大业好像提前要来临了,而决定各诸侯国选择进入哪家豪门最重要的因素就是流量入口问题,一直高喊独立发展的58同城却“仅用十来天时间”就完成与腾讯的“闪婚”更能猜出58看重的更多的是豪门嘴里所谓的移动端流量:本次交易完成后,腾讯和58将互为本地服务领域的优先合作伙伴。双方会借助彼此的平台优势,联合建设下一代的O2O(线上到线下)本地服务。58同城将有机会获得来自腾讯QQ、微信、QQ.com以及QQ浏览器等各个平台的流量[请注意是“有机会”]。腾讯则会在上述平台整合58同城的本地服务,令用户可以通过社交工具发现推荐商户。响铃这货开始犯糊涂了,不禁发问:这事真的靠谱么?

先说说流量的事吧。

我们讲互联网生意(大多数是电商),基本离不开流量、转化率、客单价三个词,电商做得好就应该依靠巨大的流量导入做转化沉淀,提高用户重复购买率的同时不对提高用户的单次购买金额,即客单价。于是电商一开始无疑是花巨资砸广告买流量,说是品牌曝光用户习惯培养,最后多数是唱戏图个热闹,广告过后又是人去楼空,有钱的就继续砸,没钱救命的就基本等死,最后大多数把电商之死归结于没有流量。可笑的是资本方也愿意听,狠狠地陪着凑热闹,后来经历了一番洗礼,发现并不是这个事,有流量并一定能把电商玩好。例子太多,百度有啊,乐酷天,爱乐活、耀点100依靠庞大的互联网流量贩子百度没有做起来。腾讯尽管觊觎电商多年,也是折戟沉沙,屡战屡败,最后不得不把拍拍、QQ商城、易迅打包给京东图个省心,在移动端对微信电商的尝试也一直看似很美好,实际很纠结,微生活、微购物、微商户、营销QQ、风铃。。。。不一而足,前途是光明的,道路是坎坷的。而靠钱买流量而死的平台更似数不胜数,最后“专”家们把这事归因为基因说,有流量输流量没流量花钱买流量最后都一命呜呼的电商不是没有流量少,而是没这个命,没这个基因。

关于流量更多的人认为是PC端的事,而入口则是移动端的家伙,事实上移动入口就真能化腐朽为神奇吗?

这几年很多有野心有梦想的企业都会说自己的事业是做未来移动端最大的入口,尤其是那些参与互联网创业和移动互联网挖金的,不再甘心做产品了,动不动把就自己的东西说是平台,未来会是个入口,于是“产品–平台–入口”三级创业论成为梦想家的座右铭,出去聊天不说自己做某个行业app了,直接说未来人民生活的必备入口。好高大上的是不是,响铃这货也崇拜过一段时间,感觉老板有梦想自己有干劲。后来门票一发,才晓得就微信拿到了,91、360、来往、易信、大众点评等一大票天子骄子还在排队中,再后来发现淘点点熬不动了,微生活没消息了,大众点评要收购饿了么,易信来往被冷落了,现实终于敲醒梦中人:入口那事是说给梦想家听的,想踏实做事的还是乖乖做好产品体验,做慢工干细活。因为即使拿到了入口,也不一定就能如想象的那般美好。为什么?我们复习一下刚刚过去的数据就能明白:

先说京东,刚刚过去的京东618电商节,腾讯入口为京东带来的订单量有多大呢,有专业人士爆料:腾讯入口其实只为京东带来了不到7%的订单量:

其一,京东下单量同比增长100%,来自移动端的下单量占总体比例约为25%。

其二,京东2014年第一季度通过移动互联网手机端下单占比已达18%。

再说大众点评,这是团800的数据:2014年4月国内团购市场成交额为48.4亿元,环比增长0.2%,同比增长102.5%;大众点评团从11.04亿元增长至11.05亿元,环比增长0.09%;美团从23.93亿元增长至24.14亿元,环比增长0.8%。2014年4月国内市场成交额为54.6亿元,环比增长12.8%,同比增长122.0%;大众点评团增长至12.05亿元,环比增长9.05%;美团增长至29.06亿元,环比增长20.4%。

一对比你会发现,尽管有腾讯流量的支持,大众点评成交额增速仍远远落后于竞争对手美团,甚至于连行业平均水平都没有达到。

对于入口论的崇拜者可能是个打击,现实就这么无情,用户不是这么好哄的,尤其是在信用缺失,支付习惯尚未成熟的移动端,流量/入口对于移动电商/O2O仍然只是海市蜃楼,用户仍然用脚投票,要知道移动端和PC端最大的区别就是把交互的主动权交还给用户,我喜欢我乐意我就用你,不喜欢看你不爽就直接删除,而在电脑上,尽管你把淘宝的网页关掉了,其他页面仍然或闪烁或漂浮着刚刚淘宝上相似或相同的产品,手机端不是购物场景的延伸,而直接引起用户与品牌原有关系的颠覆,一切装逼装清纯砸广告的品牌将失去原有的号召力,而能与用户良性互动不做作真实无下限的屌丝品牌开始逆袭,买单的前提是价值认同而不是停留在简单粗暴广告影响的层面。碎片化和移动化更加培养和保护用户的个性化、多样性,统一的入口论,集中的流量论只会是大佬们玩的概念故事。

特别口乌谢:打不死的蚂蚱——响铃微信:xiangling0815,加好友聊感想。

(加班码字而成,感谢有同理心有节操的转载分享)

曾响铃:xiangling0815

新媒体运营、O2O实践者

style=”max-width: 100%; word-wrap: break-word !important; background-color: rgb(192, 0, 0); “> style=”max-width: 100%; word-wrap: break-word !important; text-indent: 2em; “> style=”max-width: 100%; word-wrap: break-word !important; color: rgb(255, 255, 255); “>曾响铃** style=”max-width: 100%; word-wrap: break-word !important; color: rgb(51, 51, 51); “>( style=”max-width: 100%; word-wrap: break-word !important; text-indent: 2em; color: rgb(255, 255, 255); background-color: rgb(192, 0, 0); “> style=”max-width: 100%; word-wrap: break-word !important; color: rgb(255, 255, 255); text-indent: 2em; “>xiangling0815 style=”max-width: 100%; word-wrap: break-word !important; color: rgb(51, 51, 51); “>) style=”max-width: 100%; word-wrap: break-word !important; text-indent: 2em; color: rgb(192, 0, 0); “>长按复制–做有情有趣有用有品的分享**

style=”max-width: 100%; word-wrap: break-word !important; background-color: rgb(242, 242, 242); color: rgb(0, 0, 0); “>✎__ 脑力手艺人,品途、派代、易观、亿邦电商社区等专栏作者,原上市公司高级运营经理,多年新媒体运营、O2O平台运营实操经验,关注社会化媒体、O2O、垂直电商等。边探索边评论

图片 2

互联网借势政府工作报告,O2O将迎爆发热潮

• 作者 罗万攀 •
2015年03月06日14:37 • 速途专栏

style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; box-sizing: border-box !important; word-wrap: break-word !important;”>3月5日消息, style=”margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important;”>国家总理李克强在第十二届全国人民代表大会第三次会议大会上作政府工作报告,在报告中,总理首次提及“把以互联网为载体、线上线下互动的新兴消费搞得红红火火”,这是中国政府总理第一次提及鼓励O2O线上线下互动消费,对于现下正火热的O2O行业来说是一个很好的信号。

style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; box-sizing: border-box !important; word-wrap: break-word !important;”>

style=”margin: 0px; padding: 0px; max-width: 100%; color: rgb(0, 112, 192); box-sizing: border-box !important; word-wrap: break-word !important;”> style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; box-sizing: border-box !important; word-wrap: break-word !important;”>互联网借势两会,传统企业转型迎来契机

style=”margin: 0px; padding: 0px; max-width: 100%; color: rgb(0, 112, 192); box-sizing: border-box !important; word-wrap: break-word !important;”> style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; box-sizing: border-box !important; word-wrap: break-word !important;”>图片 3

style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; box-sizing: border-box !important; word-wrap: break-word !important;”>在此次大会上,作为互联网行业的代表出席此次大会的有百度CEO李彦宏、腾讯CEO马化腾、小米公司董事长雷军、苏宁董事长张近东和科大讯飞董事长刘庆峰。互联网行业的突飞猛进在今年会议上的提案中也凸显出来了,其中在两会上面马化腾提出引进互联网解决民生等重大问题,李彦宏提出“中国大脑”的计划等提议在业内也引起了很大的反响,这也可以显现出互联网在参与国家管理和引导企业转型升级上面有着很大的促进作用。在2014年互联网井喷爆发之年,各行各业借助互联网进行企业的转型升级,纷纷进行产业结构改造和商业模块的新探索,这都是得益于互联网的发展。

style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; box-sizing: border-box !important; word-wrap: break-word !important;”>

style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; box-sizing: border-box !important; word-wrap: break-word !important;”>在今年的两会上面,互联网更是写进了政府工作报告之中,这也可以看得出来政府也意识到互联网对整个国家产业的冲击。 style=”margin: 0px; padding: 0px; max-width: 100%; line-height: 25.6000003814697px; box-sizing: border-box !important; word-wrap: break-word !important;”>众所周知的是,《政府工作报告》在某种程度上来说是整个国民经济和产业发展的风向标,各行各业对政府工作报告提及的产业和项目都是相当敏感的。此次互联网被写入政府工作报告,也从侧面反映出了政府对互联网产业的发展持鼓励和支持的态度。

style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; line-height: 25.6000003814697px; box-sizing: border-box !important; word-wrap: break-word !important;”>速途网罗万攀认为,相信在未来的几年或者数年里面,“互联网+”引导产生的新产业模式将会在各行各业上面逐渐成型。对于国家产业转型升级和互联网的发展来说,这是一个很好的契机。 style=”margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important;”>互联网企业结合自身大数据的整合除了能够不断推出满足用户需求的新产品外,还可以通过开放接口和平台,推动了“垂直系列”的产业创新,带来了新产品、新模式,对于前卫的创业者来说也是一个很好的消息,可以很好地拉动GDP的增长。

style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; line-height: 25.6000003814697px; box-sizing: border-box !important; word-wrap: break-word !important;”>

style=”margin: 0px; padding: 0px; max-width: 100%; color: rgb(0, 112, 192); box-sizing: border-box !important; word-wrap: break-word !important;”> style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; line-height: 25.6000003814697px; box-sizing: border-box !important; word-wrap: break-word !important;”>O2O模式获支持,迎来爆发之年

style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; box-sizing: border-box !important; word-wrap: break-word !important;”>图片 4

style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; box-sizing: border-box !important; word-wrap: break-word !important;”>根据速途研究院的数据分析可以看出,国内本地生活服务市场用户规模在2011年仅有0.82亿人,但是2014年用户规模增长到了2.8亿人,增长幅度可以说是火箭般扩张;中国网民总数的增长情况从2011年占据整个市场的16.0%,到2014年占比已达43.3%。可以看得出来O2O的模式已经被大多数网民所认可和接受,相信在随着网民指数的倍增式发展,O2O模式在整个市场上面的潜力还是相当巨大的。

style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; box-sizing: border-box !important; word-wrap: break-word !important;”>

style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; box-sizing: border-box !important; word-wrap: break-word !important;”>2014年是O2O模式爆发之年,无论是从行业的整合还是传统企业自身融合互联网的新模式,可以说都是很成功的。随着此次O2O写进政府工作报告之中,有了政府的大力扶持,相信O2O模式在产业升级上面会起到很大的作用。结合互联网、大数据的整合分析,各个行业在产品制作和营销上面可以更加精准化。

style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; box-sizing: border-box !important; word-wrap: break-word !important;”>图片 5

style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; box-sizing: border-box !important; word-wrap: break-word !important;”>根据速途研究院分析2014年O2O市场份额可以看得出来,目前国内O2O市场占据首位的是餐饮业,休闲娱乐类和酒店服务市场份额以微小劣势分居二三位。现在看起来餐饮行业O2O市场的争夺仍是重头戏,但随着国家的大力扶持和政策导向,未来的O2O市场将会涉猎到各行各业,进行产业的转型升级。2015年伊始,房产O2O市场开始进行转型升级,58同城和安居客进行合并升级,相信这仅仅是个开始,未来的O2O市场将会是一个百花齐放、百家争鸣的景象。

style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; box-sizing: border-box !important; word-wrap: break-word !important;”>

style=”margin: 0px; padding: 0px; max-width: 100%; color: rgb(0, 112, 192); box-sizing: border-box !important; word-wrap: break-word !important;”> style=”margin: 0px; padding: 0px; max-width: 100%; border: 0px; line-height: 25.6000003814697px; font-size: 16px; box-sizing: border-box !important; word-wrap: break-word !important;”>总结

style=”margin: 0px; padding: 0px; max-width: 100%; font-size: 16px; box-sizing: border-box !important; word-wrap: break-word !important;”>速途网罗万攀认为,互联网、O2O等字眼出现在《政府工作报告》中,不仅仅是互联网爆发的信号,还是一个大趋势。2014年“互联网+”的思维已经渗透到各行各业中,2015伊始,更是有过之而无不及。两会上更是把O2O写进政府工作报告,未来的O2O市场将会更加精彩。但是我们要看到的是O2O模式在政府支持的背后,其实更重要的是市场的管控,无论是线上信息筛选,还是线下体验中服务质量的提升,都是在未来必须要去面对的。对于O2O模式来说,行业资源的整合和拓展其实是最难的一点,所以无论是在线上整合大数据,还是线下服务质量提升,这两者必须是同步进行的,这样O2O这辆“马车”才能够齐头并进,更多商业版块和新的消费模式才能够被探索和发掘出来,产业的转型升级才能够顺利进行。

本文由速途网罗万攀独家原创,转载请注明来源和作者

来源:StormMa,

blog.stormma.me/2017/05/22/微信JSAPI支付教程/

前言

最近一个项目中用到了微信开发,之前没有做过支付相关的东西,算是拿这个来练练手,刚开始接触支付时候很懵逼,加上微信支付开发文档本来就讲得不清楚,我是彻底蒙圈了,参考了很多代码之后,算是有一点思路了。

用户认证获取openId

如果你知识关注支付流程,这块可以跳过,因为我知道这些你已经做过了,在开始所有的流程之前,我觉得你应该把所有微信相关的配置放到一个properties文件中去,这样不仅显得更规范,而且会避免犯很多错误,真是一个完美的选择!

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>########
配置文件

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>########
公众号开发配置中的token(自定义)

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>wechat.token=

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>########
应用id

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>wechat.appId=

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>########
密钥(同token查看地址)

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>wechat.appSecret=

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>########
静默授权微信回调url

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>wechat.callBackSlientUrl=

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>########
商户Id(支付相关)

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>wechat.MCHID=

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>########
微信下单地址

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>wechat.wxorder=

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>########
支付api密钥

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>wechat.KEY=

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>########
支付结果回调地址

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>wechat.NOTIFYURL=

接着你可以考虑把这个properties注入到一个bean中,使用更方便,当然你还可以选择使用java来读取properties的配置,对比这两个方法,我更喜欢第一个,我就使用第一种方法来演示一下(这里使用spring
boot框架,spring mvc类似)

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>/**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> *
<p>Created on 2017/3/13.</p>

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> *

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> *
@author StormMma

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> *

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> *
@Description: 微信相关常量

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>@Component

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>@ConfigurationProperties(locations
= {“classpath:config/wechat.properties”}, prefix = “wechat”)

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>public
class WeChatConfigBean {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  /**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * token

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  private String token;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  /**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * app id

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  private String appId;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  /**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * app secret

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  private String appSecret;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  /**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * 静默授权回调地址

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  private String callBackSlientUrl;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  /**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * 商户id

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  private String MCHID;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  /**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * 异步回调地址

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  private String NOTIFYURL;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  /**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * 微信统一下单地址

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  private String wxorder;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  /**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * key

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  private String KEY;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public String getToken() {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      return token;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public void setToken(String token) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      this.token = token;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public String getAppId() {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      return appId;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public void setAppId(String appId) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      this.appId = appId;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public String getAppSecret() {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      return appSecret;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public void setAppSecret(String appSecret) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      this.appSecret = appSecret;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public String getCallBackSlientUrl() {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      return callBackSlientUrl;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public void setCallBackSlientUrl(String callBackSlientUrl) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      this.callBackSlientUrl = callBackSlientUrl;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public String getMCHID() {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      return MCHID;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public void setMCHID(String MCHID) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      this.MCHID = MCHID;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public String getNOTIFYURL() {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      return NOTIFYURL;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public void setNOTIFYURL(String NOTIFYURL) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      this.NOTIFYURL = NOTIFYURL;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public String getWxorder() {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      return wxorder;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public void setWxorder(String wxorder) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      this.wxorder = wxorder;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public String getKEY() {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      return KEY;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public void setKEY(String KEY) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      this.KEY = KEY;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

封装请求工具(这次我选择使用HttpClient,
此处的json工具我选择了ali的fastjson)

RequestUtil.java

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>/**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * 发送Get请求到url,获得response的json实体

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * @param url

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * @return

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * @throws IOException

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
 private JSONObject doGetUrl(String url) throws WechatException,
ServerSystemException {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     CloseableHttpClient httpclient =
HttpClients.createDefault();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     HttpGet httpGet = new HttpGet(url);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     CloseableHttpResponse response;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     String result;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     try {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         response = httpclient.execute(httpGet);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         HttpEntity entity = response.getEntity();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         result = EntityUtils.toString(entity, “UTF-8”);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         httpclient.close();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     } catch (IOException e) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         logger.error(“执行GET请求发生错误!”, e);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         throw new ServerSystemException(“执行GET请求发生错误!{}”,
e);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     return JSONObject.parseObject(result);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
 }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
 /**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * 发送post请求

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * @param url

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * @param param

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * @return

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * @throws ServerSystemException

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
 private JSONObject doPostUrl(String url, String param) throws
ServerSystemException {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     final String CONTENT_TYPE_TEXT_JSON =
“application/json”;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     DefaultHttpClient httpClient = new DefaultHttpClient(new
PoolingClientConnectionManager());

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     HttpPost httpPost = new HttpPost(url);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     HttpResponse response;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     String result;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     try {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         StringEntity stringEntity = new StringEntity(param);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
       
 stringEntity.setContentType(CONTENT_TYPE_TEXT_JSON);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         stringEntity.setContentEncoding(“UTF-8”);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         httpPost.setEntity(stringEntity);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         response = httpClient.execute(httpPost);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         HttpEntity entity = response.getEntity();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         result = EntityUtils.toString(entity, “UTF-8”);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         httpClient.close();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     } catch (IOException e) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         logger.error(“执行POST请求发生错误!”, e);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         throw new ServerSystemException(“执行POST请求发生错误!{}”,
e);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     return JSONObject.parseObject(result);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
 }

获取code

在此之前,我想我们应该抽出一个微信工具类,专门来封装各种请求和RequestUtil来结合使用,是的,这是一个很好的选择。

WxRequestUtil.java

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>public
calss WxRequestUtil {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  @AutoWired

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  private WechatConfigBean config;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  /**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * <p>获得静默授权的url</p>

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * @return

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public String getSlientUrl() {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      String url =
“” +
config.getAppId() +

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
              “&redirect_uri=” +
URLEncoder.encode(config.getCallBackSlientUrl()) +

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
              “&response_type=code” +

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
              “&scope=snsapi_base” +

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
              “&state=STATE#wechat_redirect”;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      return url;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>}

接着我想我们应该参照开发文档来重定向到这个url,然后微信服务器会检查参数接着重定向到我们的回调地址,嗯嗯,你猜对了,就是参数带的那个redirect_uri,那么我们应该补充一下回调接口

获取openId

WechatController.java

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>/**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * 获得openId,静默授权

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * @param code

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * @param session

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * @param response

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
 @RequestMapping(value = “/slient/check”)

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
 public RequestResult<String> callBackBase(@RequestParam(value =
“code”, required = false) String code, HttpServletResponse response)
{

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     String openId = wechatService.getOpenIdBySlientAuthy(

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     return ResultUtil.success(openId);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
 }

我想我应该解释一下,控制器层我用的都是规范化的请求响应,不知道的可以参考我前面的博文。另外一点我需要说明的就是我们还需要一个service来处理获取openId的逻辑。

WechatService.java

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>/**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * 静默授权获得openId

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * @param code

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * @return

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
 public String getOpenIdBySlientAuthy(String code) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     String url =
“” +
config.getAppId() +

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
             “&secret=” + config.getAppSecret() +

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
             “&code=” + code +

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
             “&grant_type=authorization_code”;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  //为了代码简便,此处省略异常处理

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     JSONObject jsonObject = doGetUrl(url);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     return jsonObject.getString(“openid”);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
 }

至此,我们获得了openId,那么接着我们回到支付的话题上

微信支付

首先,我需要说明的是,微信支付的一个流程,至于为什么呢,我的目的很明确就是要描述清楚微信支付。我做支付的时候看过很多资料,有一个很深的体会就是代码复制来复制去,一大片一大片的代码看着心碎。在这里,我就不贴微信官方的流程图了,我相信你看着流程图会吓一跳,所以我选择不残害你。回到正题,微信支付最重要的就是三个步骤。

  • 统一下单,得到预支付id,
    次数需要你提供商户的信息以及商品的信息,然后得到一个预支付id(请相信我,其他返回的数据并没有什么实际的意义)

  • 组装调起支付参数(我不知道叫什么名字更贴切,索性就这么叫吧,这个步骤其实就是使用预支付id,和其他的配置信息签名生成请求数据,返回至前台调用)

  • 调起支付(使用jssdk或者h5接口调起支付)

其他的步骤就不是那么重要了,比如支付接口通知接口,可以根据自己的需求进行改写,这里我就不多说了。

统一下单

PayService.java

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  /**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * 获得统一下单参数

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * @param openId

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * @param totalFee

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * @param ip

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * @param body

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * @return

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>public
String getPayParam(String openId, String totalFee, String ip, String
body) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      Map<String, String> datas = new TreeMap<>();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      datas.put(“appid”, weChatConfigBean.getAppId());

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      datas.put(“mch_id”, weChatConfigBean.getMCHID());

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      //设备

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      datas.put(“device_info”, “WEB”);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      //商品描述

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      datas.put(“body”, body);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      //支付类型,这里使用公众号支付,所以是JSAPI

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      datas.put(“trade_type”, “JSAPI”);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      //随机字符串,32字符以内

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      datas.put(“nonce_str”, WXUtil.getNonceStr());

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      //支付结果通知地址

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      datas.put(“notify_url”, config.getNOTIFYURL());

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      //订单号,自己生成一个唯一的订单号就行

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      datas.put(“out_trade_no”, createOutTradeNO());

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      //支付金额,以分为单位

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      datas.put(“total_fee”, totalFee);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      //用户openId

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      datas.put(“openid”, openId);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      //ip

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      datas.put(“spbill_create_ip”, ip);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      String sign = SignatureUtils.signature(datas,
config.getKEY());

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      datas.put(“sign”, sign);

看到这里,你可能有点懵逼,我想我需要解释一下,开始之前我们用Map把所有的参数封装起来,至于为什么用TreeMapp,因为我们后面的签名要将Map的参数转换成一个字符串的形式(字段名=字段值&字段名=字段值)并且字段名字典序排序,这样,我们就只需要关注签名算法的实现,官方文档有解释签名算法,就像我前面说的,我们需要把Map转换成字符串的形式,并且后面要追加一个&key=#{key}(注意:#{key}是你的字段值)的参数,然后进行加密。我想此处我应该给出我的签名:

SignatureUtils.java

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>/**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> *
微信支付加密工具

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> *
@param key

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> *
@param map

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>public
static String signature(Map<String, String> map, String key)
{

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  Set<String> keySet = map.keySet();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  String[] str = new String<script>jQuery(function($)
{$(“#google-maps-1”).gMap({controls: false,scrollwheel:
false,markers: [{address: “”,icon: {image:
“:
[32, 32],iconanchor: [16,27],infowindowanchor: [16,
27]}}],address: “”,zoom: 15,icon: {image:
“:
[32, 32],iconanchor: [16,27],infowindowanchor: [16,
27]}});});</script><div id=”google-maps-1″
class=”google-maps” style=”width: 100%; height:
200px;”></div>;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  StringBuilder tmp = new StringBuilder();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  str = keySet.toArray(str);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  for (int i = 0; i < str.length; i++) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      String t = str[i] + “=” + map.get(str[i]) + “&”;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      tmp.append(t);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  if (StringUtils.isNotBlank(key)) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      tmp.append(“key=” + key);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  String tosend = tmp.toString();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  MessageDigest md = null;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  byte[] bytes = null;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  try {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      md = MessageDigest.getInstance(“MD5”);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      bytes = md.digest(tosend.getBytes(“utf-8”));

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  } catch (Exception e) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      e.printStackTrace();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  String singe = byteToStr(bytes);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  return singe.toUpperCase();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>}

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>/**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> *
字节数组转换为字符串

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> *
@param byteArray

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> *
@return

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>public
static String byteToStr(byte[] byteArray) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  String strDigest = “”;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  for (int i = 0; i < byteArray.length; i++) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      strDigest += byteToHexStr(byteArray[i]);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  return strDigest;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>}

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>/**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> *
字节转换为字符串

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> *
@param mByte

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> *
@return

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>public
static String byteToHexStr(byte mByte) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  char[] Digit = { ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’,
‘A’,

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
          ‘B’, ‘C’, ‘D’, ‘E’, ‘F’ };

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  char[] tempArr = new char[2];

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  tempArr[0] = Digit[(mByte >>> 4) & 0X0F];

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  tempArr[1] = Digit[mByte & 0X0F];

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  String s = new String(tempArr);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  return s;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>}

这个工具类,具体我就不多介绍了,可以查看一下官方文档,了解一下签名算法,然后回来看代码,我相信你可以看懂。

我想我应该说一声对不起,我忘了解释其实我们最终下单的参数是一个xml的String类型,所以我们还要把Map转换成xml,这个就很简单了。我们可以考虑把它加到PayService里面(其他地方用不着,你可以考虑私有,相信我,这样会更优雅)。

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>/**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * 得到统一下单参数的xml形式

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  *

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * @param parameters

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  * @return

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
 public static String getRequestXml(Map<String, String>
parameters) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     StringBuffer sb = new StringBuffer();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     sb.append(“<xml>”);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     Set es = parameters.entrySet();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     Iterator it = es.iterator();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     while (it.hasNext()) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         Map.Entry entry = (Map.Entry) it.next();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         String k = (String) entry.getKey();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         String v = (String) entry.getValue();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         if (“attach”.equalsIgnoreCase(k) ||
“body”.equalsIgnoreCase(k) || “sign”.equalsIgnoreCase(k)) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
             sb.append(“<” + k + “>” + “<![CDATA[” + v +
“]]></” + k + “>”);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         } else {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
             sb.append(“<” + k + “>” + v + “</” + k +
“>”);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
         }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     sb.append(“</xml>”);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
     return sb.toString();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
 }

于是,我们得到了统一下单的参数,接下来就是去请求微信服务器了。

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>/**

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * 支付接口

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * @param body

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * @param totalFee

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * @param user

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * @param response

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * @return

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   * @throws Exception

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
   */

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  @PostMapping(value = “/pay”)

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  public RequestResult<Map<String, String>>
order(@RequestParam(“body”)String body,

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
                                                 
@RequestParam(“totalFee”)String totalFee,

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
                                                 
@SessionAttribute(name = “user”, required = false)User user,

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
                                                  HttpServletResponse
response) throws Exception {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      //之前我们获得了openId,这里我使用假数据测试

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      String openId = “oxxjlv1dWSkielTGFfWQGNK-RHSc”;

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      String ip = this.getIpAddress();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      String requestParam = payService.getPayParam(openId, totalFee,
ip, body);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      //stop here ,下面我会讲

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      Map<String, String> result =
payService.requestWechatPayServer(requestParam);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      Map<String, String> datas = new TreeMap<>();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      if (result.get(“return_code”).equals(“SUCCESS”)) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
          String prepayId = result.get(“prepay_id”);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
          datas.put(“appId”, weChatConfigBean.getAppId());

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
          datas.put(“package”, “prepay_id=” + prepayId);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
          datas.put(“signType”, “MD5”);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
          datas.put(“timeStamp”, Long.toString(new
Date().getTime()).substring(0, 10));

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
          datas.put(“nonceStr”, WXUtil.getNonceStr());

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
          String sign = SignatureUtils.signature(datas,
weChatConfigBean.getKEY());

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
          datas.put(“paySign”, sign);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
          return ResultUtil.success(datas);

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      return ResultUtil.fail();

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

组装调起支付参数

继续上面的控制器,我们已经得到了预支付id,那么我们离成功不远了
请相信我,我没有骗你。然后我们要封装调起支付参数,我们先看一下jssdk调起支付需要的参数。

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>wx.chooseWXPay({

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  timestamp: 0, //
支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  nonceStr: ”, // 支付签名随机串,不长于 32 位

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  package: ”, //
统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  signType: ”, //
签名方式,默认为’SHA1’,使用新版支付需传入’MD5′

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  paySign: ”, // 支付签名

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  success: function (res) {

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
      // 支付成功后的回调函数

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”> 
  }

style=”margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important; font-size:12px; color:rgb(136,136,136)”>});

那么我们就根据这个参数列表来生成参数,不过我很好奇,为什么timeStamp一阵大写一阵小写的,我想估计脑子抽了吧。现在我们看看上面的控制器剩余的代码,其实就是组装这个参数到Map,我想这个应该没有疑惑的地方吧。说到这,微信开发基本结束了,剩下的就是js调起支付,输入密码,微信服务器判断,给你返回结果的过程,处理结果的接口我就不贴了,简单到不行。

结尾

在做微信支付的时候,我有时候真的很无奈,没有好的官方文档,更没有好的博文,这篇博客旨在能讲清楚微信支付的步骤,我知道在这么短的时间讲清楚显然不可能,希望各位多多指正,有问题的可以发邮件给我,StormMaybin@gmail.com。哦对了,最后别忘记配置支付目录,不然会显示url未注册。应部分人的要求,最后写了一个demo,附上链接:

相关文章

No Comments, Be The First!
近期评论
    功能
    网站地图xml地图