| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- /**
- * 微信支付服务器端下单
- * 微信APP支付文档地址: https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_6
- * 使用示例
- * 构造方法参数
- * 'appid' => //填写微信分配的公众账号ID
- * 'mch_id' => //填写微信支付分配的商户号
- * 'notify_url'=> //填写微信支付结果回调地址
- * 'key' => //填写微信商户支付密钥
- * );
- * 统一下单方法
- * $WechatAppPay = new wechatAppPay($options);
- * $params['body'] = '商品描述'; //商品描述
- * $params['out_trade_no'] = '1217752501201407'; //自定义的订单号,不能重复
- * $params['total_fee'] = '100'; //订单金额 只能为整数 单位为分
- * $params['trade_type'] = 'APP'; //交易类型 JSAPI | NATIVE |APP | WAP
- * $wechatAppPay->unifiedOrder( $params );
- */
- class weixin
- {
- private $appid;
- private $seceret;
- public function __construct($appid, $seceret)
- {
- $this->appid = $appid;
- $this->seceret = $seceret;
- }
- /**
- * code获取openid
- * @param $code
- * @return mixed
- */
- public function code2Session($code)
- {
- $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->appid}&secret={$this->seceret}&js_code={$code}&grant_type=authorization_code";
- $res = Curl::get($url,true);
- return json_decode($res,true);
- }
- public function getAccessToken()
- {
- $rs = Baseconfig::getInfoByKey(TOKEN_BASE_KEY);
- if (empty($rs) || (time() - $rs["lastupdate"]) > TOKEN_LIMIT_TIME) {
- $accessToken = self::refreshToken();
- } else {
- $accessToken = $rs["value"];
- }
- return $accessToken;
- }
- public function refreshToken()
- {
- $token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->seceret;
- $res = file_get_contents($token_access_url); //
- $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量s
- $access_token = $result['access_token'];
- $rs = Baseconfig::getInfoByKey(TOKEN_BASE_KEY);
- if ($rs) {
- $res = Baseconfig::updateByKey(TOKEN_BASE_KEY, array("value" => $access_token, "lastupdate" => time()));
- } else {
- $attrs = array(
- "key"=>TOKEN_BASE_KEY,
- "value"=>$access_token,
- "name"=>"token"
- );
- $res = Baseconfig::add($attrs);
- }
- if ($res) {
- return $access_token;
- }
- }
- public function createBase64($path,$scene)
- {
- $accessToken = $this->getAccessToken();
- $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";
- // if(!file_exists(FILE_PATH.$destPath))
- // {
- // mkdir(FILE_PATH.$destPath,0777,true);
- // }
- // $fileName = date("YmdHis").randcode(4).".jpg";
- $resData = Curl::post($url,json_encode(array("page"=>$path,"scene"=>$scene)));
- return Image::BinaryToBase64($resData);
- // file_put_contents(FILE_PATH.$destPath.$fileName,$resData);
- }
- public function createQrCode($path,$scene,$destPath)
- {
- $accessToken = $this->getAccessToken();
- $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";
- if(!file_exists(FILE_PATH.$destPath))
- {
- mkdir(FILE_PATH.$destPath,0777,true);
- }
- $fileName = date("YmdHis").randcode(4).".jpg";
- $resData = Curl::post($url,json_encode(array("page"=>$path,"scene"=>$scene)));
- file_put_contents(FILE_PATH.$destPath.$fileName,$resData);
- return $destPath.$fileName;
- }
- public function sendBaomingInform($openId,$formId,$page,$name,$title){
- $accessToken = $this->getAccessToken();
- $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={$accessToken}";
- $time = date("Y-m-d H:i:s");
- $data = '{
- "touser": "'.$openId.'",
- "template_id": "'.BAOMING_TEMPLATE_ID.'",
- "page": "'.$page.'",
- "form_id": "'.$formId.'",
- "data": {
- "keyword1": {
- "value": "'.$name.'"
- },
- "keyword2": {
- "value": "'.$title.'"
- },
- "keyword3": {
- "value": "'.$time.'"
- }
- }
- }';
- $rs = Curl::post($url,$data);
- }
- public function sendSignSuccess($openId,$formId,$page,$name,$title){
- $accessToken = $this->getAccessToken();
- $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={$accessToken}";
- $time = date("Y-m-d H:i:s");
- $data = '{
- "touser": "'.$openId.'",
- "template_id": "'.SIGN_SUCCESS_TEMPLATE_ID.'",
- "page": "'.$page.'",
- "form_id": "'.$formId.'",
- "data": {
- "keyword1": {
- "value": "'.$name.'"
- },
- "keyword2": {
- "value": "'.$time.'"
- },
- "keyword3": {
- "value": "'.$title.'"
- }
- }
- }';
- $rs = Curl::post($url,$data);
- }
- /***发送动开始通知 订阅消息***/
- public function sendBeginInform($openId,$page,$auth,$title,$time,$address){
- $accessToken = $this->getAccessToken();
- $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$accessToken}";
- $data = '{
- "touser": "'.$openId.'",
- "template_id": "'.BAOMING_BEGIN_INFORM_TEMPLATE_ID.'",
- "page": "'.$page.'",
- "data": {
- "name1": {
- "value": "'.$auth.'"
- },
- "thing4": {
- "value": "'.$title.'"
- },
- "date5": {
- "value": "'.$time.'"
- },
- "thing6": {
- "value": "'.$address.'"
- }
- }
- }';
- $rs = Curl::post($url,$data);
- }
- /***发送奖品通知 订阅消息***/
- public function sendAwardInform($openId,$page,$auth,$title,$phone,$awardName){
- $accessToken = $this->getAccessToken();
- $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$accessToken}";
- $data = '{
- "touser": "'.$openId.'",
- "template_id": "'.BAOMING_AWARD_INFORM_TEMPLATE_ID.'",
- "page": "'.$page.'",
- "data": {
- "thing1": {
- "value": "'.$title.'"
- },
- "thing2": {
- "value": "'.$awardName.'"
- },
- "phone_number3": {
- "value": "'.$phone.'"
- },
- "name4": {
- "value": "'.$auth.'"
- }
- }
- }';
- $rs = Curl::post($url,$data);
- }
- }
|