• 小程序支付


    <?php
    namespace appweixincontroller;
    
    use thinkController;
    use thinkRequest;
    use thinkDb;
    
    class Pay extends Controller{
    
         protected $appid;
         protected $mch_id;
         protected $key;
         protected $openid;
    
         function __construct()
         {
             $this->appid= 'wx16039f716cab66cd';
             $this->mch_id= 'fdsafasdf';
             $this->key= 'dsfdsfsd';
         } 
    
         public function pay()
         {
            $this->openid = Request::instance()->param('openid');
            if(empty($this->openid )) return json(['code'=>-1,'message'=>'openid is not empty']);
             //统一下单接口
             $return=$this->weixinapp();
            
            return json(['code'=>1,'message'=>'success','data'=>$return]);
    
         }
        
         public function unifiedorder()
         {
    
             $url='https://api.mch.weixin.qq.com/pay/unifiedorder';
    
             $parameters=
             [
              'appid' => $this->appid,
              'mch_id'=> $this->mch_id,
              'nonce_str'=>$this->createNoncestr(),
              'body'=>'测试',
              'out_trade_no'=>'2019450806125346',
              'total_fee'=>floatval(0.01*100),
              'spbill_create_ip'=>$_SERVER['REMOTE_ADDR'],
              'notify_url'=>'https://www.sadprincess.com/wxpay/pay.php',
              'openid'=> $this->openid,
              'trade_type'=>'JSAPI'
             ];
    
             //统一下单签名
             $parameters['sign']= $this->getSign($parameters);
             $xmlData=arrayToXml($parameters);
             $return=$this->xmlToArray(http_post($url, $xmlData));
          
             return $return;
     }
    
     public function weixinapp()
     {
         //统一下单接口
         $unifiedorder = $this->unifiedorder();
    
         $parameters = [
          'appId'=>$this->appid,
          'timeStamp'=>''.time().'',
          'nonceStr'=>$this->createNoncestr(),
          'package'=>'prepay_id='.$unifiedorder['prepay_id'],
          'signType'=>'MD5'
         ];
    
         //签名
         $parameters['paySign']=$this->getSign($parameters);
         return $parameters;
     }
    
     private 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;
     }
    
     private function getSign($Obj)
     {
         foreach ($Obj as $k => $v)
         {
          $Parameters[$k] = $v;
         }
         ksort($Parameters);
         $String = $this->formatBizQueryParaMap($Parameters, false);
         $String = $String."&key=".$this->key;
         $String = md5($String);
         $result_ = strtoupper($String);
        return $result_;
     }
     
     private function formatBizQueryParaMap($paraMap, $urlencode)
     {
         $buff = "";
         ksort($paraMap);
         foreach ($paraMap as $k => $v){
           if($urlencode)
           {
           $v = urlencode($v);
           }
          $buff .= $k . "=" . $v . "&";
         }
         $reqPar;
         if (strlen($buff) > 0){
          $reqPar = substr($buff, 0, strlen($buff)-1);
         }
         return $reqPar;
         }
    
         function xmlToArray($xml)
         {    
             libxml_disable_entity_loader(true);
             $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);        
             return $values;
         }
    } 

     小程序页:

    //index.js
    //获取应用实例
    const app = getApp()
    
    Page({
      data:{
      },
      onLoad: function () {
       
      },
      pays:function(){
        wx.login({
          success: function (res) {
            if (res.code) {
              wx.request({
                url: 'http://***.***.com/api/weixin/oauth',
                data: {
                  code: res.code
                },
                success: function (res) {
                  console.log(res)
                  wx.request({
                    url: 'https://***.***.com/weixin/pay/pay',
                    data: {
                      openid: res.data.openid
                    },
                    method: 'POST',
                    header: { "Content-Type": "application/x-www-form-urlencoded" },
                    success: function (res2) {
                      console.log(res)
                      console.log(res2)
                      wx.requestPayment({
                        'timeStamp': String(res2.data.data.timeStamp),
                        'nonceStr': String(res2.data.data.nonceStr),
                        'package': String(res2.data.data.package),
                        'signType': 'MD5',
                        'paySign': String(res2.data.data.paySign),
                        'success': function (res) {
                          console.log(res)
                        },
                        'fail': function (res) {
                        },
                        complete:function(res){
                          console.log(res)
                        }
                      })
                    }
                  })
                }
              })
            }
    
          }
        })
        
      }
    })
  • 相关阅读:
    对数线性模型与线性链条件随机场
    25匹马,5个跑道,每个跑道最多能有1匹马进行比赛,最少比多少次能比出前3名?前5名?
    SVM 与 LR的异同
    EM算法简易推导
    K-means算法的优缺点
    自助采样包含训练集里63.2%的样本?
    指数加权移动平均
    oracle 对于用户的相关操作
    docker 安装 maven 私有库 nexus3
    idea 自动注入@Autowired 警告 Field injection is not recommended 关闭
  • 原文地址:https://www.cnblogs.com/boundless-sky/p/9469505.html
Copyright © 2020-2023  润新知