通过微信平台为商家提供代收款服务。
1.微信支付业务-商家注册微信支付业务
2.申请支付订单-商户向支付平台申请支付链接
支付订单并不是用户提交的商品订单,而是商户向微信支付平台申请的支付链接。
(1)导入微信支付的依赖
wxpay的maven依赖:
<!-- https://mvnrepository.com/artifact/com.github.wxpay/wxpay-sdk -->
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
//WXPayConfig对象表示商户信息
WXPayConfig wxPayConfig = new WXPayConfig();
//WxPay对象表示与微信支付平台进行交互的客户端
WxPay wxPay = new WxPay(wxPayConfig);
//data存放进行微信支付请求时的参数
HashMap<String,String> data = new HashMap<>();
data.put("fee_type","CNY"); //设置支付的币种:(CNY:人民币)
data.put("total_fee","9980"); //设置支付的金额
//......
//访问支付订单接口:向微信支付平台发送订单请求.
Map<String,String> resp = wxPay.unfiedOrder(data); //resp表示微信平台返回的响应数据,支付链接就在这个map中。
(2)创建微信支付配置类,配置商户信息
创建一个类,实现WxPayConfig接口。
重写三分方法,分别设置商户appID、商户ID、商户密钥。
MyPayConfig :
public class MyPayConfig implements WXPayConfig {
@Override
public String getAppID() {
return "wx632c8f211f8122c6";
}
@Override
public String getMchID() {
return "1497984412";
}
@Override
public String getKey() {
return "balabalakey";
}
@Override
public InputStream getCertStream() {
return null;
}
@Override
public int getHttpConnectTimeoutMs() {
return 0;
}
@Override
public int getHttpReadTimeoutMs() {
return 0;
}
}
(3)设置支付订单的参数
OrderController :
@RestController
@CrossOrigin
@RequestMapping("/order")
@Api(value = "提供订单相关的操作接口",tags = "订单管理")
public class OrderController {
@Autowired
private OrderService orderService;
@PostMapping("/add")
public ResultVO add(String cids,@RequestBody Orders order) {
ResultVO resultVO = null;
try {
Map<String,String> orderInfo = orderService.addOrder(cids,order);
String orderId = orderInfo.get("orderId");
//String orderId="1234561234567";
if (order != null){
//微信支付:申请支付连接
WXPay wxPay = new WXPay(new MyPayConfig());
HashMap<String,String> data = new HashMap<>();
data.put("body",orderInfo.get("productNames"));//商品描述
data.put("out_trade_no",orderId);//使用当前用户订单的编号作为当前用户订单的编号作为当前交易的交易号
data.put("fee_type","CNY");//支付币种
data.put("total_fee",order.getActualAmount()*100+"");//支付总金额(单位:分)
data.put("trade_type","NATIVE");//交易类型
data.put("notify_url","/pay/success");//订单编号
//发送请求获取响应
Map<String, String> resp = wxPay.unifiedOrder(data);
System.out.println(resp);
orderInfo.put("payUrl",resp.get("code_url"));
resultVO = new ResultVO(ResStatus.OK,"提交订单成功!",orderInfo);
}else {
resultVO = new ResultVO(ResStatus.NO,"提交订单失败",null);
}
}catch (SQLException e){
resultVO = new ResultVO(ResStatus.NO,"提交订单失败",null);
} catch (Exception e) {
e.printStackTrace();
}
return resultVO;
}
}
PayController:
@RestController
@RequestMapping("/pay")
public class PayController {
@PostMapping("/success")
public void success(){
}
}
OrderService:
public interface OrderService {
public Map<String,String> addOrder(String cids, Orders order) throws SQLException;
}
OrderServiceImpl :
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private ShoppingCartMapper shoppingCartMapper;
@Autowired
private OrdersMapper ordersMapper;
@Autowired
private OrderItemMapper orderItemMapper;
@Autowired
private ProductSkuMapper productSkuMapper;
//保存订单业务
@Transactional//事务
public Map<String,String> addOrder(String cids,Orders order) throws SQLException{
Map<String,String> map = new HashMap<>();
//1.根据cids查询当前订单中关联的购物车记录详情(包括库存)
String[] arr = cids.split(",");
List<Integer> cidsList = new ArrayList<>();
for (int i=0; i<arr.length;i++){
cidsList.add(Integer.parseInt(arr[i]));
}
List<ShoppingCartVO> list = shoppingCartMapper.selectShopcartByCids(cidsList);
//2.校验库存
boolean f = true;
String untitled = "";
for (ShoppingCartVO sc:list){
if (Integer.parseInt( sc.getCartNum() )>sc.getSkuStock()){
f=false;
//break;
}
//获取所有商品的名称,以“,”分割拼接成字符串
untitled = untitled + sc.getProductName() + ",";
}
if (f){
//3.表示用户充足----------保存订单
order.setUntitled(untitled);
order.setCreateTime(new Date());
order.setStatus("1");
//生成订单编号
String orderId = UUID.randomUUID().toString().replace("-", "");
order.setOrderId(orderId);
//4.保存订单
ordersMapper.insert(order);
//if (i>0){
//生成商品快照
//List<OrderItem> orderItems = new ArrayList<>();
for (ShoppingCartVO sc:list){
int cnum = Integer.parseInt(sc.getCartNum());
String itemId = System.currentTimeMillis()+""+ (new Random().nextInt(89999)+10000 );
OrderItem orderItem = new OrderItem(itemId, orderId, sc.getProductId(), sc.getProductName(), sc.getProductImg(),
sc.getSkuId(), sc.getSkuName(),new BigDecimal(sc.getSellPrice()) , cnum,
new BigDecimal(sc.getSellPrice() * cnum) , new Date(), new Date(), 0);
//orderItems.add(orderItem);
orderItemMapper.insert(orderItem);
}
//int j = orderItemMapper.insertList(orderItems);
//5.扣减库存
for (ShoppingCartVO sc:list){
String skuId = sc.getSkuId();
int newStock = sc.getSkuStock() - Integer.parseInt(sc.getCartNum());
// Example example = new Example(ProductSku.class);
// Example.Criteria criteria = example.createCriteria();
// criteria.andEqualTo("skuId",skuId);
// ProductSku productSku = productSkuMapper.selectByPrimaryKey(skuId);
// //ProductSku productSku = new ProductSku();
// productSku.setStock(newStock);
// int k = productSkuMapper.updateByPrimaryKey(productSku);
ProductSku productSku = new ProductSku();
productSku.setSkuId(skuId);
productSku.setStock(newStock);
productSkuMapper.updateByPrimaryKeySelective(productSku);
}
//6.删除购物车:当购物车中的记录购买成功后,购物车对应做删除操作
for (int cid:cidsList){
shoppingCartMapper.deleteByPrimaryKey(cid);
}
map.put("orderId",orderId);
map.put("productNames",untitled);
return map;
}else {
//表示库存不足
return null;
}
}
}
(4)测试(由于未申请微信商铺,所以返回的支付url未空)
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/128091.html