


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 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

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.
