赞
踩
if ($pay_name=='微信支付' || $pay_name=='支付宝支付'){ // H5 支付流程 $type = $pay_name == '微信支付' ? "wechat" : 'alipay'; $order_no = date('YmdHis') . str_replace('.', '', microtime(true)); $param = [ 'amount'=>$params['number'], 'orderid'=>$order_no, 'type'=> $type, 'title'=>"充值申请", 'notifyurl'=>"http://域名.com/api/money_charge/recharge_notify", 'returnurl'=>"http://域名/api/money_charge/recharge_return?type={$type}&out_trade_no=".$order_no, 'method'=>"wap", ]; $charge_data = [ 'user_id' => $user['id'], 'order_no' => $order_no, 'number' => $params['number'], 'charge_ways_id' => $params['charge_id'], 'status'=>4, 'trade_no' => '', 'create_time' =>time(), ]; Recharge::create($charge_data); /*if(is_weixin()) { $param['method'] = 'mp'; $wechat = new Wechat('wx4bf09d3c532f7a0f', '2f2665b6301f1c2f680ae2b3c1671557'); $openid = $wechat->getOpenid(); $param['openid'] = 'o51lq0rhR3fRfRzCknCdUkNkDN30'; }*/ if(is_weixin()) { $wechat = new Wechat('wx4b*******a0f', '2f2665b63*******3c1671557'); $pre_url = $wechat->getOpenid($order_no, 0); //dump($pre_url);die; return json(['code'=>110,'msg'=>'成功','data'=>['url'=>$pre_url,'order_no'=>$order_no]]); } $return_url = Service::submitOrder($param); if($pay_name=='微信支付') return json(['code'=>110,'msg'=>'成功','data'=>['url'=>$return_url,'order_no'=>$order_no]]); if($pay_name=='支付宝支付') return json(['code'=>120,'msg'=>'成功','data'=>$return_url,'order_no'=>$order_no]); }
Wechat.php中的代码
<?php namespace addons\epay\library; use fast\Http; use think\Cache; use think\Session; /** * 微信授权 * */ class Wechat { private $app_id = ''; private $app_secret = ''; private $scope = 'snsapi_userinfo'; public function __construct($app_id, $app_secret) { $this->app_id = $app_id; $this->app_secret = $app_secret; } /** * 获取微信授权链接 * * @return string */ public function getAuthorizeUrl($order_no, $money) { $redirect_uri = addon_url('epay/api/wechat', ['order_no'=>$order_no, 'money'=>$money], true, true); $redirect_uri = urlencode($redirect_uri); $state = \fast\Random::alnum(); Session::set('state', $state); return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope={$this->scope}&state={$state}#wechat_redirect"; } /** * 获取微信openid * * @return mixed|string */ public function getOpenid($order_no = '', $money=0) { $openid = Session::get('openid'); if (!$openid) { if (!isset($_GET['code'])) { $url = $this->getAuthorizeUrl($order_no, $money); // Header("Location:{$url}"); // exit(); return $url; } else { $code = $_GET['code']; $token = $this->getAccessToken($code); $openid = isset($token['openid']) ? $token['openid'] : ''; if ($openid) { Session::set("openid", $openid); } } } return $openid; } /** * 获取授权token网页授权 * * @param string $code * @return mixed|string */ public function getAccessToken($code = '') { $params = [ 'appid' => $this->app_id, 'secret' => $this->app_secret, 'code' => $code, 'grant_type' => 'authorization_code' ]; $ret = Http::sendRequest('https://api.weixin.qq.com/sns/oauth2/access_token', $params, 'GET'); if ($ret['ret']) { $ar = json_decode($ret['msg'], true); return $ar; } return []; } public function getJsticket() { $jsticket = Session::get('jsticket'); if (!$jsticket) { $token = $this->getAccessToken($code); $params = [ 'access_token' => 'token', 'type' => 'jsapi', ]; $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/ticket/getticket', $params, 'GET'); if ($ret['ret']) { $ar = json_decode($ret['msg'], true); return $ar; } } return $jsticket; } } commin.php中的代码(公共方法) ```php function is_weixin(){ if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) { //strpos() 函数查找字符串在另一字符串中第一次出现的位置。 return true; } return false; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。