Skip to content

Commit

Permalink
New features
Browse files Browse the repository at this point in the history
  • Loading branch information
plbin97 committed Jan 11, 2018
1 parent 771280f commit feeacd0
Show file tree
Hide file tree
Showing 21 changed files with 271 additions and 65 deletions.
Empty file modified LICENSE
100644 → 100755
Empty file.
16 changes: 14 additions & 2 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
# PHP Connect To WeChat Pay API For PayJs
一个可以直接接入系统的PayJs的微信支付接口的PHP代码。<br>

暂时这个不存在任何安全问题。
# 如何布置?
1,编辑config.php,设置里面的信息,文件内有注释来指示你怎么改。<br>
2,编辑action.php,这个里面放置你自己需要运行的PHP代码,并且里面有你需要的变量,以及什么时候被执行,文件内有注释来指示你怎么做。(当然,你可以忽略这个)<br>
3,复制整个项目到你的网站根目录或者任意一个位置,但必须能够访问得到。<br>
# 如何使用?
使用前,请确保布置完毕。<br>
使用方法:访问xxx.com/aaa/PHP-Connect-To-WeChat-Pay-API-For-PayJs/index.php?money=100&title=支付标题<br>
其xxx.com/aaa/PHP-Connect-To-WeChat-Pay-API-For-PayJs/是你访问到这个PHP的URL以及页面路径。<br>
其中:xxx.com/aaa/PHP-Connect-To-WeChat-Pay-API-For-PayJs/是你访问到这个PHP的URL以及页面路径。<br>

访问时,请确保有两个参数?money=100&title=XXX<br>
money参数指要支付多少钱,单位为分。<br>
title指即将显示在微信支付手机上的支付标题。<br>

# 我们还没做的:
1,我们正在做异步账单查询通知<br>
2,我们正在解决手机移动设备的访问。<br>

# 使用前,请注意:
Session变量:<br>
$_SESSION['payjs_payment_info']
$_SESSION['payjs_paid'] <br>
我已经用了,所以别占这两个变量。<br>

使用异步账单通知时,把这个站点的文件的PHP读写权限开启。<br>

