微信H5支付SDK

http://zhouhongliang.cn/article/35.html
  //填写微信分配的公众账号ID      *      'mch_id'    =>  //填写微信支付分配的商户号      *      'notify_url'=>  //填写微信支付结果回调地址      *      'key'       =>  //填写微信商户支付密钥      *  );      *  统一下单方法      *  $WechatAppPay = new wechatAppPay($options);      *  $params['body'] = '商品描述';                   //商品描述      *  $params['out_trade_no'] = '1217752501201407';   //自定义的订单号,不能重复      *  $params['total_fee'] = '100';                   //订单金额 只能为整数 单位为分      *  $params['trade_type'] = 'APP';                  //交易类型 JSAPI | NATIVE |APP | WAP      *  $wechatAppPay->unifiedOrder( $params );      */     class wechatAppPay     {         //接口API URL前缀         const API_URL_PREFIX = 'https://api.mch.weixin.qq.com';         //下单地址URL         const UNIFIEDORDER_URL = "/pay/unifiedorder";         //查询订单URL         const ORDERQUERY_URL = "/pay/orderquery";         //关闭订单URL         const CLOSEORDER_URL = "/pay/closeorder";         //公众账号ID         private $appid;         //商户号         private $mch_id;         //随机字符串         private $nonce_str;         //签名         private $sign;         //商品描述         private $body;         //商户订单号         private $out_trade_no;         //支付总金额         private $total_fee;         //终端IP         private $spbill_create_ip;         //支付结果回调通知地址         private $notify_url;         //交易类型         private $trade_type;         //支付密钥         private $key;         //证书路径         private $SSLCERT_PATH;         private $SSLKEY_PATH;         //所有参数         private $params = array();                  public function __construct($appid, $mch_id, $notify_url, $key)         {             $this->appid      = $appid;             $this->mch_id     = $mch_id;             $this->notify_url = $notify_url;             $this->key        = $key;         }                  /**          * 下单方法          * @param   $params 下单参数          */         public function unifiedOrder($params)         {             $this->body                       = $params['body'];             $this->out_trade_no               = $params['out_trade_no'];             $this->total_fee                  = $params['total_fee'];             $this->trade_type                 = $params['trade_type'];             $this->scene_info                 = $params['scene_info'];             $this->nonce_str                  = $this->genRandomString();             $this->spbill_create_ip           = $this->get_client_ip();             $this->params['appid']            = $this->appid;             $this->params['mch_id']           = $this->mch_id;             $this->params['nonce_str']        = $this->nonce_str;             $this->params['body']             = $this->body;             $this->params['out_trade_no']     = $this->out_trade_no;             $this->params['total_fee']        = $this->total_fee;             $this->params['spbill_create_ip'] = $this->spbill_create_ip;             $this->params['notify_url']       = $this->notify_url;             $this->params['trade_type']       = $this->trade_type;             $this->params['scene_info']       = $this->scene_info;             //获取签名数据             $this->sign           = $this->MakeSign($this->params);             $this->params['sign'] = $this->sign;             $xml                  = $this->data_to_xml($this->params);             $response             = $this->postXmlCurl($xml, self::API_URL_PREFIX . self::UNIFIEDORDER_URL);             if (!$response) {                 return false;             }             $result = $this->xml_to_data($response);             if (!empty($result['result_code']) && !empty($result['err_code'])) {                 $result['err_msg'] = $this->error_code($result['err_code']);             }                          return $result;         }                  /**          * @author:周洪亮[InvalidCharacterError: "WHITE_ZHL@163.COM" did not match the Name production]
控制器方法
;          * @copyright:2017/12/12;          * @var:H5支付,为了怕后来者看不懂,注释在下边          */         public function h5Pay()         {             $get = I('get.');             if (empty($get['order_id'])) {                 $order_id = session('order_id');             } else {                 $order_id[] = $get['order_id'];             }             $order_logic    = new OrderLogic();             $pay_order_data = $order_logic->GetPayOrderData($order_id);//获取订单信息                          Vendor('Weixinpay.WeChatH5pay');//引入微信H5支付SDK             $config                 = C('WEIXINPAY_CONFIG');//读取微信配置             $notify_url             = $config['NOTIFY_URL'] . 'out_trade_no/' . $pay_order_data['pay_sn'];//拼接回调路径,带单号方便回调             $wechatAppPay           = new \wechatAppPay($config['APPID'], $config['MCHID'], $notify_url, $config['KEY']);             $params['body']         = '';                       //商品描述             $params['out_trade_no'] = $pay_order_data['pay_sn'];    //自定义的订单号             $params['total_fee']    = $pay_order_data['sum'] * 100;                       //订单金额 只能为整数 单位为分             $params['trade_type']   = 'MWEB';                   //交易类型 JSAPI | NATIVE | APP | WAP             $params['scene_info']   = '{"h5_info": {"type":"Wap","wap_url": "https://*****.com","wap_name": "支付中"}}';             $result                 = $wechatAppPay->unifiedOrder($params);//统一下单方法                          $url_encode_redirect_url = urlencode($config['REDIRECT_URL']);//支付成功回调路径,在Home/config.php文件中可配置             $url                     = $result['mweb_url'] . '&redirect_url=' . $url_encode_redirect_url;//拼接支付链接             echo "";             exit();         }                  /**          * @author:周洪亮[InvalidCharacterError: "WHITE_ZHL@163.COM" did not match the Name production]
config.php内微信配置
'WEIXINPAY_CONFIG' => array(     'APPID'      => '****', // 微信支付APPID     'MCHID'      => '***', // 微信支付MCHID 商户收款账号     'KEY'        => '***', // 微信支付KEY     'APPSECRET'  => '***',  //公众帐号secert     'REDIRECT_URL'=>'http://m.***.com',//回调路径     'NOTIFY_URL' => 'http://***.com/index.php/Home/Weixinpay/H5Notify/', // 接收支付状态的连接 ),
控制器H5pay方法内详解: GetPayOrderData()这个方法,就是数据库查询,拿到订单信息。大家各有各的业务也就不贴图了。 unifiedOrder()统一下单方法,在上边SDK内,只需要引入类正确即可,我这里把他放在了第三方文件类库Vendor/weixinpay文件夹下
控制器H5Notify回调方法详解,这在网上百分之九十九没有开源的,也不知道为什么,我也是没找到,所以自己搞得,但是为了方便大家开发这里贡献出来了,写的若有问题可以评论给我,我好更改!但对于我们公司的业务,目前这套逻辑是够用的。
orderQuery($out_trade_no)方法,在SDK内,这里需要调用你自己生成的商户订单号这个参数用于查询,微信推荐的是他们生成的transaction_id.如果你用的是transaction_id,请把方法内参数变换过来!就可以了.
这里有一个判断,看订单是否查询得到,我这里没有三个&&去分别判断return_code这三个参数,而是直接根据返回进行判断。
getCallBackH5PayData()方法是统计返回数据,便于来插入你们公司的pay表。pay表就是微信返回成功信息表,参数不同统计的不同,就一个查询,这里就不截图了。