123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- use addons\epay\library\Service;
- use app\common\model\Wallet;
- use think\Exception;
- /**
- * 苏宁支付h5拉起
- */
- class Supay extends Api
- {
- protected $noNeedLogin = ['testpay','notify'];
- protected $noNeedRight = ['*'];
- public function vip_recharge()
- {
- Db::startTrans();
- try {
- $rc_id = input('rc_id',0);
- $pay_type = input('pay_type','wechat');
- $uid = $this->auth->id;
- if(!$rc_id){
- throw new Exception('请选择会员套餐');
- }
- //赋值money
- $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
- $money = $recharge_config['money'];
- if($money <= 0) {
- throw new Exception('支付金额必须大于0');
- }
- if($money > 10000){
- throw new Exception('支付金额太大');
- }
- //会员等级冲突
- //当前是会员,但是却要向下级续费,直接提示报错
- //修改等级,向上立刻改,向下不允许
- $wallet_info = model('wallet')->getWallet($this->auth->id);
- if($wallet_info['vip_endtime'] > time() && $recharge_config['vip_level'] < $wallet_info['vip_level']){
- throw new Exception('当前会员没有过期,不能续费');
- }
- //创建订单
- $data = [];
- $data['status'] = 0;
- $pay_no = createUniqueNo('V',$uid);
- $data['pay_no'] = $pay_no;
- $data['money'] = $money;
- $data['payment_class'] = $pay_type;
- $data['user_id'] = $uid;
- $data['ext_info'] = json_encode(['subject'=>'充值vip支付']);
- $data['memo'] = '充值会员支付';
- $data['createtime'] = time();
- //$data['payment'] = 'miniapp';
- $data['payment'] = 'app';
- $orderid = Db::name('pay_order')->insertGetId($data);
- //创建回调
- $even_data = [];
- $even_data['event'] = 'success';
- $even_data['class'] = 'app\common\model\Recharge';
- $even_data['method'] = 'vippaysucc';
- $even_data['args'] = json_encode(['user_id'=>$uid,'days'=>$recharge_config['days'],'vip_level'=>$recharge_config['vip_level'],'gold_num'=>$recharge_config['gold_num'],'money'=>$money]);
- $even_data['pay_no'] = $pay_no;
- Db::name('pay_event')->insertGetId($even_data);
- $res[] = '';
- if ($pay_type == 'wechat') {
- //$money = 0.01;
- $sandpay = new \app\common\library\SuningPay();
- $sandpayParams = [
- 'order_no' => $pay_no,
- 'goods_name' => '充值vip支付',
- 'money' => $money,
- 'type' => 'vip',
- ];
- $sandRes = $sandpay->wechath5($sandpayParams);
- if ($sandRes['status'] != 1) {
- throw new Exception('失败');
- }
-
- $res = json_decode($sandRes['data']['params'],true);
- $url = 'https://h5-min-pay-1gczed24bbbe3db8-1317709175.tcloudbaseapp.com/suning-pay.html';
- $ress['url'] = $url.'?orderId='.$res['tradeOrderId'];
-
- } else {
- //下单
- $params = [
- 'type' => $pay_type,
- 'orderid' => $pay_no,
- 'title' => $data['memo'],
- 'amount' => $data['money'],
- //'amount' => 0.01,
- 'method' => 'app',
- 'notifyurl' => 'http://app.zhiyinvip001.com/notify.php/paytype/'.$pay_type,
- 'returnurl' => '',
- ];
- $res = Service::submitOrder($params);
- }
- Db::commit();
- $this->success('success',$ress);
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- }
- //充值金币 创建订单
- public function gold_recharge()
- {
- Db::startTrans();
- try {
- $rc_id = input_post('rc_id',0);
- $pay_type = 'wechat';
- $freemoney = input_post('freemoney', 0, 'intval');
- $uid = $this->auth->id;
- if(!$rc_id && !$freemoney){
- throw new Exception('请选择或填写充值金额');
- }
- if (!in_array($pay_type,['wechat','alipay'])) {
- throw new Exception('错误的支付类型');
- }
- //赋值money
- if($rc_id){
- $recharge_config = Db::name('paygold_config')->where('id',$rc_id)->find();
- $money = $recharge_config ? $recharge_config['money']: 0;
- $gold = $recharge_config ? $recharge_config['gold'] : 0;
- $first_gold = $recharge_config ? $recharge_config['first_gold'] : 0;
- $first_vipdays = $recharge_config ? $recharge_config['first_vipdays'] : 0;
- $vip_gold = $recharge_config ? $recharge_config['vip_gold'] : 0;
- }
- //自由输入覆盖
- if(!empty($freemoney)){
- $rc_id = 0;
- $money = floatval($freemoney);
- $bili = config('site.money_to_gold') ?: 10;
- $gold = bcmul($money,$bili,0);
- $first_gold = 0;
- $first_vipdays = 0;
- $vip_gold = 0;
- }
- if($money <= 0) {
- throw new Exception('支付金额必须大于0');
- }
- if($money > 10000){
- throw new Exception('支付金额太大');
- }
- //查询是不是会员,若不是则不赠送金币
- $vip_endtime = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('vip_endtime');
- if ($vip_endtime < time()) {
- $vip_gold = 0;
- }
- //创建订单
- $data = [];
- $data['status'] = 0;
- $pay_no = createUniqueNo('P',$uid);
- $data['pay_no'] = $pay_no;
- $data['money'] = $money;
- $data['payment_class'] = $pay_type;
- $data['user_id'] = $uid;
- $data['ext_info'] = json_encode(['subject'=>'充值金币支付']);
- $data['memo'] = '充值金币支付';
- $data['createtime'] = time();
- $data['payment'] = 'app';
- $orderid = Db::name('pay_order')->insertGetId($data);
- //创建回调
- $even_data = [];
- $even_data['event'] = 'success';
- $even_data['class'] = 'app\common\model\Recharge';
- $even_data['method'] = 'goldpaysucc';
- $even_data['args'] = json_encode(['user_id'=>$uid,'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id,'first_gold'=>$first_gold,'first_vipdays'=>$first_vipdays, 'intro_uid' => $this->auth->intro_uid, 'vip_gold' => $vip_gold]);
- $even_data['pay_no'] = $pay_no;
- Db::name('pay_event')->insertGetId($even_data);
- //$money = 0.01;
- $suningpay = new \app\common\library\SuningPay();
- $suningpayParams = [
- 'order_no' => $pay_no,
- 'goods_name' => '充值金币支付',
- 'money' => $money,
- 'type' => 'gold',
- ];
- $suningRes = $suningpay->wechath5($suningpayParams);
- if ($suningRes['status'] != 1) {
- throw new Exception('失败');
- }
- $res = json_decode($suningRes['data']['params'],true);
- $url = 'https://h5-min-pay-1gczed24bbbe3db8-1317709175.tcloudbaseapp.com/suning-pay.html';
- $ress['url'] = $url.'?orderId='.$res['tradeOrderId'];
- Db::commit();
- $this->success('成功', $ress);
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- }
-
- /**
- * 支付成功
- */
- public function notify()
- {
- //$input_post_data = json_encode($_POST);
- //filePut('[wallet][paySucc]充值参数 recharge money post'.$input_post_data);
- //$postData = isset($_POST['data']) ? $_POST['data'] : [];
- //$sign = isset($_POST['sign']) ? $_POST['sign'] : '';
- $postData=file_get_contents('php://input');
- $postData = urldecode($postData);
- $arr = explode('&', $postData);
- $nowArr = array();
- foreach($arr as $v) {
- $kv = explode('=', $v);
- $nowArr[$kv[0]] = $kv[1];
- }
-
- if($nowArr['responseCode'] == 'SJ0000' && $nowArr['transactStatus'] == '00')
- {
-
-
-
- $data = $this->verification($postData);
- if($data == 1)
- { //验签名通过
-
- //查询订单信息
- $PayResult = Db::name('pay_order');
- $where['pay_no'] = $nowArr['orderId'];
- $order_info = $PayResult->where($where)->find();
- if(!$order_info)
- {
- filePut('[wallet][paySucc]充值入账更新余额失败 recharge money fail 订单数据库检索不到'.$nowArr['orderId'],'suningno.txt');
- exit;
- }
- if ($order_info['status'] == 1 || $order_info['status'] == 2)
- {
- filePut('[wallet][paySucc]充值入账更新余额失败 recharge money fail status已更新过'.$order_info['status'].'___'.$nowArr['orderId'],'suningno.txt');
- echo 'true';exit;
- }
- $orderCode = $nowArr['orderId'];
- $extendType = '';
- //区分vip 还是 冲金币
- $is_g = stripos($orderCode,'P');
- if($is_g !== false)
- {
- $extendType = 'gold';
- }else{
- $is_v = stripos($orderCode,'V');
- if($is_v !== false)
- {
- $extendType = 'vip';
- }
- }
-
- //status已更新过
- $_data['status'] = 2;
- $result = $PayResult->where($where)->setField($_data);
- if ($result || 1)
- {
- //你可以在此编写订单逻辑
- $payEventWhere['pay_no'] = $orderCode;
- $payEvent = Db::name('pay_event')->where($payEventWhere)->find();
- $args = isset($payEvent['args']) ? $payEvent['args'] : '';
- $args = json_decode($args,true);
- $rechargeM = new \app\common\model\Recharge();
- if ($extendType == 'gold') {
- $payRes = $rechargeM->goldpaysucc($orderCode,$args);
- $payTypeStr = '充值';
- } elseif($extendType == 'vip') {
- $payRes = $rechargeM->vippaysucc($orderCode,$args);
- $payTypeStr = 'vip';
- } else {
- $payRes = false;
- $payTypeStr = '未知支付类型';
- }
- if(!$payRes)
- {
- filePut('[wallet][paySucc]'.$payTypeStr.'更新失败请查看问题'.$orderCode,'suningno.txt');
- exit;
- }
- filePut('[wallet][paySucc]回调成功'.$orderCode,'suningno.txt');
- echo "true";exit;
- }
- filePut('[wallet][paySucc]充值入账更新余额失败 更新状态2不成功'.$orderCode."\n".' data:'.$postData."\n",'suningno.txt');
- exit;
- }
- }
- else
- {
-
- filePut(' recharge gold fail 用户交易操作失败'.$postData,'suningno.txt');
- echo '支付异常';exit;
- }
-
- filePut('[wallet][paySucc]充值入账更新余额失败 验证签名不通过'.$orderCode."\n".' data:'.$postData."\n",'suningno.txt');
- echo 'Fail';exit;
-
- }
- protected function verification($str)
- {
- if($str != '') {
- $arr = explode('&', $str);
- $nowArr = array();
- foreach($arr as $v) {
- $kv = explode('=', $v);
- $nowArr[$kv[0]] = $kv[1];
- }
- ksort($nowArr);
- $newstr = '';
- foreach($nowArr as $key => $value) {
- if($value == '' || $key == 'signature' || $key== 'signAlgorithm') {
- continue;
- }
- if($key == 'version')
- {
- $newstr .= $key.'='.$value;
- } else
- {
- $newstr .= $key.'='.$value.'&';
- }
-
- }
- // print_r($nowArr);
- //var_dump($newstr);
- $sign = base64_decode($nowArr['signature']);
- // exit;
- $newstr = strtoupper(md5($newstr));
- // var_dump($newstr);
- $pubfile = file_get_contents(APP_PATH.'common/library/suning/publicnew.pem');
- $ret = openssl_verify($newstr,$sign,$pubfile);
- return $ret;
- }
- }
- }
|