知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁 » 新聞資訊 » 小程序相關(guān) >
微信小程序發(fā)放紅包+領(lǐng)取紅包(PHP)
發(fā)表時(shí)間:2020-9-27
發(fā)布人:葵宇科技
瀏覽次數(shù):72
發(fā)放紅包文檔:https://pay.weixin.qq.com/wiki/doc/api/tools/miniprogram_hb.php?chapter=13_9&index=2
領(lǐng)取紅包文檔:https://pay.weixin.qq.com/wiki/doc/api/tools/miniprogram_hb.php?chapter=13_10&index=3
class Index
{
public function index(Request $request=null)
{
//商戶訂單號(hào)(每個(gè)訂單號(hào)必須唯一。取值范圍:0~9,a~z,A~Z)
//組成: mch_id+yyyymmdd+10位一天內(nèi)不能重復(fù)的數(shù)字。
$mch_billno = ''.rand(1000000000,9999999999);
$mch_id = '';//商戶號(hào)
$wxappid = '';//小程序賬號(hào)appid
$send_name = '';//商戶名稱
$re_openid = '';//用戶openid
$total_amount = 30;//付款金額,單位分
$total_num = 1;//紅包發(fā)放總?cè)藬?shù)
$wishing = '望天宇八方清似玉';//紅包祝福語
$client_ip = '';//調(diào)用接口的服務(wù)器Ip地址,提前在小程序后臺(tái)設(shè)置好
$act_name = '紅包';//活動(dòng)名稱
$remark = '6666';//備注
$notify_way = 'MINI_PROGRAM_JSAPI';//通知用戶形式JSAPI
$scene_id = 'PRODUCT_2';//發(fā)放紅包使用場(chǎng)景,紅包金額大于200時(shí)必傳
$key = '';//商戶號(hào)支付鑰匙
$weixinpay = new WeixinPay($mch_billno, $mch_id, $wxappid, $send_name,$re_openid,$total_amount,$total_num,$wishing,$client_ip,$act_name,$remark,$notify_way,$scene_id,$key);
$res=$weixinpay->sendhb();
$result = [
'timeStamp'=> time()."",
'nonceStr'=>$this->createNoncestr(),
'package'=>urlencode($res['package']),
'appId'=>$wxappid,
];
$result['paySign'] = $this->getSign($result);
return json($result);
}
private function createNoncestr($length = 32) {
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
} return $str;
}
private function getSign($Obj) {
foreach ($Obj as $k => $v) {
$Parameters[$k] = $v;
}
//簽名步驟一:按字典序排序參數(shù)
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
//簽名步驟二:在 string 后加入 KEY
$String = $String . "&key=" . '商戶號(hào)支付鑰匙';
//簽名步驟三:MD5 加密
$String = md5($String);
//簽名步驟四:所有字符轉(zhuǎn)為大寫--根據(jù)接口需要打開限制
$result_ = $String;
// $result_ = strtoupper($String);
return $result_;
}
/**
* 作用:格式化參數(shù),簽名過程需要使用
* @param $paraMap
* @param $urlencode
* @return string
*/
private function formatBizQueryParaMap($paraMap, $urlencode) {
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if ($urlencode) {
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
$reqPar = "";
if (strlen($buff) > 0) {
$reqPar = substr($buff, 0, strlen($buff) - 1);
}
return $reqPar;
}
}
class WeixinPay {
private $sendurl = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendminiprogramhb';//發(fā)放紅包接口
private $mch_billno;//商戶訂單號(hào)
private $mch_id;//商戶號(hào)
private $wxappid;//公眾賬號(hào)appid
private $send_name;//商戶名稱
private $re_openid;//用戶openid
private $total_amount;//付款金額,單位分
private $total_num;//紅包發(fā)放總?cè)藬?shù)
private $wishing;//紅包祝福語
private $client_ip;//Ip地址
private $act_name;//活動(dòng)名稱
private $remark;//備注
private $notify_way;//通知用戶形式
private $scene_id;//發(fā)放紅包使用場(chǎng)景,紅包金額大于200時(shí)必傳
private $key;//商戶號(hào)支付鑰匙
function __construct($mch_billno, $mch_id, $wxappid, $send_name,$re_openid,$total_amount,$total_num,$wishing,$client_ip,$act_name,$remark,$notify_way,$scene_id,$key) {
$this->mch_billno = $mch_billno;
$this->mch_id = $mch_id;
$this->wxappid = $wxappid;
$this->send_name = $send_name;
$this->re_openid = $re_openid;
$this->total_amount = $total_amount;
$this->total_num = $total_num;
$this->wishing = $wishing;
$this->client_ip = $client_ip;
$this->act_name = $act_name;
$this->remark = $remark;
$this->notify_way = $notify_way;
$this->scene_id = $scene_id;
$this->key = $key;
}
public function sendhb(){
//隨機(jī)字符串
$nonce_str = $this->createNoncestr();
//商戶訂單號(hào)
$mch_billno = $this->mch_billno;
//商戶號(hào)
$mch_id = $this->mch_id;
//公眾賬號(hào)appid
$wxappid = $this->wxappid;
//商戶名稱
$send_name = $this->send_name;
//用戶openid
$re_openid = $this->re_openid;
//付款金額,單位分
$total_amount = $this->total_amount;
//紅包發(fā)放總?cè)藬?shù)
$total_num = $this->total_num;
//紅包祝福語
$wishing = $this->wishing;
//Ip地址
$client_ip = $this->client_ip;
//活動(dòng)名稱
$act_name = $this->act_name;
//備注
$remark = $this->remark;
//通知用戶形式
$notify_way = $this->notify_way;
//發(fā)放紅包使用場(chǎng)景,紅包金額大于200時(shí)必傳
$scene_id = $this->scene_id;
$parameters = array(
'nonce_str' => $nonce_str,
'mch_billno' => $mch_billno,
'mch_id' => $mch_id,
'wxappid' => $wxappid,
'send_name' => $send_name,
're_openid' => $re_openid,
'total_amount' => $total_amount,
'total_num' => $total_num,
'wishing' => $wishing,
'client_ip' => $client_ip,
'act_name' => $act_name,
'remark' => $remark,
'notify_way' => $notify_way,
'scene_id' => $scene_id
);
//生成簽名,所有參數(shù)+key然后MD5
$parameters['sign'] = $this->getSign($parameters);
$xmlData = $this->arrayToXml($parameters);
// var_dump($xmlData);
$curlres = $this->postXmlCurl($xmlData, $this->sendurl);
// var_dump($curlres);
$res = $this->xmlToArray($curlres);
// var_dump($res);
return $res;
}
private function postXmlCurl($xml, $url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//設(shè)置header
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//要求結(jié)果為字符串且輸出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//post提交方式
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//證書的位置
// var_dump(curl_setopt($ch, CURLOPT_SSLCERT, __DIR__ . '/cert/apiclient_cert.pem'));die;
curl_setopt($ch, CURLOPT_SSLCERT,'D:/phpstudy_pro/WWW/weixinpem/apiclient_cert.pem');
//證書key的位置
curl_setopt($ch, CURLOPT_SSLKEY,'D:/phpstudy_pro/WWW/weixinpem/apiclient_key.pem');
//運(yùn)行curl
$data = curl_exec($ch);
//返回結(jié)果
if ($data) {
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
throw new Exception("curl出錯(cuò),錯(cuò)誤碼:$error");
}
}
//數(shù)組轉(zhuǎn)換成xml
private function arrayToXml($arr) {
$xml = "<xml>";
foreach ($arr as $key => $val) {
if (is_array($val)) {
$xml .= "<" . $key . ">" . $this->arrayToXml($val) . "</" . $key . ">";
} else {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
//xml轉(zhuǎn)換成數(shù)組
private function xmlToArray($xml) {
//禁止引用外部xml實(shí)體
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring), true);
return $val;
}
//作用:產(chǎn)生隨機(jī)字符串,不長(zhǎng)于32位
private function createNoncestr($length = 32) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
//作用:生成簽名
private function getSign($Obj) {
foreach ($Obj as $k => $v) {
//不為空的參數(shù)才參與簽名
if(!empty($v)){
$Parameters[$k] = $v;
}
}
//簽名步驟一:按字典序排序參數(shù)
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
//簽名步驟二:在string后加入KEY
$String = $String . "&key=" . $this->key;
//簽名步驟三:MD5加密
$String = md5($String);
//簽名步驟四:所有字符轉(zhuǎn)為大寫
$result = strtoupper($String);
return $result;
}
//作用:格式化參數(shù),簽名過程需要使用
private function formatBizQueryParaMap($paraMap, $urlencode) {
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if ($urlencode) {
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
$reqPar = '';
if (strlen($buff) > 0) {
$reqPar = substr($buff, 0, strlen($buff) - 1);
}
return $reqPar;
}
}
相關(guān)案例查看更多
相關(guān)閱讀
- 生成海報(bào)
- 云南網(wǎng)站建設(shè)公司
- 汽車回收管理
- 云南做網(wǎng)站
- 云南省城鄉(xiāng)建設(shè)廳網(wǎng)站
- 網(wǎng)絡(luò)公司排名
- 小程序
- 網(wǎng)站維護(hù)
- 云南網(wǎng)站建設(shè)列表網(wǎng)
- 汽車回收管理系統(tǒng)
- 云南網(wǎng)站建設(shè)制作
- 小程序設(shè)計(jì)
- 網(wǎng)站建設(shè)需要多少錢
- web教程
- typescript
- 北京小程序制作
- 云南網(wǎng)頁制作
- 云南網(wǎng)站維護(hù)
- 快排推廣
- 大理網(wǎng)站建設(shè)公司
- 汽車拆解管理軟件
- 網(wǎng)站建設(shè)方法
- 小程序開發(fā)課程
- 云南衛(wèi)視小程序
- 微信小程序開發(fā)入門課程
- 出入小程序
- 網(wǎng)站建設(shè)列表網(wǎng)
- 云南小程序開發(fā)課程
- 云南省建設(shè)廳網(wǎng)站官網(wǎng)
- 網(wǎng)站排名優(yōu)化