微信支付入门

微信支付和支付宝在线支付越来越重要,在各种产品中多多少少都会和支付打交道,此篇博客用采用第三方开源代码best-pay-sdk封装好的api作为入门代码。

导入开源jar包到SrpingBoot中:

 <dependency> <groupId>cn.springboot</groupId> <artifactId>best-pay-sdk</artifactId> <version>1.3.0</version> </dependency>

best-pay-sdk具体使用可参考文档:

https://github.com/Pay-Group/best-pay-sdk/blob/develop/doc/use.md

微信支付(native方式):

新建支付service接口:

package cn.blogsx.pay.service;import com.lly835.bestpay.model.PayResponse;import java.math.BigDecimal;public interface IPayService { /** * 创建/发起支付 */ PayResponse create(String orderId, BigDecimal amount);}

实现类:

以下公众账号appid、mchId、mchKey 均作演示,并非实际可用值

package cn.blogsx.pay.service.impl;import cn.blogsx.pay.service.IPayService;import com.lly835.bestpay.config.WxPayConfig;import com.lly835.bestpay.enums.BestPayTypeEnum;import com.lly835.bestpay.model.PayRequest;import com.lly835.bestpay.model.PayResponse;import com.lly835.bestpay.service.impl.BestPayServiceImpl;import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Service;import java.math.BigDecimal;/** * @author Alex * @create 2020-03-21 16:05 **/@Slf4j@Servicepublic class PayService implements IPayService { @Override public PayResponse create(String orderId, BigDecimal amount) { WxPayConfig wxPayConfig = new WxPayConfig(); wxPayConfig.setAppId("wxd898fcb0fasfda658"); wxPayConfig.setMchId("1483fsfdfsf2"); wxPayConfig.setMchKey("0adf23rBfdsfasB4F6"); wxPayConfig.setNotifyUrl("http://127.0.0.1"); BestPayServiceImpl bestPayService = new BestPayServiceImpl(); bestPayService.setWxPayConfig(wxPayConfig); PayRequest request = new PayRequest(); request.setOrderName("45342434-测试支付sdk"); request.setOrderId(orderId); request.setOrderAmount(amount.doubleValue()); request.setPayTypeEnum(BestPayTypeEnum.WXPAY_NATIVE); PayResponse response = bestPayService.pay(request); log.info("response={}",response); return response; }}

测试后可微信后台会以xml文档作为返回值,code_url值传递给前端可转为微信支付二维码。这里使用freemarker做测试使用。

微信支付入门
 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>

新建Controller:

package cn.blogsx.pay.controller;import cn.blogsx.pay.service.impl.PayService;import com.lly835.bestpay.model.PayResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.servlet.ModelAndView;import java.math.BigDecimal;import java.util.HashMap;import java.util.Map;/** * @author Alex * @create 2020-03-21 16:51 **/@Controller@RequestMapping("/pay")public class PayController { @Autowired private PayService payService; @GetMapping("/create") public ModelAndView create(@RequestParam("orderId") String orderId, @RequestParam("amount") BigDecimal amount) { PayResponse payResponse = payService.create(orderId, amount); Map map = new HashMap<>(); map.put("codeUrl",payResponse.getCodeUrl()); return new ModelAndView("create",map); }}

新建名为create的freemarker文件,并采用jquery.qrcode作为前端生成二维码的插件:

<!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>

浏览器输入:http://localhost:8080/pay/create?orderId=132342145342141&amount=0.01 即可得到支付二维码

相关文章