微信支付-公众号支付H5调用支付详解

微信公众号支付

最近项目需要微信支付,然后看了下微信公众号支付,,虽然不难,但是细节还是需要注意的,用了大半天时间写了个demo,并且完整的测试了一下支付流程,下面分享一下微信公众号支付的经验。


一、配置公众号微信支付  

   需要我们配置微信公众号支付地址和测试白名单。

  

     比如:支付JS页面的地址为 http://www.xxx.com/shop/pay/

            那此处配置www.xxx.com/shop/pay/


  二、开发流程

     借用微信公众号支付api(地址 http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=7_4),我们需要开发的为红色标记出的。如下:

    

 

三、向微信服务器端下订单

             调用统一下单接口,这样就能获取微信支付的prepay_id(http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=9_1)。

     在调用该接口前有几个字段是H5支付必须填写的openid

    3.1 获取openid

         可以通过网页授权形式(http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html)

       在微信中发送如下链接

      

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx520c15f417810387&redirect_uri=要跳转的下订单的url&response_type=code&scope=snsapi_base&state=123#wechat_redirect


   3.2 后台支付

    代码如下,包含预处理订单,支付订单等接口。

package org.andy.controller;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.fasterxml.jackson.databind.JsonNode;
import com.gson.oauth.Oauth;
import com.gson.oauth.Pay;
import com.gson.util.HttpKit;
import com.gson.util.Tools;
import org.andy.util.DatetimeUtil;
import org.andy.util.JsonUtil;
import org.andy.util.SessionUtil;
import org.andy.util.WebUtil;

@Controller
@RequestMapping("/pay")
public class WXPayController {

	@RequestMapping(value = "wxprepay")
	public void jspay(HttpServletRequest request, HttpServletResponse response, String callback) throws Exception {
		// 获取openid
		String openId = SessionUtil.getAtt(request, "openId");
		if (openId == null) {
			openId = getUserOpenId(request);
		}

		String appid = "wx16691fcb0523c1a4";
		String partnerid = "22223670";
		String paternerKey = "fjfjfjfjf1234567FFFFFFFFF1234567";

		String out_trade_no = getTradeNo();
		Map<String, String> paraMap = new HashMap<String, String>();
		paraMap.put("appid", appid);
		paraMap.put("attach", "测试支付");
		paraMap.put("body", "测试购买Beacon支付");
		paraMap.put("mch_id", partnerid);
		paraMap.put("nonce_str", create_nonce_str());
		paraMap.put("openid", openId);
		paraMap.put("out_trade_no", out_trade_no);
		paraMap.put("spbill_create_ip", getAddrIp(request));
		paraMap.put("total_fee", "1");
		paraMap.put("trade_type", "JSAPI");
		paraMap.put("notify_url", "http://www.xxx.co/wxpay/pay/appPay_notify.shtml");
		String sign = getSign(paraMap, paternerKey);
		paraMap.put("sign", sign);

		// 统一下单 https://api.mch.weixin.qq.com/pay/unifiedorder
		String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";

		String xml = ArrayToXml(paraMap, false);

		String xmlStr = HttpKit.post(url, xml);

		// 预付商品id
		String prepay_id = "";

		if (xmlStr.indexOf("SUCCESS") != -1) {
			Map<String, String> map = doXMLParse(xmlStr);
			prepay_id = (String) map.get("prepay_id");
		}

		Map<String, String> payMap = new HashMap<String, String>();
		payMap.put("appId", appid);
		payMap.put("timeStamp", create_timestamp());
		payMap.put("nonceStr", create_nonce_str());
		payMap.put("signType", "MD5");
		payMap.put("package", "prepay_id=" + prepay_id);
		String paySign = getSign(payMap, paternerKey);

		payMap.put("pg", prepay_id);
		payMap.put("paySign", paySign);

		WebUtil.response(response, WebUtil.packJsonp(callback,
				JsonUtil.warpJsonNodeResponse(JsonUtil.objectToJsonNode(payMap)).toString()));
	}

	@RequestMapping(value = "appPay")
	public void appPay(HttpServletRequest request, HttpServletResponse response, String body, String detail,
			String total_fee, String spbill_create_ip, String notify_url, String trade_type, String callback)
			throws Exception {

		String appid = "wx16691fcb0523c1a4";
		String partnerid = "22223670";
		String paternerKey = "fjfjfjfjf1234567FFFFFFFFF1234567";

		String out_trade_no = getTradeNo();
		Map<String, String> paraMap = new HashMap<String, String>();
		paraMap.put("appid", appid);
		paraMap.put("body", body);
		paraMap.put("mch_id", partnerid);
		paraMap.put("nonce_str", create_nonce_str());
		paraMap.put("out_trade_no", out_trade_no);
		paraMap.put("spbill_create_ip", spbill_create_ip);
		paraMap.put("total_fee", total_fee);
		paraMap.put("trade_type", trade_type);
		paraMap.put("notify_url", notify_url);
		String sign = getSign(paraMap, paternerKey);
		paraMap.put("sign", sign);

		// 统一下单 https://api.mch.weixin.qq.com/pay/unifiedorder
		String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";

		String xml = ArrayToXml(paraMap, false);

		String xmlStr = HttpKit.post(url, xml);

		// 预付商品id
		String prepay_id = "";

		Map<String, String> map = doXMLParse(xmlStr);
		if (xmlStr.indexOf("SUCCESS") != -1) {
			prepay_id = (String) map.get("prepay_id");
		}

		String result_code = map.get("result_code");
		String err_code_des = map.get("err_code_des");
		Map<String, String> payMap = new HashMap<String, String>();
		payMap.put("appid", appid);
		payMap.put("partnerid", partnerid);
		payMap.put("prepayid", prepay_id);
		payMap.put("package", "Sign=WXPay");
		payMap.put("noncestr", create_nonce_str());
		payMap.put("timestamp", create_timestamp());
		String paySign = getSign(payMap, paternerKey);

		payMap.put("sign", paySign);
		payMap.put("result_code", result_code);
		payMap.put("err_code_des", err_code_des);

		WebUtil.response(response, WebUtil.packJsonp(callback,
				JsonUtil.warpJsonNodeResponse(JsonUtil.objectToJsonNode(payMap)).toString()));
	}

	@RequestMapping("/appPay_notify")
	public void appPayNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// String xml =
		// "<xml><appid><![CDATA[wxb4dc385f953b356e]]></appid><bank_type><![CDATA[CCB_CREDIT]]></bank_type><cash_fee><![CDATA[1]]></cash_fee><fee_type><![CDATA[CNY]]></fee_type><is_subscribe><![CDATA[Y]]></is_subscribe><mch_id><![CDATA[1228442802]]></mch_id><nonce_str><![CDATA[1002477130]]></nonce_str><openid><![CDATA[o-HREuJzRr3moMvv990VdfnQ8x4k]]></openid><out_trade_no><![CDATA[1000000000051249]]></out_trade_no><result_code><![CDATA[SUCCESS]]></result_code><return_code><![CDATA[SUCCESS]]></return_code><sign><![CDATA[1269E03E43F2B8C388A414EDAE185CEE]]></sign><time_end><![CDATA[20150324100405]]></time_end><total_fee>
评论 71
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值