native支付官方文档v2:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_1
native支付用于常规的网页支付
@RestController
@RequestMapping("/payment")
public class PaymentController {
private User user = new User(1,"cc","123");
private Integer orderId = 1;
@Autowired
OrderService orderService;
@Autowired
CcWxPayConfig wxPayConfig;
@GetMapping("/native")
public void nativePayment(HttpServletResponse response) throws Exception {
Order order = orderService.getById(orderId);
String amount = order.getAmount().multiply(new BigDecimal("100")).intValue()+"";
WXPay wxPay = new WXPay(wxPayConfig);
HashMap<String, String> map = new HashMap<>();
map.put("nonce_str", WXPayUtil.generateNonceStr());
map.put("body","商品描述");
map.put("out_trade_no",order.getOrderNo());
map.put("total_fee",amount);
map.put("spbill_create_ip","127.0.0.1");
map.put("notify_url","https://127.0.0.1");
map.put("trade_type","NATIVE");
String sign = WXPayUtil.generateSignature(map,wxPayConfig.getMiniKey());
map.put("sign",sign);
Map<String, String> result = wxPay.unifiedOrder(map);
String prepayId = result.get("prepay_id");
// 支付连接
String codeUrl = result.get("code_url");
// ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ServletOutputStream outputStream = response.getOutputStream();
QrCodeUtil.generate(codeUrl,300,300,"jpg",outputStream);
outputStream.close();
}
@GetMapping("/searchOrder")
public void searchOrderStatus() throws Exception {
Order order = orderService.getById(this.orderId);
HashMap<String, String> map = new HashMap<>();
map.put("appid",wxPayConfig.getMiniAppId());
map.put("mch_id",wxPayConfig.getMiniMchId());
map.put("out_trade_no",order.getOrderNo());
map.put("nonce_str",WXPayUtil.generateNonceStr());
String sign = WXPayUtil.generateSignature(map,wxPayConfig.getMiniKey());
map.put("sign",sign);
WXPay wxPay = new WXPay(wxPayConfig);
Map<String, String> result = wxPay.orderQuery(map);
String returnCode = result.get("return_code");
String resultCode = result.get("result_code");
System.out.println("resultCode = " + resultCode);
System.out.println("returnCode = " + returnCode);
String tradeStatus = result.get("trade_state");
System.out.println("tradeStatus = " + tradeStatus);
}
}