1.0-version

This commit is contained in:
Administrator
2019-04-11 15:54:34 +08:00
commit 0e902893ca
2222 changed files with 255689 additions and 0 deletions

View File

@@ -0,0 +1,208 @@
<?php
require_once 'D:\wamp\www\erp\Wxpay\lib\WxPay.Api.php';
/**
*
* JSAPI支付实现类
* 该类实现了从微信公众平台获取code、通过code获取openid和access_token、
* 生成jsapi支付js接口所需的参数、生成获取共享收货地址所需的参数
*
* 该类是微信支付提供的样例程序商户可根据自己的需求修改或者使用lib中的api自行开发
*
* @author widy
*
*/
class JsApiPay
{
/**
*
* 网页授权接口微信服务器返回的数据,返回样例如下
* {
* "access_token":"ACCESS_TOKEN",
* "expires_in":7200,
* "refresh_token":"REFRESH_TOKEN",
* "openid":"OPENID",
* "scope":"SCOPE",
* "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
* }
* 其中access_token可用于获取共享收货地址
* openid是微信支付jsapi支付接口必须的参数
* @var array
*/
public $data = null;
/**
*
* 通过跳转获取用户的openid跳转流程如下
* 1、设置自己需要调回的url及其其他参数跳转到微信服务器https://open.weixin.qq.com/connect/oauth2/authorize
* 2、微信服务处理完成之后会跳转回用户redirect_uri地址此时会带上一些参数code
*
* @return 用户的openid
*/
public function GetOpenid()
{
//通过code获得openid
if (!isset($_GET['code'])){
//触发微信返回code码
//$baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
$baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
$url = $this->__CreateOauthUrlForCode($baseUrl);
Header("Location: $url");
exit();
} else {
//获取code码以获取openid
$code = $_GET['code'];
$openid = $this->getOpenidFromMp($code);
return $openid;
}
}
/**
*
* 获取jsapi支付的参数
* @param array $UnifiedOrderResult 统一支付接口返回的数据
* @throws WxPayException
*
* @return json数据可直接填入js函数作为参数
*/
public function GetJsApiParameters($UnifiedOrderResult)
{
if(!array_key_exists("appid", $UnifiedOrderResult)
|| !array_key_exists("prepay_id", $UnifiedOrderResult)
|| $UnifiedOrderResult['prepay_id'] == "")
{
throw new WxPayException("参数错误");
}
$jsapi = new WxPayJsApiPay();
$jsapi->SetAppid($UnifiedOrderResult["appid"]);
$timeStamp = time();
$jsapi->SetTimeStamp("$timeStamp");
$jsapi->SetNonceStr(WxPayApi::getNonceStr());
$jsapi->SetPackage("prepay_id=" . $UnifiedOrderResult['prepay_id']);
$jsapi->SetSignType("MD5");
$jsapi->SetPaySign($jsapi->MakeSign());
$parameters = json_encode($jsapi->GetValues());
return $parameters;
}
/**
*
* 通过code从工作平台获取openid机器access_token
* @param string $code 微信跳转回来带上的code
*
* @return openid
*/
public function GetOpenidFromMp($code)
{
$url = $this->__CreateOauthUrlForOpenid($code);
//初始化curl
$ch = curl_init();
//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if(WxPayConfig::CURL_PROXY_HOST != "0.0.0.0"
&& WxPayConfig::CURL_PROXY_PORT != 0){
curl_setopt($ch,CURLOPT_PROXY, WxPayConfig::CURL_PROXY_HOST);
curl_setopt($ch,CURLOPT_PROXYPORT, WxPayConfig::CURL_PROXY_PORT);
}
//运行curl结果以jason形式返回
$res = curl_exec($ch);
curl_close($ch);
//取出openid
$data = json_decode($res,true);
$this->data = $data;
$openid = $data['openid'];
return $openid;
}
/**
*
* 拼接签名字符串
* @param array $urlObj
*
* @return 返回已经拼接好的字符串
*/
private function ToUrlParams($urlObj)
{
$buff = "";
foreach ($urlObj as $k => $v)
{
if($k != "sign"){
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return $buff;
}
/**
*
* 获取地址js参数
*
* @return 获取共享收货地址js函数需要的参数json格式可以直接做参数使用
*/
public function GetEditAddressParameters()
{
$getData = $this->data;
$data = array();
$data["appid"] = WxPayConfig::APPID;
$data["url"] = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$time = time();
$data["timestamp"] = "$time";
$data["noncestr"] = "1234568";
$data["accesstoken"] = $getData["access_token"];
ksort($data);
$params = $this->ToUrlParams($data);
$addrSign = sha1($params);
$afterData = array(
"addrSign" => $addrSign,
"signType" => "sha1",
"scope" => "jsapi_address",
"appId" => WxPayConfig::APPID,
"timeStamp" => $data["timestamp"],
"nonceStr" => $data["noncestr"]
);
$parameters = json_encode($afterData);
return $parameters;
}
/**
*
* 构造获取code的url连接
* @param string $redirectUrl 微信服务器回跳的url需要url编码
*
* @return 返回构造好的url
*/
private function __CreateOauthUrlForCode($redirectUrl)
{
$urlObj["appid"] = WxPayConfig::APPID;
$urlObj["redirect_uri"] = "$redirectUrl";
$urlObj["response_type"] = "code";
$urlObj["scope"] = "snsapi_base";
$urlObj["state"] = "STATE"."#wechat_redirect";
$bizString = $this->ToUrlParams($urlObj);
return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
}
/**
*
* 构造获取open和access_toke的url地址
* @param string $code微信跳转带回的code
*
* @return 请求的url
*/
private function __CreateOauthUrlForOpenid($code)
{
$urlObj["appid"] = WxPayConfig::APPID;
$urlObj["secret"] = WxPayConfig::APPSECRET;
$urlObj["code"] = $code;
$urlObj["grant_type"] = "authorization_code";
$bizString = $this->ToUrlParams($urlObj);
return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
}
}

View File

@@ -0,0 +1,147 @@
<?php
require_once "../lib/WxPay.Api.php";
/**
*
* 刷卡支付实现类
* 该类实现了一个刷卡支付的流程,流程如下:
* 1、提交刷卡支付
* 2、根据返回结果决定是否需要查询订单如果查询之后订单还未变则需要返回查询一般反复查10次
* 3、如果反复查询10订单依然不变则发起撤销订单
* 4、撤销订单需要循环撤销一直撤销成功为止注意循环次数建议10次
*
* 该类是微信支付提供的样例程序商户可根据自己的需求修改或者使用lib中的api自行开发为了防止
* 查询时hold住后台php进程商户查询和撤销逻辑可在前端调用
*
* @author widy
*
*/
class MicroPay
{
/**
*
* 提交刷卡支付,并且确认结果,接口比较慢
* @param WxPayMicroPay $microPayInput
* @throws WxpayException
* @return 返回查询接口的结果
*/
public function pay($microPayInput)
{
//①、提交被扫支付
$result = WxPayApi::micropay($microPayInput, 5);
//如果返回成功
if(!array_key_exists("return_code", $result)
|| !array_key_exists("out_trade_no", $result)
|| !array_key_exists("result_code", $result))
{
echo "接口调用失败,请确认是否输入是否有误!";
throw new WxPayException("接口调用失败!");
}
//签名验证
$out_trade_no = $microPayInput->GetOut_trade_no();
//②、接口调用成功,明确返回调用失败
if($result["return_code"] == "SUCCESS" &&
$result["result_code"] == "FAIL" &&
$result["err_code"] != "USERPAYING" &&
$result["err_code"] != "SYSTEMERROR")
{
return false;
}
//③、确认支付是否成功
$queryTimes = 10;
while($queryTimes > 0)
{
$succResult = 0;
$queryResult = $this->query($out_trade_no, $succResult);
//如果需要等待1s后继续
if($succResult == 2){
sleep(2);
continue;
} else if($succResult == 1){//查询成功
return $queryResult;
} else {//订单交易失败
return false;
}
}
//④、次确认失败,则撤销订单
if(!$this->cancel($out_trade_no))
{
throw new WxpayException("撤销单失败!");
}
return false;
}
/**
*
* 查询订单情况
* @param string $out_trade_no 商户订单号
* @param int $succCode 查询订单结果
* @return 0 订单不成功1表示订单成功2表示继续等待
*/
public function query($out_trade_no, &$succCode)
{
$queryOrderInput = new WxPayOrderQuery();
$queryOrderInput->SetOut_trade_no($out_trade_no);
$result = WxPayApi::orderQuery($queryOrderInput);
if($result["return_code"] == "SUCCESS"
&& $result["result_code"] == "SUCCESS")
{
//支付成功
if($result["trade_state"] == "SUCCESS"){
$succCode = 1;
return $result;
}
//用户支付中
else if($result["trade_state"] == "USERPAYING"){
$succCode = 2;
return false;
}
}
//如果返回错误码为“此交易订单号不存在”则直接认定失败
if($result["err_code"] == "ORDERNOTEXIST")
{
$succCode = 0;
} else{
//如果是系统错误,则后续继续
$succCode = 2;
}
return false;
}
/**
*
* 撤销订单如果失败会重复调用10次
* @param string $out_trade_no
* @param 调用深度 $depth
*/
public function cancel($out_trade_no, $depth = 0)
{
if($depth > 10){
return false;
}
$clostOrder = new WxPayReverse();
$clostOrder->SetOut_trade_no($out_trade_no);
$result = WxPayApi::reverse($clostOrder);
//接口调用失败
if($result["return_code"] != "SUCCESS"){
return false;
}
//如果结果为success且不需要重新调用撤销则表示撤销成功
if($result["result_code"] != "SUCCESS"
&& $result["recall"] == "N"){
return true;
} else if($result["recall"] == "Y") {
return $this->cancel($out_trade_no, ++$depth);
}
return false;
}
}

View File

@@ -0,0 +1,56 @@
<?php
require_once "D:\www\Wxpay\lib\WxPay.Api.php";
/**
*
* 刷卡支付实现类
* @author widyhu
*
*/
class NativePay
{
/**
*
* 生成扫描支付URL,模式一
* @param BizPayUrlInput $bizUrlInfo
*/
public function GetPrePayUrl($productId)
{
$biz = new WxPayBizPayUrl();
$biz->SetProduct_id($productId);
$values = WxpayApi::bizpayurl($biz);
$url = "weixin://wxpay/bizpayurl?" . $this->ToUrlParams($values);
return $url;
}
/**
*
* 参数数组转换为url参数
* @param array $urlObj
*/
private function ToUrlParams($urlObj)
{
$buff = "";
foreach ($urlObj as $k => $v)
{
$buff .= $k . "=" . $v . "&";
}
$buff = trim($buff, "&");
return $buff;
}
/**
*
* 生成直接支付url支付url有效期为2小时,模式二
* @param UnifiedOrderInput $input
*/
public function GetPayUrl($input)
{
if($input->GetTrade_type() == "NATIVE")
{
$result = WxPayApi::unifiedOrder($input);
return $result;
}
}
}

View File

@@ -0,0 +1,40 @@
<?php
require_once "../lib/WxPay.Api.php";
//require_once "../lib/WxPay.MicroPay.php";
if(isset($_REQUEST["bill_date"]) && $_REQUEST["bill_date"] != ""){
$bill_date = $_REQUEST["bill_date"];
$bill_type = $_REQUEST["bill_type"];
$input = new WxPayDownloadBill();
$input->SetBill_date($bill_date);
$input->SetBill_type($bill_type);
$file = WxPayApi::downloadBill($input);
echo $file;
//TODO 对账单文件处理
exit(0);
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>微信支付样例-查退款单</title>
</head>
<body>
<form action="#" method="post">
<div style="margin-left:2%;">对账日期:</div><br/>
<input type="text" style="width:96%;height:35px;margin-left:2%;" name="bill_date" /><br /><br />
<div style="margin-left:2%;">账单类型:</div><br/>
<select style="width:96%;height:35px;margin-left:2%;" name="bill_type">
<option value ="ALL">所有订单信息</option>
<option value ="SUCCESS">成功支付的订单</option>
<option value="REFUND">退款订单</option>
<option value="REVOKED">撤销的订单</option>
</select><br /><br />
<div align="center">
<input type="submit" value="下载订单" 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()" />
</div>
</form>
</body>
</html>

159
Wxpay/example/jsapi.php Normal file
View File

@@ -0,0 +1,159 @@
<?php
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);
require_once 'D:\wamp\www\erp\Wxpay\lib\WxPay.Api.php';
require_once 'D:\wamp\www\erp\Wxpay\example\WxPay.JsApiPay.php';
$userid = $_GET['userid'];
$type = $_GET['type'];
if ($type == "ad")
{
$myfile = fopen('http://erp.itxxoo.com/upload/' . $userid . '.txt', "r");
$ordermsg = fgets($myfile);
fclose($myfile);
$arr = explode('|', $ordermsg);
$total = 0;
foreach($arr as $key => $value)
{
$_tmp = explode(',', $value);
$total += (int)$_tmp[12];
}
$total = $total * 100;
$tools = new JsApiPay();
$openId = $tools->GetOpenid();
unlink('../../upload/' . $userid . '.txt');
$input = new WxPayUnifiedOrder();
$input->SetBody("广告投放");
$input->SetAttach($ordermsg);
$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
$input->SetTotal_fee($total);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetNotify_url("http://erp.itxxoo.com/Wxpay/notify");
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = WxPayApi::unifiedOrder($input);
$jsApiParameters = $tools->GetJsApiParameters($order);
$editAddress = $tools->GetEditAddressParameters();
}
else
{
$myfile = fopen('http://jiazheng.itxxoo.com/upload/' . $userid . '.txt', "r");
$ordermsg = fgets($myfile);
fclose($myfile);
$arr = explode('#', $ordermsg);
$total = $arr[1] * 100;
$tools = new JsApiPay();
$openId = $tools->GetOpenid();
unlink('../../../jiazheng/upload/' . $userid . '.txt');
$input = new WxPayUnifiedOrder();
$input->SetBody("家政预约");
$input->SetAttach($ordermsg);
$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
$input->SetTotal_fee($total);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetNotify_url("http://jiazheng.itxxoo.com/Wxpay/notify");
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = WxPayApi::unifiedOrder($input);
$jsApiParameters = $tools->GetJsApiParameters($order);
$editAddress = $tools->GetEditAddressParameters();
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>支付</title>
<script type="text/javascript">
var type='<?php echo (string)$type; ?>';
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
<?php echo $jsApiParameters; ?>,
function(res){
WeixinJSBridge.log(res.err_msg);
if (type=="ad")
window.location.href = "http://erp.itxxoo.com/login/wap";
else
window.location.href = "http://jiazheng.itxxoo.com/login/wap";
}
);
}
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();
}
}
</script>
<script type="text/javascript">
function editAddress()
{
WeixinJSBridge.invoke(
'editAddress',
<?php echo $editAddress; ?>,
function(res){
var value1 = res.proviceFirstStageName;
var value2 = res.addressCitySecondStageName;
var value3 = res.addressCountiesThirdStageName;
var value4 = res.addressDetailInfo;
var tel = res.telNumber;
}
);
}
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();
}
};
callpay();
</script>
</head>
<body>
<br/>
<font color="#9ACD32"><b>支付中...</b></font><br/><br/>
</body>
</html>

