首頁 後端開發 php教程 PHP微信支付开发

PHP微信支付开发

Jun 23, 2016 pm 01:29 PM

1.开发环境
Thinkphp 3.2.3
微信:服务号,已认证
开发域名:http://test.paywechat.com (自定义的域名,外网不可访问)

2.需要相关文件和权限
微信支付需申请开通
微信公众平台开发者文档:http://mp.weixin.qq.com/wiki/home/index.html
微信支付开发者文档:https://pay.weixin.qq.com/wiki/doc/api/index.html
微信支付SDK下载地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1

3.开发
下载好微信支付PHP版本的SDK,文件目录为下图:


把微信支付SDK的Cert和Lib目录放入Thinkphp,目录为

现在介绍微信支付授权目录问题,首先是微信支付开发配置里面的支付授权目录填写,

然后填写JS接口安全域。

最后设置网页授权

这些设置完,基本完成一半,注意设置的目录和我thinkphp里面的目录。

4.微信支付配置

把相关配置填写正确。

/** * 配置账号信息 */class WxPayConfig{    //=======【基本信息设置】=====================================    //    /** * TODO: 修改这里配置为您自己申请的商户信息 * 微信公众号信息配置 * * APPID:绑定支付的APPID(必须配置,开户邮件中可查看) * * MCHID:商户号(必须配置,开户邮件中可查看) * * KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置) * 设置地址:https://pay.weixin.qq.com/index.php/account/api_cert * * APPSECRET:公众帐号secert(仅JSAPI支付的时候需要配置, 登录公众平台,进入开发者中心可设置), * 获取地址:https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=2005451881&lang=zh_CN * @var string */    const APPID = '';    const MCHID = '';    const KEY = '';    const APPSECRET = '';    //=======【证书路径设置】=====================================    /** * TODO:设置商户证书路径 * 证书路径,注意应该填写绝对路径(仅退款、撤销订单时需要,可登录商户平台下载, * API证书下载地址:https://pay.weixin.qq.com/index.php/account/api_cert,下载之前需要安装商户操作证书) * @var path */    const SSLCERT_PATH = '../cert/apiclient_cert.pem';    const SSLKEY_PATH = '../cert/apiclient_key.pem';    //=======【curl代理设置】===================================    /** * TODO:这里设置代理机器,只有需要代理的时候才设置,不需要代理,请设置为0.0.0.0和0 * 本例程通过curl使用HTTP POST方法,此处可修改代理服务器, * 默认CURL_PROXY_HOST=0.0.0.0和CURL_PROXY_PORT=0,此时不开启代理(如有需要才设置) * @var unknown_type */    const CURL_PROXY_HOST = "0.0.0.0";//"10.152.18.220";    const CURL_PROXY_PORT = 0;//8080;    //=======【上报信息配置】===================================    /** * TODO:接口调用上报等级,默认紧错误上报(注意:上报超时间为【1s】,上报无论成败【永不抛出异常】, * 不会影响接口调用流程),开启上报之后,方便微信监控请求调用的质量,建议至少 * 开启错误上报。 * 上报等级,0.关闭上报; 1.仅错误出错上报; 2.全量上报 * @var int */    const REPORT_LEVENL = 1;}
登入後複製

现在开始贴出代码:

namespace Wechat\Controller;use Think\Controller;/** * 父类控制器,需要继承 * @file ParentController.class.php * @author Gary <lizhiyong2204@sina.com> * @date 2015年8月4日 * @todu */class ParentController extends Controller {     protected $options = array (            'token' => '', // 填写你设定的key            'encodingaeskey' => '', // 填写加密用的EncodingAESKey            'appid' => '', // 填写高级调用功能的app id            'appsecret' => '', // 填写高级调用功能的密钥            'debug' => false,            'logcallback' => ''    );          public $errCode = 40001;       public $errMsg = "no access";      /** * 获取access_token * @return mixed|boolean|unknown */    public function getToken(){        $cache_token = S('exp_wechat_pay_token');        if(!empty($cache_token)){            return $cache_token;        }        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';        $url = sprintf($url,$this->options['appid'],$this->options['appsecret']);              $result = $this->http_get($url);        $result = json_decode($result,true);          if(empty($result)){            return false;        }           S('exp_wechat_pay_token',$result['access_token'],array('type'=>'file','expire'=>3600));        return $result['access_token'];    }    /** * 发送客服消息 * @param array $data 消息结构{"touser":"OPENID","msgtype":"news","news":{...}} */    public function sendCustomMessage($data){        $token = $this->getToken();        if (empty($token)) return false;               $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s';        $url = sprintf($url,$token);        $result = $this->http_post($url,self::json_encode($data));        if ($result)        {            $json = json_decode($result,true);            if (!$json || !empty($json['errcode'])) {                $this->errCode = $json['errcode'];                $this->errMsg = $json['errmsg'];                return false;            }            return $json;        }        return false;    }    /** * 发送模板消息 * @param unknown $data * @return boolean|unknown */    public function sendTemplateMessage($data){        $token = $this->getToken();        if (empty($token)) return false;        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s";        $url = sprintf($url,$token);        $result = $this->http_post($url,self::json_encode($data));        if ($result)        {            $json = json_decode($result,true);            if (!$json || !empty($json['errcode'])) {                $this->errCode = $json['errcode'];                $this->errMsg = $json['errmsg'];                return false;            }            return $json;        }        return false;    }    public function getFileCache($name){        return S($name);    }    /** * 微信api不支持中文转义的json结构 * @param array $arr */    static function json_encode($arr) {        $parts = array ();        $is_list = false;        //Find out if the given array is a numerical array        $keys = array_keys ( $arr );        $max_length = count ( $arr ) - 1;        if (($keys [0] === 0) && ($keys [$max_length] === $max_length )) { //See if the first key is 0 and last key is length - 1            $is_list = true;            for($i = 0; $i < count ( $keys ); $i ++) { //See if each key correspondes to its position                if ($i != $keys [$i]) { //A key fails at position check.                    $is_list = false; //It is an associative array.                    break;                }            }        }        foreach ( $arr as $key => $value ) {            if (is_array ( $value )) { //Custom handling for arrays                if ($is_list)                    $parts [] = self::json_encode ( $value ); /* :RECURSION: */                else                    $parts [] = '"' . $key . '":' . self::json_encode ( $value ); /* :RECURSION: */            } else {                $str = '';                if (! $is_list)                    $str = '"' . $key . '":';                //Custom handling for multiple data types                if (!is_string ( $value ) && is_numeric ( $value ) && $value<2000000000)                    $str .= $value; //Numbers                elseif ($value === false)                $str .= 'false'; //The booleans                elseif ($value === true)                $str .= 'true';                else                    $str .= '"' . addslashes ( $value ) . '"'; //All other things                // :TODO: Is there any more datatype we should be in the lookout for? (Object?)                $parts [] = $str;            }        }        $json = implode ( ',', $parts );        if ($is_list)            return '[' . $json . ']'; //Return numerical JSON        return '{' . $json . '}'; //Return associative JSON    }    /** +---------------------------------------------------------- * 生成随机字符串 +---------------------------------------------------------- * @param int $length 要生成的随机字符串长度 * @param string $type 随机码类型:0,数字+大小写字母;1,数字;2,小写字母;3,大写字母;4,特殊字符;-1,数字+大小写字母+特殊字符 +---------------------------------------------------------- * @return string +---------------------------------------------------------- */    static public function randCode($length = 5, $type = 2){        $arr = array(1 => "0123456789", 2 => "abcdefghijklmnopqrstuvwxyz", 3 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4 => "~@#$%^&*(){}[]|");        if ($type == 0) {            array_pop($arr);            $string = implode("", $arr);        } elseif ($type == "-1") {            $string = implode("", $arr);        } else {            $string = $arr[$type];        }        $count = strlen($string) - 1;        $code = '';        for ($i = 0; $i < $length; $i++) {            $code .= $string[rand(0, $count)];        }        return $code;    }       /** * GET 请求 * @param string $url */    private function http_get($url){        $oCurl = curl_init();        if(stripos($url,"https://")!==FALSE){            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1        }        curl_setopt($oCurl, CURLOPT_URL, $url);        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );        $sContent = curl_exec($oCurl);        $aStatus = curl_getinfo($oCurl);        curl_close($oCurl);        if(intval($aStatus["http_code"])==200){            return $sContent;        }else{            return false;        }    }    /** * POST 请求 * @param string $url * @param array $param * @param boolean $post_file 是否文件上传 * @return string content */    private function http_post($url,$param,$post_file=false){        $oCurl = curl_init();        if(stripos($url,"https://")!==FALSE){            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1        }        if (is_string($param) || $post_file) {            $strPOST = $param;        } else {            $aPOST = array();            foreach($param as $key=>$val){                $aPOST[] = $key."=".urlencode($val);            }            $strPOST =  join("&", $aPOST);        }        curl_setopt($oCurl, CURLOPT_URL, $url);        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );        curl_setopt($oCurl, CURLOPT_POST,true);        curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);        $sContent = curl_exec($oCurl);        $aStatus = curl_getinfo($oCurl);        curl_close($oCurl);        if(intval($aStatus["http_code"])==200){            return $sContent;        }else{            return false;        }    }}
登入後複製
namespace Wechat\Controller;use Wechat\Controller\ParentController;/** * 微信支付测试控制器 * @file TestController.class.php * @author Gary <lizhiyong2204@sina.com> * @date 2015年8月4日 * @todu */class TestController extends ParentController {    private $_order_body       = 'xxx';    private $_order_goods_tag  = 'xxx';    public function __construct(){        parent::__construct();        require_once ROOT_PATH."Api/lib/WxPay.Api.php";        require_once ROOT_PATH."Api/lib/WxPay.JsApiPay.php";    }    public function index(){        //①、获取用户openid        $tools = new \JsApiPay();        $openId = $tools->GetOpenid();               //②、统一下单        $input = new \WxPayUnifiedOrder();             //商品描述        $input->SetBody($this->_order_body);        //附加数据,可以添加自己需要的数据,微信回异步回调时会附加这个数据        $input->SetAttach('xxx');        //商户订单号        $out_trade_no = \WxPayConfig::MCHID.date("YmdHis");        $input->SetOut_trade_no($out_trade_no);        //总金额,订单总金额,只能为整数,单位为分         $input->SetTotal_fee(1);        //交易起始时间        $input->SetTime_start(date("YmdHis"));        //交易结束时间        $input->SetTime_expire(date("YmdHis", time() + 600));        //商品标记        $input->SetGoods_tag($this->_order_goods_tag);        //通知地址,接收微信支付异步通知回调地址 SITE_URL=http://test.paywechat.com/Charge        $notify_url = SITE_URL.'/index.php/Test/notify.html';        $input->SetNotify_url($notify_url);        //交易类型        $input->SetTrade_type("JSAPI");        $input->SetOpenid($openId);        $order = \WxPayApi::unifiedOrder($input);        $jsApiParameters = $tools->GetJsApiParameters($order);        //获取共享收货地址js函数参数        $editAddress = $tools->GetEditAddressParameters();        $this->assign('openId',$openId);        $this->assign('jsApiParameters',$jsApiParameters);        $this->assign('editAddress',$editAddress);        $this->display();          }    /** * 异步通知回调方法 */    public function notify(){        require_once ROOT_PATH."Api/lib/notify.php";        $notify = new \PayNotifyCallBack();        $notify->Handle(false);        //这里的IsSuccess是我自定义的一个方法,后面我会贴出这个文件的代码,供参考。        $is_success = $notify->IsSuccess();          $bdata 		= $is_success['data'];           //支付成功        if($is_success['code'] == 1){                      $news = array(                    'touser' => $bdata['openid'],                    'msgtype' => 'news',                    'news'  => array (                            'articles'=> array (                                    array(                                            'title' => '订单支付成功',                                            'description' => "支付金额:{$bdata['total_fee']}\n".                                            "微信订单号:{$bdata['transaction_id']}\n"                                            'picurl' => '',                                            'url' => ''                                             )                            )                    )            );            //发送微信支付通知            $this->sendCustomMessage($news);                    }else{//支付失败        }    }    /** * 支付成功页面 * 不可靠的回调 */    public function ajax_PaySuccess(){        //订单号        $out_trade_no = I('post.out_trade_no');        //支付金额        $total_fee    = I('post.total_fee');        /*相关逻辑处理*/    }
登入後複製

贴上模板HTML

<html><head>    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>    <meta name="viewport" content="width=device-width, initial-scale=1"/>     <title>微信支付样例-支付</title>    <script type="text/javascript"> //调用微信JS api 支付 function jsApiCall() { WeixinJSBridge.invoke( 'getBrandWCPayRequest', {$jsApiParameters}, function(res){ WeixinJSBridge.log(res.err_msg); //取消支付 if(res.err_msg == 'get_brand_wcpay_request:cancel'){ //处理取消支付的事件逻辑 }else if(res.err_msg == "get_brand_wcpay_request:ok"){ /*使用以上方式判断前端返回,微信团队郑重提示: res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。 这里可以使用Ajax提交到后台,处理一些日志,如Test控制器里面的ajax_PaySuccess方法。 */ } alert(res.err_code+res.err_desc+res.err_msg); } ); } function callpay() { if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', jsApiCall, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', jsApiCall); document.attachEvent('onWeixinJSBridgeReady', jsApiCall); } }else{ jsApiCall(); } } //获取共享地址 function editAddress() { WeixinJSBridge.invoke( 'editAddress', {$editAddress}, function(res){ var value1 = res.proviceFirstStageName; var value2 = res.addressCitySecondStageName; var value3 = res.addressCountiesThirdStageName; var value4 = res.addressDetailInfo; var tel = res.telNumber; alert(value1 + value2 + value3 + value4 + ":" + tel); } ); } window.onload = function(){ if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', editAddress, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', editAddress); document.attachEvent('onWeixinJSBridgeReady', editAddress); } }else{ editAddress(); } }; </script></head><body>    <br/>    <font color="#9ACD32"><b>该笔订单支付金额为<span style="color:#f00;font-size:50px">1分</span>钱</b></font><br/><br/>    <div align="center">        <button style="width:210px; height:50px; border-radius: 15px;background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer; color:white; font-size:16px;" type="button" onclick="callpay()" >立即支付</button>    </div></body></html>
登入後複製

notify.php文件代码,这里有在官方文件里新添加的一个自定义方法。

require_once ROOT_PATH."Api/lib/WxPay.Api.php";require_once ROOT_PATH.'Api/lib/WxPay.Notify.php';require_once ROOT_PATH.'Api/lib/log.php';//初始化日志$logHandler= new \CLogFileHandler(ROOT_PATH."/logs/".date('Y-m-d').'.log');$log = \Log::Init($logHandler, 15);class PayNotifyCallBack extends WxPayNotify {    protected $para = array('code'=>0,'data'=>'');    //查询订单    public function Queryorder($transaction_id) {        $input = new \WxPayOrderQuery();        $input->SetTransaction_id($transaction_id);        $result = \WxPayApi::orderQuery($input);        \Log::DEBUG("query:" . json_encode($result));        if(array_key_exists("return_code", $result)            && array_key_exists("result_code", $result)            && $result["return_code"] == "SUCCESS"            && $result["result_code"] == "SUCCESS")        {            return true;        }        $this->para['code'] = 0;        $this->para['data'] = '';        return false;    }    //重写回调处理函数    public function NotifyProcess($data, &$msg) {        \Log::DEBUG("call back:" . json_encode($data));        $notfiyOutput = array();        if(!array_key_exists("transaction_id", $data)){            $msg = "输入参数不正确";            $this->para['code'] = 0;            $this->para['data'] = '';            return false;        }        //查询订单,判断订单真实性        if(!$this->Queryorder($data["transaction_id"])){            $msg = "订单查询失败";            $this->para['code'] = 0;            $this->para['data'] = '';            return false;        }        $this->para['code'] = 1;        $this->para['data'] = $data;        return true;    }    /** * 自定义方法 检测微信端是否回调成功方法 * @return multitype:number string */    public function IsSuccess(){        return $this->para;    }}
登入後複製

到这里基本上完成,可以在微信端打开http://test.paywechat.com/Charge/index.php/Test/index/
我的环境,HTTP服务器没有重写url,微信支付继续探索中,有些地方可能写的有问题或不足,望大家谅解,互相学习。

版权声明:本文为博主原创文章,未经博主允许不得转载。

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1665
14
CakePHP 教程
1424
52
Laravel 教程
1322
25
PHP教程
1270
29
C# 教程
1250
24
說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? 說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? Apr 17, 2025 am 12:06 AM

在PHP中,應使用password_hash和password_verify函數實現安全的密碼哈希處理,不應使用MD5或SHA1。1)password_hash生成包含鹽值的哈希,增強安全性。 2)password_verify驗證密碼,通過比較哈希值確保安全。 3)MD5和SHA1易受攻擊且缺乏鹽值,不適合現代密碼安全。

PHP和Python:比較兩種流行的編程語言 PHP和Python:比較兩種流行的編程語言 Apr 14, 2025 am 12:13 AM

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP行動:現實世界中的示例和應用程序 PHP行動:現實世界中的示例和應用程序 Apr 14, 2025 am 12:19 AM

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP:網絡開發的關鍵語言 PHP:網絡開發的關鍵語言 Apr 13, 2025 am 12:08 AM

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP的持久相關性:它還活著嗎? PHP的持久相關性:它還活著嗎? Apr 14, 2025 am 12:12 AM

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? Apr 17, 2025 am 12:25 AM

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

PHP和Python:代碼示例和比較 PHP和Python:代碼示例和比較 Apr 15, 2025 am 12:07 AM

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP與其他語言:比較 PHP與其他語言:比較 Apr 13, 2025 am 12:19 AM

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

See all articles