支付项目实战(二)——将微信返回的codeUrl在前端页面生成二维码

service层

  1. IPayService
	public interface IPayService {
	    /*
	    * 创建支付
	    * */
	    PayResponse create(Long orderId, BigDecimal amount);
	}
  1. PayServiceImpl
	@Service
	@Slf4j
	public class PayServiceImpl implements IPayService {
	    @Override
	    public PayResponse create(Long orderId,BigDecimal amount) {
	        /*微信支付配置*/
	        WxPayConfig wxPayConfig =new WxPayConfig();
	        wxPayConfig.setAppId("xxx");//申请的公众号id
	        wxPayConfig.setMchId("xxx");//商户id
	        wxPayConfig.setMchKey("xxx");//商户秘钥
	        wxPayConfig.setNotifyUrl("xxx");//通知地址,该地址需要公网能够访问。发起支付后,微信会返回一些信息,该地址是用于接收微信的异步通知
	
	        /*支付类*/
	        BestPayServiceImpl bestPayService=new BestPayServiceImpl();
	        bestPayService.setWxPayConfig(wxPayConfig);
	
	        /*发起支付*/
	        PayRequest payRequest = new PayRequest();
	        payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_NATIVE);//支付方式
	        payRequest.setOrderId("23423947293847293842");//订单号
	        payRequest.setOrderAmount(0.01);//订单金额
	        payRequest.setOrderName("8804022-全世界最蠢的猪");//订单名称
	        PayResponse response = bestPayService.pay(payRequest);//微信返回信息
	        log.info("response={}",response);//用日志的方式打印通知信息
	        return response;
	    }
	}

controller层

@Controller
@RequestMapping("/pay")
public class PayController {
    @Autowired
    private IPayService payService;
    @GetMapping("/create")
    //@RequestParam("orderId") Long orderId 用于接收参数:订单号
    //ModelAndView 视图
    public ModelAndView create(@RequestParam("orderId") Long orderId,
                               @RequestParam("amount") BigDecimal amount){
        PayResponse response = payService.create(orderId, amount);
        Map<String,String> map=new HashMap<>();
        map.put("codeUrl",response.getCodeUrl());//将获得的codeUrl放入map中
        return new ModelAndView("create",map);//携带数据map到create.ftl页面
    }
}

在resources ->templatess里创建create.ftl文件

	<!DOCTYPE html>
	<html>
	    <head>
	        <meta charset="utf-8">
	        <title>支付</title>
	    </head>
	    <body>
	
	        <div id="myQrcode"></div>
	
	        <script src="https://cdn.bootcss.com/jquery/1.5.1/jquery.min.js"></script>
	        <script src="https://cdn.bootcss.com/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
	        <script>
	            jQuery('#myQrcode').qrcode({
	                text: "${codeUrl}"
	            });
	        </script>
	    </body>
	</html>

运行结果

微信扫码可以支付0.01元
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值