Home php教程 php手册 微信红包,企业支付完整命名空间版

微信红包,企业支付完整命名空间版

Jun 07, 2016 am 11:35 AM

包含微信普通红包,裂变红包,企业支付,红包查询,支付查询
<?php <br /> namespace Com;<br> <br> // +----------------------------------------------------------------------<br> // | Author: 左边 (加群:366504956(刚建,欢迎)  交流thinkphp下微信开发)<br> // +----------------------------------------------------------------------<br> <br> class ComPay {<br>     //证书<br>     private $apiclient_cert = '';<br>     private $apiclient_key = '';<br>     //pay的秘钥值<br>     private $apikey = "e10adc3949ba59abbe56e057f20f883e";<br>     //错误信息<br>     private $error = '';<br> <br>     private $mchid = '1309319401';//商户号<br>     private $mchappid = 'wx9dc408cb04cd6f2d';//公众号<br>     private $openid = 'oql2ZwUwTvQsD73jTZuzRc2KFYEA';//接收者openid<br>     private $amount = 100;//金额<br>     private $partnertradeno = '';//订单号<br>     private $spbillcreateip = '';//触发ip<br>     private $checkname = 'NO_CHECK';//校验要求<br> <br>     private $sendname = '发送者名字';<br>     private $wishing = '祝福语';<br>     private $actname = '活动名称';<br>     private $remark = '有钱,任性';<br> <br>     private $totalnum =3;<br>     private $amttype ='ALL_RAND';<br> <br>     //裂变红包<br>     private $api_group = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack";<br>     //普通红包<br>     private $api_single = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";<br>     //企业支付<br>     private $api_compay = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";<br>     //约包查询<br>     private $api_redbag_select = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo";<br>     //企业支付查询<br>     private $api_compay_select = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo";<br>     <br>     <br>     /**<br>     *公用-支付用商户号<br>     *@var string<br>     */<br>     public function setMchid($mchid){<br>         $this->mchid = $mchid;<br>     }<br>     /**<br>     *公用-pay的秘钥值<br>     *@var string<br>     */<br>     public function setApiKey($apikey){<br>         $this->apikey = $apikey;<br>     }<br> <br> <br>     /**<br>     *企业支付用微信公众号<br>     *@var string<br>     */<br>     public function setMchAppid($mchappid){<br>         $this->mchappid = $mchappid;<br>     }<br>     /**<br>     *企业支付接收用户openid<br>     *@var string<br>     */<br>     public function setOpenid($openid){<br>         $this->openid = $openid;<br>     }<br> <br>     /**<br>     *企业支付金额<br>     *@var integer<br>     */<br>     public function setAmount($amount){<br>         $this->amount = $amount;<br>     }<br>     /**<br>     *企业支付描述<br>     *@var string<br>     */<br>     public function setDesc($desc){<br>         $this->remark = $desc;<br>     }<br>     <br>     /**<br>     *企业支付订单号<br>     *@var string<br>     */<br>     public function setPartnerTradeNo($partnertradeno){<br>         $this->partnertradeno = $partnertradeno;<br>     }<br>     /**<br>     *企业支付触发ip<br>     *@var string<br>     */<br>     public function setSpbillCreateIp($spbillcreateip){<br>         $this->spbillcreateip = $spbillcreateip;<br>     }<br>     /**<br>     *企业支付校验规则<br>     *@var string<br>     */<br>     public function setCheckName($checkname){<br>         $this->checkname = $checkname;<br>     }<br> <br>     /**<br>     *红包支付公众号<br>     *@var string<br>     */<br>     public function setWxappid($wxappid){<br>         $this->mchappid = $wxappid;<br>     }<br>     /**<br>     *红包支付订单号<br>     *@var string<br>     */<br>     public function setMchBillNo($mchbillno){<br>         $this->partnertradeno = $mchbillno;<br>     }<br>     /**<br>     *红包支付触发ip<br>     *@var string<br>     */<br>     public function setClientIp($clientip){<br>         $this->spbillcreateip = $clientip;<br>     }<br>     /**<br>     *红包接收者/裂一变红包的种子接收者<br>     *@var string<br>     */<br>     public function setReOpenid($reopenid){<br>         $this->openid = $reopenid;<br>     }<br>     /**<br>     *红包支付金额<br>     *@var integer<br>     */<br>     public function setTotalAmount($totalamount){<br>         $this->amount = $totalamount;<br>     }<br>     /**<br>     *红包支付公众号<br>     *@var string<br>     */<br>     public function setSendName($sendname){<br>         $this->sendname = $sendname;<br>     }<br>     /**<br>     *红包支祝福语<br>     *@var string<br>     */<br>     public function setWishing($wishing){<br>         $this->wishing = $wishing;<br>     }<br>     /**<br>     *红包支付活动名称<br>     *@var string<br>     */<br>     public function setActName($actname){<br>         $this->actname = $actname;<br>     }<br>     /**<br>     *红包支付备注信息<br>     *@var string<br>     */<br>     public function setRemark($remark){<br>         $this->remark = $remark;<br>     }<br>     /**<br>     *红包支付个数-裂变专用<br>     *@var string<br>     */<br>     public function setTotalNum($totalnum){<br>         $this->totalnum = $totalnum;<br>     }<br> <br>     public function setAppId($appid){<br>         $this->mchappid = $appid;<br>     }<br>     /**<br>     *错误反馈<br>     *@return string<br>     */<br>     public function error(){<br>         return $this->error;<br>     }<br> <br>     /**<br>     *普通红包支付<br>     *@return boolean<br>     */<br>     public function RedBag(){<br>         if(!$this->inited()) return;<br>         $obj = array();<br>         $obj['wxappid'] = $this->mchappid;<br>         $obj['mch_id'] = $this->mchid;<br>         $obj['mch_billno'] = $this->partnertradeno;<br>         $obj['client_ip'] = $this->spbillcreateip;<br>         $obj['re_openid'] = $this->openid;<br>         $obj['total_amount'] = $this->amount;<br>         $obj['total_num'] = 1;<br>         $obj['send_name'] = $this->sendname;<br>         $obj['wishing'] = $this->wishing;<br>         $obj['act_name'] = $this->actname;<br>         $obj['remark'] = $this->remark;<br>         $url = $this->api_single;<br>         return $this->Pay($url,$obj);<br>     }<br> <br> <br> <br>     /**<br>     *裂变红包支付<br>     *@return boolean<br>     */<br>     public function RedBagGroup(){<br>         if(!$this->inited()) return;<br>         $obj = array();<br>         $obj['wxappid'] = $this->mchappid;<br>         $obj['mch_id'] = $this->mchid;<br>         $obj['mch_billno'] = $this->partnertradeno;<br>         $obj['re_openid'] = $this->openid;<br>         $obj['total_amount'] = $this->amount;<br>         $obj['total_num'] = $this->totalnum;<br>         $obj['amt_type'] = $this->amttype;<br>         $obj['send_name'] = $this->sendname;<br>         $obj['wishing'] = $this->wishing;<br>         $obj['act_name'] = $this->actname;<br>         $obj['remark'] = $this->remark;<br>         $url = $this->api_single;<br>         return $this->Pay($url,$obj);<br>     }<br>     /**<br>     *企业支付<br>     *@return boolean<br>     */<br>     public function ComPay(){<br>         if(!$this->inited()) return;<br>         $obj = array();<br>         $obj['openid'] = $this->openid;<br>         $obj['amount'] = $this->amount;<br>         $obj['desc'] = $this->remark;<br>         $obj['mch_appid'] = $this->mchappid;<br>         $obj['mchid'] = $this->mchid;<br>         $obj['partner_trade_no'] = $this->partnertradeno;<br>         $obj['spbill_create_ip'] = $this->spbillcreateip;<br>         $obj['check_name'] = $this->checkname;<br>         $url = $this->api_compay;<br>         return $this->Pay($url,$obj);<br>     }<br>     /**<br>     *红包查询<br>     *@return array<br>     */<br>     public function BagSelect(){<br>         $this->license();<br>         $obj = array();<br>         $obj['appid'] = $this->mchappid;<br>         $obj['mch_id'] = $this->mchid;<br>         $obj['mch_billno'] = $this->partnertradeno;<br>         $obj['bill_type'] = 'MCHT';<br>         $url = $this->api_redbag_select;<br>         return $this->Pay($url,$obj);<br>     }<br>     /**<br>     *企业支付查询<br>     *@return array<br>     */<br>     public function ComPaySelect(){<br>         $this->license();<br>         $obj = array();<br>         $obj['appid'] = $this->mchappid;<br>         $obj['mch_id'] = $this->mchid;<br>         $obj['partner_trade_no'] = $this->partnertradeno;<br>         $url = $this->api_compay_select;<br>         return $this->Pay($url,$obj);<br>     }<br> <br>     /**<br>     *支付前准备<br>     *@return boolean<br>     */<br>     private function inited(){<br>         $inited = true;<br>         $amount = $this->amount;<br>         if(!is_numeric($amount)){<br>             $this->error = "金额参数错误";<br>             $inited = false;<br>         }elseif($amount             $this->error = "金额太小";<br>             $inited = false;<br>         }elseif($amount>20000){<br>             $this->error = "金额太大";<br>             $inited = false;<br>         }<br>         if(!$this->partnertradeno){<br>             $this->partnertradeno = $this->GenBillNo();<br>         }<br>         if(!$this->spbillcreateip)<br>             $this->spbillcreateip = $_SERVER['REMOTE_ADDR'];<br>         $this->license();<br>         return $inited;<br>     }<br>     /**<br>     *证书初始化<br>     *放在同目录 cacert/文件夹下<br>     */<br>     private function license(){<br>         if(!$this->apiclient_cert) <br>             $this->apiclient_cert = dirname(__FILE__)."/cacert/apiclient_cert.pem";<br>         if(!$this->apiclient_key) <br>             $this->apiclient_key = dirname(__FILE__)."/cacert/apiclient_key.pem";<br>     }<br> <br>     /**<br>     *生在订单号<br>     *@return boolean<br>     */<br>     private function GenBillNo(){<br>         $rnd_num = array('0','1','2','3','4','5','6','7','8','9');<br>         $rndstr = "";<br>         while(strlen($rndstr)             $rndstr .= $rnd_num[array_rand($rnd_num)];    <br>         }<br> <br>         return $this->mchid.date("Ymd").$rndstr;<br>     }<br> <br>     /**<br>     *完成支付操作<br>     *@url string<br>     *@obj array<br>     *@return boolean<br>     */<br>     private function Pay($url,$obj){    <br>         $obj['nonce_str'] = $this->create_noncestr();<br>         $sign = $this->getSign($obj);<br>         $obj['sign'] = $sign;<br>         $postXml = $this->arrayToXml($obj);<br>         $responseXml = $this->CurlPostSsl($url,$postXml);<br>         return $responseXml;<br>     }<br>     /**<br>     *创建随机字串<br>     *@return string<br>     */<br>     private function create_noncestr($length = 32){<br>         $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';<br>         $str = '';<br>         for ($i = 0; $i              $str .= substr($chars,mt_rand(0,strlen($chars)-1),1);<br>         }<br>         return $str;<br>     }<br>     /**<br>     *创建签名<br>     *@return string<br>     */<br>     private function getSign($arr){<br>         ksort($arr); //按照键名排序<br>         $sign_raw = '';<br>         foreach($arr as $k => $v){<br>             $sign_raw .= $k.'='.$v.'&';<br>         }<br>         $sign_raw .= 'key='.$this->apikey;<br> <br>         return strtoupper(md5($sign_raw));<br>     }<br> <br>     /**<br>      * WXHongBao::genXMLParam()<br>      * 生成post的参数xml数据包<br>      * @return $xml<br>      */<br>     private function arrayToXml($arr){<br>         $xml ="<xml>";<br>         foreach ($arr as $key => $val) {<br>             if (is_numeric($val)) {<br>                 $xml .= "".$val."".$key.">";<br>             }else{<br>                 $xml .= "".$key.">";<br>             }<br>         }<br>         $xml .= "</xml>";<br>         return $xml;        <br>     }<br> <br>     /**<br>      * curl提交<br>      * @return $boolean<br>      */<br>     private function CurlPostSsl($url,$xml,$second = 10){<br>         $ch = curl_init();       <br>         curl_setopt($ch,CURLOPT_TIMEOUT,$second);<br>         curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);        <br>         curl_setopt($ch,CURLOPT_URL,$url);<br>         curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);<br>         curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);<br> <br>         curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');<br>         curl_setopt($ch,CURLOPT_SSLCERT,$this->apiclient_cert);        <br>         curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');<br>         curl_setopt($ch,CURLOPT_SSLKEY,$this->apiclient_key);<br>         curl_setopt($ch,CURLOPT_POST, 1);<br>         curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);<br>         $data = curl_exec($ch);<br>         if($data){<br>             curl_close($ch);            <br>             $rsxml = simplexml_load_string($data);<br>             if($rsxml->return_code == 'SUCCESS' ){<br>                 return $data;<br>             }else{<br>                 $this->error = $rsxml->return_msg;<br>                 return false;    <br>             }<br>         }else{ <br>             $this->error = curl_errno($ch);<br>             curl_close($ch);<br>             return false;<br>         }<br>     }<br> <br> <br> <br> <br> <br> }

