PHP implements WeChat payment function development code sharing
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
##/**
- ##* Configure account information
- */
##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 = '' ;
- ##const
MCHID = '';
##const -
KEY = '';
const -
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]================================== ====
- ##/**
- * TODO: Interface call reporting level, default error reporting (note: reporting timeout is [1s], reporting regardless of success or failure [Never throw an exception],
- * Will not affect the interface calling process), after turning on the reporting, it is convenient for WeChat monitoring request calls For quality, it is recommended to at least
- * Enable error reporting.
-
-
##*/
-
- REPORT_LEVENL = 1;
}
Start posting the code now:
# #[php] view plain copy
##namespace Wechat\ Controller;
- ##use
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
- ##'appsecret'
=> '', // 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
- ## * @param array $param
- * @param boolean $post_file Whether to upload files
-
-
-
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;
}
}
}
[php] view plain copy
namespace Wechat\Controller;
use Wechat\Controller\ParentController;
/**
## * WeChat payment test controller
* @file TestController.class.php
## * @author Gary
- ## * @date August 4, 2015
- * @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(){
//①. Get user openid
## $tools = new \JsApiPay();
# #$openId = $tools->GetOpenid();
-
//②. Unified order placement
- ##$input
= new \WxPayUnifiedOrder();
#//Product description -
- ->SetBody(
$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);
- #//Total amount, the total amount of the order, can only be an integer, the unit is cents
- $input
->SetTotal_fee(1);
- //Transaction start time
- $input
->SetTime_start(date("YmdHis"));
- //Transaction end time
$input->SetTime_expire(date("YmdHis ", time() + 600));
##//Product tag
##$input->SetGoods_tag($this->_order_goods_tag);
- #//Notification address, receive WeChat payment asynchronous notification callback address SITE_URL=http://test .paywechat.com/Charge
-
= SITE_URL. '/index.php/Test/notify.html';
$input -
->SetNotify_url($notify_url);
##/ /Trade Type -
##$input
->SetTrade_type( -
"JSAPI"); ##$input
->SetOpenid( - $openId
);
$order = \WxPayApi::unifiedOrder($input);
$jsApiParameters = $tools->GetJsApiParameters($order);
##//Get the shared delivery address js function parameters
##$editAddress = $tools->GetEditAddressParameters();
- ## $this
->assign('openId',$openId);
##$this -
->assign('jsApiParameters',$jsApiParameters);
##$this - ->assign (
'editAddress',$editAddress);
$this->display();
# # }
#/**
- ## * Asynchronous notification callback method
- */
- public
function notify (){
##require_once -
ROOT_PATH."Api/lib/notify.php ";
##$notify - =
new \PayNotifyCallBack(); ##$notify
->Handle(false); //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".
- ##"WeChat order number: {$bdata['transaction_id']}\n"
- 'picurl'
=> '',
- 'url'
=> ''
) -
## )
-
)
## );
//Send WeChat payment notification
# #$this->sendCustomMessage($news);
}else{//Payment failed
}
- ## }
-
- ##/**
-
-
-
-
function ajax_PaySuccess(){
//Order number -
= I('post.out_trade_no');
//Payment amount
$ total_fee = I('post.total_fee');
/*Related logic processing*/
-
## }
view plain copy
- #html> ##
##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(
- ## 'getBrandWCPayRequest',
- {$jsApiParameters},
- function(res){
-
## //Cancel payment
-
## if(
res.err_msg - == 'get_brand_wcpay_request:cancel'){
## //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
- ## Here you can use Ajax to submit to the background and process some logs, such as ajax_PaySuccess in the Test controller. method.
- */
- }
- 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>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>
notify.php file code, here is a custom method newly added in the official file.
head>
copy
- ##require_once
ROOT_PATH."Api/lib/WxPay.Api.php";
require_onceROOT_PATH.'Api/lib/WxPay.Notify.php';
require_onceROOT_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)
- {
- " call back:"
. json_encode($data));
- $notfiyOutput
= 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(){
- ##return
$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!

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment
