PHP微信支付开发实例详细教程​(附代码)

HP微信支付开发过程,分享给大家,供大家参考,具体内容如下

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,文件目录为下图:

PHP微信支付开发实例详细教程​(附代码)

PHP微信支付开发实例详细教程​(附代码)

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

PHP微信支付开发实例详细教程​(附代码)

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

然后填写JS接口安全域。

PHP微信支付开发实例详细教程​(附代码)

最后设置网页授权

PHP微信支付开发实例详细教程​(附代码)

PHP微信支付开发实例详细教程​(附代码)

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

PHP微信支付开发实例详细教程​(附代码)

4.微信支付配置

PHP微信支付开发实例详细教程​(附代码)

把相关配置填写正确。

 


 * @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;
 }
 }
}
 
 * @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模板的源码

 

 
  
 微信支付样例-支付
 


 
该笔订单支付金额为1分

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

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;
 }
}

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

本文来自投稿,不代表重蔚自留地立场,如若转载,请注明出处https://www.cwhello.com/4523.html

如有侵犯您的合法权益请发邮件951076433@qq.com联系删除

(0)
重蔚的头像重蔚管理团队
上一篇 2018年3月7日 10:28
下一篇 2018年3月7日 11:14

相关推荐

  • PHP8中的新函数:fdiv()的多种使用技巧。

    随着PHP8的推出,一个新的函数fdiv()也出现在了开发者面前。它可以简化代码、提高精度和效率。在这篇文章中,我们将探讨fdiv()的多种使用技巧。 一、提高精度: 在PHP7中,我们可以使用函数bcdiv()来实现高精度计算…

    2023年5月21日
    08
  • PHP8中的数组函数:array_key_first(),获取数组首个键名的技巧。

    PHP8中新增的数组函数array_key_first(),用于获取给定数组的首个键名。在很多情况下,需要获取一个数组中的首个元素,这时就可以用到这个函数。本文将介绍如何使用array_key_first()函数以及它带来的便利性。 一、…

    2023年5月21日
    03
  • PHP8中的函数:str_contains()的新特性。

    随着互联网的快速发展,编程语言也在不断地更新和升级。作为一种常用的编程语言,PHP也经历了无数次的改进和提升。PHP8作为最新版本,不仅引入了全新的特性,还升级了很多功能。其中,函数str_contains()就是PHP8中…

    2023年5月21日
    04
  • PHP实现数据库容器化监控的方法。

    随着容器化技术在云计算领域的广泛应用,大量的应用程序也开始在容器中运行,其中数据库也不例外。但是容器的动态性和快速的扩展能力,也给数据库的监控和管理带来一些困难。为了解决这个问题,本文将介绍一种基于P…

    2023年5月21日
    00
  • PHP8.0中的try语句块支持表达式

    随着计算机技术的飞速发展,编程语言也在不断地升级和完善。其中,PHP作为一种常用的Web开发语言,也在不断地推陈出新,不断地推出新的版本。最近,PHP8.0版本的发布引起了广泛的关注。其中,新版本中对于异常处理…

    2023年5月18日
    03
  • PHP产生不重复随机数的5个方法总结

    无论是Web应用,还是WAP或者移动应用,随机数都有其用武之地。PHP项目中,PHP程序员是需要经常的和随机数或者随机数组打交道,所以,本文就分享PHP如何产生不重复随机数常用的几种方法! 方法一: 方法二: 方法三 …

    2018年5月2日
    0281
  • PHP中的测试数据管理工具。

    PHP是一种广泛应用于Web开发的脚本语言,由于其易于学习和扩展性,已经成为大多数Web开发人员的首选语言。随着Web应用程序越来越复杂,测试也变得更加重要。为了成功地测试PHP代码,测试数据是必不可少的。在本文中…

    2023年5月28日
    00
  • 五种PHP实现定时任务的方法

    定时运行任务对于一个网站来说,是一个比较重要的任务,比如定时发布文档,定时清理垃圾信息等,现在的网站大多数都是采用PHP动态语言开发的,而对于PHP的实现决定了它没有Java和.Net这种AppServer的概念,而http协…

    2022年6月27日
    0118

联系我们

QQ:951076433

在线咨询:点击这里给我发消息邮件:951076433@qq.com工作时间:周一至周五,9:30-18:30,节假日休息