当使用手机支付宝扫码之后,支付宝会解析二维码中的网址,向我们的服务器发起请求;此时拿到请求携带的订单编号,查询支付记录;如果已支付则直接返回,否则拼接参数向支付宝发起支付请求,支付宝会返回一个form表单页面,用于拉取支付宝手机支付。
接口信息
支付记录Service
根据订单编号查询支付记录
在com.swx.orders.service
包下的 XcPayRecordService 中定义查询支付记录方法:
XcPayRecordServicepublic interface XcPayRecordService extends IService<XcPayRecord> {
XcPayRecord getOneByPayNo(String payNo); }
|
在实现类中实现该方法
XcPayRecordServiceImpl
@Service public class XcPayRecordServiceImpl extends ServiceImpl<XcPayRecordMapper, XcPayRecord> implements XcPayRecordService {
@Override public XcPayRecord getOneByPayNo(String payNo) { return getOne(Wrappers.<XcPayRecord>lambdaQuery().eq(XcPayRecord::getPayNo, payNo)); } }
|
扫码下单Controller
OrderController@ApiOperation("扫码下单接口") @RequestMapping("/requestpay") public void requestPay(String payNo, HttpServletResponse httpResponse) throws IOException { XcPayRecord xcPayRecord = xcPayRecordService.getOneByPayNo(payNo); if (xcPayRecord == null) { throw new BizException("支付记录不存在"); } if (xcPayRecord.getStatus().equals("601002")) { throw new BizException("已支付,无需重复支付"); } AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, APP_ID, APP_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE); AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); request.setNotifyUrl(NOTIFY_URL); JSONObject bizContent = new JSONObject(); bizContent.put("out_trade_no", payNo); bizContent.put("total_amount", xcPayRecord.getTotalPrice()); bizContent.put("subject", xcPayRecord.getOrderName());
bizContent.put("product_code", "QUICK_WAP_WAY");
request.setBizContent(bizContent.toString()); String form = ""; try { form = alipayClient.pageExecute(request).getBody(); } catch (AlipayApiException e) { e.printStackTrace(); throw new BizException("拉取支付宝支付失败"); } httpResponse.setContentType("text/html;charset=" + AlipayConfig.CHARSET); httpResponse.getWriter().write(form); httpResponse.getWriter().flush(); }
|