125
Wxpay/example/log.php Normal file
View File

@@ -0,0 +1,125 @@
<?php
//以下为日志
interface ILogHandler
{
public function write($msg);
}
class CLogFileHandler implements ILogHandler
{
private $handle = null;
public function __construct($file = '')
{
$this->handle = fopen($file,'a');
}
public function write($msg)
{
fwrite($this->handle, $msg, 4096);
}
public function __destruct()
{
fclose($this->handle);
}
}
class Log
{
private $handler = null;
private $level = 15;
private static $instance = null;
private function __construct(){}
private function __clone(){}
public static function Init($handler = null,$level = 15)
{
if(!self::$instance instanceof self)
{
self::$instance = new self();
self::$instance->__setHandle($handler);
self::$instance->__setLevel($level);
}
return self::$instance;
}
private function __setHandle($handler){
$this->handler = $handler;
}
private function __setLevel($level)
{
$this->level = $level;
}
public static function DEBUG($msg)
{
self::$instance->write(1, $msg);
}
public static function WARN($msg)
{
self::$instance->write(4, $msg);
}
public static function ERROR($msg)
{
$debugInfo = debug_backtrace();
$stack = "[";
foreach($debugInfo as $key => $val){
if(array_key_exists("file", $val)){
$stack .= ",file:" . $val["file"];
}
if(array_key_exists("line", $val)){
$stack .= ",line:" . $val["line"];
}
if(array_key_exists("function", $val)){
$stack .= ",function:" . $val["function"];
}
}
$stack .= "]";
self::$instance->write(8, $stack . $msg);
}
public static function INFO($msg)
{
self::$instance->write(2, $msg);
}
private function getLevelStr($level)
{
switch ($level)
{
case 1:
return 'debug';
break;
case 2:
return 'info';
break;
case 4:
return 'warn';
break;
case 8:
return 'error';
break;
default:
}
}
protected function write($level,$msg)
{
if(($level & $this->level) == $level )
{
$msg = '['.date('Y-m-d H:i:s').']['.$this->getLevelStr($level).'] '.$msg."\n";
$this->handler->write($msg);
}
}
}

