php development example of wechat payment enterprise payment

Keywords: PHP xml JSON Windows

The application scenario of enterprise payment: official account pays to concerned users, such as refund, settlement, etc.

Material Science: Wechat public platform development document

Explain

1. The certificate must use the certificate in its own merchant (Note: the certificate path must be an absolute path. If the relative path is used, the following error will be reported.

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

2. Fill in your own appid, secret and key.

First, let's talk about the implementation idea:

1. First obtain openid. See the following for details.

2. Fill in required parameters, generate signature, etc. see the following for specific methods.

/** 
*API parameters
* @var array 
* mch_appid 'official account APPID
*"mchid" merchant No
*"Device" info "device No
*'nonce_str' ා random string
*"Partner trade no" merchant order number
*'openid' -- collection user openid
*"Check" name "option is for real name authenticated users
*'re user's name'
*'amount' -- payment amount
*'desc' ා enterprise payment description information
*"Spbill" create "Ip" Ip address
*Signature
*/

Parameter reference: Documentation for enterprise payment API

1. Get CODE (index.php page)

<?php 
//Server location of information callback file
$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);


?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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

<?php
$appid = "wxxxxx3505e";//appid of your wechat public platform
$secret = "fxxxxx71xxx4cda2a671";//secret of your wechat public platform
$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, 10);
$res = curl_exec($ch);
curl_close($ch);
$json_obj = json_decode($res,true);

//Query user information according to 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, 10);
$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 No
$nonce_str='vhmake'.rand(100000, 999999);//random number
$partner_trade_no='VH'.time().rand(10000, 99999);//Merchant order number
$openid=$openid;//User unique identification
$check_name='NO_CHECK';//Verify user name option, no "check: do not verify the real name force" check: strongly verify the real name (users without real name authentication will fail to verify and cannot transfer) option "check: verify the real name only for users with real name authentication (users without real name authentication can transfer successfully without verification)
$re_user_name='[Beijing Weihan Gongfang Technology Co., Ltd](http://www.vhmake.com)';//User name
$amount=100;//Amount (in minutes, must be greater than 100)
$desc='[Beijing Weihan Gongfang Technology Co., Ltd](http://www.vhmake.com)';//describe
$spbill_create_ip=$_SERVER["REMOTE_ADDR"];//Request ip
//Encapsulate 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, FALSE );

$zs1="/xxxx/xxx/xxxxxx/apiclient_cert.pem";//Note: the path to be filled in must be absolute, not relative
$zs2="/xxxx/xxx/xxxxx/apiclient_key.pem";
curl_setopt($ch,CURLOPT_SSLCERT,$zs1);
curl_setopt($ch,CURLOPT_SSLKEY,$zs2);
// 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/>*****";
?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106

3. Generate signature function file (api.php)

<?php
/**
 *  Function: format parameters. 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;
}

/**
 *  Role: generate signature
 */
function getSign($Obj)
{
    foreach ($Obj as $k => $v)
    {
        $Parameters[$k] = $v;
    }
    //Signature step 1: sort parameters in dictionary order
    ksort($Parameters);
    $String = formatBizQueryParaMap($Parameters, false);
    //echo '[string1]'.$String.'</br>';
    //Signature step 2: add KEY after string
    $String = $String."&key=vhmake666vhmake666vhmake666vhmak";
    //echo "[string2]".$String."</br>";
    //Signature step 3: MD5 encryption
    $String = md5($String);
    //echo "[string3] ".$String."</br>";
    //Step 4: change all characters to uppercase
    $result_ = strtoupper($String);
    //echo "[result] ".$result_."</br>";
    return $result_;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

After success, return the return parameters in xml format (for details, please refer to Development documents)

<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Mobile broadcast     BT factory

Posted by I WanT To Code PHP on Fri, 01 May 2020 05:22:31 -0700