微信企业付款到零钱

  1. 须在微信支付平台申请开通相应资格  

    https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_1

    

2、设置支付平台密钥和下载安全证书

  https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=4_3

3、对照官方文档,传递相应字段,根据官方提供的其他支付DEMO改写代码

  

/**
     * 企业付款
     */
    public function transfer($id)
    {
        $orderData = ShopOrder::get($id);//获取订单
        trace($orderData,'wxpayorder');
        $mp_id = '******';//区分后台公众号标识;
        $appinfo = get_mpid_appinfo ( $mp_id );   //获取公众号信息
        $cfg = array(
            'APPID'     => $appinfo['appid'],
            'MCHID'     => $appinfo['mchid'],
            'KEY'       => $appinfo['mchkey'],
            'APPSECRET' => $appinfo['secret'],
            'NOTIFY_URL' => $appinfo['notify_url'],
        );
        WxPayConfig::setConfig($cfg);//设置公众号信息

        if (isset($id) && $id != 0) {
            //商户订单
            $outid = $orderData->id.date("YmdHis",time()).rand(0,9);
            $price = intval($orderData->goods_price*100);


            //调用封装类
            $input = new WxPayTransfer();
            $input->setDesc('商家结算入账');
            $input->setPartnerTradeNo($outid);  //建议默认的预支付交易单商户订单号由date("YmdHis").'_'.order_id组成
            $input->setAmount(100);
            $input->setOpenid($orderData->owner_openid);
//            $input->setCheckName('FORCE_CHECK');
            $input->setCheckName('NO_CHECK');
//            $input->setReUserName();


            $order = WxPayApi::transfer($input);//传递对象到相应的处理方法

            if ($order['return_code'] == 'SUCCESS'){
                $orderData->is_checkout =1;
                $orderData->check_time = $order['payment_time'];
                $orderData->save();

                return true;
            }else{
                trace($order,'企业付款input');
                return false;
            }

        }
    }

  

/**
     * 企业付款,WxPayTransfer中好多参数必填必填。文档 https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2
     * appid、mchid、spbill_create_ip、nonce_str不需要填入
     * @param comwxpaydatabaseWxPayTransfer $inputObj
     * @param int $timeOut
     * @throws WxPayException
     * @return array 成功时返回,其他抛异常
     * @throws comwxpayWxPayException
     */
    public static function transfer($inputObj, $timeOut = 6)
    {
        $url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
        //检测必填参数
        if (!$inputObj->isPartnerTradeNoSet()) {
            throw new WxPayException("缺少统一支付接口必填参数PartnerTradeNoSet!");
        } else if (!$inputObj->isDescSet()) {
            throw new WxPayException("缺少统一支付接口必填参数desc!");
        } else if (!$inputObj->isAmountSet()) {
            throw new WxPayException("缺少统一支付接口必填参数金钱amount!");
        } else if (!$inputObj->isOpenidSet()) {
            throw new WxPayException("缺少统一支付接口必填参数openid!");
        }
        //还有很多,不写了,传够就好




        $inputObj->setMchAppid(WxPayConfig::getConfig('APPID'));//公众账号ID
        $inputObj->setMchId(WxPayConfig::getConfig('MCHID'));//商户号
        $inputObj->setSpbillCreateIp($_SERVER['REMOTE_ADDR']);//终端ip
        $inputObj->setNonceStr(self::getNonceStr());//随机字符串

        //签名
        $inputObj->setSign();
        $xml = $inputObj->toXml();

        $startTimeStamp = self::getMillisecond();//请求开始时间
        $response = self::postXmlCurl($xml, $url, true, $timeOut);//发送请求

//        trace($response,'返回');
        $result = $inputObj->fromXml($response);
//        trace($result,'返回结果');
        self::reportCostTime($url, $startTimeStamp, $result);//上报请求花费时间

        return $result;
    }

  

注意:

1、下载的证书后,代码设置的证书路径为绝对路径。

2、如果不懂哪里出现了问题,可以抓取微信服务器返回的信息,提示一目了然

原文地址:https://www.cnblogs.com/pangxiaox/p/7802687.html