This article mainly introduces the development process of PHP WeChat payment in detail. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.
1. Development environment
Thinkphp 3.2.3
WeChat: Service account, certified
Development domain name: http://test.paywechat.com (since The defined domain name is not accessible from the external network)
2. Requires relevant documents and permissions
WeChat payment needs to be activated
WeChat public platform developer documentation: http:// mp.weixin.qq.com/wiki/home/index.html
WeChat Payment Developer Documentation: https://pay.weixin.qq.com/wiki/doc/api/index.html
WeChat Payment SDK download address: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1
3. Development
Download WeChat To pay for the PHP version of the SDK, the file directory is as shown below:
Put the Cert and Lib directories of the WeChat payment SDK Thinkphp, the directory is
Now we will introduce the WeChat payment authorization directory issue. The first is to fill in the payment authorization directory in the WeChat payment development configuration,
Then fill in the js interface security domain.
Finally set the web page authorization
After these settings are completed, it is basically half completed. Pay attention to the set directory and the directory in my thinkphp.
4. WeChat payment configuration
Fill in the relevant configuration correctly.
##[php] view plain copy
##/**
##class WxPayConfig
//========[Basic information settings]==================== =================
//
/**
* TODO: Modify the configuration here to apply for your own merchant information
* WeChat public account information configuration
## *
## * APPSECRET: Public account secert (only required for JSAPI payment, log in to the public platform and enter the developer center to set),
## * Obtain address: https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=2005451881&lang=zh_CN
* @var string
## */
const APPID = '' ;
MCHID = '';
KEY = '';
APPSECRET = '';
##//=======【 Certificate path settings】======================================
/**
* TODO :Set the merchant certificate path
* Certificate path, please note that the absolute path should be filled in (only required for refunds and order cancellations, you can log in Merchant platform download,
* API certificate download address: https://pay.weixin.qq.com/index.php/account/api_cert, you need to install the merchant operation certificate before downloading)
## * @var path
*/
const SSLCERT_PATH = '. ./cert/apiclient_cert.pem';
const SSLKEY_PATH = '../cert/apiclient_key.pem';
##//========[curl proxy settings]======================== ============
## * This routine uses the HTTP POST method through curl. The proxy server can be modified here,
* The default is CURL_PROXY_HOST=0.0.0.0 and CURL_PROXY_PORT=0, the proxy is not enabled at this time (set it if necessary)
* @var unknown_type
##*/
const CURL_PROXY_HOST = "0.0.0.0";// "10.152.18.220";
##const CURL_PROXY_PORT = 0; //8080;
##//========[Report information configuration]================================== ====
##*/
const
}
Start posting the code now:
# #[php] view plain copy
##namespace Wechat\ Controller;
Think\Controller;
## * @author Gary
# * @date August 4, 2015
* @todu
*/
##class ParentController extends Controller {
protected $options = array (
'token' => '', // Fill in the key you set
##'encodingaeskey' => '', // Fill in the EncodingAESKey for encryption
'appid' => '', // Fill in the advanced The app id of the calling function
=> '', // Fill in the key for the advanced calling function
'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'];
}
/**
## * Send customer service message
* @param array $data Message structure {"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;
}
#
/**
## * Send template message
* @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);
}
/**
## * WeChat API does not support Chinese escaped json structure
* @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
$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
}
/**
+--------------------------------- ----------------------------
* Generate random string
+------------------ ----------------------------------------
* @param int $length The length of the random string to be generated
* @param string $type Random code type: 0, numbers + upper and lower case letters; 1, numbers; 2, lower case letters; 3, upper case letters; 4, special characters; -1, numbers + upper and lower case letters + special characters
+----------------------------------------------------------
* @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 request
# # * @param string $url
function http_post($url,$param,$post_file=false){
= 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;
}
}
}
[php] view plain copy
namespace Wechat\Controller;
use Wechat\Controller\ParentController;
/**
## * WeChat payment test controller
* @file TestController.class.php
## * @author Gary
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(){
//①. Get user openid
## $tools = new \JsApiPay();
# #$openId = $tools->GetOpenid();
//②. Unified order placement
= new \WxPayUnifiedOrder();
##$input
$this->_order_body); ##//Additional data, you can add the data you need, and WeChat will return an asynchronous callback This data will be appended
$input->SetAttach('xxx');
//Merchant order number
##$out_trade_no = \WxPayConfig::MCHID.date("YmdHis");
##$input->SetOut_trade_no($out_trade_no);
->SetTotal_fee(1);
->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis ", time() + 600));
##//Product tag
##$input->SetGoods_tag($this->_order_goods_tag);
= SITE_URL. '/index.php/Test/notify.html';
->SetNotify_url($notify_url);
##/ /Trade Type##$input
"JSAPI"); ##$input
->SetOpenid();
$order = \WxPayApi::unifiedOrder($input);
$jsApiParameters = $tools->GetJsApiParameters($order);
##//Get the shared delivery address js function parameters
##$editAddress = $tools->GetEditAddressParameters();
->assign('openId',$openId);
->assign('jsApiParameters',$jsApiParameters);
##$this'editAddress',$editAddress);
$this->display();
# # }
#/**
function notify (){
ROOT_PATH."Api/lib/notify.php ";
##$notifynew \PayNotifyCallBack(); ##$notify
//The IsSuccess here is a method customized by me. I will post the code of this file later for reference.
$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' => 'Order payment successful',
'description' => "Payment amount: {$bdata['total_fee']}\n".
=> '',
=> ''
## )
)
## );
//Send WeChat payment notification
# #$this->sendCustomMessage($news);
}else{//Payment failed
}
function ajax_PaySuccess(){
= I('post.out_trade_no');
//Payment amount
$ total_fee = I('post.total_fee');
/*Related logic processing*/
## }
view plain copy
head> ##meta http-equiv="content-type" content="text/html;charset=utf-8" />
##meta name="viewport" content="width= device-width, initial-scale=1"/>
title>WeChat payment sample-paymenttitle >
script type= "text/javascript">
## //Call WeChat JS api payment
function jsApiCall()
{
WeixinJSBridge.invoke(
## //Cancel payment
## if(
res.err_msg## //Event logic for processing payment cancellation
## }else if(res.err_msg
== "get_brand_wcpay_request:ok"){/*Use the above method to determine the front-end return. The WeChat team solemnly reminds:
res.err_msg will be used when the user pays Returns ok after success, but it does not guarantee that it is absolutely reliable
}
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>The payment amount for this order isspan style="color:#f00;font-size:50px">1 pointspan>钱b>font>< ;br/>br/>
p 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>
p>
##body>
##html>
copy
ROOT_PATH."Api/lib/WxPay.Api.php";
ROOT_PATH.'Api/lib/WxPay.Notify.php';
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;
}
//Rewrite the callback processing function
##public function NotifyProcess($data, &$msg)
. json_encode($data));
= array();
if(!array_key_exists("transaction_id", $data)){
$msg = "Input parameters are incorrect";
$this->para['code'] = 0;
$this->para['data'] = '';
##return false;
}
//Query the order to determine if the order is genuine sex
if(!$this->Queryorder($ data["transaction_id"])){
# #$msg = "Order query failed";
$this->para['code'] = 0;
$this->para['data'] = '';
#return false;
$this->para['code'] = 1;
$this->para['data'] = $data;
return true;
}
/**
## * Custom method to detect whether the callback on WeChat is successful
* @return multitype:number string
*/
##public function IsSuccess(){
$this->para;
## }}
This is basically completed. You can open
Related recommendations: detailed explanation of nodejs implementation of WeChat payment function example
The above is the detailed content of PHP implements WeChat payment function development code sharing. For more information, please follow other related articles on the PHP Chinese website!