php版 小程序微信支付(thinkphp3.2)

    //小程序微信支付(一定要先把appid和nchid绑定好)
    public function appletWxPay(){
        $openId = I('openid');   //微信openid
        $orderId = I('orderid');  //订单id
        
        $wechatAppId = **********;  //appid
        $wechatMchId = **********;  //mchid
        $wechatKey   = **********;  //key
        
		$webConfig = M('web_config')->find();
		
		$orderModel = D("ProductOrder");
		//查询订单详情的方法
		$order = $orderModel->selectOneOrderProductByCondition(array('order_id' => $orderId ));
			
		$orderPrice =$order['order_price'];
        

        //获取微信支付信息
        $wechat['appid'] = $wechatAppId;
        $wechat['body'] = '订单支付';
        $wechat['mch_id'] = $wechatMchId;
        $wechat['nonce_str'] = strtoupper(md5(time() . rand(100, 999)));
        $wechat['notify_url'] = $webConfig['web_url'] . '/index.php/WxappMall/wechatNotifyUrl?orderid='.$orderId;  //回调地址
        $wechat['openid'] = $openId;
        $wechat['out_trade_no'] = time();
        $wechat['spbill_create_ip'] = get_client_ip();  //thinkphp方法
        $wechat['total_fee'] = intval($orderPrice * 100);
        $wechat['trade_type'] = 'JSAPI';
        $wechat['key'] = $wechatKey;
        $wechat['sign'] = strtoupper(md5('appid=' . $wechat['appid'] . '&body=' . $wechat['body'] . '&mch_id=' . $wechat['mch_id'] . '&nonce_str=' . $wechat['nonce_str'] . '&notify_url=' . $wechat['notify_url'] . '&openid=' . $wechat['openid'] . '&out_trade_no=' . $wechat['out_trade_no'] . '&spbill_create_ip=' . $wechat['spbill_create_ip'] . '&total_fee=' . $wechat['total_fee'] . '&trade_type=' . $wechat['trade_type'] . '&key=' . $wechat['key']));
        $wechat['time_stamp'] = time();
		
		
        $formData = '<xml>';
        $formData .= '<appid><![CDATA[' . $wechat['appid'] . ']]></appid>';
        $formData .= '<body><![CDATA[' . $wechat['body'] . ']]></body>';
        $formData .= '<mch_id><![CDATA[' . $wechat['mch_id'] . ']]></mch_id>';
        $formData .= '<nonce_str><![CDATA[' . $wechat['nonce_str'] . ']]></nonce_str>';
        $formData .= '<notify_url><![CDATA[' . $wechat['notify_url'] . ']]></notify_url>';
        $formData .= '<openid><![CDATA[' . $wechat['openid'] . ']]></openid>';
        $formData .= '<out_trade_no><![CDATA[' . $wechat['out_trade_no'] . ']]></out_trade_no>';
        $formData .= '<spbill_create_ip><![CDATA[' . $wechat['spbill_create_ip'] . ']]></spbill_create_ip>';
        $formData .= '<total_fee><![CDATA[' . $wechat['total_fee'] . ']]></total_fee>';
        $formData .= '<trade_type><![CDATA[' . $wechat['trade_type'] . ']]></trade_type>';
        $formData .= '<sign><![CDATA[' . $wechat['sign'] . ']]></sign>';
        $formData .= '</xml>';

        $xml = api_notice_increment('https:/' . '/api.mch.weixin.qq.com/pay/unifiedorder', $formData);		
		
		
        if ($xml) {
            $resultCode = resolveXmlCode($xml, 'result_code');

            if ($resultCode == 'SUCCESS') {
                //生成签名
                $prepay_id = resolveXmlCode($xml, 'prepay_id');
                $payData['appId'] = $wechat['appid'];
                $payData['timeStamp'] = $wechat['time_stamp'] . '';
                $payData['nonceStr'] = $wechat['nonce_str'];
                $payData['package'] = 'prepay_id=' . $prepay_id;
                $payData['signType'] = 'MD5';
                $payData['paySign'] = md5('appId=' . $wechat['appid'] . '&nonceStr=' . $wechat['nonce_str'] . '&package=prepay_id=' . $prepay_id . '&signType=MD5&timeStamp=' . $wechat['time_stamp'] . '&key=' . $wechat['key']);

                $this->ajaxReturn(array('status' => 1, 'info' => $payData,'orderid' => $orderId));
            } else {
                $this->ajaxReturn(array('status' => 0, 'info' => resolveXmlCode($xml, 'err_code_des')));
            }
        } else {
            $this->ajaxReturn(array('status' => 0, 'info' => '参数错误,请稍后再试!'));
        }
    }
//获取微信返回数据
function api_notice_increment($url, $data) {
	
    $ch = curl_init();
    $header = "Accept-Charset: utf-8";
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    @curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $tmpInfo = curl_exec($ch);
    if (curl_errno($ch)) {
        return $ch;
    } else {
        return $tmpInfo;
    }
    curl_close($ch);
}
/**
 * 解析xml参数值[CDATA[FAIL]]
 */
function resolveXmlCode($str, $code) {
	header("Content-Type: text/html;charset=utf-8");
    $findStartStr = "![CDATA[";
    $findEndStr = "]]";
    $tempContent = "";
    $startLocation = mb_stripos($str, $code, 0, 'utf-8'); //截取起始位置
    if ($startLocation === false) {
        return "";
    }
    $targetLocation = mb_stripos($str, $findStartStr, $startLocation, 'utf-8');
    if ($targetLocation !== false) {
        $tempLocation = mb_stripos($str, $findEndStr, $targetLocation, 'utf-8');
        $tempContent = mb_substr($str, $targetLocation + mb_strlen($findStartStr, 'utf-8'), $tempLocation - ($targetLocation + mb_strlen($findStartStr, 'utf-8')), 'utf-8');
    }
    return $tempContent ? $tempContent : "";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值