好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java开发SSM框架微信支付的实现

微信小程序的java支付开发一直是一块坑,网上的教程也是琳琅满目。笔者六月的时候接触到了微信的小程序开发摸到了 微信支付 方面的东西,腾讯的官方文档也是一言难尽很多地方看不懂,而且官方也没有提供java的示范导致java做微信支付不得不自己踩坑。现在我把自己微信支付开发的步骤和代码都在下面展示出来,希望有没有做出来的朋友不要心急跟着我的步骤走就没问题。

第一步:首先微信支付的话只能是企业的开发账户才能使用的如果你是个人开发者是无法开通微信支付的。我们首先拿到账号,然后拿到微信支付相关的商户号和商户支付密钥,这些东西公司都会提供。有了这些以后就可以进行开发了。

?

1

2

3

4

5

6

7

8

9

public class configure {

   //商户支付密钥

   private static string key = "****************************" ;

   //小程序id

   private static string appid = "***************" ;

   //商户号

   private static string mch_id = "*********" ;

   //小程序密钥

   private static string secret = "********************" ;

我把开发者账号和商户号都放在了一个工具类方便在后面的调用。

第二步:做支付要先获取到用户的openid这是一个很重要的参数你必须要拿到的东西不然就无法完成支付。下面是我获取用户的openid的代码。

?

1

2

3

4

5

6

7

8

9

10

protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {

     string code = request.getparameter( "code" );

     httpget httpget = new httpget( "https://api.weixin.qq.com/sns/jscode2session?appid=" +configure.getappid()+ "&secret=" +configure.getsecret()+ "&js_code=" +code+ "&grant_type=authorization_code" );

         //设置请求器的配置

     httpclient httpclient = httpclients.createdefault();

     httpresponse res = httpclient.execute(httpget);

     httpentity entity = res.getentity();

     string result = entityutils.tostring(entity, "utf-8" );

     response.getwriter().append(result);

   }

第三步:获取到用户的openid之后我们就要进行下单。简单来说要把微信支付所需要的参数都准备好然后给到官方提供的api。这些东西在官方的文档里面各位可以去查阅。下面是我传递参数的代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

string openid = request.getparameter( "openid" );

int totalfee=integer.parseint(request.getparameter( "totalfee" )); //获取支付金额

string out_trade_no=randomstringgenerator.getrandomstringbylength( 32 ); //商户订单号

orderinfo order = new orderinfo();

order.setappid(configure.getappid());

order.setmch_id(configure.getmch_id());

order.setnonce_str(randomstringgenerator.getrandomstringbylength( 32 ));

order.setbody( "测试" );

order.setout_trade_no(out_trade_no);

order.settotal_fee(totalfee);

order.setspbill_create_ip( "***.***.***.**" );

order.setnotify_url( "https://www.see-source.com/weixinpay/payresult" );

order.settrade_type( "jsapi" );

order.setopenid(openid);

order.setsign_type( "md5" );

可以看到上面的传递的参数中有需要支付的金额、和随机生成的32位商户订单号、以及开发者的appid和支付说明支付的ip地址这些参数我们想办法都拿到之后是否就能丢给官方api呢?当然还不是,往下看第四步。

第四步:我们拿到了参数之后能否直接给到api呢?微信支付出于安全考虑参数需要进行加密之后发送给微信支付api。我们得把刚刚拿到的参数进行签名加密,我这里用到的是md5的加密方式,代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

try {

       string repay_id = request.getparameter( "repay_id" );

       signinfo signinfo = new signinfo();

       signinfo.setappid(configure.getappid());

       long time = system.currenttimemillis()/ 1000 ;

       signinfo.settimestamp(string.valueof(time));

       signinfo.setnoncestr(randomstringgenerator.getrandomstringbylength( 32 ));

       signinfo.setrepay_id( "prepay_id=" +repay_id);

       signinfo.setsigntype( "md5" );

       //生成签名

       string sign = signature.getsign(signinfo);

 

       jsonobject json = new jsonobject();

       json.put( "timestamp" , signinfo.gettimestamp());

       json.put( "noncestr" , signinfo.getnoncestr());

       json.put( "package" , signinfo.getrepay_id());

       json.put( "signtype" , signinfo.getsigntype());

       json.put( "paysign" , sign);

       l.info( "-------再签名:" +json.tojsonstring());

       response.getwriter().append(json.tojsonstring());

     } catch (exception e) {

       // todo auto-generated catch block

       e.printstacktrace();

       l.error( "-------" , e);

     }

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

public static string getsign(object o) throws illegalaccessexception {

     arraylist<string> list = new arraylist<string>();

     @suppresswarnings ( "rawtypes" )

     class cls = o.getclass();

     field[] fields = cls.getdeclaredfields();

     for (field f : fields) {

       f.setaccessible( true );

       if (f.get(o) != null && f.get(o) != "" ) {

         string name = f.getname();

         xstreamalias anno = f.getannotation(xstreamalias. class );

         if (anno != null )

           name = anno.value();

         list.add(name + "=" + f.get(o) + "&" );

       }

     }

     int size = list.size();

     string [] arraytosort = list.toarray( new string[size]);

     arrays.sort(arraytosort, string.case_insensitive_order);

     stringbuilder sb = new stringbuilder();

     for ( int i = 0 ; i < size; i ++) {

       sb.append(arraytosort[i]);

     }

     string result = sb.tostring();

     result += "key=" + configure.getkey();

     system.out.println( "签名数据:" +result);

     result = md5util.md5encode(result, "utf-8" ).touppercase();

     return result;

   }

到这一步就已经支付完成了。

通过官方api返回的回调类型来判断支付是否成功。

只要大家按照步骤来就可以实现,以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:http://blog.51cto.com/13981400/2309067

查看更多关于Java开发SSM框架微信支付的实现的详细内容...

  阅读:16次