支付开发过程,分享给大家,供大家参考,具体内容如下
1.开发环境
Thinkphp 3.2.3
:服务号,已认证
开发域名: (自定义的域名,外网不可访问)
2.需要相关文件和权限
微信支付需申请开通
微信公众平台开发者文档:
微信支付开发者文档:
微信支付SDK下载地址:
3.开发
下载好微信支付PHP版本的SDK,文件目录为下图:
把微信支付SDK的Cert和Lib目录放入Thinkphp,目录为
现在介绍微信支付授权目录问题,首先是微信支付开发配置里面的支付授权目录填写,
然后填写JS接口安全域。
最后设置网页授权
这些设置完,基本完成一半,注意设置的目录和我thinkphp里面的目录。
4.微信支付配置
把相关配置填写正确。
-
/**
-
* 配置账号信息
-
*/
-
-
class WxPayConfig
-
{
-
//=======【基本信息设置】=====================================
-
//
-
/**
-
* TODO: 修改这里配置为您自己申请的商户信息
-
* 微信公众号信息配置
-
*
-
* APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
-
*
-
* MCHID:商户号(必须配置,开户邮件中可查看)
-
*
-
* KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
-
* 设置地址:
-
*
-
* 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证书下载地址:,下载之前需要安装商户操作证书)
-
* @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;
-
-
-
-
-
-
-
-
class ParentController extends Controller {
-
protected $options = array (
-
'token' => '',
-
'encodingaeskey' => '',
-
'appid' => '',
-
'appsecret' => '',
-
'debug' => false,
-
'logcallback' => ''
-
);
-
public $errCode = 40001;
-
public $errMsg = "no access";
-
-
-
-
-
-
public function getToken(){
-
$cache_token = S('exp_wechat_pay_token');
-
if(!empty($cache_token)){
-
return $cache_token;
-
}
-
$url = '%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'];
-
}
-
-
-
-
-
-
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;
-
}
-
-
-
-
-
-
-
public function sendTemplateMessage($data){
-
$token = $this->getToken();
-
if (empty($token)) return false;
-
$url = "%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);
-
}
-
-
-
-
-
-
static function json_encode($arr) {
-
$parts = array ();
-
$is_list = false;
-
-
$keys = array_keys ( $arr );
-
$max_length = count ( $arr ) - 1;
-
if (($keys [0] === 0) && ($keys [$max_length] === $max_length )) {
-
$is_list = true;
-
for($i = 0; $i < count ( $keys ); $i ++) {
-
if ($i != $keys [$i]) {
-
$is_list = false;
-
break;
-
}
-
}
-
}
-
foreach ( $arr as $key => $value ) {
-
if (is_array ( $value )) {
-
if ($is_list)
-
$parts [] = self::json_encode ( $value );
-
else
-
$parts [] = '"' . $key . '":' . self::json_encode ( $value );
-
} else {
-
$str = '';
-
if (! $is_list)
-
$str = '"' . $key . '":';
-
-
if (!is_string ( $value ) && is_numeric ( $value ) && $value<2000000000)
-
$str .= $value;
-
elseif ($value === false)
-
$str .= 'false';
-
elseif ($value === true)
-
$str .= 'true';
-
else
-
$str .= '"' . addslashes ( $value ) . '"';
-
-
$parts [] = $str;
-
}
-
}
-
$json = implode ( ',', $parts );
-
if ($is_list)
-
return '[' . $json . ']';
-
return '{' . $json . '}';
-
}
-
-
-
-
-
-
-
-
-
-
-
-
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;
-
}
-
-
-
-
-
-
-
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_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;
-
}
-
}
-
-
-
-
-
-
-
-
-
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);
-
}
-
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;
-
}
-
}
-
}
贴上模板HTML
-
-
-
"content-type" content="text/html;charset=utf-8"/>
-
"viewport" content="width=device-width, initial-scale=1"/>
-
微信支付样例-支付
-
-
-
-
-
"#9ACD32">该笔订单支付金额为"color:#f00;font-size:50px">1分钱
-
"center">
-
type="button" onclick="callpay()" >立即支付
-
-
-
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;
-
}
-
-
-
-
-
-
public function IsSuccess(){
-
return $this->para;
-
}
-
}