🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 微信支付--服务端 引入该文件 WechatAppPay.php 下载地址: http://pan.baidu.com/s/1qXMnLhy ![](https://box.kancloud.cn/621b4ecd46b1b63012cf65e2692ca50c_1928x1048.png) **以下代码是统一下单获取预支付订单号** ![](https://box.kancloud.cn/ce13cb2c108c92e8985aef6c4596a852_1928x1048.png) 具体代码如下 ~~~ <?php /** * Created by PhpStorm. * User: 喵小猫 * Date: 2017/3/31 * Time: 11:28 */ namespace Payment\Controller; use Common\Controller\FrontbaseController; class WxpayController extends FrontbaseController{ //api--获取的订单号 public function wechatPay(){ require_once C(EXTEND_PATH).'/Wxpay/WechatAppPay.php'; $appid = 'wx************'; $mch_id = '**商户id**'; $notify_url = 'http://**********/application/Payment/notify.php'; $key = '*******商户秘钥*********'; $description = I("description");//商品描述 $wxPrice = I("wxPrice"); //订单金额 只能为整数 单位为分 $tradeNo = I("tradeNo"); //自定义的订单号 //1.统一下单方法 $wechatAppPay = new \WechatAppPay($appid, $mch_id, $notify_url, $key); $params['body'] = $description; //商品描述 $params['out_trade_no'] = $tradeNo; //自定义的订单号 $params['total_fee'] = $wxPrice; //订单金额 只能为整数 单位为分 $params['trade_type'] = 'APP'; //交易类型 JSAPI | NATIVE | APP | WAP $result = $wechatAppPay->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); } } ~~~ **回调部分** ~~~ <?php /** * Created by PhpStorm. * User: 喵小猫 * Date: 2017/5/18 * Time: 14:46 */ //include_once ("./log_.php"); //$log = new log_(); //$log_name="../../data/runtime/Logs/notify_url.log";//log文件路径 $xml = file_get_contents('php://input'); //$log->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 = "<xml>"; foreach ($post as $key=>$val){ if(is_array($val)){ $xml.="<".$key.">".arrayToXml($val)."</".$key.">"; }else{ $xml.="<".$key.">".$val."</".$key.">"; } } $xml.="</xml>"; return $xml; ~~~ **回调中用到的log类** ~~~ <?php /** * Created by PhpStorm. * User: 喵小猫 * Date: 2017/5/19 * Time: 10:28 */ class Log_ { // 打印log function log_result($file,$word) { $fp = fopen($file,"a"); flock($fp, LOCK_EX) ; fwrite($fp,"执行日期:".strftime("%Y-%m-%d-%H:%M:%S",time())."\n".$word."\n\n"); flock($fp, LOCK_UN); fclose($fp); } } ~~~