• 微信支付


    private $app_id = 'wxa767bfc7f1f8164d';                                                        //Your appid
    private $mch_id = '1600682671'; //Your 商户号
    private $makesign ='muxuuxhsjakkdhdbeunxnjkaidhcnbcg'; //Your API支付的签名(在商户平台API安全按钮中获取)
    private $parameters=NULL;
    private $notify='http://gxwy.a40.com.cn/staff/user/wxhd'; //配置回调地址(给pays中转文件上传到根目录下面)
    private $app_secret='4d02d414ff822a57a85d944c919692d8'; //Your appSecret 微信官方获取
    public $error = 0;
    private $scope = 'snsapi_userinfo';


    //进行微信支付
    public function wxpayinfo(){
    $d = explode(" ", microtime());
    $reannumb= date('YmdHis').substr($d[0], 2).rand(1000000000, $d[1]);
    Session::set('reannumb',$reannumb);
    $pays = $this->request->param('money');
    $user_id=Session::get('user_id');
    $openid =Session::get('openid');
    $data=["uid"=>$user_id,"order_sn"=>$reannumb,'createtime'=>time(),'amount'=>$pays];
    $res=Db::name('order')->insert($data);
    $order_id=Db::getLastInsID($res);
    $order=Db::name('order')->where("id",$order_id)->value('order_sn');
    $conf = $this->payconfig($order,$pays * 100, '报名费用支付');

    if (!$conf || $conf['return_code'] == 'FAIL'){
    exit("<script>alert('对不起,微信支付接口调用错误!" . $conf['return_msg'] . "');history.go(-1);</script>");
    }else{
    $this->orderid = $conf['prepay_id'];
    //微信相关配置如果不正的话,进入支付页面会出现错误信息
    //生成页面调用参数
    $jsApiObj["appId"] = $conf['appid'];
    $timeStamp = time();
    $jsApiObj["timeStamp"] = "$timeStamp";
    $jsApiObj["nonceStr"] = $this->createNoncestr();
    $jsApiObj["package"] = "prepay_id=" . $conf['prepay_id'];
    $jsApiObj["signType"] = "MD5";
    $jsApiObj["paySign"] = $this->MakeSign($jsApiObj);

    //返回支付页面,并将相关参数返回给JS
    return $jsApiObj;
    }


    }







    protected function payconfig($no, $fee, $body)
    {
    $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
    $openid=Session::get('openid');
    if ($openid){
    Db::startTrans();
    try {
    $data['appid'] ="wxa767bfc7f1f8164d";
    $data['mch_id'] = $this->mch_id; //商户号
    $data['device_info'] = 'WEB';
    $data['body'] = $body;
    $data['out_trade_no'] = $no; //订单号
    $data['total_fee'] = $fee; //金额
    $data['spbill_create_ip'] = $_SERVER["REMOTE_ADDR"]; //ip地址
    $data['notify_url'] ="http://gxwy.a40.com.cn/staff/user/wxhd";
    $data['trade_type'] = 'JSAPI';
    $data['openid'] =$openid; //获取保存用户的openid
    $data['nonce_str'] = $this->createNoncestr();
    $data['sign'] = $this->MakeSign($data);
    $xml = $this->ToXml($data);
    Db::commit();
    }catch (Exception $e){
    Db::rollback();
    $this->error($e->getMessage());
    }
    }
    $curl = curl_init(); // 启动一个CURL会话

    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

    //设置header
    curl_setopt($curl, CURLOPT_HEADER, FALSE);

    //要求结果为字符串且输出到屏幕上
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_POST, TRUE); // 发送一个常规的Post请求
    curl_setopt($curl, CURLOPT_POSTFIELDS, $xml); // Post提交的数据包
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
    $tmpInfo = curl_exec($curl); // 执行操作
    curl_close($curl); // 关闭CURL会话
    $arr = $this->FromXml($tmpInfo);
    return $arr;
    }




      /**
    * 作用:产生随机字符串,不长于32位
    */
    public function createNoncestr($length = 32){
    $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    $str = "";
    for ($i = 0; $i < $length; $i++) {
    $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
    }
    //
    /**
    * 作用:产生随机字符串,不长于32位
    */
    public function randomkeys($length)
    {
    $pattern = '1234567890123456789012345678905678901234';
    $key = null;
    for ($i = 0; $i < $length; $i++) {
    $key .= $pattern{mt_rand(0, 30)}; //生成php随机数
    }
    return $key;
    }

    /**
    * 将xml转为array
    * @param string $xml
    * @throws WxPayException
    */
    public function FromXml($xml)
    {
    //将XML转为array
    return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    }

    /**
    * 输出xml字符
    * @throws WxPayException
    **/
    public function ToXml($arr)
    {
    $xml = "<xml>";
    foreach ($arr as $key => $val) {
    if (is_numeric($val)) {
    $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
    } else {
    $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
    }
    }
    $xml .= "</xml>";
    return $xml;
    }

    /**
    * 生成签名
    * @return 签名,本函数不覆盖sign成员变量,如要设置签名需要调用SetSign方法赋值
    */
    protected function MakeSign($arr)
    {
    //签名步骤一:按字典序排序参数
    ksort($arr);
    $string = $this->ToUrlParams($arr);
    //签名步骤二:在string后加入KEY
    $string = $string . "&key=" . $this->makesign;
    //签名步骤三:MD5加密
    $string = md5($string);
    //签名步骤四:所有字符转为大写
    $result = strtoupper($string);
    return $result;
    }

    /**
    * 格式化参数格式化成url参数
    */
    protected function ToUrlParams($arr)
    {
    $buff = "";
    foreach ($arr as $k => $v) {
    if ($k != "sign" && $v != "" && !is_array($v)) {
    $buff .= $k . "=" . $v . "&";
    }
    }

    $buff = trim($buff, "&");
    return $buff;
    }








    html

    $(".button-buy").click(function () {
    var money = $("input[name='money']").val();
    $.ajax({
    url: "{:url('user/wxpayinfo')}",
    type: 'post',
    dataType: 'json',
    data: {money: money},
    success:function(data){
    WeixinJSBridge.invoke(
    'getBrandWCPayRequest',
    {
    "appId":data.appId,
    "nonceStr":data.nonceStr,
    "package":data.package,
    "paySign":data.paySign,
    "signType":data.signType,
    "timeStamp":data.timeStamp
    },
    function(res){
    if(res.err_msg == 'get_brand_wcpay_request:ok'){

    alert('恭喜您,支付成功');
    }
    }
    );
    }
    });
    })



    function callpay(){

    if (typeof WeixinJSBridge == "undefined"){

    if( document.addEventListener ){

    document.addEventListener('WeixinJSBridgeReady', jsApiCall,false);

    }else if (document.attachEvent){

    document.attachEvent('WeixinJSBridgeReady', jsApiCall);
    document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
    }

    }else{

    jsApiCall();

    }
    }
  • 相关阅读:
    C++如何在Dialog和View中显示梯度背景颜色
    C++MFC的关键类(View,Application,Frame,Document等等)之间访问方法列表
    C++深入分析MFC文档视图结构(项目实践)
    C++如何修改SDI程序的默认背景颜色
    BAPI使用HR_INFOTYPE_OPERATION函数批量导入HR信息纪录代码样例(0759信息类型)
    C++在单文档的应用程序增加多个视图
    SD定价过程的16个字段的作用说明
    HR上载信息类型的长文本的样例代码
    C++在工具条中加入组合框控件
    C++如何锁定splitter窗口
  • 原文地址:https://www.cnblogs.com/maohongli/p/13277577.html
Copyright © 2020-2023  润新知