public function getPrePayOrders($body, $out_trade_no, $total_fee, $notify_url,$param)
{
$user = Db::name('user')->where('id', $param['user_id'])->find();
if (!$user) {
return null;
}
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
$onoce_str = $this->createNonceStr();
$data["appid"] = "***********************";
$data["body"] = $body;
$data["mch_id"] = $this->config['mch_id'];
$data["nonce_str"] = $onoce_str;
$data["notify_url"] = $notify_url;
$data["out_trade_no"] = $out_trade_no;
$data["spbill_create_ip"] = $this->get_client_ip();
$data["total_fee"] = $total_fee*100;
$data["trade_type"] = "JSAPI";
$data["sign_type"] = "MD5";
$data["openid"] = $user['openid'];
$s = $this->getSigns($data);
$data["sign"] = $s;
$xml = $this->arrayToXml($data);
$response = $this->postXmlCurl($xml, $url);
$res = (array)simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
$time=time();
if($res['return_code']=='SUCCESS'){
if ($res['result_code'] == 'SUCCESS') {
unset($data);
$data['appId'] = "wxaca009842a6168ec";
$data['timeStamp'] = (string)$time;
$data['nonceStr'] = $onoce_str;
$data['package'] = 'prepay_id='.$res['prepay_id'];
$data['signType'] = 'MD5';
$data['paySign'] = $this->getSigns($data);
// // dump($data);die;
// $time=time();
// $tmp = [];//临时数组用于签名
// $tmp['appId'] = $this->appid;
// $tmp['nonceStr'] = $this->createNonceStr();
// $tmp['package'] = 'prepay_id='.$res['prepay_id'];
// $tmp['signType'] = 'MD5';
// $tmp['timeStamp'] = (string)$time;
// $result['appId'] =$this->appid;
// $result['timeStamp'] =(string)$time;
// $result['nonceStr'] = $this->createNonceStr();//随机字符串
// $result['signType'] = 'MD5';//签名算法,暂支持 MD5
// $result['package'] = 'prepay_id='.$res['prepay_id'];//统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*
// $result['paySign'] = $this->getSign($tmp);
return $data;
}
if($res['result_code']=='FAIL'&& $res['err_code']=='OUT_TRADE_NO_USED'){
return "repeat";//订单号重复
}
if($res['result_code']=='FAIL'&& $res['err_code']=='ORDERPAID'){
return "payed";//已经支付
}
}else{
return $res;
}
}
public function getSigns($Obj)
{
ksort($Obj);
$st_data='';
foreach ($Obj as $k => $v){
$st_data.=$k.'='.$v.'&';
}
//签名步骤二:在string后加入KEY
$st_data=$st_data."key=********************";
//签名步骤三:MD5加密
// dump($st_data);
$sign = md5($st_data);
// 签名步骤四:所有字符转为大写
$result=strtoupper($sign);
// dump($result);die;
return $result;
}
微信公众号 jsapi 支付
于 2022-07-04 16:17:13 首次发布