Files
mier-php/application/api/controller/Notifyapp.php
2025-08-11 10:22:05 +08:00

99 lines
3.6 KiB
PHP

<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use app\api\wxapi\pay\WxPayApi;
use app\api\wxapi\pay\JsApiPay;
use app\api\wxapi\pay\WxPayUnifiedOrder;
use app\api\wxapi\pay\WxPayConfig;
use app\api\wxapi\pay\WxPayNotify;
use app\api\wxapi\pay\WxPayOrderQuery;
use app\api\wxapi\pay\WxPayDataBase;
class Notifyapp extends Controller
{
public function wxnotify(){
// dump(111);exit;
$xml = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");
// error_log($xml, 3, 'xml.log');
// exit;
// $xml = '<xml><appid><![CDATA[wx6274462930e3488e]]></appid>
// <attach><![CDATA[订单支付]]></attach>
// <bank_type><![CDATA[OTHERS]]></bank_type>
// <cash_fee><![CDATA[1]]></cash_fee>
// <fee_type><![CDATA[CNY]]></fee_type>
// <is_subscribe><![CDATA[N]]></is_subscribe>
// <mch_id><![CDATA[1612760327]]></mch_id>
// <nonce_str><![CDATA[vk18rwouhpselq44zbnddsmzglojmk5c]]></nonce_str>
// <openid><![CDATA[ocR_J6Qy6YKze82nugSW7Ork6Al4]]></openid>
// <out_trade_no><![CDATA[JSAPI18148202108281842417290]]></out_trade_no>
// <result_code><![CDATA[SUCCESS]]></result_code>
// <return_code><![CDATA[SUCCESS]]></return_code>
// <sign><![CDATA[7DCCA4B31D0FD90E97CBD303FD75F0D93AD94C137AB165E8A7212055CE42B101]]></sign>
// <time_end><![CDATA[20210828184247]]></time_end>
// <total_fee>1</total_fee>
// <trade_type><![CDATA[JSAPI]]></trade_type>
// <transaction_id><![CDATA[4200001184202108286171818578]]></transaction_id>
// </xml>';
if(!$xml){exit;}
$input = new WxPayOrderQuery();
$result =$input->FromXml($xml);
$config = new WxPayConfig();
$_sign=$result['sign'];
unset($result['sign']);
$sign = $this->getSign($result, $config);
if($_sign==$sign && array_key_exists("return_code", $result) && array_key_exists("result_code", $result) && $result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS"){
$fee=$result['total_fee']/100;
model('UserRecharge')->pay_notify_success($result['out_trade_no'],$fee);
exit('<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>');
//TS
// $out_trade_no=$result['out_trade_no'];
// $fee=$result['total_fee']/100;
// $transaction_id=$result['transaction_id'];
// $order=DB::name('order')->where(['order_no'=>$out_trade_no, 'status'=>1, 'price'=>$fee,])->find();
// //$order=DB::name('order')->where(['order_no'=>$orderid, 'status'=>1, ])->find();
// if(isset($order['id'])){
// //业务逻辑
// $arr['status']=2;
// $arr['paytime']=time();
// $res=DB::name('order')->where('id',$order['id'])->update($arr);
// if($res){
// //增加用户米钻及充值记录
// userStoreInc($order['user_id'],$order['mizuan'],11,'mizuan');
// exit('OK');
// }
// }
// exit('fail');
}
}
private function getSign($params, $config) {
ksort($params); //将参数数组按照参数名ASCII码从小到大排序
foreach ($params as $key => $item) {
if (!empty($item)) { //剔除参数值为空的参数
$newArr[] = $key.'='.$item; // 整合新的参数数组
}
}
$stringA = implode("&", $newArr); //使用 & 符号连接参数
$stringSignTemp = $stringA."&key=".$config->GetKey(); //拼接key
if($config->GetSignType() == "MD5"){
$stringSignTemp = md5($stringSignTemp);
} else if($config->GetSignType() == "HMAC-SHA256") {
$stringSignTemp = hash_hmac("sha256",$stringSignTemp ,$config->GetKey());
} else {
throw new WxPayException("签名类型不支持!");
}
$sign = strtoupper($stringSignTemp); //将所有字符转换为大写
return $sign;
}
}