微信支付–wxPay(服务端)
最后更新于:2022-04-01 23:48:05
# 微信支付--服务端
引入该文件 WechatAppPay.php
下载地址: http://pan.baidu.com/s/1qXMnLhy
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/621b4ecd46b1b63012cf65e2692ca50c_1928x1048.png)
**以下代码是统一下单获取预支付订单号**
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/ce13cb2c108c92e8985aef6c4596a852_1928x1048.png)
具体代码如下
~~~
unifiedOrder( $params );
// print_r($result); // result中就是返回的各种信息信息,成功的情况下也包含很重要的prepay_id
//2.创建APP端预支付参数
/** @var TYPE_NAME $result */
$data = @$wechatAppPay->getAppPayParams( $result['prepay_id'] );
// 根据上行取得的支付参数请求支付即可
if($data){
$arr['status'] = 1;
$arr['appid'] = $data['appid']; //应用APPID
$arr['partnerid'] = $data['partnerid']; //商户号
$arr['prepayid'] = $data['prepayid']; //预支付交易会话标识
$arr['package'] = $data['package']; //交易类型
$arr['noncestr'] = $data['noncestr']; //微信返回的随机字符串
$arr['timestamp'] = $data['timestamp']; //时间戳
$arr['sign'] = $data['sign']; //微信返回的签名
}else{
$arr['status'] = 0;
$arr['msg'] = '获取微信返回信息失败';
}
$this->output($arr);
}
}
~~~
**回调部分**
~~~
log_result($log_name,"【xml】:\n".json_encode($xml)."\n");
$arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
//$log->log_result($log_name,"【arr】:\n".json_encode($arr)."\n");
$post = array();
$post['appid'] = $arr['appid'];
$post['mch_id'] = $arr['mch_id'];
$post['nonce_str'] = $arr['nonce_str'];
$post['out_trade_no'] = $arr['out_trade_no'];
$post['total_fee'] = $arr['total_fee'];
$post['notify_url'] = $arr['notify_url'];
$post['trade_type'] = $arr['trade_type'];
$post['openid'] = $arr['openid'];
$post['bank_type'] = $arr['bank_type'];
$post['cash_fee'] = $arr['cash_fee'];
$post['fee_type'] = $arr['fee_type'];
$post['is_subscribe'] = $arr['is_subscribe'];
$post['result_code'] = $arr['result_code'];
$post['return_code'] = $arr['return_code'];
$post['time_end'] = $arr['time_end'];
$post['transaction_id'] = $arr['transaction_id'];
//$log->log_result($log_name,"【post】:\n".json_encode($post)."\n");
//排序
ksort($post);
//生成sign
$str = urldecode(http_build_query($post)).'&key=lituoquanjingrongfuwupingtai1990';
$sign = strtoupper(md5($str));
//$log->log_result($log_name,"【sign】:\n".$sign."\n");
if($sign === $arr['sign']){
// $log->log_result($log_name,"【验证】:\n".验证成功."\n");
//此处处理业务逻辑
// $log->log_result($log_name,"【sql】:\n".$sql."\n");
}
$xml = "";
foreach ($post as $key=>$val){
if(is_array($val)){
$xml.="<".$key.">".arrayToXml($val)."".$key.">";
}else{
$xml.="<".$key.">".$val."".$key.">";
}
}
$xml.=" ";
return $xml;
~~~
**回调中用到的log类**
~~~
';