


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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
