Procházet zdrojové kódy

集成微信H5(WAP)支付

小柒2012 před 7 roky
rodič
revize
85ee8fd8d6

+ 12 - 0
src/main/java/com/itstyle/modules/weixinpay/controller/WeixinMobilePayController.java

@@ -8,6 +8,7 @@ import java.util.TreeMap;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -46,6 +47,17 @@ public class WeixinMobilePayController {
 		String url =  weixinPayService.weixinPayMobile(product);
 		return "redirect:"+url;
     }
+	@RequestMapping("/h5pay")
+    public String  h5pay(Product product,ModelMap map) {
+		logger.info("纯H5支付(不建议在APP端使用)");
+		//mweb_url为拉起微信支付收银台的中间页面,可通过访问该url来拉起微信客户端,完成支付,mweb_url的有效期为5分钟。
+		String mweb_url =  weixinPayService.weixinPayH5(product);
+		if(StringUtils.isNotBlank(mweb_url)){
+			return "redirect:"+mweb_url;
+		}else{
+			return "redirect:https://blog.52itstyle.com";//自定义错误页面
+		}
+    }
 	/**
 	 * 预下单(对于已经产生的订单)
 	 * @Author  科帮网

+ 13 - 2
src/main/java/com/itstyle/modules/weixinpay/service/IWeixinPayService.java

@@ -48,7 +48,7 @@ public interface IWeixinPayService {
 	 */
 	void saveBill();
     /**
-     * 微信手机支付返回一个url地址
+     * 微信公众号支付返回一个url地址
      * @Author  科帮网
      * @param product
      * @return  String
@@ -58,5 +58,16 @@ public interface IWeixinPayService {
      *
      */
 	String weixinPayMobile(Product product);
-	
+	/**
+	 * H5支付 唤醒 微信APP 进行支付
+	 * 申请入口:登录商户平台-->产品中心-->我的产品-->支付产品-->H5支付
+	 * @Author  科帮网
+	 * @param product
+	 * @return  String
+	 * @Date	2017年8月9日
+	 * 更新日志
+	 * 2017年8月9日  科帮网 首次创建
+	 *
+	 */
+	String weixinPayH5(Product product);
 }

+ 55 - 4
src/main/java/com/itstyle/modules/weixinpay/service/impl/WeixinPayServiceImpl.java

@@ -205,10 +205,6 @@ public class WeixinPayServiceImpl implements IWeixinPayService {
 		}
 		
 	}
-	public static void main(String[] args) {
-		JSONObject obj = new JSONObject();
-		obj.put("attach","11");
-	}
 	@Override
 	public String weixinPayMobile(Product product) {
 		StringBuffer url = new StringBuffer();
@@ -223,4 +219,59 @@ public class WeixinPayServiceImpl implements IWeixinPayService {
 		url.append("#wechat_redirect");
 		return  url.toString();
 	}
+	@SuppressWarnings("rawtypes")
+	@Override
+	public String weixinPayH5(Product product) {
+		logger.info("订单号:{}发起H5支付",product.getOutTradeNo());
+		String  mweb_url = "";
+		try {
+			// 账号信息
+			String key = ConfigUtil.API_KEY; // key
+			String trade_type = "MWEB";//交易类型 H5 支付 
+			SortedMap<Object, Object> packageParams = new TreeMap<Object, Object>();
+			ConfigUtil.commonParams(packageParams);
+			packageParams.put("product_id", product.getProductId());// 商品ID
+			packageParams.put("body", product.getBody());// 商品描述
+			packageParams.put("out_trade_no", product.getOutTradeNo());// 商户订单号
+			String totalFee = product.getTotalFee();
+			totalFee =  CommonUtil.subZeroAndDot(totalFee);
+			packageParams.put("total_fee", totalFee);// 总金额
+			//H5支付要求商户在统一下单接口中上传用户真实ip地址 spbill_create_ip
+			packageParams.put("spbill_create_ip", product.getSpbillCreateIp());// 发起人IP地址
+			packageParams.put("notify_url", notify_url);// 回调地址
+			packageParams.put("trade_type", trade_type);// 交易类型
+			//H5支付专用 
+			JSONObject value = new JSONObject();
+			value.put("type", "WAP");
+			value.put("wap_url", "https://blog.52itstyle.com");////WAP网站URL地址
+			value.put("wap_name", "科帮网充值");//WAP 网站名
+			JSONObject scene_info = new JSONObject();
+			scene_info.put("h5_info", value);
+			packageParams.put("scene_info", scene_info.toString());
+			
+			String sign = PayCommonUtil.createSign("UTF-8", packageParams, key);
+			packageParams.put("sign", sign);// 签名
+
+			String requestXML = PayCommonUtil.getRequestXml(packageParams);
+			String resXml = HttpUtil.postData(ConfigUtil.UNIFIED_ORDER_URL, requestXML);
+			Map map = XMLUtil.doXMLParse(resXml);
+			String returnCode = (String) map.get("return_code");
+			if("SUCCESS".equals(returnCode)){
+				String resultCode = (String) map.get("result_code");
+				if("SUCCESS".equals(resultCode)){
+					logger.info("订单号:{}发起H5支付成功",product.getOutTradeNo());
+					mweb_url = (String) map.get("mweb_url");
+				}else{
+					String errCodeDes = (String) map.get("err_code_des");
+					logger.info("订单号:{}发起H5支付(系统)失败:{}",product.getOutTradeNo(),errCodeDes);
+				}
+			}else{
+				String returnMsg = (String) map.get("return_msg");
+				logger.info("(订单号:{}发起H5支付(通信)失败:{}",product.getOutTradeNo(),returnMsg);
+			}
+		} catch (Exception e) {
+			logger.error("订单号:{}发起H5支付失败(系统异常))",product.getOutTradeNo(),e);
+		}
+		return mweb_url;
+	}
 }