微信统一下单_公众号支付_扫码支付

微信扫码支付流程图:https://pay.weixin.qq.com/wiki/doc/api/native_sl.php?chapter=6_5&index=4

微信公众号支付流程图:https://pay.weixin.qq.com/wiki/doc/api/jsapi_sl.php?chapter=7_4

微信扫码支付,微信公众号支付官方文档:https://pay.weixin.qq.com/wiki/doc/api/native_sl.php?chapter=9_1

微信官方demosdk:https://pay.weixin.qq.com/wiki/doc/api/jsapi_sl.php?chapter=11_1


微信扫码支付,那段代码可以直接使用。

微信公众号支付,需要前端配合。需要配置微信jsconfig。

微信jsapi配置官方文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115

直接上代码

/**
 * 
 * 微信支付
 *
 */
public class WXPayService {
	// 统一下单URL
	private static String unifiedorder_url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
	
	/**
	 * 统一下单,公众号支付
	 * 
	 * @param body 内容
	 * @param out_trade_no 订单号
	 * @param total_fee 价格
	 * @param spbill_create_ip 用户的IP地址
	 * @param notify_url 支付完成后通知路径
	 * @param openid // 用户的openId
	 * 
	 * @throws Exception 
	 */
	public Map<String, String> unifiedOrder(String body, String out_trade_no, String total_fee,
			String spbill_create_ip, String notify_url, String openid) throws Exception {
		// 请求参数
		Map<String, String> param = new HashMap<String, String>();
		param.put("appid", WXConstant.APPID); // 公众账号ID
		param.put("mch_id", WXConstant.MCH_ID); // 商户号
		param.put("nonce_str", WXPayUtil.generateNonceStr()); // 
		param.put("body", body); // 内容(随意填,具体参照微信官方文档)
		param.put("out_trade_no", out_trade_no); // 订单号
		param.put("total_fee", total_fee); // 支付价格.<注意:这里的价格单位是分,必须是整数,不能带小数点的>
		param.put("spbill_create_ip", spbill_create_ip); // IP地址 ַ
		param.put("notify_url", notify_url); // 通知路径
		param.put("trade_type", "JSAPI"); // JSAPI-微信公众号支付
		param.put("openid", openid); // 用户的openId
		// 生成带有 sign 的 XML 格式字符串
		String xmlparam = WXPayUtil.generateSignedXml(param, WXConstant.KEY);
		// 发送请求
		String resultStr = SendHttpRequest.sendPostRequest(unifiedorder_url, xmlparam);
		// 转成MAP
		Map<String, String> wxResultMap = WXPayUtil.xmlToMap(resultStr);
		// 是否统一下单成功
		if (wxResultMap.get("return_code").equals("SUCCESS") &&
				wxResultMap.get("result_code").equals("SUCCESS")) {
			// 时间戳
			String timeStamp = new Long(WXPayUtil.getCurrentTimestamp()).toString();
			// 创建返回值
			Map<String, String> resultMap = new HashMap<String, String>();
			resultMap.put("appId", wxResultMap.get("appid"));
			resultMap.put("timeStamp", timeStamp);
			resultMap.put("nonceStr", wxResultMap.get("nonce_str"));
			resultMap.put("package", "prepay_id=" + wxResultMap.get("prepay_id"));
			resultMap.put("signType", "MD5");
			// 生成签名
			String paySign = WXPayUtil.generateSignature(resultMap, WXConstant.KEY);
			resultMap.put("paySign", paySign);
			return resultMap;
		} else {
			throw new Exception("错误结果Map = " + wxResultMap.toString());
		}
	}
	
	/**
	 * 统一下单,扫码支付
	 * 
	 * @param body 内容
	 * @param out_trade_no 订单号
	 * @param total_fee 价格
	 * @param spbill_create_ip 用户的IP地址
	 * @param notify_url 支付完成后通知路径
	 * 
	 * @throws Exception 
	 */
	public Map<String, String> unifiedOrder(String body, String out_trade_no, String total_fee,
			String spbill_create_ip, String notify_url) throws Exception {
		// 请求参数
		Map<String, String> param = new HashMap<String, String>();
		param.put("appid", WXConstant.APPID); // 公众账号ID
		param.put("mch_id", WXConstant.MCH_ID); // 商户号
		param.put("nonce_str", WXPayUtil.generateNonceStr()); // 
		param.put("body", body); // 内容(随意填,具体参照微信官方文档)
		param.put("out_trade_no", out_trade_no); // 订单号
		param.put("total_fee", total_fee); // 支付价格.<注意:这里的价格单位是分,必须是整数,不能带小数点的>
		param.put("spbill_create_ip", spbill_create_ip); // IP地址 ַ
		param.put("notify_url", notify
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值