PHP Development Micro-credit Payment Enterprise payment Instance code

Source: Internet
Author: User
Tags cdata md5 encryption openid urlencode
Enterprise Payment Application Scenario: The public number to pay attention to users, such as processing refunds, financial settlement, and so on, this article mainly share with you the PHP development Payment Enterprise Payment instance code, hope to help everyone.

Description

1. Certificates need to use the certificate inside their merchant (note: The certificate path must be an absolute path and the following error will be reported if a relative path is used.)

Unable to use client certificate (no key found or wrong pass phrase?)

2.appid,secret and key to fill their own can.

Let's talk about the realization of the idea:

1. First get OpenID, see below for specific methods.

2. Fill in the required parameters, generate signatures, etc., see below for specific methods.

/** * API Parameters * @var array * ' mch_appid ' # public number AppID * ' Mchid ' # merchant number * ' Device_info ' # device number * ' Nonce_str ' # random String * ' Partner_ Trade_no ' # Merchant Order number * ' OpenID ' # Beneficiary User OpenID * ' check_name ' # Verify user name option for authenticated Users * ' Re_user_name ' # Payee User name * ' Amount ' # Payment amount * ' desc ' # Enterprise Payment description * ' spbill_create_ip ' # IP address * ' sign ' # signature */

Parameter reference: documentation for the Enterprise payment API

1. Get Code (index.php page)

<?php//Information callback file is located on the server location $str= "http://www.xxx.com/company_pay/getInfo.php"; $str _url=urlencode ($STR); $appid = " xxxx3e5273505e "; $url = ' https://open.weixin.qq.com/connect/oauth2/authorize?appid= '. $appid. ' &redirect_uri= '. $str _url. ' &response_type=code&scope=snsapi_userinfo&state=state#wechat_redirect '; header ("Location:". $url);? >

2. Information callback page code processing (getinfo.php)

