首页
友人帐
留言板
关于
Search
1
IDE Eval Resetter:JetBrains 全家桶无限试用插件
378 阅读
2
影视资源采集站收录大全
302 阅读
3
linux安装或升级protoc
223 阅读
4
VFM 3.7.5 源码 - 一个极简的 PHP 私人云盘!
161 阅读
5
批量采集美女写真等图片做图片站
149 阅读
谈天说地
程序源码
技术教程
成品源码
登录
Search
标签搜索
PHP
linux
源码
go
windows
centos
原创
mysql
微信
激活
采集
宝塔
绿色版
API
解析
SDK
图片
破解
域名
html
云青
累计撰写
150
篇文章
累计收到
78
条评论
首页
栏目
谈天说地
程序源码
技术教程
成品源码
页面
友人帐
留言板
关于
搜索到
7
篇与
的结果
2019-11-02
[更新]php微信支付类(含h5支付、APP二次签名、jsapi支付)
博主之封装过一个微信支付类 很长时间没有更新了 现在分享一下自用的微信支付类该类含有微信app支付 h5支付 公众号支付(jsapi) 订单查询native扫码支付后面会添加异步主要商户订单号查询 并确定金额是否与数据库中一致以及判断该商户订单号是否处理过(谨记)个人觉得验签并不重要 有订单查询以及数据库订单验证就行了过多的就不介绍了 源码有注释<?php /** * @author anderyly * @email anderyly@sina.com * @link https://blog.aaayun.cc/ * @copyright Copyright (c) 2018 */ class WxPay { // 微信分配的公众账号ID public $appId; // 微信支付分配的商户号 public $mch_id; // 微信支付分配的商户密钥 public $key; // 回调地址 public $notify_url; // 随机字符串 public $nonce_str; // 站点名称 public $webName; // 站点url地址 public $webUrl; // h5支付成功后跳转的地址 public $redirect_url; /** * [__construct 初始化配置] */ public function __construct($data) { $this->appId = $data['appId']; $this->mch_id = $data['mch_id']; $this->key = $data['key']; $this->notify_url = $data['notify_url']; if (isset($data['webName'])) $this->webName = $data['webName']; if (isset($data['webUrl'])) $this->webUrl = $data['webUrl']; if (isset($data['redirect_url'])) $this->redirect_url = $data['redirect_url']; $this->nonce_str = $this->rand_code(); } /** * [h5 html5支付] * @param out_trade_no [订单号] * @param $body [商品描述] * @param $ip [ip地址] * @param $total_fee [订单总金额] * @return array|bool */ public function h5($out_trade_no, $body, $ip, $total_fee) { $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; $data = [ 'appid' => $this->appId, 'mch_id' => $this->mch_id, 'body' => $body, 'spbill_create_ip' => $ip, 'total_fee' => $total_fee * 100, 'out_trade_no' => $out_trade_no, 'nonce_str' => $this->nonce_str, 'notify_url' => $this->notify_url, 'trade_type' => 'MWEB', 'scene_info' => "{'h5_info':{'type': 'WAP','wap_url': $this->webUrl,'wap_name': $this->webName}}" ]; $data['sign'] = $this->sign($data); //获取签名 $data = $this->curlWx($url, $this->ToXml($data)); if ($data) { $res = $this->FromXml($data); if (isset($res['return_code']) and $res['return_code'] != 'SUCCESS' and isset($res['return_msg']) and $res['return_msg'] == 'OK' and isset($res['result_code']) and $res['result_code'] == 'SUCCESS') { exit("签名失败!"); } else { $url = $res['mweb_url'] . '&redirect_url=' . urlencode($this->redirect_url); exit('<script src="https://open.mobile.qq.com/sdk/qqapi.js?_bid=152"></script><script type="text/javascript">mqq.ui.openUrl({ target: 2,url: "' . $url . '"});</script>'); } } } /** * [app 移动端二次签名] * @param out_trade_no [订单号] * @param $body [商品描述] * @param $ip [ip地址] * @param $total_fee [订单总金额] * @return array|bool */ public function app($out_trade_no, $body, $ip, $total_fee) { $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; $data = [ 'appid' => $this->appId, 'mch_id' => $this->mch_id, 'body' => $body, 'spbill_create_ip' => $ip, 'total_fee' => $total_fee * 100, 'out_trade_no' => $out_trade_no, 'nonce_str' => $this->nonce_str, 'notify_url' => $this->notify_url, 'trade_type' => 'APP', ]; $data['sign'] = $this->Sign($data); $data = $this->curlWx($url, $this->ToXml($data)); if ($data) { $res = $this->FromXml($data); if ($res['return_code'] != 'SUCCESS') { return false; } else { $arr = [ 'prepayid' => $res['prepay_id'], 'appid' => $this->appId, 'partnerid' => $this->mch_id, 'package' => 'Sign=WXPay', 'noncestr' => $this->nonce_str, 'timestamp' => time(), ]; $sign = $this->sign($arr, 1); $arr['out_trade_no'] = $out_trade_no; $arr['return_code'] = $res['return_code']; $arr['sign'] = $sign; return $arr; } } else { return false; } } /** * [h5 html5支付] * @param $out_trade_no [订单号] * @param $body [商品描述] * @param $ip [ip地址] * @param $total_fee [订单总金额] * @param $openid [授权用户的openid] * @return array|bool */ public function jsapi($out_trade_no, $body, $ip, $total_fee, $openid) { $url = 'https://api2.mch.weixin.qq.com/pay/unifiedorder'; $data = [ 'appid' => $this->appId, 'mch_id' => $this->mch_id, 'body' => $body, 'spbill_create_ip' => $ip, 'total_fee' => $total_fee * 100, 'out_trade_no' => $out_trade_no, 'nonce_str' => $this->nonce_str, 'notify_url' => $this->notify_url, 'trade_type' => 'JSAPI', 'openid' => $openid ]; $data['sign'] = $this->Sign($data, 1); $data = $this->curlWx($url, $this->ToXml($data)); if ($data) { $res = $this->FromXml($data); if ($res['return_code'] != 'SUCCESS') { return false; } else { $arr = [ 'appId' => $res['appid'], 'package' => 'prepay_id=' . $res['prepay_id'], 'nonceStr' => $this->nonce_str, 'timeStamp' => time(), 'signType' => 'MD5' ]; $sign = $this->sign($arr); $as['nonceStr'] = $arr['nonceStr']; $as['package'] = $arr['package']; $as['sign'] = $sign; $as['timestamp'] = $arr['timeStamp']; $as['appid'] = $arr['appId']; return $as; } } else { return false; } } /** * [curlWx 发送curl请求] * @param $url [url地址] * @param $xml [xml] * @return mixed */ protected function curlWx($url, $xml) { //header("Content-type:text/xml"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); if (stripos($url, "https://") !== FALSE) { curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); } else { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); } curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); $data = curl_exec($ch); curl_close($ch); return $data; } /** * [sign 签名] * @param null $app [不为null开启二次签名] * @return string */ protected function sign($params, $app = NULL) { ksort($params); foreach ($params as $key => $item) { if (!empty($item)) $newArr[] = $key . '=' . $item; } $stringA = implode("&", $newArr); $stringSignTemp = $stringA . "&key=" . $this->key; $sign = MD5($stringSignTemp); if (is_null($app)) $sign = strtoupper($sign); return $sign; } /** * [FromXml xml转数组] * @param $xml * @return bool|mixed */ public function FromXml($xml) { if (!$xml) return false; libxml_disable_entity_loader(true); $data = @json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $data; } /** * [ToXml 参数组装成xml] * @param array $data * @return bool|string */ public function ToXml($data = []) { if (!is_array($data) || count($data) <= 0) return false; $xml = "<xml>"; foreach ($data as $key => $val) { if (is_numeric($val)) { $xml .= "<" . $key . ">" . $val . "</" . $key . ">"; } else { $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">"; } } $xml .= "</xml>"; return $xml; } /** * [rand_code 生成随机字符串] * @return bool|string */ protected function rand_code() { $str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';//62个字符 $str = str_shuffle($str); $str = substr($str, 0, 32); return $str; } /** * [orderQuery 查询订单] * @param $order [商户订单号] * @param $money [订单金额] * @return bool [false | 成功返回流水号] */ public function orderQuery($order, $money) { $url = 'https://api.mch.weixin.qq.com/pay/orderquery'; $data = array( 'appid' => $this->appId, 'mch_id' => $this->mch_id, 'nonce_str' => $this->nonce_str, 'out_trade_no' => $order ); $data['sign'] = $this->sign($data); $data = $this->curlWx($url, $this->ToXml($data)); if ($data) { $res = $this->FromXml($data); if ($res['return_code'] == 'FAIL') { return false; } else { // if (isset($res['return_code']) and $res['return_code'] == 'SUCCESS' and isset($res['return_msg']) and $res['return_msg'] == 'OK' and isset($res['result_code']) and $res['result_code'] == 'SUCCESS' and isset($res['trade_state']) and $res['trade_state'] == 'SUCCESS') { // if ($res['total_fee'] == ($money * 100)) { return $res['transaction_id']; } else { return false; } // } else { return false; } // } // } else { return false; } } }该类含有微信app支付 h5支付 公众号支付(jsapi) 订单查询native扫码支付后面会添加异步主要商户订单号查询 并确定金额是否与数据库中一致以及判断该商户订单号是否处理过(谨记)个人觉得验签并不重要 有订单查询以及数据库订单验证就行了过多的就不介绍了 源码有注释
2019年11月02日
13 阅读
0 评论
0 点赞
2018-11-13
微信支付类(含h5支付、APP二次签名)
最新地址 https://vclove.cn/archives/492.html什么是微信H5支付H5支付是指商户在微信客户端外的移动端网页展示商品或服务,用户在前述页面确认使用微信支付时,商户发起本服务呼起微信客户端进行支付主要用于触屏版的手机浏览器请求微信支付的场景。可以方便的从外部浏览器唤起微信支付开发流程1、用户在商户侧完成下单,使用微信支付进行支付2、由商户后台向微信支付发起下单请求(调用统一下单接口)注:交易类型trade_type=MWEB3、统一下单接口返回支付相关参数给商户后台,如支付跳转url(参数名“mweb_url”),商户通过mweb_url调起微信支付中间页4、中间页进行H5权限的校验,安全性检查(此处常见错误请见下文)5、如支付成功,商户后台会接收到微信侧的异步通知6、用户在微信支付收银台完成支付或取消支付,返回商户页面(默认为返回支付发起页面)7、商户在展示页面,引导用户主动发起支付结果的查询8、商户后台判断是否接到收微信侧的支付结果通知,如没有,后台调用订单查询接口确认订单状态9、展示最终的订单支付结果给用户相信大家都用过企鹅的sdk吧,那酸爽让人难以接受h5的支付我就不给大家截图了,看调用方式就知道了APP签名结果微信支付类库下载地址:wepay.tar.gz
2018年11月13日
6 阅读
0 评论
0 点赞
1
2