附件 ComPay.class.zip ( 3.03 KB 下载:138 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Learn about introductory code examples for Python programming Learn about introductory code examples for Python programming Jan 04, 2024 am 10:50 AM

Learn about Python programming with introductory code examples Python is an easy-to-learn, yet powerful programming language. For beginners, it is very important to understand the introductory code examples of Python programming. This article will provide you with some concrete code examples to help you get started quickly. Print HelloWorldprint("HelloWorld") This is the simplest code example in Python. The print() function is used to output the specified content

Go language programming examples: code examples in web development Go language programming examples: code examples in web development Mar 04, 2024 pm 04:54 PM

"Go Language Programming Examples: Code Examples in Web Development" With the rapid development of the Internet, Web development has become an indispensable part of various industries. As a programming language with powerful functions and superior performance, Go language is increasingly favored by developers in web development. This article will introduce how to use Go language for Web development through specific code examples, so that readers can better understand and use Go language to build their own Web applications. 1. Simple HTTP Server First, let’s start with a

PHP variables in action: 10 real-life examples of use PHP variables in action: 10 real-life examples of use Feb 19, 2024 pm 03:00 PM

PHP variables store values ​​during program runtime and are crucial for building dynamic and interactive WEB applications. This article takes an in-depth look at PHP variables and shows them in action with 10 real-life examples. 1. Store user input $username=$_POST["username"];$passWord=$_POST["password"]; This example extracts the username and password from the form submission and stores them in variables for further processing. 2. Set the configuration value $database_host="localhost";$database_username="username";$database_pa

Java implements simple bubble sort code Java implements simple bubble sort code Jan 30, 2024 am 09:34 AM

The simplest code example of Java bubble sort Bubble sort is a common sorting algorithm. Its basic idea is to gradually adjust the sequence to be sorted into an ordered sequence through the comparison and exchange of adjacent elements. Here is a simple Java code example that demonstrates how to implement bubble sort: publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

From beginner to proficient: Code implementation of commonly used data structures in Go language From beginner to proficient: Code implementation of commonly used data structures in Go language Mar 04, 2024 pm 03:09 PM

Title: From Beginner to Mastery: Code Implementation of Commonly Used Data Structures in Go Language Data structures play a vital role in programming and are the basis of programming. In the Go language, there are many commonly used data structures, and mastering the implementation of these data structures is crucial to becoming a good programmer. This article will introduce the commonly used data structures in the Go language and give corresponding code examples to help readers from getting started to becoming proficient in these data structures. 1. Array Array is a basic data structure, a group of the same type

Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Jul 05, 2023 pm 09:57 PM

Huawei Cloud Edge Computing Interconnection Guide: Java Code Samples to Quickly Implement Interfaces With the rapid development of IoT technology and the rise of edge computing, more and more enterprises are beginning to pay attention to the application of edge computing. Huawei Cloud provides edge computing services, providing enterprises with highly reliable computing resources and a convenient development environment, making edge computing applications easier to implement. This article will introduce how to quickly implement the Huawei Cloud edge computing interface through Java code. First, we need to prepare the development environment. Make sure you have the Java Development Kit installed (

How to use PHP to write inventory management function code in the inventory management system How to use PHP to write inventory management function code in the inventory management system Aug 06, 2023 pm 04:49 PM

How to use PHP to write the inventory management function code in the inventory management system. Inventory management is an indispensable part of many enterprises. For companies with multiple warehouses, the inventory management function is particularly important. By properly managing and tracking inventory, companies can allocate inventory between different warehouses, optimize operating costs, and improve collaboration efficiency. This article will introduce how to use PHP to write code for inventory warehouse management functions, and provide you with relevant code examples. 1. Establish the database before starting to write the code for the inventory warehouse management function.

Guidance and Examples: Learn to implement the selection sort algorithm in Java Guidance and Examples: Learn to implement the selection sort algorithm in Java Feb 18, 2024 am 10:52 AM

Java Selection Sorting Method Code Writing Guide and Examples Selection sorting is a simple and intuitive sorting algorithm. The idea is to select the smallest (or largest) element from the unsorted elements each time and exchange it until all elements are sorted. This article will provide a code writing guide for selection sorting, and attach specific Java sample code. Algorithm Principle The basic principle of selection sort is to divide the array to be sorted into two parts, sorted and unsorted. Each time, the smallest (or largest) element is selected from the unsorted part and placed at the end of the sorted part. Repeat the above

See all articles