标签:data pos bean content dubbo private utils blog order
pom坐标
<dependency> <groupId>com.github.wxpay</groupId> <artifactId>wxpay-sdk</artifactId> <version>0.0.3</version> </dependency>
weixinpay.properties
appid=wx**********
partner=**********
partnerkey=*******
notifyurl=http://******.ngrok.io/WeChatPay/WeChatPayNotify
import com.alibaba.dubbo.config.annotation.Service; import com.github.wxpay.sdk.WXPayUtil; import com.pinyougou.mapper.TbOrderMapper; import com.pinyougou.mapper.TbPayLogMapper; import com.pinyougou.pay.service.PayService; import com.pinyougou.pojo.TbOrder; import com.pinyougou.pojo.TbPayLog; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; import utils.HttpClientUtil; import javax.jws.Oneway; import java.util.Date; import java.util.HashMap; import java.util.Map; @Service public class PayServiceImpl implements PayService { @Autowired private RedisTemplate redisTemplate; @Value("${appid}") private String appid; //商家id @Value("${partner}") private String partner; //商户号 @Value("${partnerkey}") private String partnerkey; //商户密钥 @Value("${notifyurl}") private String notifyurl; //回调地址,不是必要参数,但是必须传 @Autowired private TbPayLogMapper payLogMapper; @Autowired private TbOrderMapper orderMapper; @Override public Map createNative(String out_trade_no, String total_fee) { //准备请求微信的api try { //参数设置微信支付连接地址 HttpClientUtil clientUtil = new HttpClientUtil("https://api.mch.weixin.qq.com/pay/unifiedorder"); //设置请求的参数 Map map = new HashMap(); map.put("appid", appid); map.put("mch_id", partner); map.put("nonce_str", WXPayUtil.generateUUID()); //32位要求的字符串 map.put("body", "随便写"); map.put("out_trade_no", out_trade_no); //内部订单id map.put("total_fee", total_fee);//总价格 map.put("spbill_create_ip", "127.0.0.1");//写个ip就行 map.put("notify_url", notifyurl); map.put("trade_type", "NATIVE"); //NATIVE是二维码原生方式 String signedXml = WXPayUtil.generateSignedXml(map, partnerkey);//微信提供的方法 System.out.println("=请求内容=:"+signedXml); clientUtil.setHttps(true); //微信接口是https协议 clientUtil.setXmlParam(signedXml); //需要将参数设置为xml格式的string字符串 clientUtil.post(); //发送post请求 //获取返回值xml类型的string字符串 String content = clientUtil.getContent(); System.out.println("=返回内容="+content); //通过微信工具类将xml转成map Map responseMap = WXPayUtil.xmlToMap(content);//微信提供的方法 Map returnMap = new HashMap<>(); //封装map是为了指定返回的参数内容 returnMap.put("out_trade_no", out_trade_no); returnMap.put("total_fee",total_fee ); returnMap.put("code_url", responseMap.get("code_url")); //微信支付地址 return returnMap; } catch (Exception e) { e.printStackTrace(); return null; } } //查询支付状态 @Override public Map queryPayStatus(String out_trade_no) { try { //准备map请求对象 Map map = new HashMap(); map.put("appid", appid); map.put("mch_id", partner); map.put("out_trade_no", out_trade_no); map.put("nonce_str", WXPayUtil.generateUUID()); //再通过微信工具类将map转xml,并带上签名 String signedXml = WXPayUtil.generateSignedXml(map, partnerkey); System.out.println("=请求内容=:"+signedXml); //准备发送请求 HttpClientUtil clientUtil = new HttpClientUtil("https://api.mch.weixin.qq.com/pay/orderquery"); //设置请求方式是https clientUtil.setHttps(true); //设置请求参数 clientUtil.setXmlParam(signedXml); //发送请求 clientUtil.post(); //获取返回结果并且将xml转map对象 String content = clientUtil.getContent();//xml的string字符串 System.out.println("=返回内容="+content); Map<String, String> returnMap = WXPayUtil.xmlToMap(content); return returnMap; } catch (Exception e) { e.printStackTrace(); return null; } } }
略
会使用一个自定义工具类HttpClientUtil
标签:data pos bean content dubbo private utils blog order
原文地址:https://www.cnblogs.com/kitor/p/11296139.html