微信商家转账接口

微信商家转账接口实例:

/***
 * 提现打款
 */
class tixian{
    public $mch_appid = APPID_;

    public function zhuanzhang($open_id,$order_no,$pay_money)
    {
        $post_data = [
            "appid" => $this->mch_appid,//appid
            "out_bill_no" => $order_no,//商家单号
            "transfer_scene_id" => "1005",//商家批次单号
            'openid' => $open_id,
            "transfer_amount" => intval(strval($pay_money)),// 转账金额单位为“分”
            "transfer_remark" => "用户提现转账", // 转账总笔数
//            "user_recv_perception" => "现金奖励",
            "transfer_scene_report_infos" => [
                [
                    'info_type' => "岗位类型",// 固定值
                    'info_content' => "系统推广员",// 示例值
                ],
                [
                    'info_type' => "报酬说明",// 固定值
                    'info_content' => "用户佣金",// 示例值
                ]

            ]
        ];
        $url = 'https://api.mch.weixin.qq.com/v3/fund-app/mch-transfer/transfer-bills';
        $result= self::wx_post($url, json_encode($post_data,
            JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
        $result = json_decode($result, true);
        AddLog('提现参数:' . json_encode($post_data, JSON_UNESCAPED_UNICODE) . ";结果:" . json_encode($result,JSON_UNESCAPED_UNICODE));
        return $result;

//        if(isset($result['code'])){
//            $this->error($result['massage']);
//        }
    }

    public static function wx_post($url, $param)
    {
        $authorization = self::getV3Sign($url, "POST", $param);
        $curl = curl_init();
        $headers = [
            'Authorization:' . $authorization,
            'Accept:application/json',
            'Content-Type:application/json;charset=utf-8',
            'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
        ];
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
        curl_setopt($curl, CURLOPT_POST, true);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }

    public static function getV3Sign($url, $http_method, $body)
    {
        $nonce = strtoupper(self::createNonceStr(32));
        $timestamp = time();
        $url_parts = parse_url($url);
        $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
        $cert_dir = "证书路径"; //绝对路径
        $sslKeyPath = $cert_dir."apiclient_key.pem";
        //拼接参数
        $message = $http_method . "\n" .
            $canonical_url . "\n" .
            $timestamp . "\n" .
            $nonce . "\n" .
            $body . "\n";
        $private_key = self::getPrivateKey($sslKeyPath);
        openssl_sign($message, $raw_sign, $private_key, 'sha256WithRSAEncryption');
        $sign   = base64_encode($raw_sign);
        //商户序列号
        $xulie_number = '56F03CA1113C6EB1799961A988D3E8D1B9B2A7B5';
        $token = sprintf('WECHATPAY2-SHA256-RSA2048 mchid="%s",nonce_str="%s",timestamp="%s",serial_no="%s",signature="%s"', MCHID_, $nonce, $timestamp,$xulie_number, $sign);
        return $token;
    }

    public static function createNonceStr($length = 16) { //生成随机16个字符的字符串
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    public static function getPrivateKey($filepath = '')
    {
        if (empty($filepath)) {
            //私钥位置
            $cert_dir = "证书路径";
            $filepath = $cert_dir."apiclient_key.pem";
        }
        return openssl_get_privatekey(file_get_contents($filepath));
    }

}

$tixian = new tixian();
$res = $tixian->zhuanzhang($data_openid,$orderid,$money);
var_dump($res);die;

 

 

成功返回:

提现参数:{"appid":"wx45000d4fe35ff03e","out_bill_no":"TGTX116120251120034050","transfer_scene_id":"1005","openid":"owsmlvgjPMgPMTFoVvK3wy0UjxzU","transfer_amount":100,"transfer_remark":"用户提现转账","transfer_scene_report_infos":[{"info_type":"岗位类型","info_content":"系统推广员"},{"info_type":"报酬说明","info_content":"用户佣金"}]};结果:{"create_time":"2025-11-21T18:14:28+08:00","out_bill_no":"TGTX116120251120034050","package_info":"ABBQO+oYAAABAAAAAAALvwHS6QT/QZwzhDsgaRAAAADnGpepZahT9IkJjn90+1qgEVGqai4bNyttiEqgnrSiDD8xCKL0IYw9jQUUDDyY5b3lmHg19bPKTw5MXsSy9CeBXRcWgQpm4/yy0mFzd+KlgxdDzhQ=","state":"WAIT_USER_CONFIRM","transfer_bill_no":"1330008014268852511210060338406255"}

 

posted @ 2025-11-22 09:22  流浪2024  阅读(0)  评论(0)    收藏  举报