文章有不当之处,欢迎指正,若是喜欢微信阅读,你也能够关注个人微信公众号:
好好学java
,获取优质学习资源。php
首先咱们到微信支付的官方文档的开发步骤部分查看一下须要的设置。 [图片上传失败...(image-5eb825-1531014079742)]java
由于微信支付须要较高的权限,只有认证了得服务号才有使用微信支付接口的权限,咱们我的很难申请到,因此须要向其余朋友借用帐号。git
来到文档的业务流程部分,查看微信支付的流程(我以为这个仍是须要十分仔细的了解和查看的,这有助于你理解微信开发的流程)。 github
而后,访问微信支付接口是要传递的参数不少,见统一下单 [图片上传失败...(image-df7051-1531014079742)]api
经过查看上面的这些微信支付的官方文档以后,我相信你对这些应该有了必定的了解了,可是仍是以为微信支付的开发十分的麻烦,因此咱们接下来使用第三方的sdk来开发。浏览器
这个是公众号支付,咱们使用**best-pay-sdk**,这个SDK使用PayRequest
和PayResponse
对请求接口和相应结果作了大量的封装,主要须要动态传入的参数是openid
(用户惟一标识)和orderId
。接下来咱们看看如何开发。bash
//微信公众帐号支付配置
WxPayH5Config wxPayH5Config = new WxPayH5Config();
wxPayH5Config.setAppId("xxxxx");
wxPayH5Config.setAppSecret("xxxxxxxx");
wxPayH5Config.setMchId("xxxxxx");
wxPayH5Config.setMchKey("xxxxxxx");
wxPayH5Config.setNotifyUrl("http://xxxxx");
//支付类, 全部方法都在这个类里
BestPayServiceImpl bestPayService = new BestPayServiceImpl();
bestPayService.setWxPayH5Config(wxPayH5Config);
复制代码
PayRequest payRequest = new PayRequest();
payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
payRequest.setOrderId("123456");
payRequest.setOrderName("微信公众帐号支付订单");
payRequest.setOrderAmount(0.01);
payRequest.setOpenid("openid_xxxxxx");
bestPayService.pay(payRequest);
复制代码
bestPayService.asyncNotify();
复制代码
这就是这个sdk所说的10行代码解决微信支付。微信
支付完成后,微信会返回给把支付结果以一段支付xml的数据返回给咱们,咱们须要将这段数据传递给异步通知url(notify_url)
,来完成支付结果的验证(验证签名,验证支付状态),这两步SDK都为咱们作好了,只需这样调用bestPayService.asyncNotify(notifyData)
;,完成验证后,咱们须要返回给微信这样一段数据:微信开发
<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg>
</xml>
复制代码
告诉微信已完成验证,不要再给咱们发送异步通知的请求。app
是否是仍是不太清楚如何集成到项目?不要紧,这个还有示例demo,能够更加明了的清楚。
demo网址为:https://github.com/Pay-Group/best-pay-demo
咱们最主要的controller
放在这里:
@Controller
@Slf4j
public class PayController {
@Autowired
private BestPayServiceImpl bestPayService;
/**
* 发起支付
*/
@GetMapping(value = "/pay")
public ModelAndView pay(@RequestParam("openid") String openid,
Map<String, Object> map) {
PayRequest request = new PayRequest();
Random random = new Random();
//支付请求参数
request.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
request.setOrderId(String.valueOf(random.nextInt(1000000000)));
request.setOrderAmount(0.01);
request.setOrderName("最好的支付sdk");
request.setOpenid(openid);
log.info("【发起支付】request={}", JsonUtil.toJson(request));
PayResponse payResponse = bestPayService.pay(request);
log.info("【发起支付】response={}", JsonUtil.toJson(payResponse));
map.put("payResponse", payResponse);
return new ModelAndView("pay/create", map);
}
/**
* 异步回调
*/
@PostMapping(value = "/notify")
public ModelAndView notify(@RequestBody String notifyData) throws Exception {
log.info("【异步回调】request={}", notifyData);
PayResponse response = bestPayService.asyncNotify(notifyData);
log.info("【异步回调】response={}", JsonUtil.toJson(response));
return new ModelAndView("pay/success");
}
}
复制代码
这个能够本身去下载就能够,下面看一下一下如何运行
须要在Jdk版本>1.8上运行 本项目采用SpringBoot1.5.1开发
src/main/java/com/github/lly835
├── PayDemoApplication.java
├── ServletInitializer.java
├── config
│ └── PayConfig.java //支付密钥配置类
└── controller
└── PayController.java //支付调用
复制代码
运行前须要先配置好密钥, 见PayConfig.java
运行命令
git clone https://github.com/Pay-Group/best-pay-demo
cd best-pay-demo
mvn clean package
java -jar target/*.war
复制代码
浏览器访问http://127.0.0.1:8080/pay