企业付款至用户零钱 php

企业付款至用户零钱 php


namespace app\admin\controller;
use think\Controller;
use \app\admin\common\model\User;
class Corpay extends Controller
{
     
    protected $SSLCERT_PATH = './cert/apiclient_cert.pem';//证书
    protected $SSLKEY_PATH = './cert/apiclient_key.pem';//证书
    //企业支付给用户微信余额
    public function c_pay_balance($user_id = 3){
     
        $url ="https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
        $mch_appid = config('appid'); //小程序账号appid
        $mchid = config('mch_ids');//商户号
        $nonce_str = $this->createNoncestr();//随机字符串
        $key=config('mch_keys');//商户密钥
        $partner_trade_no = 'txorder'.time().rand(1000,9999);//商户订单号
        $openid = User::where('id',$user_id)->value('openid');//用户openid
        $amount	= 30;//金额
        $desc = '用户提现';//企业付款备注
        $data = array(
            'mch_appid'=>$mch_appid,
            'mchid'=>$mchid,
            'nonce_str'=>$nonce_str,
            'partner_trade_no'=>$partner_trade_no,
            'openid'=>$openid,
            'check_name'=>'NO_CHECK',
            'amount'=>$amount,
            'desc'=>$desc
        );
        $data['sign'] =$this->getSign($data,$key);//签名
        $xmldata = $this->arrayToXml($data);
        $return = $this->xmlToArray($this->postXmlCurl($xmldata, $url, 60));
        print_r($return);
    }

    protected function postXmlCurl($xml, $url, $second = 30)
    {
     
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
        curl_setopt($ch, CURLOPT_SSLCERT, $this->SSLCERT_PATH);
        curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
        curl_setopt($ch, CURLOPT_SSLKEY, $this->SSLKEY_PATH);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        $data = curl_exec($ch);
        if ($data) {
     
            curl_close($ch);
            return $data;
        } else {
     
            $error = curl_errno($ch);
            echo "curl出错,错误码:$error" . "
"
; curl_close($ch); return false; } } //数组转换成xml private function arrayToXml($arr) { $xml = ""; foreach ($arr as $key => $val) { if (is_array($val)) { $xml .= "<" . $key . ">" . arrayToXml($val) . " . $key . ">"; } else { $xml .= "<" . $key . ">" . $val . " . $key . ">"; } } $xml .= ""; return $xml; } //Xml转数组 private function xmlToArray($xml) { if($xml == '') return ''; libxml_disable_entity_loader(true); $arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $arr; } //作用:产生随机字符串,不长于32位 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; } //作用:生成签名 protected function getSign($params,$key) { ksort($params, SORT_STRING); $unSignParaString = $this->formatQueryParaMap($params, false); return $signStr = strtoupper(md5($unSignParaString . "&key=" . $key)); } ///作用:格式化参数,签名过程需要使用 protected function formatQueryParaMap($paraMap, $urlEncode = false) { $buff = ""; ksort($paraMap); foreach ($paraMap as $k => $v) { if (null != $v && "null" != $v) { if ($urlEncode) { $v = urlencode($v); } $buff .= $k . "=" . $v . "&"; } } $reqPar = ''; if (strlen($buff) > 0) { $reqPar = substr($buff, 0, strlen($buff) - 1); } return $reqPar; } }

你可能感兴趣的:(php,小程序)