require_once PUBLIC_PATH."/WxpayAPI/lib/WxPay.Config.php";
require_once PUBLIC_PATH."/WxpayAPI/lib/WxPay.Api.php";
require_once PUBLIC_PATH."/WxpayAPI/lib/WxPay.Data.php";
public function weChatPay()
{
$goods_name = empty($this->goods_name) ? 'NULL' : $this->goods_name;
$order_sn = empty($this->order_sn) ? 'NULL' : $this->order_sn;
$order_amount = empty($this->order_amount) ? 'NULL' : $this->order_amount;
$versionCode = empty($this->versionCode) ? 'NULL' : $this->versionCode;
$result = '';
$input = new WxPayUnifiedOrder();
$input->SetAppid(WxPayConfig::APPID);
$input->SetMch_id(WxPayConfig::MCHID);
$input->SetNonce_str(WxPayApi::getNonceStr());
$input->SetBody($goods_name);
$input->SetOut_trade_no($order_sn);
$input->SetTotal_fee($order_amount * 100);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetNotify_url("https://www.xxx.com/wxpay_notify.php");
$input->SetSpbill_create_ip(get_client_ip());
$input->SetTrade_type("APP");
$results = WxPayApi::unifiedOrder($input);
if (!empty($results)) {
$wxParam = '';
$signRes = $this->sign($weChat['prepay_id']);
unset($signRes['package']);
$sign_arr = [
'partnerid' =>$signRes['partnerid'],
'appid' =>$signRes['appid'],
'prepayid' =>$signRes['prepayid'],
'sign' =>$signRes['sign'],
'timestamp' =>$signRes['timestamp'],
'noncestr' =>$signRes['noncestr'],
];
return json_encode(array('error'=>0,'result'=>$sign_arr));
}
return $result;
}
public function sign($prepay_id = '')
{
$results = [];
if (!empty($prepay_id)) {
$input = new WxPayApp();
$input->SetAppid(WxPayConfig::APPID);
$input->SetPartnerid(WxPayConfig::MCHID);
$input->SetNonce_str(WxPayApi::getNonceStr());
$input->SetPrepayid($prepayid);
$input->SetTimeStamp(time());
$input->SetPackage('Sign=WXPay');
$results = WxPayApi::appSign($input);
if (empty($results)) {
return false;
}
}
return $results;
}
require_once "./WxpayAPI/lib/WxPay.Config.php";
require_once "./WxpayAPI/lib/WxPay.Api.php";
require_once "./WxpayAPI/lib/WxPay.Notify.php";
$xml_data = file_get_contents('php://input', 'r');
$data = json_decode(json_encode(simplexml_load_string($xml_data, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
$sign = $data['sign'];
unset($data['sign']);
$key = WxPayConfig::KEY;
function MakeSign($data,$key)
{
ksort($data);
$string = ToUrlParams($data);
$string = $string . "&key=".$key;
$string = md5($string);
$result = strtoupper($string);
return $result;
}
function ToUrlParams($params)
{
$buff = "";
foreach ($params as $k => $v)
{
if($k != "sign" && $v != "" && !is_array($v)){
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return $buff;
}
if(MakeSign($data,$key) == $sign){
NotifyCheck($data);
}
function NotifyCheck($data){
if (!empty($data)) {
$orderInfo = 'order_info表下的order_sn = $data["out_trade_no"] ';
if(empty($orderInfo)){
exit('订单不存在');
}
if ($orderInfo['pay_status'] == 0) {
if (isset($data['result_code']) && $data['result_code'] == 'SUCCESS' && $data['total_fee'] == $orderInfo['order_amount']) {
}
}
$notify = new PayNotifyCallBack();
$notify->Handle(false);
}
}