最后 ,一定要看config.php和action.php的注释
39 changes: 20 additions & 19 deletions action.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
function after_payment_created($payjs_order_id, $out_trade_no, $total_fee, $code_url) {

/*
* $payjs_order_id PAYJS 平台订单号 String[32]
* $out_trade_no 用户端自主生成的订单号 String[32]
* $total_fee 金额。单位:分 integer[16]
* $code_url 扫码的二维码地址 String[64]
*
* 这里放置用户在账单刚刚创建之后,用户扫码之前,执行的代码。
* 总共有4个你需要的变量,在上面。
* 总共有4个你需要的变量:
* $payjs_order_id PAYJS平台订单号 String[32]
* $out_trade_no 用户端自主生成的订单号 String[32]
* $total_fee 金额。单位:分 integer[16]
* $code_url 扫码的二维码地址 String[64]
* 建议你将这些数据存入你的数据库
*
*/
//======================================================================
//代码放这里:
Expand All @@ -33,16 +33,18 @@ function after_payment_created($payjs_order_id, $out_trade_no, $total_fee, $code
//======================================================================
}

function after_payment_has_been_paid($payjs_order_id, $out_trade_no, $total_fee, $code_url) {
sleep(3); //停顿三秒,让用户看看我做的精良界面
function after_payment_has_been_paid($payjs_order_id, $out_trade_no, $total_fee) {

/*
* $payjs_order_id PAYJS 平台订单号 String[32]
* $out_trade_no 用户端自主生成的订单号 String[32]
* $total_fee 金额。单位:分 integer[16]
* $code_url 扫码的二维码地址 String[64]
*
* 这里放置用户在付款成功之后,跳转到你页面之前,执行的代码
* 总共4个你需要的变量,在上面。
*
* 注意!如果你开启了异步账单通知,这里的代码有可能(我说的是有可能)执行两次,所以建议做好做好去重逻辑。
*
* 总共3个你需要的变量:
* $payjs_order_id PAYJS 平台订单号 String[32]
* $out_trade_no 用户端自主生成的订单号 String[32]
* $total_fee 金额。单位:分 integer[16]
*/
//=======================================================================
//代码放这里:
Expand All @@ -53,19 +55,18 @@ function after_payment_has_been_paid($payjs_order_id, $out_trade_no, $total_fee,


//========================================================================
//但是小心一点,如果你用了异步账单消息推送,这个代码会执行两次,一次是在用户点击账单确认按钮的时候,一次是异步消息推送的时候。
}

function after_cancel_payment($payjs_order_id, $out_trade_no, $total_fee, $code_url) {
/*
* $payjs_order_id PAYJS 平台订单号 String[32]
* $out_trade_no 用户端自主生成的订单号 String[32]
* $total_fee 金额。单位:分 integer[16]
* $code_url 扫码的二维码地址 String[64]
*
* 这里放置用户创建订单后,但又取消了订单,的时候执行的代码。
* 一旦用户创建订单后,又不想付了,于是就会执行这里的代码
* 总共4个你需要的变量,在上面。
* 总共4个你需要的变量:
* $payjs_order_id PAYJS 平台订单号 String[32]
* $out_trade_no 用户端自主生成的订单号 String[32]
* $total_fee 金额。单位:分 integer[16]
* $code_url 扫码的二维码地址 String[64]
*/
//=========================================================================
//代码放这里:
Expand Down
6 changes: 4 additions & 2 deletions cancel_payment.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
session_start();

//通过Session验证订单是否以及创建
if(!isset($_SESSION['payjs_payment_info']) || !isset($_SESSION['payjs_paid'])) {
if(!isset($_SESSION['payjs_payment_info'])) {
die("error | 错误");
}
$payment = $_SESSION['payjs_payment_info'];

//取消Session变量
unset($_SESSION['payjs_payment_info']);
unset($_SESSION['payjs_paid']);
if(!$use_asynchronous_payment_check) {
unset($_SESSION['payjs_paid']);
}

//执行action.php里面的代码
after_cancel_payment($payment -> payjs_order_id, $payment -> out_trade_no, $payment -> total_fee, $payment -> code_url);
Expand Down
11 changes: 7 additions & 4 deletions check_payment.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@

//调用corn文件夹里的函数来判断账单是否已付款
if (does_payment_has_been_paid($_SESSION['payjs_payment_info'] -> payjs_order_id)) {
$_SESSION['payjs_paid'] = true;
if(!$use_asynchronous_payment_check) {
$_SESSION['payjs_paid'] = true;
}
echo "1";
}else {
$_SESSION['payjs_paid'] = FALSE;
if(!$use_asynchronous_payment_check) {
$_SESSION['payjs_paid'] = FALSE;
}
echo "0";
}

}
30 changes: 24 additions & 6 deletions config.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
/*
* $config_mchid是你的微信支付接口的商户号。
*/
$config_mchid = "XXXX"; //商户号 Your business Number
$config_mchid = "ZMEKQZ"; //商户号 Your business Number

/*
* $config_key是你的微信支付接口的密钥。
*/
$config_key = "xxxxxx"; //密钥 Your private Key
$config_key = "0hC7YSaoS5zCTDuN"; //密钥 Your private Key

//====================================================================================

Expand All @@ -37,7 +37,7 @@
//====================================================================================

/*
* $use_validation_code是决定你是否在别人支付前启用验证码,
* $use_validation_code是决定你是否在别人支付前启用验证码,(我还没做,这里先别管)
* 如果你想要启用,那么请改成:
* $use_validation = true;
* 如果你不想,那就这样:
Expand All @@ -48,13 +48,31 @@
//====================================================================================

/*
* $use_notification_of_pay 是决定你是否使用异步通知。
* $use_notification_of_pay 是决定你是否使用异步账单通知,不懂?往下看
*
* 如果你用异步账单通知的话,用户在扫完二维码付完款之后,会自动跳转到你的页面。
* 如果你没用异步账单通知的话,用户在扫完二维码付完款之后,用户还需要点击一下按钮,才能跳转到你的页面。
*
* (重点!)如果要用异步账单通知的话,你需要给这整个文件夹添加PHP的读写权限,不懂?建议你别开异步账单通知了。
*
* 如果你想用的话,请改成:
* $use_notification_of_pay = true;
* 如果不想,那就这样:
* $use_notification_of_pay = false;
*/
$use_notification_of_pay = false;
$use_asynchronous_payment_check = true;


/*
*
* 如果你开了异步账单通知,那你就得处理下面这个:
* 如果你的页面协议是http的话,请把下面这个变量写成:
* $connection_protocol = "http";
* 如果你用的是https的话(SSL/TLS加密),那么,请把下面这个变量改成:
* $connection_protocol = "http";
* 懂?
* */
$connection_protocol = "http";

//===========================================================================

Expand All @@ -65,7 +83,7 @@
$create_payment_url = "https://payjs.cn/api/native"; //The API's url for creating payment. 用于创建订单的URL
$check_payment_url = "https://payjs.cn/api/check"; //The API's url for checking the payment. 用于查询订单的URL


//报错内容
$sign_verification_error_msg = "Sign Verification Error, please make sure that your key is correct. <br> 签字错误,建议您检查您的密钥是否正确";
$apt_connect_error_msg = "API Call Error, please ask the the administrater of the API provider. <br> API调用错误,请联系API提供商";
$parameter_error_msg = "Parameter Error <br> 参数传递错误";
Empty file modified corn/about_this_folder
100644 → 100755
Empty file.
20 changes: 18 additions & 2 deletions corn/pay_js_conn.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function post_to_payjs(array $data, $url) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$rst = json_decode(curl_exec($ch));
curl_close($ch);
if(sign_verify($rst)){
if(sign_verify_for_object($rst)){
return $rst;
}else{
echo $GLOBALS['sign_verification_error_msg'];
Expand All @@ -46,7 +46,7 @@ function sign(array $attributes) {
}


function sign_verify($attributes){
function sign_verify_for_object($attributes){
/*
* 函数sign_verify()是一个用于验证数字签名的函数
* 参数$attributes是一个来自API返回的对象,具体请参考API文档。
Expand All @@ -61,4 +61,20 @@ function sign_verify($attributes){
}else{
return FALSE;
}
}

function sign_verify_for_array($arr){
/*
* 函数sign_verify()是一个用于验证数字签名的函数
* 参数$attributes是一个来自API返回的对象,具体请参考API文档。
* 当验证成功时,返回true。
* 当验证失败时,返回false,意味着有人动过这个传来的数据。
*/
$sign_verify = $arr['sign'];
unset($arr['sign']);
if(sign($arr) == $sign_verify){
return TRUE;
}else{
return FALSE;
}
}
25 changes: 24 additions & 1 deletion corn/payment.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ function create_payment_return_info($body,$total_fee){
return null;
}
$data = ['body' => $body, 'out_trade_no' => time(), 'total_fee' => $total_fee, 'mchid' => $GLOBALS['config_mchid']];
//判断是否使用异步账单推送,如果使用,那么就添加notify_url。
if ($GLOBALS['use_asynchronous_payment_check']) {
//explode("?",$_SERVER['REQUEST_URI'])
$data['notify_url'] = $GLOBALS['connection_protocol'] . "://" . $_SERVER['HTTP_HOST'] . str_replace("index.php","",$_SERVER['PHP_SELF']) . "notifi.php";
}
$data['sign'] = sign($data);
$return_data = post_to_payjs($data, $GLOBALS['create_payment_url']);
if ($return_data -> return_code !=1 || $return_data -> return_msg != "SUCCESS") {
Expand Down Expand Up @@ -66,6 +71,10 @@ function does_payment_has_been_paid($payjs_order_id) {
echo $GLOBALS['parameter_error_msg'];
return null;
}
//判断是否使用异步账单通知
if ($GLOBALS['use_asynchronous_payment_check']) {
return check_payment_statue_by_local_temp($payjs_order_id);
}
$return_data = return_payment_info($payjs_order_id);
if ($return_data == null) {
return FALSE;
Expand All @@ -76,4 +85,18 @@ function does_payment_has_been_paid($payjs_order_id) {
}else{
return TRUE;
}
}
}

function check_payment_statue_by_local_temp($payjs_order_id) {
/*
* 函数check_payment_statue_by_local_temp()与上一个函数一样,都是用于确认订单是否被支付。
* 但是此函数是用于异步订单消息推送的订单查询,原理就是查看本地的缓存文件来实现。
* 函数返回true意味着账单已付,
* 如果函数返回false意味着账单未付,或者执行出错。
* */
if(check_payment_by_paid_index($payjs_order_id) == "1") {
return true;
}else{
return false;
}
}
47 changes: 47 additions & 0 deletions corn/utility.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,50 @@ function object_to_array($e){
}
return $e;
}

//=====================================================================================
/*
* 以下函数是用于异步账单信息推送,且这些函数执行条件是在有读写权限的条件下。
* PHP会在本地文件夹temp下创建一个以订单号(官方文档命名为payjs_order_id)为文件名的文件,用于记录这个账单是否被付款。
* 文件刚刚创立的时候是账单刚刚创建的时候,文件内容为"0",代表未支付。
* 当账单已经被支付后,文件内容会变成"1",这个过程是异步账单查询接口实现。
* */


function create_payment_paid_index($payjs_order_id) {
/*
* 函数create_payment_paid_index()用于创建一个本地文件,位于执行文件的当前目录的temp文件夹中
* 主要用于创建一个账单是否被支付的记录
* 参数$payjs_order_id为文件名,通常是账单ID,在payjs的官方文档中,这个指payjs_order_id(订单号),数据类型String[32]
* 文件内容为“0”(代表未支付)
* 没有返回值
* */
$file = fopen("temp/" . $payjs_order_id, "w") or die("没有文件读写权限");
fwrite($file, "0");
fclose($file);
}
function check_payment_by_paid_index($payjs_order_id) {
/*
* 函数check_payment_by_paid_index()用于读取一个本地文件,位于执行文件的当前目录的temp文件夹中
* 主要用于查询一个账单是否支付
* 参数$payjs_order_id为文件名,通常是账单ID,在payjs的官方文档中,这个指payjs_order_id(订单号),数据类型String[32]
* 返回值为"0"时代表未支付,"1"代表已支付,数据格式为String[1]
* */
$file = fopen("temp/" . $payjs_order_id,"r") or die("运行出错,找不到文件");
return fgetc($file);
fclose($file);
}

function change_payment_paid_index_statue($payjs_order_id) {
/*
* 函数change_payment_paid_index_statue()用于改变一个本地文件的内容变成"1",位于执行文件的当前目录的temp文件夹中
* 用于改变一个账单的支付状态,从未支付变成已支付。
* 参数$payjs_order_id为文件名,通常是账单ID,在payjs的官方文档中,这个指payjs_order_id(订单号),数据类型String[32]
* 无返回值
* */
$file = fopen("temp/" . $payjs_order_id, "w") or die("没有文件读写权限");
fwrite($file, "1");
fclose($file);
}

//=========================================================================================
Empty file modified css/about_this_folder
100644 → 100755
Empty file.
Empty file modified css/bootstrap.min.css
100644 → 100755
Empty file.
Empty file modified example/finished.php
100644 → 100755
Empty file.
Empty file modified example/quit.php
100644 → 100755
Empty file.
Loading

0 comments on commit feeacd0

Please sign in to comment.