Bootstrap

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

注意事项

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

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: 请求单次分账
     * @param string $transaction_id
     * @param string $out_order_no 商户系统内部的分账单号,在商户系统内部唯一(单次分账、多次分账、完结分账应使用不同的商户分账单号),同一分账单号多次请求等同一次。只能是数字、大小写字母_-|*@
     * @return array
     */
    function _execSplitAccount($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->getNonceStr(),
            'sign_type' => 'HMAC-SHA256',
            'transaction_id' => $transaction_id,
            'out_order_no' => $out_order_no,
            'receivers' => $receivers['res']
        ];
        $tmp_splitting_data['sign'] = $this->makeSign($tmp_splitting_data, $this->mch_secrect);
        $xml    = $this->arrayToXml($tmp_splitting_data);
        $do_arr = $this->curlPostSsl($this->sep_url, $xml);
        $result = $this->xmlToArray($do_arr);
        return $result;
    }

    /**
     * Notes: 获取随机数
     * @param int $length
     * @return string
     */
    private function getNonceStr($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 string $out_order_no 商户内部的分账单号
     */
    private function receivers($out_order_no)
    {
        
        /*CREATE TABLE `split_account_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 DEFAULT CHARSET=utf8 COMMENT='分账订单';*/

        $out_order = db('split_account_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 array $arr
     * @param string $secret
     * @return string
     */
    private function makeSign($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 array $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 array $arr
     * @return string
     */
    private function arrayToXml($arr){
        $xml = '<?xml version="1.0" encoding="UTF-8"?><xml>';
        foreach ($arr as $key => $val) {
            $xml.="<".$key.">$val</".$key.">";
        }
        $xml.="</xml>";
        return $xml;
    }

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

    /**
     * Notes: POST 请求 此处需要证书
     * @param string    $url
     * @param string    $vars
     * @param int       $second
     * @param array     $aHeader
     * @return bool|string
     */
    function curlPostSsl($url, $vars, $second = 30, $aHeader = array())
    {
        $isdir = __DIR__ . "/../../../../cert/";    //证书位置
        $ch = curl_init();
        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');
        curl_setopt($ch, CURLOPT_SSLKEY, $isdir . 'apiclient_key.pem');
        if (count($aHeader) >= 1) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        }
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
        $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;
        }
    }

}
;