持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第20天,点击查看活动详情
每日英语:
Be not cheap or mediocre in desiring.
翻译:欲望不要落俗,也不得平庸。 ——埃兹拉·庞德
支付下单
支付流程如上图,用户下单后会调用支付系统执行预支付下单(统一下单)获取二维码的链接地址。此时是从客户端向支付系统发起预支付下单,预支付下单需要调用微信支付统一下单接口。
统一下单
统一下单的API地址pay.weixin.qq.com/wiki/doc/ap…,该地址详细讲解了统一下单接口所需参数以及返回数据,我们参考微信支付 Java SDK使用说明.md中使用SDK实现统一下单操作。
使用微信支付SDK我们需要先创建WXPay的实例并交给Spring容器管理,修改com.xz.mall.MallPayApplication添加如下方法:
/****
* 微信支付SDK对象
* @param weixinPayConfig
* @return
* @throws Exception
*/
@Bean
public WXPay wxPay(WeixinPayConfig weixinPayConfig) throws Exception {
return new WXPay(weixinPayConfig);
}
1)Service
接口:创建com.xz.mall.pay.service.WeixinPayService,创建统一下单方法
public interface WeixinPayService {
/***
* 统一下单,获取支付二维码
*/
Map<String,String> preOrder(Map<String,String> dataMap) throws Exception;
}
实现类:创建com.xz.mall.pay.service.impl.WeixinPayServiceImpl实现统一下单
@Service
public class WeixinPayServiceImpl implements WeixinPayService {
@Autowired
private WXPay wxPay;
/***
* 统一下单,获取支付二维码
*/
@Override
public Map<String, String> preOrder(Map<String, String> dataMap) throws Exception {
Map<String, String> resp = wxPay.unifiedOrder(dataMap);
return resp;
}
}
2)Controller
创建com.xz.mall.pay.controller.WeixinPayController,并创建支付预下单方法,代码如下:
@RestController
@RequestMapping(value = "/wx")
@CrossOrigin
public class WeixinPayController {
@Autowired
private WeixinPayService weixinPayService;
/*****
* 预下单
*/
@GetMapping(value = "/pay")
public RespResult<Map> pay(@RequestParam Map<String,String> map) throws Exception {
//1分钱测试
if(map!=null){
Map<String, String> resultMap = weixinPayService.preOrder(map);
resultMap.put("orderNumber",map.get("out_trade_no"));
resultMap.put("money",map.get("total_fee"));
return RespResult.ok(resultMap);
}
return RespResult.error("支付系统繁忙,请稍后再试!");
}
}
请求测试(请求路径中的参数是订单信息,可以参考统一下单API):http://localhost:8090/wx/pay?out_trade_no=ooopppuuuu&device_info=aa&total_fee=1&spbill_create_ip=127.0.0.1¬ify_url=aa&trade_type=NATIVE&body=aa
效果如下:
{
"code": 20000,
"data": {
"nonce_str": "wJrO7WF8ilj94PjA",
"orderNumber": "ooopppuuuu",
"sign": "56604E571F782AD0194249EAC3C09EEB",
"return_msg": "OK",
"mch_id": "1576040561",
"prepay_id": "wx271508177677061a80be4139d5dd1f0000",
"device_info": "aa",
"money": "1",
"code_url": "weixin://wxpay/bizpayurl?pr=EDSssZM00",
"appid": "wx9f1fa58451efa9b2",
"trade_type": "NATIVE",
"result_code": "SUCCESS",
"return_code": "SUCCESS"
},
"message": "操作成功"
}
总结
本篇主要介绍了一下支付流程,还有统一下单时,对微信支付1分钱的一个测试。