1, 'msg' => "手机号 不能为空"];// 商户私钥文件路径。// if (empty($post['phone'])||empty($post['code'])) return jsonFail("请求参数错误");_微信支付 op">
当前位置:   article > 正文

微信支付需要验证微信身份部分openid,token_微信支付 openid

微信支付 openid
  1. <?php
  2. namespace app\api\controller;
  3. use alisms\SendSms;
  4. use app\index\controller\Api;
  5. use think\Controller;
  6. use think\Db;
  7. use think\Request;
  8. //use wxappletpay\Pay;
  9. use think\Exception;
  10. use GuzzleHttp\Exception\RequestException;
  11. use WechatPay\GuzzleMiddleware\WechatPayMiddleware;
  12. use WeChatPay\Util\PemUtil;
  13. //use GuzzleHttp\HandlerStack;
  14. use GuzzleHttp\HandlerStack;
  15. //use WechatPay\GuzzleMiddleware\Util\AesUtil;
  16. class WxApplet extends Controller
  17. {
  18. public function __construct()
  19. {
  20. // $result=$this->verifyPermissions();
  21. // if (!$result){
  22. // die(json_encode(['code'=>40010,'errmsg'=>'权限不足']));
  23. // }
  24. }
  25. private function verifyPermissions()
  26. {
  27. $token = Request::instance()->header('token');
  28. return redis()->exists($token);
  29. }
  30. public function cardList(){
  31. $cardList=db('cardinfo')->select();
  32. return jsonSuccess($cardList);
  33. }
  34. //简单的调用
  35. public function sendSms(Request $request)
  36. {
  37. //判断是否ajax提交
  38. if($request->isPost())
  39. {
  40. //获取mobile参数
  41. $mobile = $request->param('phone');
  42. if(!preg_match("/^1[3456789]\d{9}$/", $mobile)){
  43. return jsonFail("手机号不正确");
  44. }
  45. //新建code
  46. $templateParam = array('code' => rand(10000,99999));
  47. //保存在session进行验证
  48. session('sms_code',['mobile'=>$mobile,'code'=>$templateParam['code']]);
  49. //实例化类
  50. $send = new SendSms();
  51. //调用类里面send方法 send()方法需要俩个参数;
  52. //发送短信~
  53. $result = $send->send($mobile,$templateParam,config('AliSms.payTemplateCode'));
  54. //是否成功
  55. if($result)
  56. {
  57. //返回
  58. return jsonSuccess('','发送成功!');
  59. }else{
  60. return jsonFail("发送短信失败");
  61. }
  62. }
  63. }
  64. public function verifyFaces(){
  65. $api = new Api();
  66. header("Access-Control-Allow-Origin: *");
  67. if (request()->isPost()) {
  68. //1。上传图片方式
  69. $farr = $api->upload("faces");
  70. $img_dir = ROOT_PATH . 'public' . DS . 'uploads' . DS . $farr['save'];
  71. $img_base64 = $api->imgToBase64($img_dir);
  72. $param['image'] = substr($img_base64, 22);
  73. //2。base64字符串形式
  74. // $img_base64 = input("post.faces");
  75. // $param['image'] = $img_base64;
  76. // return jsonSuccess(['picPath'=>DS . 'uploads' . DS . $farr['save']],"通过");
  77. // $membernum = input("post.membernum")?input("post.membernum"):1;
  78. $membernum=1;
  79. $url = "https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=" . $api->getbdtoken();
  80. $param['image_type'] = "BASE64";
  81. $param['face_type'] = "LIVE";
  82. $param['face_field'] = "eye_status,quality,glasses,angle,beauty,expression,mask";
  83. $param['max_face_num'] = 10;
  84. // var_dump($param['image']);
  85. // exit();
  86. $o = "";
  87. foreach ($param as $k => $v) {
  88. $o .= "$k=" . urlencode($v) . "&";
  89. }
  90. $post_data = substr($o, 0, -1);
  91. // var_dump($post_data);
  92. // exit();
  93. $res = $api->request_post($url, $post_data);
  94. $res = json_decode($res);
  95. if ($res->error_code === 0) {
  96. if($res->result->face_num!=$membernum){
  97. return jsonFail("人脸数量不一致");
  98. }
  99. $result = $api->face_pic_check($res);
  100. $picPath = DS . 'uploads' . DS . $farr['save'];
  101. if ($result['error_code']===0){
  102. return jsonSuccess($picPath);
  103. }else{
  104. return jsonFail($result['error_msg']);
  105. }
  106. } else {
  107. return jsonFail($res->error_msg,'',$res->error_code);
  108. }
  109. // return json_encode($arr, JSON_UNESCAPED_UNICODE);
  110. }else{
  111. return jsonFail("非法请求");
  112. }
  113. }
  114. public function uploadTmpPic()
  115. {
  116. // 获取表单上传文件 例如上传了001.jpg
  117. $file = request()->file("faces");
  118. // 移动到框架应用根目录/public/uploads/ 目录下
  119. $dirPath = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'tmp';
  120. if (!is_dir($dirPath)){
  121. mkdir(iconv("UTF-8", "GBK", $dirPath),0777,true);
  122. }
  123. $info = $file->move($dirPath);
  124. // $info = $file->move(ROOT_PATH . 'uploads');
  125. if ($info) {
  126. // 成功上传后 获取上传信息
  127. // 输出 jpg
  128. $upload['houzhui'] = $info->getExtension();
  129. // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
  130. $upload['save'] = $info->getSaveName();
  131. // 输出 42a79759f284b767dfcb2a0197904287.jpg
  132. $upload['name'] = $info->getFilename();
  133. } else {
  134. // 上传失败获取错误信息
  135. $upload['error'] = $file->getError();
  136. }
  137. return jsonSuccess($upload);
  138. }
  139. public function verifyCode(){
  140. $post = input('post.');
  141. $sms_code = session("sms_code");
  142. // if (empty($post['phone'])||empty($post['code'])) return jsonFail("请求参数错误");
  143. // if (empty($sms_code)) return jsonFail("session不存在");
  144. // trace(request()->url().":".json_encode($sms_code), 'api');
  145. // if($sms_code['mobile']!=$post['phone']) return jsonFail("手机号不正确");
  146. // if($sms_code['code']!=$post['code']) return jsonFail("验证码不正确");
  147. return jsonSuccess();
  148. }
  149. public function addCard(){
  150. $post = input('post.');
  151. trace(request()->url().":".json_encode($post), 'api');
  152. if (!is_array($post['username'])) return jsonFail('username必须是Array');
  153. if (!is_array($post['phone'])) return jsonFail('phone必须是Array');
  154. if (!is_array($post['shenfenz'])) return jsonFail('shenfenz必须是Array');
  155. if (!is_array($post['photo'])) return jsonFail('photo必须是Array');
  156. if(empty($post['cardid'])||empty($post['starttime'])||empty($post['endtime'])){
  157. return json(['code' => 1, "msg" => "cardid,starttime,endtime 不能为空"]);
  158. }
  159. // return false;
  160. $data['cardid'] = $post['cardid'];
  161. $data['startdate'] = $post['starttime'];
  162. $data['enddate'] = $post['endtime'];
  163. $data['pay_way'] = isset($post['pay_way'])?$post['pay_way']:2;
  164. $data['pay_fee'] = db('cardinfo')->where('cardid',$post['cardid'])->value("cardamt");
  165. $data['card_type']=$post['card_type'];
  166. $data['photo']=null;
  167. $data['client'] = input("post.openid");
  168. $ret=Db::transaction(function () use ($post, $data) {
  169. try {
  170. $getid = db('clubinfo')->insertGetId($data);
  171. $push_data=db('clubinfo')->where('cardno', $getid)->find();
  172. for ($i = 0; $i < count($post['photo']); $i++) {
  173. if (empty($post['photo'][$i])||empty($post['username'][$i])){
  174. return ['code' => 1, 'msg' => "photo,username 不能为空"];
  175. }
  176. if ($i===0){
  177. if ($post['cardid']!=1&&$post['cardid']!=5){
  178. if (empty($post['shenfenz'][$i])) return ['code' => 1, 'msg' => "身份证号 不能为空"];
  179. }
  180. if (empty($post['phone'][$i])) return ['code' => 1, 'msg' => "手机号 不能为空"];
  181. }
  182. if (!empty($post['shenfenz'][$i])){
  183. if(preg_match("/([\x81-\xfe][\x40-\xfe])/", $post['shenfenz'][$i], $match)){
  184. return ['code' => 1, 'msg' => "身份证号不能有中文"];
  185. }
  186. }
  187. if (!empty($post['phone'][$i])){
  188. if(!is_numeric($post['phone'][$i])){
  189. return ['code' => 1, 'msg' => "手机号必须是数字"];
  190. }
  191. }
  192. $data2 = [];
  193. $data2['cardno'] = $getid;
  194. $data2['membername'] = $post['username'][$i];
  195. $data2['phone'] = isset($post['phone'][$i])?$post['phone'][$i]:"";
  196. $data2['idnumber'] = isset($post['shenfenz'][$i])?$post['shenfenz'][$i]:"";
  197. $data2['memberbirthday'] = empty($data2['idnumber']) ? "" : substr($data2['idnumber'], 6, 8);
  198. $data2['photo'] = $post['photo'][$i];
  199. $data2['client'] = $data['client'];
  200. $data2['issub'] = ($i === 0) ? 0 : 1;
  201. $memberid=db('memberinfo')->insertGetId($data2);
  202. $data3=db('memberinfo')->where('id', $memberid)->find();
  203. $push_data['member'][$i] = $data3;
  204. }
  205. return ['code'=>0,"msg"=>"添加成功",'data'=>['cardno'=>$getid]];
  206. } catch (Exception $e) {
  207. trace(request()->url().":".$e->getMessage(), 'api');
  208. return ['code'=>1,"msg"=>"添加失败"];
  209. }
  210. });
  211. if (isset($ret['data']['cardno'])){
  212. $data10['cardno'] = $ret['data']['cardno'];
  213. $clubInfo=db('clubinfo')->where('cardno', $data10['cardno'])->find();
  214. $data10['pay_fee'] = $clubInfo['pay_fee'];
  215. $data10['client'] = $data['client'];
  216. $data10['order_no'] = 'wx' . time() . $data10['cardno'] ;
  217. $resultId=db('order')->insertGetId($data10);
  218. if ($resultId) {
  219. return jsonSuccess($resultId);
  220. } else {
  221. return jsonFail("添加支付数据失败");
  222. }
  223. } else {
  224. return jsonFail("添加年卡数据失败,err:".json_encode($ret));
  225. }
  226. }
  227. public function getCardInfo(){
  228. $cardNo = input('post.cardno');
  229. if (!empty($cardNo)){
  230. $cardInfo=db('clubinfo')->where('cardno', $cardNo)->find();
  231. if ($cardInfo){
  232. return jsonSuccess($cardInfo);
  233. }else{
  234. return jsonFail();
  235. }
  236. }else{
  237. return jsonFail();
  238. }
  239. }
  240. protected function getsign($data, $appid ,$keypath){
  241. // $content = $appid . '\n'. $data['timeStamp'] . '\n'. $data['nonceStr'] . '\n'. $data['package'] . '\n';
  242. // var_dump($content);
  243. $key = openssl_get_privatekey($keypath);
  244. // $key = openssl_pkey_get_private($keypath);
  245. openssl_sign($content, $signature, $key, "SHA256");
  246. openssl_sign($content, $signature, $key, 'sha256WithRSAEncryption');
  247. openssl_sign($content, $signature, $key, OPENSSL_ALGO_SHA256);
  248. // openssl_sign($content, $signature, $key);
  249. // openssl_free_key($key);
  250. // $sign = base64_encode($signature);
  251. // return $sign;
  252. $str = "{$appid}\n{$data['timeStamp']}\n{$data['nonceStr']}\n{$data['package']}\n";
  253. $privateKey = PemUtil::loadPrivateKey($keypath); // $this->private_key
  254. openssl_sign($str, $encrypt_data, openssl_pkey_get_private($privateKey), 'sha256WithRSAEncryption');
  255. $encrypt_data = base64_encode($encrypt_data);
  256. return $encrypt_data;
  257. }
  258. public function pay()
  259. {
  260. $openid = input('post.openid'); //opendi可以自己重新获取也可以存起来 必须要!!!
  261. $orderid = input('post.orderid'); //这里是订单的id 根据自己需要来
  262. // echo date('Y-m-d\TH:i:sP');
  263. // echo date('c'); die();
  264. //查询里面的价格
  265. $orderdata = db('order')->where('id',$orderid)->field('order_no,pay_fee')->find();
  266. if (is_null($orderdata) ){
  267. $orderdata = db('order')->where('order_no',$orderid)->field('order_no,pay_fee')->find();
  268. }
  269. //引入支付的类 第二个代码块
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/266837
    推荐阅读
    相关标签