<?php
namespace app\weixin\controller;

use think\Controller;
use think\Request;
use think\Db;

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)
                    }
                  })
                }
              })
            }
          })
        }

      }
    })
    
  }
})

 

posted on 2018-08-13 17:13  上善若水-随  阅读(341)  评论(0编辑  收藏  举报