PHP 微信支付类 demo
程序员文章站
2022-04-27 23:25:16
一切尽在代码中,代码附有注释,欢迎大家参考。
一切尽在代码中,代码附有注释,欢迎大家参考。
<?php class wxpayservice { protected $mchid; protected $appid; protected $key; public function __construct($mchid, $appid, $key) { $this->mchid = $mchid; // 微信支付商户号 partnerid 通过微信支付商户资料审核后邮件发送 $this->appid = $appid; //公众号appid 通过微信支付商户资料审核后邮件发送 $this->key = $key; //https://pay.weixin.qq.com 帐户设置-安全设置-api安全-api密钥-设置api密钥 } /** * @param string $openid 调用【网页授权获取用户信息】接口获取到用户在该公众号下的openid * @param float $totalfee 收款总费用 单位元 * @param string $outtradeno 唯一的订单号 * @param string $ordername 订单名称 * @param string $notifyurl 支付结果通知url 不要有问号 * https://mp.weixin.qq.com/ 微信支付-开发配置-测试目录 * 测试目录 http://mp.izhanlue.com/paytest/ 最后需要斜线,(需要精确到二级或三级目录) * @return string */ public function createjsbizpackage($openid, $totalfee, $outtradeno, $ordername, $notifyurl, $timestamp) { $config = array( 'mch_id' => $this->mchid, 'appid' => $this->appid, 'key' => $this->key, ); $unified = array( 'appid' => $config['appid'], 'attach' => '支付', //商家数据包,原样返回 'body' => $ordername, 'mch_id' => $config['mch_id'], 'nonce_str' => self::createnoncestr(), 'notify_url' => $notifyurl, 'openid' => $openid, //rade_type=jsapi,此参数必传 'out_trade_no' => $outtradeno, 'spbill_create_ip' => '127.0.0.1', 'total_fee' => intval($totalfee * 100), //单位 转为分 'trade_type' => 'jsapi', ); $unified['sign'] = self::getsign($unified, $config['key']); $responsexml = self::curlpost('https://api.mch.weixin.qq.com/pay/unifiedorder', self::arraytoxml($unified)); /* <xml> <return_code><![cdata[success]]></return_code> <return_msg><![cdata[ok]]></return_msg> <appid><![cdata[wx00e5904efec77699]]></appid> <mch_id><![cdata[1220647301]]></mch_id> <nonce_str><![cdata[1lhbrosdmqfxowqr]]></nonce_str> <sign><![cdata[aca7bc8a9164d1fbed06c7dfc13ec839]]></sign> <result_code><![cdata[success]]></result_code> <prepay_id><![cdata[wx2015032016590503f1bcd9c30421762652]]></prepay_id> <trade_type><![cdata[jsapi]]></trade_type> </xml> */ $unifiedorder = simplexml_load_string($responsexml, 'simplexmlelement', libxml_nocdata); if ($unifiedorder === false) { die('parse xml error'); } if ($unifiedorder->return_code != 'success') { die($unifiedorder->return_msg); } if ($unifiedorder->result_code != 'success') { die($unifiedorder->err_code); /* noauth 商户无此接口权限 notenough 余额不足 orderpaid 商户订单已支付 orderclosed 订单已关闭 systemerror 系统错误 appid_not_exist appid不存在 mchid_not_exist mchid不存在 appid_mchid_not_match appid和mch_id不匹配 lack_params 缺少参数 out_trade_no_used 商户订单号重复 signerror 签名错误 xml_format_error xml格式错误 require_post_method 请使用post方法 post_data_empty post数据为空 not_utf8 编码格式错误 */ } //$unifiedorder->trade_type 交易类型 调用接口提交的交易类型,取值如下:jsapi,native,app //$unifiedorder->prepay_id 预支付交易会话标识 微信生成的预支付回话标识,用于后续接口调用中使用,该值有效期为2小时 //$unifiedorder->code_url 二维码链接 trade_type为native是有返回,可将该参数值生成二维码展示出来进行扫码支付 $arr = array( "appid" => $config['appid'], "timestamp" => $timestamp, "noncestr" => self::createnoncestr(), "package" => "prepay_id=" . $unifiedorder->prepay_id, "signtype" => 'md5', ); $arr['paysign'] = self::getsign($arr, $config['key']); return $arr; } public function notify() { $config = array( 'mch_id' => $this->mchid, 'appid' => $this->appid, 'key' => $this->key, ); $poststr = $globals["http_raw_post_data"]; //error_log($poststr, 3, './str.txt'); /* $poststr = '<xml> <appid><![cdata[wx00e5904efec77699]]></appid> <attach><![cdata[支付测试]]></attach> <bank_type><![cdata[cmb_credit]]></bank_type> <cash_fee><![cdata[1]]></cash_fee> <fee_type><![cdata[cny]]></fee_type> <is_subscribe><![cdata[y]]></is_subscribe> <mch_id><![cdata[1220647301]]></mch_id> <nonce_str><![cdata[a0tz41phihm8zfmo]]></nonce_str> <openid><![cdata[ou3oct5o46pumn7ie87wcoyzy9r0]]></openid> <out_trade_no><![cdata[550bf2990c51f]]></out_trade_no> <result_code><![cdata[success]]></result_code> <return_code><![cdata[success]]></return_code> <sign><![cdata[f6f519b4dd8db978040f8c866c1e6250]]></sign> <time_end><![cdata[20150320181606]]></time_end> <total_fee>1</total_fee> <trade_type><![cdata[jsapi]]></trade_type> <transaction_id><![cdata[1008840847201503200034663980]]></transaction_id> </xml>'; */ $postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata); if ($postobj === false) { die('parse xml error'); } if ($postobj->return_code != 'success') { die($postobj->return_msg); } if ($postobj->result_code != 'success') { die($postobj->err_code); } $arr = (array)$postobj; unset($arr['sign']); if (self::getsign($arr, $config['key']) == $postobj->sign) { // $mch_id = $postobj->mch_id; //微信支付分配的商户号 // $appid = $postobj->appid; //微信分配的公众账号id // $openid = $postobj->openid; //用户在商户appid下的唯一标识 // $transaction_id = $postobj->transaction_id;//微信支付订单号 // $out_trade_no = $postobj->out_trade_no;//商户订单号 // $total_fee = $postobj->total_fee; //订单总金额,单位为分 // $is_subscribe = $postobj->is_subscribe; //用户是否关注公众账号,y-关注,n-未关注,仅在公众账号类型支付有效 // $attach = $postobj->attach;//商家数据包,原样返回 // $time_end = $postobj->time_end;//支付完成时间 echo '<xml><return_code><![cdata[success]]></return_code><return_msg><![cdata[ok]]></return_msg></xml>'; return $postobj; } } /** * curl get * * @param string $url * @param array $options * @return mixed */ public static function curlget($url = '', $options = array()) { $ch = curl_init($url); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_timeout, 30); if (!empty($options)) { curl_setopt_array($ch, $options); } //https请求 不验证证书和host curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_ssl_verifyhost, false); $data = curl_exec($ch); curl_close($ch); return $data; } public static function curlpost($url = '', $postdata = '', $options = array()) { if (is_array($postdata)) { $postdata = http_build_query($postdata); } $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_postfields, $postdata); curl_setopt($ch, curlopt_timeout, 30); //设置curl允许执行的最长秒数 if (!empty($options)) { curl_setopt_array($ch, $options); } //https请求 不验证证书和host curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_ssl_verifyhost, false); $data = curl_exec($ch); curl_close($ch); return $data; } public static function createnoncestr($length = 16) { $chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789'; $str = ''; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } public static function arraytoxml($arr) { $xml = "<xml>"; foreach ($arr as $key => $val) { if (is_numeric($val)) { $xml .= "<" . $key . ">" . $val . "</" . $key . ">"; } else $xml .= "<" . $key . "><![cdata[" . $val . "]]></" . $key . ">"; } $xml .= "</xml>"; return $xml; } /** * 例如: * appid: wxd930ea5d5a258f4f * mch_id: 10000100 * device_info: 1000 * body: test * nonce_str: ibuaivckdprxkhja * 第一步:对参数按照 key=value 的格式,并按照参数名 ascii 字典序排序如下: * stringa="appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_i * d=10000100&nonce_str=ibuaivckdprxkhja"; * 第二步:拼接支付密钥: * stringsigntemp="stringa&key=192006250b4c09247ec02edce69f6a2d" * sign=md5(stringsigntemp).touppercase()="9a0a8659f005d6984697e2ca0a9cf3b7" */ public static function getsign($params, $key) { ksort($params, sort_string); $unsignparastring = self::formatqueryparamap($params, false); $signstr = strtoupper(md5($unsignparastring . "&key=" . $key)); return $signstr; } protected static function formatqueryparamap($paramap, $urlencode = false) { $buff = ""; ksort($paramap); foreach ($paramap as $k => $v) { if (null != $v && "null" != $v) { if ($urlencode) { $v = urlencode($v); } $buff .= $k . "=" . $v . "&"; } } $reqpar = ''; if (strlen($buff) > 0) { $reqpar = substr($buff, 0, strlen($buff) - 1); } return $reqpar; } }
以上代码大家都能看得懂吧,有哪里不明白的地方欢迎给我留言,我会在第一时间和大家取得联系的。谢谢大家对网站的支持。
下一篇: c# 位运算