search
HomeBackend DevelopmentPHP Tutorial微信支付,企业号支付个人,php实现

导语:分销商,微商提现怎么提?直接用微信支付。

实现如下:

  • 微信支付配置
/*微信支付*/    'PAY_WEIXIN'                => array(        'appid'                 => 'xxx',        'appsecret'             => 'xxxxx',        'mchid'                 => '1283301801',                                                //商户号        'key'                   => 'zhudianbaodiandodozhudianbao0527',                          //商户支付秘钥        'apiclient_cert'        => 'Conf/cert/apiclient_cert.pem',                              //商户证书apiclient_cert.pem        'apiclient_key'         => 'Conf/cert/apiclient_key.pem',                               //商户证书apiclient_key.pem    )
  • arrayToXml

    /**   *     array转xml   */  function arrayToXml($arr)  {      $xml = "<xml>";      foreach ($arr as $key=>$val)      {          if (is_numeric($val))          {                 $xml.="<".$key.">".$val."</".$key.">";           }          else          $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";        }      $xml.="</xml>";      return $xml;   }
  • 使用证书,以post方式提交xml到对应的接口url

/**    *     作用:使用证书,以post方式提交xml到对应的接口url    */    function postXmlSSLCurl($xml, $url, $second, $cert, $key)    {        $ch = curl_init();        //超时时间        curl_setopt($ch,CURLOPT_TIMEOUT,$second ? $second : $this->timeout);        //这里设置代理,如果有的话        //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');        //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);        curl_setopt($ch,CURLOPT_URL, $url);        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);        //设置header        curl_setopt($ch,CURLOPT_HEADER,FALSE);        //要求结果为字符串且输出到屏幕上        curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);        //设置证书        //使用证书:cert 与 key 分别属于两个.pem文件        //默认格式为PEM,可以注释        curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');        curl_setopt($ch,CURLOPT_SSLCERT,$cert);        //默认格式为PEM,可以注释        curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');        curl_setopt($ch,CURLOPT_SSLKEY, $key);        //post提交方式        curl_setopt($ch,CURLOPT_POST, true);        curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);        $data = curl_exec($ch);        //返回结果        if($data){            curl_close($ch);            return $this->xmlToArray($data);        }        else {            $error = curl_errno($ch);            echo "curl出错,错误码:$error"."<br>";             curl_close($ch);            return false;        }    }
  • 企业向个人付款
//企业向个人付款    public function payToUser($params, $key, $apicent_cert, $apiclient_key) {        $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';        //检测必填参数        if($params["partner_trade_no"] == null) {   //            exit("退款申请接口中,缺少必填参数partner_trade_no!"."<br>");        }elseif($params["openid"] == null){            exit("退款申请接口中,缺少必填参数openid!"."<br>");        }elseif($params["check_name"] == null){             //NO_CHECK:不校验真实姓名 FORCE_CHECK:强校验真实姓名(未实名认证的用户会校验失败,无法转账)OPTION_CHECK:针对已实名认证的用户才校验真实姓名(未实名认证用户不校验,可以转账成功)            exit("退款申请接口中,缺少必填参数check_name!"."<br>");        }elseif(($params["check_name"] == 'FORCE_CHECK' or $params["check_name"] == 'OPTION_CHECK') && ($params["re_user_name"] == null)){  //收款用户真实姓名。            exit("退款申请接口中,缺少必填参数re_user_name!"."<br>");        }elseif($params["amount"] == null){            exit("退款申请接口中,缺少必填参数amount!"."<br>");        }elseif($params["desc"] == null){            exit("退款申请接口中,缺少必填参数desc!"."<br>");        }        $params["mch_appid"] = $this->appid;//公众账号ID        $params["mchid"] = $this->mchid;//商户号        $params["nonce_str"] = $this->createNoncestr();//随机字符串        $params['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'] == '::1' ? '192.127.1.1' : $_SERVER['REMOTE_ADDR'];//获取IP        $params["sign"] = $this->getSign($params, $key);//签名        $xml = $this->arrayToXml($params);        return $this->postXmlSSLCurl($xml, $url, false, $apicent_cert, $apiclient_key);    }
  • 企业付款
private function _enterprisePay($number, $member_id, $amount, $desc)    {        // 获取openid        $wxuser_id = M('Member')->where(array('id' => $member_id))->getField('wxuser_id');        $openid    = M('Wxuser')->where(array('id' => $wxuser_id))->getField('openid');        $pay = C('PAY_WEIXIN');        import('@.Action.WxDevelop');        $enterprise = new WxEnterprise($pay['appid'], $pay['appsecret'], $pay['mchid']);        $params = array(            'partner_trade_no' => $number,            'openid' => $openid,            'check_name' => 'NO_CHECK',            'amount' => $amount, // 总计            'desc' => $desc,        );        $result = $enterprise->payToUser($params, $pay['key'], $pay['apiclient_cert'], $pay['apiclient_key']);        return $result;    }
  • 处理分销商提现
private function _handle($truename, $price) { // 处理分销商提现        $withdrawid = date("ymdHis") . strval(rand(1000, 9999));        $data = array('withdrawid' => $withdrawid, 'store_id' => $this->store_id, 'member_id' => $this->member_id, 'truename' => $truename, 'price' => $price, 'addtime' => time());        //免审核        if ($price >= C('withdraw_uncheck_value')) {            $data['need_check'] = 0;            $data['status'] = 1;            if ($this->withdrawModel->add($data)) {                $result = $this->_enterprisePay($withdrawid, $this->member_id, $price * 100, '分销商(' . $truename . ')提现');                //遇到支付信息出错,转为需审核提现                if ($result['return_code'] != 'SUCCESS') {                    $this->withdrawModel->where(array('withdrawid' => $withdrawid))->save(array('need_check' => 1, 'status' => 0));                    $this->assign('success', 2);                }                else {                    //设置微信交易号                    $this->withdrawModel->where(array('withdrawid' => $withdrawid))->save(array('payment_no' => $result['payment_no']));                    //增加佣金流水,待修复                    $data = array('store_id' => $this->store_id, 'user_type' => 2, 'user_id' => $this->shop_id, 'trade_type' => 2, 'trade_no' => $withdrawid, 'price' => -$price, 'status'=> 1,  'message' => $truename.'提现', 'addtime' => time());                    M('Twitter_log')->add($data);                    //减少相应可提佣金                    M('Member')->where(array('id' => $this->member_id))->setInc('money', -$price);                    $this->assign('success', 1);                    //发送佣金变动消息                    import('@.Action.Tmplmsg');                    $tmplmsg = new Tmplmsg();                    $tmplmsg->send(Tmplmsg::PRICE_CHANGE, $this->member_id, array('token' => $this->token, 'intro' => '分销佣金提现转出', 'price' => $price, 'business' => BUSINESS));                }            }            else {                $this->error('提现信息错误!');            }        }        //需要审核        else {            $this->withdrawModel->add($data);            $this->assign('success' , 2);        }    }

提供企业向用户付款的功能,支持企业通过API接口付款,或通过微信支付商户平台网页功能操作付款。

温馨提示:◆ 给同一个实名用户付款,单笔单日限额2W/2W◆ 给同一个非实名用户付款,单笔单日限额2000/2000◆ 一个商户同一日付款总额限额100W◆ 仅支持商户号已绑定的APPID;◆ 针对付款的目标用户,已微信支付实名认证的用户可提供校验真实姓名的功能,未实名认证的用户无法校验,企业可根据自身业务的安全级别选择验证类型;◆ 付款金额必须小于或等于商户当前可用余额的金额;◆ 已付款的记录,企业可通过企业付款查询查看相应数据。

到账 付款资金将进入目标用户的零钱(微信-我-钱包-零钱)。微信支付将做零钱入账消息通知,零钱收支明细会展示相应记录。

温馨提示:针对无零钱账户的历史客户端版本,资金将进入用户的红包账户,微信支付无消息通知用户,企业可选择自行触达用户。

接口链接:https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers

是否需要证书请求需要双向证书。

数据示例:

<xml><mch_appid>wxe062425f740c30d8</mch_appid><mchid>10000098</mchid><nonce_str>3PG2J4ILTKCH16CQ2502SI8ZNMTM67VS</nonce_str><partner_trade_no>100000982014120919616</partner_trade_no><openid>ohO4Gt7wVPxIT1A9GjFaMYMiZY1s</openid><check_name>OPTION_CHECK</check_name><re_user_name>张三</re_user_name><amount>100</amount><desc>节日快乐!</desc><spbill_create_ip>10.2.3.10</spbill_create_ip><sign>C97BDBACF37622775366F38B629F45E3</sign></xml>

成功示例:

<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[]]></return_msg><mch_appid><![CDATA[wxec38b8ff840bd989]]></mch_appid><mchid><![CDATA[10013274]]></mchid><device_info><![CDATA[]]></device_info><nonce_str><![CDATA[lxuDzMnRjpcXzxLx0q]]></nonce_str><result_code><![CDATA[SUCCESS]]></result_code><partner_trade_no><![CDATA[10013574201505191526582441]]></partner_trade_no><payment_no><![CDATA[1000018301201505190181489473]]></payment_no><payment_time><![CDATA[2015-05-19 15:26:59]]></payment_time></xml>

错误示例:

<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[系统繁忙,请稍后再试.]]></return_msg><result_code><![CDATA[FAIL]]></result_code><err_code><![CDATA[SYSTEMERROR]]></err_code><err_code_des><![CDATA[系统繁忙,请稍后再试.]]></err_code_des></xml>

参考资料:https://pay.weixin.qq.com/wiki/doc/api/mch_pay.php?chapter=14_2

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Apr 17, 2025 am 12:22 AM

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft