今天是 :

微信打款到个人零钱账户

<?php
namespace app\utils;

class WechatPayUser{


    // 生成随机数
    function createNoncestr($len = 32){
        $chars  = 'abcdefghijklmnopqrstuvwxyz0123456789';
        $str = '';
        for($i=0; $i < $len ; $i++){
            $str .= substr($chars , mt_rand(0, strlen($chars)-1 ) , 1);
        }
        return $str;
    }

    function unicode(){
        $str = uniqid(mt_rand() , 1);
        $str = sha1($str);
        return md5($str);
    }

    function arraytoxml($data){

        $str = '<xml>';
        foreach ($data as $k => $v){
            $str .= '<'.$k.'>' . $v . '</'.$k.'>';
        }
        $str .= '</xml>';
        return $str;
    }

    function  xmltoarray($xml){
     libxml_disable_entity_loader(true);
     $xmlstring = simplexml_load_string($xml , 'SimpleXMLElement' , LIBXML_NOCDATA);
     $data = json_decode(json_encode($xmlstring) , true);
     return $data;
    }

    function requestUrl($params = '' , $url){

        $ROOT = $_SERVER["DOCUMENT_ROOT"];
        $posturl = $url;
        $curlpost = $params;
        $ch = curl_init();
        curl_setopt($ch ,    CURLOPT_URL ,$posturl);
        curl_setopt($ch ,    CURLOPT_HEADER , 0);
        curl_setopt($ch ,    CURLOPT_RETURNTRANSFER , 1);
        curl_setopt($ch ,    CURLOPT_POST , 1);
        curl_setopt($ch ,    CURLOPT_POSTFIELDS , $curlpost);
        curl_setopt($ch ,    CURLOPT_SSL_VERIFYPEER , false);
        curl_setopt($ch ,    CURLOPT_SSL_VERIFYHOST , false);
        curl_setopt($ch ,    CURLOPT_SSLCERT , $ROOT.'/web/wxpay/wx_mini_pro_cert/apiclient_cert.pem');
        curl_setopt($ch ,    CURLOPT_SSLKEY ,$ROOT .'/web/wxpay/wx_mini_pro_cert/apiclient_key.pem');
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    /**
     * @param $amount
     * @param $receive_open_id
     * @param string $desc
     * @param string $check_name
     * ;
     */
    function SendMoney($app_id , $mch_id , $secret , $amount , $receive_open_id , $desc =  '' , $check_name='' ){

        $total_amount = sprintf('%.2f', 100 * $amount);
        $data = array(
            'mch_appid' => $app_id,
            'mchid' => $mch_id,
            'nonce_str' => $this->createNoncestr(),
            'partner_trade_no' => 'testryanno',
            'openid' => $receive_open_id,
            'check_name' => 'NO_CHECK',
            're_user_name' => $check_name,
            'amount' => floatval($total_amount),
            'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
            'desc' => $desc,
        );
        $secredKey =$secret;
        $data = array_filter($data);
        ksort($data);
        $str = '';

        foreach ($data as $k => $v){
            $str .=  $k . '=' . $v . '&';
        }
        $str .=  'key=' . $secredKey;
        $data['sign'] = strtoupper(md5($str));
        $xml =  $this->arraytoxml($data);
        $api = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
        $res = $this->requestUrl($xml, $api);
        return  $this->xmltoarray($res);
    }

}

调用方式:

 $wechat = $this->wechat_app;
         $wxpay = new WechatPayUser();
         $user_open_id = 'oSL1a48*****DyW_WajWk';

         $res = $wxpay->SendMoney($wechat->app_id , $wechat->mch_id  , $wechat->key , 1  , $user_open_id , '测试' , 'ryan');
         if(strtolower($res['return_code'] ) == 'success' && strtolower($res['result_code']) == 'success'){
             return new ApiResponse(0 , '支付成功' , $res);
         }else{
             return new ApiResponse(1, '支付失败' , $res);
         }

 

调用成功后的返回值:

公告:

  本站采用 gin作为后台服务
前端使用 sematicui 框架搭建(拼凑)。
有喜欢本站的同学。欢迎到聊天室留言

评论: