java实现微信扫码支付 java实现微信扫码支付功能
品学兼忧 人气:01、maven项目的pom.xml中添加如下jar包:
<dependency> <groupId>com.github.wxpay</groupId> <artifactId>wxpay-sdk</artifactId> <version>0.0.3</version> </dependency>
2、编写WeWxConfig类:
package com.xx.wxpay; import com.github.wxpay.sdk.WXPayConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.io.InputStream; /** * 描述:微信支付配置信息 * * @author ssl * @create 2018/04/24 19:25 */ @Component public class WeWxConfig implements WXPayConfig { @Value("${wechat.public.appid}") private String appId; @Value("${wechat.merchant}") private String mchId; @Value("${wechat.public.apikey}") private String apiKey; /** * 公众账号ID:微信支付分配的公众账号ID(企业号corpid即为此appId) * * @return */ @Override public String getAppID() { return appId; } /** * 商户号:微信支付分配的商户号 * * @return */ @Override public String getMchID() { return mchId; } /** * @return */ @Override public String getKey() { return apiKey; } @Override public InputStream getCertStream() { return null; } @Override public int getHttpConnectTimeoutMs() { return 0; } @Override public int getHttpReadTimeoutMs() { return 0; } }
3、编写WeWxPayService:
package com.xx.wxpay; import com.alibaba.fastjson.JSONObject; import com.github.wxpay.sdk.WXPay; import com.google.common.collect.Maps; import com.xx.model.Order; import com.xx.model.Product; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.text.MessageFormat; import java.util.HashMap; import java.util.Map; /** * 描述: * * @author ssl * @create 2018/04/24 20:15 */ @Service public class WeWxPayService { protected Logger logger = LoggerFactory.getLogger(this.getClass()); @Value("${project.url}") private String projectUrl; @Autowired private WeWxConfig weWxConfig; /** * 统一下单 * * @param product * @param order * @return */ public Map<String, String> unifiedOrder(Product product, Order order) { Map<String, String> data = Maps.newHashMap(); WXPay wxpay = new WXPay(weWxConfig); data.put("body", "XX-" + product.getName()); data.put("detail", "详细信息"); data.put("out_trade_no", order.getOrderNo()); data.put("device_info", "WEB"); data.put("fee_type", "CNY"); data.put("total_fee", order.getAmount() + ""); data.put("spbill_create_ip", "127.0.0.1"); data.put("notify_url", projectUrl + "/base/order/notifyUrl"); data.put("trade_type", "NATIVE"); // 此处指定为扫码支付 data.put("product_id", product.getId() + ""); try { Map<String, String> resp = wxpay.unifiedOrder(data); logger.debug(JSONObject.toJSONString(resp)); return resp; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 订单查询 * * @param orderNo:订单号 * @return */ public Map<String, String> orderQuery(String orderNo) { Map<String, String> reqData = Maps.newHashMap(); reqData.put("out_trade_no", orderNo); WXPay wxpay = new WXPay(weWxConfig); try { Map<String, String> resp = wxpay.orderQuery(reqData); logger.debug(JSONObject.toJSONString(resp)); return resp; } catch (Exception e) { e.printStackTrace(); } return null; } public static String getUrl() { WXPay wxpay = new WXPay(new WeWxConfig()); Map<String, String> data = new HashMap<String, String>(); data.put("body", "上屏名称"); data.put("detail", "商品详情"); data.put("out_trade_no", "2ab9071b06b9f739b950ddb41db2690d"); data.put("device_info", ""); data.put("fee_type", "CNY"); data.put("total_fee", "1"); data.put("spbill_create_ip", "218.17.160.245"); data.put("notify_url", "http://www.example.com/wxpay/notify"); data.put("trade_type", "NATIVE"); // 此处指定为扫码支付 data.put("product_id", "12"); try { Map<String, String> resp = wxpay.unifiedOrder(data); System.out.println(resp); } catch (Exception e) { e.printStackTrace(); } return ""; } }
4、调用:
/** 向微信支付系统下单,并得到二维码返回给用户 */ Map<String, String> resData = weWxPayService.unifiedOrder(product, order);
5、resData.get("code_url")为微信下单成功后返回的二维码地址,页面中用QRCode.js来显示该二维码,且该页面用定时器定时查询订单支付状态
加载全部内容