<?php$appid = "wxxxxx3505e";//The Appid$secret of your public platform = "fxxxxx71xxx4cda2a671";//Your public platform Secret$code = $_get["code"];$ Get_token_url = ' https://api.weixin.qq.com/sns/oauth2/access_token?appid= '. $appid. ' &secret= '. $secret. ' &code= '. $code. ' &grant_type=authorization_code '; $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $get _token_url); curl_setopt ($ ch,curlopt_header,0); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout, ten); $res = Curl_exec ($ch); Curl_close ($ch); $json _obj = Json_decode ($res, true);//query user information based on OpenID and access_token $access_token = $ json_obj[' Access_token '; $openid = $json _obj[' OpenID ']; $get _user_info_url = ' https://api.weixin.qq.com/sns/ Userinfo?access_token= '. $access _token. ' &openid= '. $openid. ' &lang=zh_cn '; $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $get _user_info_url); curl_setopt ($ch, Curlopt_ header,0); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout, ten); $res = Curl_exec ( $ch); Curl_close ($ch);//Parsing JSon$user_obj = Json_decode ($res, True);//var_dump ($user _obj); echo "<br/>". " -----". $openid." "; $mch _appid= $appid; $mchid = ' 10000401 ';//Merchant number $nonce_str= ' Vhmake '. Rand (100000, 999999);//random number $partner_trade_no = ' VH '. Time (). Rand (10000, 99999);//Merchant order number $openid= $openid;//user uniquely identifies $check_name= ' No_check ';//verify User name option, No_ Check: Do not verify the real name Force_check: Strong Check the real name (non-verified users will verify the failure, unable to transfer) Option_check: For the real-name authentication of the user to verify the actual names (not verified users do not verify, can be transferred successfully) $re _user_ Name= ' [Beijing Micro-letter Workshop Technology Co., Ltd.] (http://www.vhmake.com) ';//user name $amount=100;//amount (to be divided into units, must be greater than three) $desc = ' [Beijing micro-function Workshop Technology Co., Ltd.] (http:/ /www.vhmake.com) ';//Description $spbill_create_ip=$_server["REMOTE_ADDR"];//request ip//encapsulation into Data $dataarr=array (); $DATAARR [' Amount ']= $amount; $dataArr [' Check_name ']= $check _name; $dataArr [' Desc ']= $desc; $dataArr [' Mch_appid ']= $mch _appid;$ dataarr[' Mchid ']= $mchid; $dataArr [' Nonce_str ']= $nonce _str; $dataArr [' OpenID ']= $openid; $DATAARR [' Partner_trade_ No ']= $partner _trade_no; $dataArr [' Re_user_name ']= $re _user_name; $dataArr [' Spbill_create_ip ']= $spbill _create_ip; Require ' api.php '; $sign =getsign ($DATAARR);echo "-----<br/> Signature:" $sign. " <br/>***** ";//die; $data =" <xml><mch_appid> ". $mch _appid." </mch_appid><mchid> ". $mchid." </mchid><nonce_str> ". $nonce _str." </nonce_str><partner_trade_no> ". $partner _trade_no." </partner_trade_no><openid> ". $openid." </openid><check_name> ". $check _name." </check_name><re_user_name> ". $re _user_name." </re_user_name><amount> ". $amount." </amount><desc> ". $desc." </desc><spbill_create_ip> ". $spbill _create_ip." </spbill_create_ip><sign> ". $sign." </sign></xml> ";//var_dump ($data); $ch = Curl_init (); $MENU _url=" https://api.mch.weixin.qq.com/ Mmpaymkttransfers/promotion/transfers "; curl_setopt ($ch, Curlopt_url, $MENU _url); curl_setopt ($ch, Curlopt_ Customrequest, "POST"); curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE) curl_setopt ($ch, Curlopt_ssl_verifyhost, FAL SE); $zs 1= "/XXXX/XXX/XXXXXX/APICLIENT_CERT.PEM";//Note: The completed path must be an absolute path, notTo fill in the relative path $zs2= "/xxxx/xxx/xxxxx/apiclient_key.pem"; curl_setopt ($ch, Curlopt_sslcert, $zs 1); curl_setopt ($ch, CURLOPT_ Sslkey, $zs 2);//curl_setopt ($ch, Curlopt_useragent, ' mozilla/5.0 (compatible; MSIE 5.01;//Windows NT 5.0) curl_setopt ($ch, curlopt_followlocation, 1); curl_setopt ($ch, Curlopt_autoreferer, 1); curl_setopt ($ch, Curlopt_postfields, $data); curl_setopt ($ch, Curlopt_returntransfer, true); $info = Curl_exec ($ch) , $infos =simplexml_load_string ($info), if (Curl_errno ($ch)) {echo ' errno::: '. Curl_error ($ch);} Curl_close ($ch); echo "-----<br/> Request return value:"; Echo $infos->return_code;echo "<br/>*****";? >

3. Generating a signature function file (api.php)

<?php/** * Function: Format parameters, the signature process needs to use */function Formatbizqueryparamap ($paraMap, $urlencode) {$buff = "";    Ksort ($PARAMAP);        foreach ($paraMap as $k = + $v) {if ($urlencode) {$v = UrlEncode ($v); } $buff. = $k. "=" . $v.    "&";    } if (strlen ($buff) > 0) {$reqPar = substr ($buff, 0, strlen ($buff)-1); } return $reqPar;}    /** * Function: Generate signature */function getsign ($OBJ) {foreach ($Obj as $k = = $v) {$Parameters [$k] = $v;    }//Signature step one: sorted by dictionary order parameter Ksort ($Parameters);    $String = Formatbizqueryparamap ($Parameters, false); Echo ' "string1" '. $String. '    </br> '; Signature Step Two: Add key $String after String = $String. "    &key=vhmake666vhmake666vhmake666vhmak "; echo "string2" ". $String."    </br> ";    Signature Step Three: MD5 encryption $String = MD5 ($String); echo "String3" ". $String."    </br> ";    Signature Step four: All characters to uppercase $result _ = Strtoupper ($String); echo "Result". $result _. "    </br> "; return $result _;}

return parameters in XML format after success (refer to Development documentation for details)

<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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.