phpcms封装的微信h5支付源码DEMO参考
admin
2019-08-30 22:48:03
根据phpcms封装的微信H5支付类
- class Wxh5pay
- {
-
- const API_URL_PREFIX = 'https://api.mch.weixin.qq.com';
-
- const UNIFIEDORDER_URL = "/pay/unifiedorder";
-
- const ORDERQUERY_URL = "/pay/orderquery";
-
- const CLOSEORDER_URL = "/pay/closeorder";
- protected $mchid;
- protected $appid;
- protected $apiKey;
- public function __construct($mchid, $appid, $key){
- $this->mchid = $mchid;
- $this->appid = $appid;
- $this->apiKey = $key;
- $this->trade_type = 'MWEB';
- }
-
-
-
-
- public function unifiedOrder($totalFee, $outTradeNo, $orderName, $notifyUrl, $timestamp){
- $sceneInfo = '{"h5_info": {"type":"Wap","wap_url": '.APP_PATH.',"wap_name": "在线订单"}}';
- $this->body = $orderName;
- $this->out_trade_no = $outTradeNo;
- $this->total_fee = $totalFee;
- $this->trade_type = $this->trade_type;
- $this->scene_info = $sceneInfo;
- $this->notify_url = $notifyUrl;
-
- $this->nonce_str = $this->genRandomString();
- $this->spbill_create_ip = $_SERVER['REMOTE_ADDR'];
- $this->params['appid'] = $this->appid;
- $this->params['mch_id'] = $this->mchid;
- $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->get_client_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;
- }
-
-
-
-
-
- public function orderQuery( $out_trade_no ){
- $this->params['appid'] = $this->appid;
- $this->params['mch_id'] = $this->mchid;
- $this->params['nonce_str'] = $this->genRandomString();
- $this->params['out_trade_no'] = $out_trade_no;
-
- $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::ORDERQUERY_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;
- }
-
-
-
-
-
- public function closeOrder( $out_trade_no ){
- $this->params['appid'] = $this->appid;
- $this->params['mch_id'] = $this->mchid;
- $this->params['nonce_str'] = $this->genRandomString();
- $this->params['out_trade_no'] = $out_trade_no;
-
- $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::CLOSEORDER_URL);
- if( !$response ){
- return false;
- }
- $result = $this->xml_to_data( $response );
- return $result;
- }
-
-
-
-
-
- public function getNotifyData(){
-
- $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
-
- $data = array();
- if( empty($xml) ){
- return false;
- }
- $data = $this->xml_to_data( $xml );
- if( !empty($data['return_code']) ){
- if( $data['return_code'] == 'FAIL' ){
- return false;
- }
- }
- return $data;
- }
-
-
-
-
- public function replyNotify(){
- $data['return_code'] = 'SUCCESS';
- $data['return_msg'] = 'OK';
- $xml = $this->data_to_xml( $data );
- echo $xml;
- die();
- }
-
-
-
-
- public function getAppPayParams( $prepayid ){
- $data['appid'] = $this->appid;
- $data['partnerid'] = $this->mchid;
- $data['prepayid'] = $prepayid;
- $data['package'] = 'Sign=WXPay';
- $data['noncestr'] = $this->genRandomString();
- $data['timestamp'] = time();
- $data['sign'] = $this->MakeSign( $data );
- return $data;
- }
-
-
-
-
- public function MakeSign( $params ){
-
- ksort($params);
- $string = $this->ToUrlParams($params);
-
- $string = $string . "&key=".$this->apiKey;
-
- $string = md5($string);
-
- $result = strtoupper($string);
- return $result;
- }
-
-
-
-
-
- public function ToUrlParams( $params ){
- $string = '';
- if( !empty($params) ){
- $array = array();
- foreach( $params as $key => $value ){
- $array[] = $key.'='.$value;
- }
- $string = implode("&",$array);
- }
- return $string;
- }
-
-
-
-
-
- public function data_to_xml( $params ){
- if(!is_array($params)|| count($params) <= 0)
- {
- return false;
- }
- $xml = "<xml>";
- foreach ($params as $key=>$val)
- {
- if (is_numeric($val)){
- $xml.="<".$key.">".$val."</".$key.">";
- }else{
- $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
- }
- }
- $xml.="</xml>";
- return $xml;
- }
-
-
-
-
-
- public function xml_to_data($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;
- }
-
-
-
- private static function getMillisecond(){
-
- $time = explode ( " ", microtime () );
- $time = $time[1] . ($time[0] * 1000);
- $time2 = explode( ".", $time );
- $time = $time2[0];
- return $time;
- }
-
-
-
-
-
- private function genRandomString($len = 32) {
- $chars = array(
- "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
- "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
- "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
- "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
- "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
- "3", "4", "5", "6", "7", "8", "9"
- );
- $charsLen = count($chars) - 1;
-
- shuffle($chars);
- $output = "";
- for ($i = 0; $i < $len; $i++) {
- $output .= $chars[mt_rand(0, $charsLen)];
- }
- return $output;
- }
-
-
-
-
-
-
-
-
-
- private function postXmlCurl($xml, $url, $useCert = false, $second = 30){
- $ch = curl_init();
-
- curl_setopt($ch, CURLOPT_TIMEOUT, $second);
- curl_setopt($ch,CURLOPT_URL, $url);
- curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
- curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);
-
- curl_setopt($ch, CURLOPT_HEADER, FALSE);
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- if($useCert == true){
-
-
- curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
-
- curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
-
- }
-
- curl_setopt($ch, CURLOPT_POST, TRUE);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
-
- $data = curl_exec($ch);
-
- if($data){
- curl_close($ch);
- return $data;
- } else {
- $error = curl_errno($ch);
- curl_close($ch);
- return false;
- }
- }
- function get_client_ip() {
- static $realip;
- if (isset($_SERVER)) {
- if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
- $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
- } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
- $realip = $_SERVER["HTTP_CLIENT_IP"];
- } else {
- $realip = $_SERVER["REMOTE_ADDR"];
- }
- } else {
- if (getenv("HTTP_X_FORWARDED_FOR")) {
- $realip = getenv("HTTP_X_FORWARDED_FOR");
- } else if (getenv("HTTP_CLIENT_IP")) {
- $realip = getenv("HTTP_CLIENT_IP");
- } else {
- $realip = getenv("REMOTE_ADDR");
- }
- }
- return $realip;
- }
-
-
-
-
-
- public function error_code( $code ){
- $errList = array(
- 'NOAUTH' => '商户未开通此接口权限',
- 'NOTENOUGH' => '用户帐号余额不足',
- 'ORDERNOTEXIST' => '订单号不存在',
- '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' => '未使用指定编码格式',
- );
- if( array_key_exists( $code , $errList ) ){
- return $errList[$code];
- }
- }
- }
调用方法:
- $outTradeNo = $trade_sn;
- $payAmount = $allmoney*100;
- $orderName = $trade_sn;
- $notifyUrl = APP_PATH.'wx/h5notify.php';
- $payTime = time();
- pc_base::load_app_class('Wxh5pay','pay',0);
- $wxh5Pay = new Wxh5pay($mchid,$appid,$apiKey);
- $result = $wxh5Pay->unifiedOrder($payAmount,$outTradeNo,$orderName,$notifyUrl,$payTime);
- $redirect_url = urlencode(APP_PATH.'wx/wxh5notify.php?trade_sn='.$trade_sn);
- $wxh5url = $result['mweb_url'].'&redirect_url='.$redirect_url;
前端调用
- <a href="{$wxh5url}"><button class="wx-btn">手机微信支付</button></a>
¥ 打赏
×
如果您觉得文章帮助了您就打赏一下吧
非常感谢你的打赏,我们将继续分享更多优质内容,让我们一起创建更加美好的网络世界!
支付宝扫一扫打赏
微信扫一扫打赏
本文《phpcms封装的微信h5支付源码DEMO参考》发布于石头博客文章,作者:admin,如若转载,请注明出处:https://www.pweb123.com/html/php/782.html,否则禁止转载,谢谢配合!
文章点评