商户系统主动查询支付结果
<h3>编写UpdateOrderStatusForm.java</h3>
<pre><code class="language-java">@Data
@ApiModel(value = "更新订单状态的表单")
public class UpdateOrderStatusForm {
@ApiModelProperty(value = "订单ID")
@Min(1)
private Integer orderId;
}</code></pre>
<h3>编写WXController.java</h3>
<pre><code class="language-java">@Login
@PostMapping("/updateOrderStatus")
@ApiOperation("更新商品订单状态")
public R updateOrderStatus(@RequestBody UpdateOrderStatusForm form, @RequestHeader HashMap header) {
ValidatorUtils.validateEntity(form);
String token = header.get("token").toString();
int userId = Integer.parseInt(jwtUtils.getClaimByToken(token).getSubject());
int orderId = form.getOrderId();
OrderEntity orderEntity = new OrderEntity();
orderEntity.setUserId(userId);
orderEntity.setId(orderId);
QueryWrapper wrapper = new QueryWrapper(orderEntity);
int count = orderService.count(wrapper);
if (count == 0) {
return R.error("用户与订单不匹配");
}
orderEntity = orderService.getOne(wrapper);
String code = orderEntity.getCode();
HashMap map = new HashMap();
map.put("appid", appId);
map.put("mch_id", mchId);
map.put("out_trade_no", code);
map.put("nonce_str", WXPayUtil.generateNonceStr());
try {
String sign = WXPayUtil.generateSignature(map, key);
map.put("sign", sign);
WXPay wxPay = new WXPay(myWXPayConfig);
Map<String, String> result = wxPay.orderQuery(map);
String returnCode = result.get("return_code");
String resultCode = result.get("result_code");
if ("SUCCESS".equals(returnCode) && "SUCCESS".equals(resultCode)) {
String tradeState = result.get("trade_state");
if ("SUCCESS".equals(tradeState)) {
UpdateWrapper updateWrapper = new UpdateWrapper();
updateWrapper.eq("code", code);
updateWrapper.set("status", 2);
updateWrapper.set("payment_type",1);
orderService.update(updateWrapper);
return R.ok("订单状态已修改");
} else {
return R.ok("订单状态未修改");
}
}
return R.ok("订单状态未修改");
} catch (Exception e) {
e.printStackTrace();
return R.error("查询支付订单失败");
}
}</code></pre>
<h3>编写order.js</h3>
<pre><code class="language-javascript">function searchOrderStatus(orderId){
let flag=false
$.ajax({
url:wxUrl.searchOrderStatus,
type:"post",
dataType:"json",
contentType:"application/json",
async:false,
beforeSend: function(request) {
request.setRequestHeader("token", localStorage.getItem("token"));
},
data:JSON.stringify({
"orderId":orderId
}),
success:function(resp){
// console.log(resp)
if(resp.msg=="订单状态已修改"){
flag=true
}
}
})
return flag
}</code></pre>