View File

@@ -0,0 +1,56 @@
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>微信支付样例-查退款单</title>
</head>
<?php
require_once "../lib/WxPay.Api.php";
require_once "WxPay.MicroPay.php";
require_once 'log.php';
//初始化日志
$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);
//打印输出数组信息
function printf_info($data)
{
foreach($data as $key=>$value){
echo "<font color='#00ff55;'>$key</font> : $value <br/>";
}
}
if(isset($_REQUEST["auth_code"]) && $_REQUEST["auth_code"] != ""){
$auth_code = $_REQUEST["auth_code"];
$input = new WxPayMicroPay();
$input->SetAuth_code($auth_code);
$input->SetBody("刷卡测试样例-支付");
$input->SetTotal_fee("1");
$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
$microPay = new MicroPay();
printf_info($microPay->pay($input));
}
/**
* 注意:
* 1、提交被扫之后返回系统繁忙、用户输入密码等错误信息时需要循环查单以确定是否支付成功
* 2、多次一半10次确认都未明确成功时需要调用撤单接口撤单防止用户重复支付
*/
?>
<body>
<form action="#" method="post">
<div style="margin-left:2%;">商品描述:</div><br/>
<input type="text" style="width:96%;height:35px;margin-left:2%;" readonly value="刷卡测试样例-支付" name="auth_code" /><br /><br />
<div style="margin-left:2%;">支付金额:</div><br/>
<input type="text" style="width:96%;height:35px;margin-left:2%;" readonly value="1分" name="auth_code" /><br /><br />
<div style="margin-left:2%;">授权码:</div><br/>
<input type="text" style="width:96%;height:35px;margin-left:2%;" name="auth_code" /><br /><br />
<div align="center">
<input type="submit" value="提交刷卡" 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()" />
</div>
</form>
</body>
</html>

61
Wxpay/example/native.php Normal file
View File

@@ -0,0 +1,61 @@
<?php
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);
require_once 'D:\wamp\www\erp\Wxpay\lib\WxPay.Api.php';
require_once "WxPay.NativePay.php";
require_once 'log.php';
//模式一
/**
* 流程:
* 1、组装包含支付信息的url生成二维码
* 2、用户扫描二维码进行支付
* 3、确定支付之后微信服务器会回调预先配置的回调地址在【微信开放平台-微信支付-支付配置】中进行配置
* 4、在接到回调通知之后用户进行统一下单支付并返回支付信息以完成支付native_notify.php
* 5、支付完成之后微信服务器会通知支付成功
* 6、在支付成功通知中需要查单确认是否真正支付成功notify.php
*/
$notify = new NativePay();
$url1 = $notify->GetPrePayUrl("123456789");
//模式二
/**
* 流程:
* 1、调用统一下单取得code_url生成二维码
* 2、用户扫描二维码进行支付
* 3、支付完成之后微信服务器会通知支付成功
* 4、在支付成功通知中需要查单确认是否真正支付成功notify.php
*/
$input = new WxPayUnifiedOrder();
$input->SetBody("广告位");//商品描述
$input->SetAttach("test");//附加数据
$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));//商户订单号
$input->SetTotal_fee("2");//总金额
$input->SetTime_start(date("YmdHis"));//交易起始时间
$input->SetTime_expire(date("YmdHis", time() + 600));//交易结束时间
$input->SetGoods_tag("test标记");//商品标记
$input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php");//通知地址
$input->SetTrade_type("NATIVE");//交易类型
$input->SetProduct_id("123456789");//商品ID
$result = $notify->GetPayUrl($input);
$url2 = $result["code_url"];
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>微信支付样例</title>
</head>
<body>
<!--
<div style="margin-left: 10px;color:#556B2F;font-size:30px;font-weight: bolder;">扫描支付模式一</div><br/>
<img alt="模式一扫码支付" src="http://paysdk.weixin.qq.com/example/qrcode.php?data=<?php echo urlencode($url1);?>" style="width:150px;height:150px;"/>
<br/><br/><br/>
-->
<div style="margin-left: 10px;color:#556B2F;font-size:30px;font-weight: bolder;">下载二维码并扫描支付</div><br/>
<img alt="模式二扫码支付" src="http://paysdk.weixin.qq.com/example/qrcode.php?data=<?php echo urlencode($url2);?>" style="width:150px;height:150px;"/>
</body>
</html>

