引入依赖
在github上搜索best-pay-sdk,点击使用文档有详细的使用教程:best-pay-sdk使用教程
<dependency>
<groupId>cn.springboot</groupId>
<artifactId>best-pay-sdk</artifactId>
<version>1.3.0</version>
</dependency>
在业务层创建接口及其实现类
- IPayService
public interface IPayService {
/*
* 创建支付
* */
void create();
}
- PayServiceImpl
@Service
@Slf4j
public class PayServiceImpl implements IPayService {
@Override
public void create() {
/*微信支付配置*/
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);//用日志的方式打印通知信息
}
}
测试类
@SpringBootTest
@RunWith(SpringRunner.class)
public class PayServiceImplTest{
@Autowired
private PayServiceImpl payService;
@Test
public void create() {
payService.create();
}
}
运行结果
其中codeUrl是微信返回的链接,将该链接生成二维码,微信扫码是能够付款的