微信支付普通商户分账-请求单次分账

注意事项

下预付单时 务必要添加 profit_sharing 为 Y 否则该笔订单不支持分账。 参考链接 https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=26_3
订单支付成功后不能立刻执行分账逻辑 要任务操作 大于订单支付时间1分钟以上 且有QPS限制 建议做好日志警告 必要时切换到服务商


namespace app\api\lib\Weixin;

use think\Controller;

class Weixin extends Controller
{
    private $sep_url;                           // 单次分账请求URL
    private $mch_id;                            // 商户号
    private $appid;                             // 公众号appid
    private $mch_secrect;                       // 此处是商户key!!!

    function __construct()
    {
        $this->sep_url = 'https://api.mch.weixin.qq.com/secapi/pay/profitsharing';
        $this->mch_id = config('wechat.pay_config.mch_id');
        $this->appid = config('wechat.pay_config.app_id');
        $this->mch_secrect = config('wechat.pay_config.key');
    }

    /**
     * Notes: 请求单次分账
     * User: googol
     * Date: 2020-07-15
     * Time: 10:28
     * Url:
     * @param $transaction_id   微信支付交易单号
     * @param $out_order_no     商户系统内部的分账单号,在商户系统内部唯一(单次分账、多次分账、完结分账应使用不同的商户分账单号),同一分账单号多次请求等同一次。只能是数字、大小写字母_-|*@
     */
    function requestsingleaccountsplitting($transaction_id, $out_order_no)
    {
        $receivers = $this->receivers($out_order_no);
        if ($receivers['code'] == 0) return ['code' => '分账失败!'];
        $tmp_splitting_data = [
            'appid' => $this->appid,
            'mch_id' => $this->mch_id,
            'nonce_str' => $this->get_nonce_str(),
            'sign_type' => 'HMAC-SHA256',
            'transaction_id' => $transaction_id,
            'out_order_no' => $out_order_no,
            'receivers' => $receivers['res']
        ];
        $tmp_splitting_data['sign'] = $this->make_sign($tmp_splitting_data, $this->mch_secrect);
        $xml = $this->array_to_xml($tmp_splitting_data);
        $do_arr = $this->curl_post_ssl($this->sep_url, $xml);
        $result = $this->xml_to_array($do_arr);
        return $result;
    }

    /**
     * Notes: 获取随机数
     * @param int $length
     * @return string
     */
    private function get_nonce_str($length = 32)
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    /**
     * Notes: 获取分账详细列表信息
     * User: googol
     * @param $out_order_no     商户内部的分账单号
     */
    private function receivers($out_order_no)
    {
        
        /*CREATE TABLE `m_separateaccounts_order` (
          `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
          `trade` varchar(32) NOT NULL DEFAULT '' COMMENT '分账单号',
          `m_id` int(11) NOT NULL DEFAULT '0' COMMENT '机器id',
          `s_id` int(11) NOT NULL DEFAULT '0' COMMENT '店铺id',
          `agent_id` int(11) NOT NULL COMMENT '代理id',
          `account` varchar(64) NOT NULL DEFAULT '' COMMENT '分账接收openid',
          `agent_type` tinyint(1) NOT NULL DEFAULT '3' COMMENT '0平台 1一级代理 2二级代理',
          `proportion` int(5) NOT NULL DEFAULT '0' COMMENT '比例',
          `payment` decimal(12,2) NOT NULL DEFAULT '0.00' COMMENT '实际付款金额',
          `shareamount` int(11) NOT NULL DEFAULT '0' COMMENT '分得金额 分',
          `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '结算状态 0待分账 1已分账 2分账失败',
          `cdate` varchar(12) NOT NULL DEFAULT '' COMMENT '日期',
          `regdate` int(11) NOT NULL COMMENT '时间',
          `shareddate` int(11) NOT NULL DEFAULT '0' COMMENT '分账时间',
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COMMENT='分账订单';*/

        $out_order = db('separateaccounts_order')
            ->where(['trade' => $out_order_no, 'status' => 0])
            ->field('payment, account, shareamount')
            ->select();
        if (!empty($out_order)) {
            $receivers_arr = [];
            foreach ($out_order as $key => $val) {
                $receivers_arr[$key]['type'] = 'PERSONAL_OPENID';
                $receivers_arr[$key]['account'] = $val['account'];
                $receivers_arr[$key]['amount'] = $val['shareamount'];
                $receivers_arr[$key]['description'] = 'payment';
            }
            return ['code' => 1, 'res' => json_encode($receivers_arr)];
        }
        return ['code' => 0];
    }

    /**
     * Notes: 生成sign
     * @param $arr
     * @param $secret
     * @return string
     */
    private function make_sign($arr, $secret)
    {
        //签名步骤一:按字典序排序参数
        ksort($arr);
        $str = $this->to_url_params($arr);
        //签名步骤二:在str后加入KEY
        $str = $str . "&key=" . $secret;
        //签名步骤三:HMAC-SHA256 类型  加密的字符串 key是商户秘钥
        $str = hash_hmac('sha256', $str, $this->mch_secrect);
        //签名步骤四:所有字符转为大写
        $result = strtoupper($str);
        return $result;
    }

    /**
     * Notes: 数组转字符串
     * @param $arr
     * @return string
     */
    private function to_url_params($arr)
    {
        $str = "";
        foreach ($arr as $k => $v) {
            if (!empty($v) && ($k != 'sign')) {
                $str .= "$k" . "=" . $v . "&";
            }
        }
        $str = rtrim($str, "&");
        return $str;
    }

    /**
     * Notes: 数组转XML
     * @param $arr
     * @return string
     */
    private function array_to_xml($arr){
        $xml = '';
        foreach ($arr as $key => $val) {
            $xml.="<".$key.">$val.$key.">";
        }
        $xml.="";
        return $xml;
    }

    /**
     * Notes: XML转数组
     * @param $xml
     * @return mixed
     */
    private function xml_to_array($xml){
        libxml_disable_entity_loader(true);
        $arr= json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $arr;
    }

    /**
     * Notes: POST 请求 此处不需要证书
     * @param $xml
     * @param $url
     * @param int $second
     * @return bool|string
     */
    function curl_post_ssl($url, $vars, $second = 30, $aHeader = array())
    {
        $isdir = __DIR__ . "/../../../../cert/";//证书位置

        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);//设置执行最长秒数
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//
        curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');//证书类型
        curl_setopt($ch, CURLOPT_SSLCERT, $isdir . 'apiclient_cert.pem');//证书位置
        curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');//CURLOPT_SSLKEY中规定的私钥的加密类型
        curl_setopt($ch, CURLOPT_SSLKEY, $isdir . 'apiclient_key.pem');//证书位置
//        curl_setopt($ch, CURLOPT_CAINFO, 'PEM');
//        curl_setopt($ch, CURLOPT_CAINFO, $isdir . 'rootca.pem');
        if (count($aHeader) >= 1) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);//设置头部
        }
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);//全部数据使用HTTP协议中的"POST"操作来发送

        $data = curl_exec($ch);//执行回话
        if ($data) {
            curl_close($ch);
            return $data;
        } else {
            $error = curl_errno($ch);
            echo "call faild, errorCode:$error\n";
            curl_close($ch);
            return false;
        }
    }

}

你可能感兴趣的:(支付)