View File

@@ -0,0 +1,72 @@
<?php
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);
require_once "../lib/WxPay.Api.php";
require_once '../lib/WxPay.Notify.php';
require_once 'log.php';
//初始化日志
$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);
class NativeNotifyCallBack extends WxPayNotify
{
public function unifiedorder($openId, $product_id)
{
//统一下单
$input = new WxPayUnifiedOrder();
$input->SetBody("test");
$input->SetAttach("test");
$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
$input->SetTotal_fee("1");
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("test");
$input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php");
$input->SetTrade_type("NATIVE");
$input->SetOpenid($openId);
$input->SetProduct_id($product_id);
$result = WxPayApi::unifiedOrder($input);
Log::DEBUG("unifiedorder:" . json_encode($result));
return $result;
}
public function NotifyProcess($data, &$msg)
{
//echo "处理回调";
Log::DEBUG("call back:" . json_encode($data));
if(!array_key_exists("openid", $data) ||
!array_key_exists("product_id", $data))
{
$msg = "回调数据异常";
return false;
}
$openid = $data["openid"];
$product_id = $data["product_id"];
//统一下单
$result = $this->unifiedorder($openid, $product_id);
if(!array_key_exists("appid", $result) ||
!array_key_exists("mch_id", $result) ||
!array_key_exists("prepay_id", $result))
{
$msg = "统一下单失败";
return false;
}
$this->SetData("appid", $result["appid"]);
$this->SetData("mch_id", $result["mch_id"]);
$this->SetData("nonce_str", WxPayApi::getNonceStr());
$this->SetData("prepay_id", $result["prepay_id"]);
$this->SetData("result_code", "SUCCESS");
$this->SetData("err_code_des", "OK");
return true;
}
}
Log::DEBUG("begin notify!");
$notify = new NativeNotifyCallBack();
$notify->Handle(true);

