微信支付SDK-两行代码解决支付

背景

让使用微信支付的朋友最快速度接入微信支付.

核心

两行代码解决微信支付提供的各种服务, 开箱即用, 可扩展性超强(只需根据服务的上下行协议定义协议类后, 放入工厂即可获取调用结果).

架构图

项目源代码

  • 源码地址 http://wocoding.com/item.htm?hashId=wZlZgM81

目前支持的服务及调用示例

所有服务在单元测试类(WXPayClientTest.java)中均已测试通过, 下行参数response.isSuccess == true表示服务调用成功.

扫码支付

文档详见: https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1

String nonceStr = SDKUtils.genRandomStringByLength(32);
UnifiedOrderRequest request = new UnifiedOrderRequest("wuspace-899",SDKUtils.genOutTradeNo(),1, "192.168.1.1", asyncNotifyUrl, "NATIVE", nonceStr);
UnifiedOrderResponse response = wxPayClient.execute(request);
Assert.assertNotNull(response);
LOG.info(JSON.toJSONString(response));

公众号支付

文档详见: https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1

String nonceStr = SDKUtils.genRandomStringByLength(32);
UnifiedOrderRequest request = new UnifiedOrderRequest("wuspace-899",SDKUtils.genOutTradeNo(),
                1, "192.168.1.1", asyncNotifyUrl, "JSAPI", nonceStr);
request.setOpenId("oKVmeuHht8J0Ni58CSNe474AHA3E");
UnifiedOrderResponse response = wxPayClient.execute(request);
Assert.assertNotNull(response);
LOG.info(JSON.toJSONString(response));

APP支付

文档详见: https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1

String nonceStr = SDKUtils.genRandomStringByLength(32);
UnifiedOrderRequest request = new UnifiedOrderRequest("wuspace-899",SDKUtils.genOutTradeNo(),
                1, "192.168.1.1", asyncNotifyUrl, "APP", nonceStr);
UnifiedOrderResponse response = wxPayClient.execute(request);
Assert.assertNotNull(response);
LOG.info(JSON.toJSONString(response));

商家支付

文档详见: https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2

String nonceStr = SDKUtils.genRandomStringByLength(32);
String customerOpenId = "oKVmeuHht8J0Ni58CSNe474AHA3E";
MchPayRequest mchPayRequest = new MchPayRequest(SDKUtils.genOutTradeNo(),
                customerOpenId, "NO_CHECK", 100, "xxxx年xx月结算", "192.168.1.1", nonceStr);
MchPayResponse response = wxPayVIPClient.execute(mchPayRequest);
Assert.assertNotNull(response);
LOG.info(JSON.toJSONString(response));

退款

文档详见: https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_4&index=6

String nonceStr = SDKUtils.genRandomStringByLength(32);
RefundRequest request = new RefundRequest("T15121416014891124211768",
                SDKUtils.genOutRefundNo(), 1, 1, "112102020", nonceStr);
RefundResponse response = wxPayVIPClient.execute(request);
Assert.assertNotNull(response);
LOG.info(JSON.toJSONString(response));

支付异步通知解析

文档详见: https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_7

String notifyTxt = "\n" +
                "  \n" +
                "  \n" +
                "  \n" +
                "  \n" +
                "  \n" +
                "  \n" +
                "  \n" +
                "  \n" +
                "  \n" +
                "  \n" +
                "  \n" +
                "  \n" +
                "  \n" +
                "  \n" +
                "  1\n" +
                "  \n" +
                "  \n" +
                "";
PayNotifyResponse response = wxPayClient.parseNotify(notifyTxt, PayNotifyResponse.class);
Assert.assertNotNull(response);
LOG.info(JSON.toJSONString(response));

刷卡支付

文档详见: https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10&index=1

目前公司未使用, 待续......

扩展

该SDK设计了一个服务工厂, 该工厂中包含HTTP执行器/返回数据解析方式(json/xml)/入参数据格式(json/xml)构造等, 开发人员需要增加服务仅需要根据服务协议文档编写上下行协议, 并在协议中指明API接口和返回数据类型, 再将上行协议放入工厂中执行即可; 可参考已完成的服务协议进行扩展编写.

本系列文章

  • 微信支付SDK-两行代码解决支付

  • 微信服务号SDK-两行代码解决API调用

  • 支付宝支付SDK-两行代码解决支付

  • 平安银行银企直连SDK-两行代码解决API调用

你可能感兴趣的:(ac,java,sdk)