WxToShopMoney
2022-10-11 本文已影响0人
JUN888
demo WxToShopMoney
<?php
namespace wxpay;
class WxToShopMoney
{
private $APPID = "wx************";
private $MCHID = "14***************";
private $SECRECT_KEY = "ming***************";
private $IP = "192.168.1.0";
public function __construct($type = 'mp')
{
$wechatConfig = config('wechat');
if ($type != 'mp') {
$this->APPID = $wechatConfig['miniprogram']['appid'];
}
}
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 unicode()
{
$str = uniqid(mt_rand(), 1);
$str = sha1($str);
return md5($str);
}
private function arraytoxml($data)
{
$str = '<xml>';
foreach ($data as $k => $v) {
$str .= '<' . $k . '>' . $v . '</' . $k . '>';
}
$str .= '</xml>';
return $str;
}
private function xmltoarray($xml)
{
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring), true);
return $val;
}
private function curl($param = "", $url)
{
$dir = "/www/wwwroot/www.ldyizhibao.com/wjxjpjajyjpjejm/";
$dir = "E:/cert/";
$postUrl = $url;
$curlPost = $param;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSLCERT, $dir.'apiclient_cert.pem');
curl_setopt($ch, CURLOPT_SSLKEY, $dir.'apiclient_key.pem');
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
public function sendMoney($amount, $re_openid, $desc = '测试', $check_name = '')
{
$total_amount = 100 * $amount;
$data = array('mch_appid' => $this->APPID, 'mchid' => $this->MCHID, 'nonce_str' => $this->createNoncestr(), 'partner_trade_no' => date('YmdHis') . rand(1000, 9999), 'openid' => $re_openid, 'check_name' => 'NO_CHECK', 're_user_name' => $check_name, 'amount' => $total_amount, 'desc' => $desc, 'spbill_create_ip' => $this->IP);
$secrect_key = $this->SECRECT_KEY;
$data = array_filter($data);
ksort($data);
$str = '';
foreach ($data as $k => $v) {
$str .= $k . '=' . $v . '&';
}
$str .= 'key=' . $secrect_key;
$data['sign'] = md5($str);
$xml = $this->arraytoxml($data);
$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
$res = $this->curl($xml, $url);
$return = $this->xmltoarray($res);
if($return['result_code'] == 'SUCCESS'){
log2txt('企业付款到openid:'.$re_openid.' 成功');
} else {
log2txt('企业付款到openid:'.$re_openid.' 失败');
}
log2txt($return);
$res = $return['result_code'];
return $res;
}
}