64
Wxpay/example/notify.php Normal file
View File

@@ -0,0 +1,64 @@
<?php
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);
require_once 'D:\wamp\www\erp\Wxpay\lib\WxPay.Api.php';
require_once 'D:\wamp\www\erp\Wxpay\lib\WxPay.Notify.php';
//require_once 'log.php';
//$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
//$log = Log::Init($logHandler, 15);
class PayNotifyCallBack extends WxPayNotify
{
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;
}
return false;
}
public function NotifyProcess($data, &$msg)
{
Log::DEBUG("call back:" . json_encode($data));
$notfiyOutput = array();
if(!array_key_exists("transaction_id", $data)){
$msg = "输入参数不正确";
return false;
}
if(!$this->Queryorder($data["transaction_id"])){
$msg = "订单查询失败";
return false;
}
return true;
}
}
//Log::DEBUG("begin notify");
/*
$notify = new PayNotifyCallBack();
$notify->Handle(false);
$postXml = $GLOBALS["HTTP_RAW_POST_DATA"];
$postArr = xmlToArray($postXml);
$r = $notify->Queryorder($postArr['transaction_id']);
if ($r) {
$str = $postArr['attach'];
$myfile = fopen("upload/aaa.txt", "w");
fwrite($myfile, $str);
fclose($myfile);
}
*/

View File

@@ -0,0 +1,53 @@
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>微信支付样例-订单查询</title>
</head>
<?php
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);
require_once "../lib/WxPay.Api.php";
require_once 'log.php';
//初始化日志
$logHandler= new CLogFileHandler("./logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);
function printf_info($data)
{
foreach($data as $key=>$value){
echo "<font color='#f00;'>$key</font> : $value <br/>";
}
}
if(isset($_REQUEST["transaction_id"]) && $_REQUEST["transaction_id"] != ""){
$transaction_id = $_REQUEST["transaction_id"];
$input = new WxPayOrderQuery();
$input->SetTransaction_id($transaction_id);
printf_info(WxPayApi::orderQuery($input));
exit();
}
if(isset($_REQUEST["out_trade_no"]) && $_REQUEST["out_trade_no"] != ""){
$out_trade_no = $_REQUEST["out_trade_no"];
$input = new WxPayOrderQuery();
$input->SetOut_trade_no($out_trade_no);
printf_info(WxPayApi::orderQuery($input));
exit();
}
?>
<body>
<form action="#" method="post">
<div style="margin-left:2%;color:#f00">微信订单号和商户订单号选少填一个,微信订单号优先:</div><br/>
<div style="margin-left:2%;">微信订单号:</div><br/>
<input type="text" style="width:96%;height:35px;margin-left:2%;" name="transaction_id" /><br /><br />
<div style="margin-left:2%;">商户订单号:</div><br/>
<input type="text" style="width:96%;height:35px;margin-left:2%;" name="out_trade_no" /><br /><br />
<div align="center">
<input type="submit" value="查询" 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()" />
</div>
</form>
</body>
</html>

File diff suppressed because it is too large Load Diff

5
Wxpay/example/qrcode.php Normal file
View File

@@ -0,0 +1,5 @@
<?php
error_reporting(E_ERROR);
require_once 'phpqrcode/phpqrcode.php';
$url = urldecode($_GET["data"]);
QRcode::png($url);

71
Wxpay/example/refund.php Normal file
View File

@@ -0,0 +1,71 @@
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>微信支付样例-退款</title>
</head>
<?php
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);
require_once "../lib/WxPay.Api.php";
require_once 'log.php';
//初始化日志
$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);
function printf_info($data)
{
foreach($data as $key=>$value){
echo "<font color='#f00;'>$key</font> : $value <br/>";
}
}
if(isset($_REQUEST["transaction_id"]) && $_REQUEST["transaction_id"] != ""){
$transaction_id = $_REQUEST["transaction_id"];
$total_fee = $_REQUEST["total_fee"];
$refund_fee = $_REQUEST["refund_fee"];
$input = new WxPayRefund();
$input->SetTransaction_id($transaction_id);
$input->SetTotal_fee($total_fee);
$input->SetRefund_fee($refund_fee);
$input->SetOut_refund_no(WxPayConfig::MCHID.date("YmdHis"));
$input->SetOp_user_id(WxPayConfig::MCHID);
printf_info(WxPayApi::refund($input));
exit();
}
//$_REQUEST["out_trade_no"]= "122531270220150304194108";
///$_REQUEST["total_fee"]= "1";
//$_REQUEST["refund_fee"] = "1";
if(isset($_REQUEST["out_trade_no"]) && $_REQUEST["out_trade_no"] != ""){
$out_trade_no = $_REQUEST["out_trade_no"];
$total_fee = $_REQUEST["total_fee"];
$refund_fee = $_REQUEST["refund_fee"];
$input = new WxPayRefund();
$input->SetOut_trade_no($out_trade_no);
$input->SetTotal_fee($total_fee);
$input->SetRefund_fee($refund_fee);
$input->SetOut_refund_no(WxPayConfig::MCHID.date("YmdHis"));
$input->SetOp_user_id(WxPayConfig::MCHID);
printf_info(WxPayApi::refund($input));
exit();
}
?>
<body>
<form action="#" method="post">
<div style="margin-left:2%;color:#f00">微信订单号和商户订单号选少填一个,微信订单号优先:</div><br/>
<div style="margin-left:2%;">微信订单号:</div><br/>
<input type="text" style="width:96%;height:35px;margin-left:2%;" name="transaction_id" /><br /><br />
<div style="margin-left:2%;">商户订单号:</div><br/>
<input type="text" style="width:96%;height:35px;margin-left:2%;" name="out_trade_no" /><br /><br />
<div style="margin-left:2%;">订单总金额(分)</div><br/>
<input type="text" style="width:96%;height:35px;margin-left:2%;" name="total_fee" /><br /><br />
<div style="margin-left:2%;">退款金额(分)</div><br/>
<input type="text" style="width:96%;height:35px;margin-left:2%;" name="refund_fee" /><br /><br />
<div align="center">
<input type="submit" value="提交退款" 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()" />
</div>
</form>
</body>
</html>

