APP微信支付实现
参考官方文档
设计到的API: 统一下单API、支付结果通知API和查询订单API
下面代码是请求预支付ID
// 构建下单bean final WxPayUnifiedOrderBean unifiedOrderBean = new WxPayUnifiedOrderBean(); unifiedOrderBean.setAppid(wxPayConfig.getAppId()); unifiedOrderBean.setMch_id(wxPayConfig.getMchId()); unifiedOrderBean.setNonce_str(this.getNonceStr(16)); unifiedOrderBean.setBody("支付"); unifiedOrderBean.setOut_trade_no(order.getHubOrderNo()); unifiedOrderBean.setTotal_fee(order.getTotalFee()); unifiedOrderBean.setSpbill_create_ip(order.getSpbill_create_ip()); unifiedOrderBean.setNotify_url(this.paymentProperties.getWeixin().getNotify_url()); unifiedOrderBean.setSign(this.generateSignForPrePayId(unifiedOrderBean, wxPayConfig.getSecret())); // 请求统一下单接口 final WxPayPrePayBean bean = this.requestWx(unifiedOrderBean); /** * 请求统一下单接口 * * @param unifiedOrderBean * @return */ private WxPayPrePayBean requestWx(final WxPayUnifiedOrderBean unifiedOrderBean) { final XStream xStream = new XStream(new DomDriver("UTF-8", new NoNameCoder())); xStream.autodetectAnnotations(true); final String postXml = xStream.toXML(unifiedOrderBean); // 发送请求 final HttpHeaders headers = new HttpHeaders(); final MediaType type = MediaType.parseMediaType("application/x-www-form-urlencoded; charset=UTF-8"); headers.setContentType(type); final HttpEntity<String> requestEntity = new HttpEntity<>(postXml, headers); final RestTemplate restTemplate = new RestTemplate(); final ResponseEntity<String> responseEntity = restTemplate.postForEntity( PaymentConstant.WX_PREPARE_ORDER_URL, requestEntity, String.class); // 解析请求结果 final XStream xStreamResult = new XStream(new DomDriver("UTF-8", new NoNameCoder())); xStreamResult.processAnnotations(WxPayPrePayBean.class); try { final String returnStr = new String(responseEntity.getBody().getBytes("ISO-8859-1"), "UTF-8"); System.out.println(responseEntity.getBody()); System.out.println(returnStr); final WxPayPrePayBean bean = (WxPayPrePayBean) xStreamResult.fromXML(returnStr); return bean; } catch (final Exception e) { e.printStackTrace(); return null; } }
---栖息之鹰(一个外表懒洋洋的内心有激情的程序员)
此博客为笔者原著,转载时请注明出处,谢谢!