Files
yuyin-php/application/api/controller/Payapp.php

303 lines
9.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\api\controller;
use think\Db;
use think\Controller;
use app\api\wxapi\AppPayWx;
use app\api\wxapi\pay\WxPayApi;
use app\api\wxapi\pay\JsApiPay;
use app\api\wxapi\pay\WxPayUnifiedOrder;
use app\api\wxapi\pay\WxPayConfig;
use app\api\wxapi\pay\WxPayNotify;
class Payapp extends Controller
{
public function checkuser(){
// $this->AssignJson(3001,'系统维护!');
$user_id = request()->get('user_id');
$userInfo=DB::name('user')->where('uid|user_name', $user_id)->field('uid,nick_name')->order('uid desc')->find();
if(!isset($userInfo['uid'])){
$this->AssignJson(3001,'充值对象不存在,请确认!');
}
$userInfo['nickname']=urldecode($userInfo['nick_name']);
$this->AssignJson(200, '请求成功', $userInfo);
}
public function wxjsapi() {
$user_id = request()->post('user_id', 0);
if($user_id<>6012){
//exit('升级中');
}
$map[] = ['is_delete', '=', 1];
$item = Db::name('can_recharge')->field('crid,money,integral')->where($map)->select();
// $_item=DB::name('goods')->where('id', ">", 0)->order("price asc")->select();
// $item=[];
// foreach($_item as $val){
// $item[$val['price']]=$val['mizuan'];
// }
if(request()->isPost()){
// $this->AssignJson(3001,'系统维护!');
$pay_type = request()->post('pay_type', 10); //10微信公众号-11支付宝H5
$user_id = request()->post('user_id');
$user_id_has = request()->post('user_id_has');
if($user_id_has>0){
$user_id=$user_id_has;
}
if(empty($user_id)){
exit('充值对象不存在,请确认!');
$this->AssignJson(3001,'充值对象不存在,请确认!');
}
$userInfo=DB::name('user')->where('uid|user_name', $user_id)->field('uid,nick_name,is_real,card_id')->order('uid desc')->find();
if(!isset($userInfo['uid'])){
exit('充值对象不存在,请确认!');
$this->AssignJson(3001,'充值对象不存在,请确认!');
}
if($userInfo['is_real'] != 1){
exit('请先实名认证!');
$this->AssignJson(3001,'请先实名认证!');
}
$age = getAgeId($userInfo['card_id']);
if(!$age){
exit('该身份证号未满18岁');
$this->AssignJson(3001,'该身份证号未满18岁');
}
$money = request()->post('money');
if($money < 1){
exit('充值金额必须大于1');
$this->AssignJson(3001,'充值金额必须大于1');
}
if($money != ceil($money)){
exit('充值金额必须为整数!');
$this->AssignJson(3001,'充值金额必须为整数!');
}
// $money = (int)$money;
$map = [];
$map[] = ['money','=',$money];
$map[] = ['is_delete', '=', 1];
$money_info = db::name('can_recharge')->where($map)->find();
// dump($money_info);exit;
if(!isset($money_info)){
$money = (int)$money;
$money_info['integral'] = $money * 10;
// exit('充值金额不存在,请确认!');
// $this->AssignJson(3002,'充值金额不存在,请确认!');
}
//
if($pay_type == 10){
// exit('暂未开放!');
// $this->AssignJson(3003, '暂未开放!');
}
$pre='wx';
if($pay_type==11){
$pre='alipay';
}
$data = [];
$data['order_sn'] = $pre.$user_id.date("YmdHis") . rand(1000, 9999);
$data['money'] = $money;
$data['integral'] = $money_info['integral'];
$data['pay_type'] = 6;
$data['uid'] = $user_id;
$data['pay_status'] = 1;
$data['pay_time'] = 0;
$data['remarke'] = '';
$data['add_time'] = time();
$data['update_time'] = time();
$status = Db::name('user_recharge')->insertGetId($data);
// $data = [];
// if ($status) {
// if ($pay_type == 1) {
// $data = $this->alipayHand($order_sn, $pay_money);
// } else {
// $data = $this->wxpayHand($order_sn, $pay_money);
// }
// }
// $mizuan= $money_info['integral'];
// /*$money=0.01;
// $mizuan=1;*/
// $arr['order_no'] =
// $arr['user_id'] = $user_id;
// $arr['price'] = $money;
// $arr['mizuan'] = $mizuan;
// $arr['pay_type'] = $pay_type;
// $arr['remark'] = '';
// $arr['addtime'] = time();
// $res = DB::name('order')->insertGetId($arr);
if ($status) {
if($pay_type==11){
header('location:/api/Payapp/alipayh5?user_id='.$user_id.'&id='.$status);
exit;
}
header('location:/api/Payapp/jsapi?user_id='.$user_id.'&id='.$status);
exit;
}
exit('请求失败!');
$this->AssignJson(3003, '请求失败!');
}
$this -> assign('item', $item);
return $this -> fetch();
}
public function alipayh5(){
$id = request()->get('id');
$user_id = request()->get('user_id');
$order=DB::name('user_recharge')->where("uid", $user_id)->where("rid", $id)->find();
if(!isset($order['rid'])){
exit("传参异常");
}
$order_sn=$order['order_sn'];
$money=$order['money'];
/*$order_sn=time();
$money=0.01;*/
model('UserRecharge')->payh5alipay($order_sn, $money);
}
public function jsapi(){
$id = request()->get('id');
$user_id = request()->get('user_id');
$order=DB::name('user_recharge')->where("uid", $user_id)->where("rid", $id)->find();
if(!isset($order['rid'])){
exit("传参异常");
}
$order_sn=$order['order_sn'];
$money=$order['money'];
$notify_url='http://'.$_SERVER['HTTP_HOST']."/api/Notifyapp/wxnotify";// /api/wxnotify /api/pay_notify/wx_pay_notify
$title='订单支付';
try{
$tools = new JsApiPay();
$openId = $tools->GetOpenid();
/* print_r($openId);
exit;*/
//②、统一下单
$input = new WxPayUnifiedOrder();
$input->SetBody($title);
$input->SetAttach($title);
$input->SetOut_trade_no($order_sn);
$input->SetTotal_fee($money*100);
$input->SetTime_start(date("YmdHis"));
//$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag($title);
$input->SetNotify_url($notify_url);
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$config = new WxPayConfig();
$order = WxPayApi::unifiedOrder($config, $input);
//echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
//$this->printf_info($order);
/*echo '<pre>';
print_r($order);*/
$PayConf['jsApiParameters'] = $tools->GetJsApiParameters($order);
//获取共享收货地址js函数参数
$PayConf['editAddress'] = $tools->GetEditAddressParameters();
$this -> assign('PayConf', $PayConf);
return $this -> fetch();
} catch(Exception $e) {
echo $e->getMessage();
}
}
protected function paywxapp($order_sn, $money, $notify_url='', $title='订单支付'){
//②、统一下单
$input = new WxPayUnifiedOrder();
$input->SetBody($title);
$input->SetOut_trade_no($order_sn);
$input->SetTotal_fee($money*100);
$input->SetNotify_url($notify_url);
$input->SetTrade_type("APP");
$config = new WxPayConfig();
$order = WxPayApi::unifiedOrder($config, $input);
//echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
//echo json_encode($order);
if($order['return_code'] =='SUCCESS'){
$arr =array(
'appid' => $config->GetAppId(),
'partnerid' => $config->GetMerchantId(),
'prepayid' =>$order['prepay_id'],
'package' => 'Sign=WXPay',
'noncestr' => $order['nonce_str'],//WxPayApi::getNonceStr(),
'timestamp' =>time(),
);
//第二次生成签名
$sign = $this->getSign($arr, $config);
$arr['sign'] = $sign;
return json_encode($arr);
}
return false;
}
private function getSign($params, $config) {
ksort($params); //将参数数组按照参数名ASCII码从小到大排序
foreach ($params as $key => $item) {
if (!empty($item)) { //剔除参数值为空的参数
$newArr[] = $key.'='.$item; // 整合新的参数数组
}
}
$stringA = implode("&", $newArr); //使用 & 符号连接参数
$stringSignTemp = $stringA."&key=".$config->GetKey(); //拼接key
if($config->GetSignType() == "MD5"){
$stringSignTemp = md5($stringSignTemp);
} else if($config->GetSignType() == "HMAC-SHA256") {
$stringSignTemp = hash_hmac("sha256",$stringSignTemp ,$config->GetKey());
} else {
throw new WxPayException("签名类型不支持!");
}
$sign = strtoupper($stringSignTemp); //将所有字符转换为大写
return $sign;
}
public function AssignJson($code='',$value='成功!',$result=[]) {
$reqs['code']=$code;
if(is_string($value)){
$reqs['message']=$value;
}else if(is_array($value)){
if(isset($value['message'])){
$reqs['message']=$value['message'];
unset($value['message']);
}else{
$reqs['message']='请求成功!';
}
$reqs=array_merge($reqs,$value);
}
if(!isset($value['result'])){
$reqs['result']=$result;
}
echo json_encode($reqs);
exit;
}
}