View File

@@ -0,0 +1,73 @@
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>微信支付样例-查退款单</title>
</head>
<?php
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);
require_once "../lib/WxPay.Api.php";
require_once 'log.php';
//初始化日志
$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);
function printf_info($data)
{
foreach($data as $key=>$value){
echo "<font color='#f00;'>$key</font> : $value <br/>";
}
}
if(isset($_REQUEST["transaction_id"]) && $_REQUEST["transaction_id"] != ""){
$transaction_id = $_REQUEST["transaction_id"];
$input = new WxPayRefundQuery();
$input->SetTransaction_id($transaction_id);
printf_info(WxPayApi::refundQuery($input));
}
if(isset($_REQUEST["out_trade_no"]) && $_REQUEST["out_trade_no"] != ""){
$out_trade_no = $_REQUEST["out_trade_no"];
$input = new WxPayRefundQuery();
$input->SetOut_trade_no($out_trade_no);
printf_info(WxPayApi::refundQuery($input));
exit();
}
if(isset($_REQUEST["out_refund_no"]) && $_REQUEST["out_refund_no"] != ""){
$out_refund_no = $_REQUEST["out_refund_no"];
$input = new WxPayRefundQuery();
$input->SetOut_refund_no($out_refund_no);
printf_info(WxPayApi::refundQuery($input));
exit();
}
if(isset($_REQUEST["refund_id"]) && $_REQUEST["refund_id"] != ""){
$refund_id = $_REQUEST["refund_id"];
$input = new WxPayRefundQuery();
$input->SetRefund_id($refund_id);
printf_info(WxPayApi::refundQuery($input));
exit();
}
?>
<body>
<form action="#" method="post">
<div style="margin-left:2%;color:#f00">微信订单号、商户订单号、微信订单号、微信退款单号选填至少一个,微信退款单号优先:</div><br/>
<div style="margin-left:2%;">微信订单号:</div><br/>
<input type="text" style="width:96%;height:35px;margin-left:2%;" name="transaction_id" /><br /><br />
<div style="margin-left:2%;">商户订单号:</div><br/>
<input type="text" style="width:96%;height:35px;margin-left:2%;" name="out_trade_no" /><br /><br />
<div style="margin-left:2%;">商户退款单号:</div><br/>
<input type="text" style="width:96%;height:35px;margin-left:2%;" name="out_refund_no" /><br /><br />
<div style="margin-left:2%;">微信退款单号:</div><br/>
<input type="text" style="width:96%;height:35px;margin-left:2%;" name="refund_id" /><br /><br />
<div align="center">
<input type="submit" value="查询" 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()" />
</div>
</form>
</body>
</html>